From 4a5f9c7d42c6b1fcd3d73ab878321f887a153aa7 Mon Sep 17 00:00:00 2001 From: ARATA Mizuki Date: Tue, 25 Jul 2023 22:59:40 +0900 Subject: [PATCH] Implement rest of Byte --- doc/BasisLibrary.md | 8 ++--- lib/lunarml/ml/basis/byte.sml | 6 ++-- .../ml/basis/js-common/word-sequence.sml | 32 +++++++++++++------ lib/lunarml/ml/basis/lua/word-sequence.sml | 32 +++++++++++++------ 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/doc/BasisLibrary.md b/doc/BasisLibrary.md index fbc045a7..e54cccfe 100644 --- a/doc/BasisLibrary.md +++ b/doc/BasisLibrary.md @@ -1045,7 +1045,7 @@ structure RealArraySlice : MONO_ARRAY_SLICE where type vector = RealVector.vecto where type elem = real ``` -## structure Byte - partial +## structure Byte - complete ```sml signature BYTE = sig @@ -1053,9 +1053,9 @@ 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 -> unit *) + val unpackStringVec : Word8VectorSlice.slice -> string + val unpackString : Word8ArraySlice.slice -> string + val packString : Word8Array.array * int * substring -> unit end structure Byte :> BYTE ``` diff --git a/lib/lunarml/ml/basis/byte.sml b/lib/lunarml/ml/basis/byte.sml index b16f5324..5b622664 100644 --- a/lib/lunarml/ml/basis/byte.sml +++ b/lib/lunarml/ml/basis/byte.sml @@ -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; diff --git a/lib/lunarml/ml/basis/js-common/word-sequence.sml b/lib/lunarml/ml/basis/js-common/word-sequence.sml index bb81a5db..88440526 100644 --- a/lib/lunarml/ml/basis/js-common/word-sequence.sml +++ b/lib/lunarml/ml/basis/js-common/word-sequence.sml @@ -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 @@ -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 *) diff --git a/lib/lunarml/ml/basis/lua/word-sequence.sml b/lib/lunarml/ml/basis/lua/word-sequence.sml index bb81a5db..88440526 100644 --- a/lib/lunarml/ml/basis/lua/word-sequence.sml +++ b/lib/lunarml/ml/basis/lua/word-sequence.sml @@ -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 @@ -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 *)