"String" (Compiled Type)
-
See Also
- Typed
- TypeHint
- TypeSpecifier
- FunctionCompile
-
- Compiled Types
- CString
- CArray
- CChar
- Managed
- Related Guides
- Tech Notes
"String" (Compiled Type)
"String"
represents a string type specifier.
Details
- "String" can be used in Typed and related constructs to specify a type.
- "String" objects are automatically memory managed.
Properties
- The characters stored in a "String" type value are the same as those in a String expression.
Constructors
- CreateTypeInstance["String",cstr,len] creates a string from a C string cstr with length len. cstr can have type "CString" or "Managed"::["CString"].
- CreateTypeInstance["String",cstr] creates a string from a null-terminated C string cstr.
Examples
open all close allBasic Examples (1)
"String" can be used as input and output from a CompiledCodeFunction:
cf = FunctionCompile[Function[Typed[arg, "String"], arg <> arg]]cf[ "abcd"]Scope (1)
Cast a string to a "Managed"::["CString"] and print its contents:
cf = FunctionCompile@Function[Typed[arg, "String"],
Module[{bytes, len},
bytes = Cast[arg, "Managed"::["CString"]];len = LibraryFunction["strlen"][bytes];
Do[Print@bytes[[i]], {i, 0, len - 1}]
]
]cf["a"];ToCharacterCode["a", "UTF-8"]cf["α"];ToCharacterCode["α", "UTF-8"]When a managed object is passed to a function from a library, the raw object is automatically unpacked.
When a managed object is no longer used, it frees its contents.
Possible Issues (1)
A string can be cast to a "CString", but the result will need to be freed with a call to DeleteObject when it is no longer used:
cf = FunctionCompile@Function[Typed[arg, "String"],
Module[{bytes, len},
bytes = Cast[arg, "CString"];len = LibraryFunction["strlen"][bytes];
Do[Print@bytes[[i]], {i, 0, len - 1}];
DeleteObject[bytes]
]
]cf["a"];ToCharacterCode["a", "UTF-8"]cf["α"];ToCharacterCode["α", "UTF-8"]Tech Notes
Related Guides
History
Introduced in 2019 (12.0) | Updated in 2022 (13.1)