1 USING: help.markup help.syntax strings byte-arrays alien libc
2 debugger io.encodings.string sequences ;
6 { $values { "string" string } { "encoding" "an encoding descriptor" } { "byte-array" byte-array } }
7 { $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated byte array." }
8 { $errors "Throws an error if the string contains null characters, or characters not representable in the given encoding." } ;
10 { string>alien alien>string malloc-string } related-words
13 { $values { "c-ptr" c-ptr } { "encoding" "an encoding descriptor" } { "string/f" "a string or " { $link f } } }
14 { $description "Reads a null-terminated C string from the specified address with the given encoding." } ;
17 { $values { "string" string } { "encoding" "an encoding descriptor" } { "alien" c-ptr } }
18 { $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated unmanaged memory block." }
19 { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
20 { $errors "Throws an error if one of the following conditions occurs:"
22 "the string contains null code points"
23 "the string contains characters not representable using the encoding specified"
24 "memory allocation fails"
29 { $values { "str" string } { "alien" alien } }
30 { $description "Converts the string to a format which is a valid symbol name for the Factor VM's compiled code linker. By performing this conversion ahead of time, the image loader can run without allocating memory."
32 "On Windows CE, symbols are represented as UCS2 strings, and on all other platforms they are ASCII strings." } ;
34 ARTICLE: "c-strings" "C strings"
35 "C string types are arrays with shape " { $snippet "{ \"char*\" encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $snippet "\"char*\"" } " is an alias for " { $snippet "{ \"char*\" utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors."
37 "Passing a Factor string to a C function expecting a C string allocates a " { $link byte-array } " in the Factor heap; the string is then converted to the requested format and a raw pointer is passed to the function."
39 "If the conversion fails, for example if the string contains null bytes or characters with values higher than 255, a " { $link c-string-error. } " is thrown."
41 "Care must be taken if the C function expects a " { $snippet "char*" } " with a length in bytes, rather than a null-terminated " { $snippet "char*" } "; passing the result of calling " { $link length } " on the string object will not suffice. This is because a Factor string of " { $emphasis "n" } " characters will not necessarily encode to " { $emphasis "n" } " bytes. The correct idiom for C functions which take a string with a length is to first encode the string using " { $link encode } ", and then pass the resulting byte array together with the length of this byte array."
43 "Sometimes a C function has a parameter type of " { $snippet "void*" } ", and various data types, among them strings, can be passed in. In this case, strings are not automatically converted to aliens, and instead you must call one of these words:"
44 { $subsection string>alien }
45 { $subsection malloc-string }
46 "The first allocates " { $link byte-array } "s, and the latter allocates manually-managed memory which is not moved by the garbage collector and has to be explicitly freed by calling " { $link free } ". See " { $link "byte-arrays-gc" } " for a discussion of the two approaches."
48 "A word to read strings from arbitrary addresses:"
49 { $subsection alien>string }
50 "For example, if a C function returns a " { $snippet "char*" } " but stipulates that the caller must deallocate the memory afterward, you must define the function as returning " { $snippet "void*" } ", and call one of the above words before passing the pointer to " { $link free } "." ;