2 * 2005 Matthew
Fluet (mfluet@acm
.org
)
7 * Functions for translating between
0-terminated C strings
and native
10 * (C
) 2001, Lucent Technologies
, Bell Laboratories
12 * author
: Matthias
Blume (blume@research
.bell
-labs
.com
)
14 structure ZString
: ZSTRING
= struct
17 fun get
' p
= Get
.uchar
' (Ptr
.|
*! p
)
18 fun set
' (p
, w
) = Set
.uchar
' (Ptr
.|
*! p
, w
)
19 fun nxt
' p
= Ptr
.|
+! S
.uchar (p
, 1)
21 type 'c zstring
= (uchar
, 'c
) obj ptr
22 type 'c zstring
' = (uchar
, 'c
) obj ptr
'
25 fun loop (n
, p
) = if get
' p
= 0w0
then n
else loop (n
+ 1, nxt
' p
)
29 fun length p
= length
' (Light
.ptr p
)
34 0w0
=> String.implode (rev l
)
35 | c
=> loop ((Byte
.byteToChar c
) :: l
, nxt
' p
)
39 fun toML p
= toML
' (Light
.ptr p
)
41 fun cpML
' { from
, to
} = let
42 val n
= String.size from
44 if i
>= n
then set
' (p
, 0w0
)
45 else (set
' (p
, Byte
.charToByte (String.sub (from
, i
)));
50 fun cpML
{ from
, to
} = cpML
' { from
= from
, to
= Light
.ptr to
}
53 val z
= C
.alloc
' C
.S
.uchar (Word.fromInt (size s
+ 1))
55 cpML
' { from
= s
, to
= z
};
60 val z
= C
.alloc C
.T
.uchar (Word.fromInt (size s
+ 1))
62 cpML
{ from
= s
, to
= z
};