1    module cstring
2    
3    def strcpy @nonnull (dst : mut ptr CChar @restrict
4                         src : ptr CChar @restrict) : MutPtr<CChar>
5    
6    def strlen (s : Ptr<CChar>) : usize
7    
8    endmodule
9    
10   type CString = ptr CChar
11   
12   type CString ptr
13       fun get @secondary (i : u32) = ptr[i]
14       fun get @secondary (i : i32) =
15           assert i >= 0
16           ptr[i]
17   
18       def length = cstring/strlen ptr |> as<u32>
19       def to_string "kd_c_string_to_string" : String
20   
21   type String
22       def to_c_string =
23           val c_ptr = alloc<CChar> (length + 1)
24           for i = 0 until length do
25               c_ptr[i] = ptr[i] as CChar
26   
27           assert c_ptr[length] == '\0'
28           c_ptr as Ptr<CChar> |> CString
29