0 | {--
 1 | Copyright (C) 2026  Joel Berkeley
 2 |
 3 | This program is free software: you can redistribute it and/or modify
 4 | it under the terms of the GNU Affero General Public License as published
 5 | by the Free Software Foundation, either version 3 of the License, or
 6 | (at your option) any later version.
 7 |
 8 | This program is distributed in the hope that it will be useful,
 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 | GNU Affero General Public License for more details.
12 |
13 | You should have received a copy of the GNU Affero General Public License
14 | along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 | --}
16 | ||| For internal spidr use only.
17 | module Compiler.String
18 |
19 | import Compiler.FFI
20 |
21 | ffi : String -> String
22 | ffi = libxla "c/string.h"
23 |
24 | public export
25 | data CppString = MkCppString AnyPtr
26 |
27 | export
28 | %foreign (ffi "string_new")
29 | prim__mkString : PrimIO AnyPtr
30 |
31 | ||| It is up to the caller to `delete` the string.
32 | export
33 | cppString : HasIO io => io CppString
34 | cppString = MkCppString <$> primIO prim__mkString
35 |
36 | export
37 | %foreign (ffi "string_delete")
38 | prim__stringDelete : AnyPtr -> PrimIO ()
39 |
40 | namespace CppString
41 |   export
42 |   delete : HasIO io => CppString -> io ()
43 |   delete (MkCppString str) = primIO $ prim__stringDelete str
44 |
45 | %foreign (ffi "string_c_str")
46 | prim__stringCStr : AnyPtr -> String
47 |
48 | export
49 | c_str : CppString -> String
50 | c_str (MkCppString str) = prim__stringCStr str
51 |
52 | export
53 | %foreign (ffi "string_data")
54 | prim__stringData : AnyPtr -> Ptr Char
55 |
56 | export
57 | %foreign (ffi "string_size")
58 | prim__stringSize : AnyPtr -> Bits64
59 |