Skip to content

Commit

Permalink
Implement rest of Byte
Browse files Browse the repository at this point in the history
  • Loading branch information
minoki committed Jul 25, 2023
1 parent f2b718e commit 4a5f9c7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
8 changes: 4 additions & 4 deletions doc/BasisLibrary.md
Original file line number Diff line number Diff line change
Expand Up @@ -1045,17 +1045,17 @@ structure RealArraySlice : MONO_ARRAY_SLICE where type vector = RealVector.vecto
where type elem = real
```

## structure Byte - partial
## structure Byte - complete

```sml
signature BYTE = sig
val byteToChar : Word8.word -> char
val charToByte : char -> Word8.word
val bytesToString : Word8Vector.vector -> string
val stringToBytes : string -> Word8Vector.vector
(* val unpackStringVec : Word8VectorSlice.slice -> string *)
(* val unpackString : Word8ArraySlice.slice -> string *)
(* val packString : Word8Array.array * int * substring -> unit *)
val unpackStringVec : Word8VectorSlice.slice -> string
val unpackString : Word8ArraySlice.slice -> string
val packString : Word8Array.array * int * substring -> unit
end
structure Byte :> BYTE
```
Expand Down
6 changes: 4 additions & 2 deletions lib/lunarml/ml/basis/byte.sml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ signature BYTE = sig
val charToByte : char -> Word8.word
val bytesToString : Word8Vector.vector -> string
val stringToBytes : string -> Word8Vector.vector
val unpackStringVec : Word8VectorSlice.slice -> string
val unpackString : Word8ArraySlice.slice -> string
val packString : Word8Array.array * int * Substring.substring -> unit
end

structure Byte :> BYTE = struct
fun byteToChar x = Char.chr (Word8.toInt x)
fun charToByte x = Word8.fromInt (Char.ord x)
val bytesToString = Word8VectorExtra.bytesToString
val stringToBytes = Word8VectorExtra.stringToBytes
open ByteImpl
end;
32 changes: 22 additions & 10 deletions lib/lunarml/ml/basis/js-common/word-sequence.sml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
signature WORD8_VECTOR_EXTRA = sig
include MONO_VECTOR where type elem = Word8.word
val bytesToString : vector -> string
val stringToBytes : string -> vector
end

local
structure Word8Sequence :> sig
structure Word8VectorExtra : WORD8_VECTOR_EXTRA
structure Word8Vector : MONO_VECTOR where type elem = Word8.word
structure Word8VectorSlice : MONO_VECTOR_SLICE where type elem = Word8.word where type vector = Word8Vector.vector
structure Word8Array : MONO_ARRAY where type elem = Word8.word
structure Word8ArraySlice : MONO_ARRAY_SLICE where type elem = Word8.word
structure ByteImpl : sig
val bytesToString : Word8Vector.vector -> string
val stringToBytes : string -> Word8Vector.vector
val unpackStringVec : Word8VectorSlice.slice -> string
val unpackString : Word8ArraySlice.slice -> string
val packString : Word8Array.array * int * Substring.substring -> unit
end
structure UnsafeWord8Vector : UNSAFE_MONO_VECTOR where type elem = Word8.word
structure UnsafeWord8Array : UNSAFE_MONO_ARRAY where type elem = Word8.word
sharing type Word8VectorExtra.vector = Word8Vector.vector = Word8Array.vector = Word8ArraySlice.vector = UnsafeWord8Vector.vector
sharing type Word8Vector.vector = Word8Array.vector = Word8ArraySlice.vector = UnsafeWord8Vector.vector
sharing type Word8Array.array = Word8ArraySlice.array = UnsafeWord8Array.array
sharing type Word8VectorSlice.slice = Word8ArraySlice.vector_slice
end = struct
Expand Down Expand Up @@ -53,10 +53,22 @@ local
structure Word8ArraySlice = Base.MonoArraySlice
structure UnsafeWord8Vector = Base.UnsafeMonoVector
structure UnsafeWord8Array = Base.UnsafeMonoArray
structure Word8VectorExtra = struct
structure ByteImpl = struct
fun bytesToString x = x
fun stringToBytes x = x
open Word8Vector
fun unpackStringVec { base, start, length } = String.substring (base, start, length)
fun unpackString { base, start, length } = CharVector.tabulate (length, fn i => Char.chr (Word8.toInt (Unsafe.Array.sub (base, start + i))))
fun packString (arr, i, s) = let val length = Substring.size s
in if i < 0 orelse length + i > Array.length arr then
raise Subscript
else
let fun go j = if j < length then
( Unsafe.Array.update (arr, i + j, Word8.fromInt (Char.ord (Substring.sub (s, i)))); go (j + 1) )
else
()
in go 0
end
end
end (* structure Word8VectorExtra *)
end (* local *)
end (* structure Word8Sequence *)
Expand Down
32 changes: 22 additions & 10 deletions lib/lunarml/ml/basis/lua/word-sequence.sml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
signature WORD8_VECTOR_EXTRA = sig
include MONO_VECTOR where type elem = Word8.word
val bytesToString : vector -> string
val stringToBytes : string -> vector
end

local
structure Word8Sequence :> sig
structure Word8VectorExtra : WORD8_VECTOR_EXTRA
structure Word8Vector : MONO_VECTOR where type elem = Word8.word
structure Word8VectorSlice : MONO_VECTOR_SLICE where type elem = Word8.word where type vector = Word8Vector.vector
structure Word8Array : MONO_ARRAY where type elem = Word8.word
structure Word8ArraySlice : MONO_ARRAY_SLICE where type elem = Word8.word
structure ByteImpl : sig
val bytesToString : Word8Vector.vector -> string
val stringToBytes : string -> Word8Vector.vector
val unpackStringVec : Word8VectorSlice.slice -> string
val unpackString : Word8ArraySlice.slice -> string
val packString : Word8Array.array * int * Substring.substring -> unit
end
structure UnsafeWord8Vector : UNSAFE_MONO_VECTOR where type elem = Word8.word
structure UnsafeWord8Array : UNSAFE_MONO_ARRAY where type elem = Word8.word
sharing type Word8VectorExtra.vector = Word8Vector.vector = Word8Array.vector = Word8ArraySlice.vector = UnsafeWord8Vector.vector
sharing type Word8Vector.vector = Word8Array.vector = Word8ArraySlice.vector = UnsafeWord8Vector.vector
sharing type Word8Array.array = Word8ArraySlice.array = UnsafeWord8Array.array
sharing type Word8VectorSlice.slice = Word8ArraySlice.vector_slice
end = struct
Expand Down Expand Up @@ -53,10 +53,22 @@ local
structure Word8ArraySlice = Base.MonoArraySlice
structure UnsafeWord8Vector = Base.UnsafeMonoVector
structure UnsafeWord8Array = Base.UnsafeMonoArray
structure Word8VectorExtra = struct
structure ByteImpl = struct
fun bytesToString x = x
fun stringToBytes x = x
open Word8Vector
fun unpackStringVec { base, start, length } = String.substring (base, start, length)
fun unpackString { base, start, length } = CharVector.tabulate (length, fn i => Char.chr (Word8.toInt (Unsafe.Array.sub (base, start + i))))
fun packString (arr, i, s) = let val length = Substring.size s
in if i < 0 orelse length + i > Array.length arr then
raise Subscript
else
let fun go j = if j < length then
( Unsafe.Array.update (arr, i + j, Word8.fromInt (Char.ord (Substring.sub (s, i)))); go (j + 1) )
else
()
in go 0
end
end
end (* structure Word8VectorExtra *)
end (* local *)
end (* structure Word8Sequence *)
Expand Down

0 comments on commit 4a5f9c7

Please sign in to comment.