1 (* Copyright (C
) 1999-2008 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
3 * Copyright (C
) 1997-2000 NEC Research Institute
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
9 structure CUtil
: C_UTIL
=
13 structure Pointer
= Primitive
.MLton
.Pointer
15 fun makeLength (sub
, term
) p
=
24 fun toArrayOfLength (s
: 'a
,
39 val null
= Pointer
.toWord Pointer
.null
40 fun isNull p
= p
= null
48 Primitive
.Char8
.idFromWord8
53 fun update (cs
, i
, c
) =
57 Primitive
.Char8
.idToWord8 c
)
59 val length
= makeLength (sub
, fn #
"\000" => true | _
=> false)
61 fun toCharArrayOfLength (cs
, n
) =
62 toArrayOfLength (cs
, sub
, n
)
64 fun toStringOfLength (cs
, n
) =
65 String.unsafeFromArray
66 (CharArray
.fromPoly (toCharArrayOfLength (cs
, n
)))
68 fun toString cs
= toStringOfLength (cs
, length cs
)
71 structure C_StringArray
=
73 type t
= C_StringArray
.t
76 (Pointer
.toWord
o Pointer
.getCPointer
)
77 (Pointer
.fromWord css
,
80 val length
= makeLength (sub
, C_Pointer
.isNull
)
84 toArrayOfLength (css
, C_String
.toString
o sub
, n
)
86 fun toArray css
= toArrayOfLength (css
, length css
)
88 val toList
= Array
.toList
o toArray
90 (* The C side converts the last element
of the array
, "",
91 * to the null terminator that C primitives expect
.
92 * As far
as C can tell
, the other elements
of the array
99 (1 +?
List.length l
, l
, fn (_
, l
) =>
101 [] => (NullString
.empty
, l
)
102 | s
::l
=> (NullString
.nullTerm s
, l
))