]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/strings/strings-docs.factor
Create basis vocab root
[factor.git] / basis / alien / strings / strings-docs.factor
1 USING: help.markup help.syntax strings byte-arrays alien libc
2 debugger io.encodings.string sequences ;
3 IN: alien.strings
4
5 HELP: string>alien
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." } ;
9
10 { string>alien alien>string malloc-string } related-words
11
12 HELP: alien>string
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." } ;
15
16 HELP: malloc-string
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:"
21     { $list
22         "the string contains null code points"
23         "the string contains characters not representable using the encoding specified"
24         "memory allocation fails"
25     }
26 } ;
27
28 HELP: string>symbol
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."
31 $nl
32 "On Windows CE, symbols are represented as UCS2 strings, and on all other platforms they are ASCII strings." } ;
33
34 HELP: utf16n
35 { $class-description "The encoding descriptor for UTF-16 without a byte order mark in native endian order. This is useful mostly for FFI calls which take input of strings of the type wchar_t*" }
36 { $see-also "encodings-introduction" } ;
37
38 ARTICLE: "c-strings" "C strings"
39 "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."
40 $nl
41 "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."
42 $nl
43 "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."
44 $nl
45 "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."
46 $nl
47 "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:"
48 { $subsection string>alien }
49 { $subsection malloc-string }
50 "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."
51 $nl
52 "A word to read strings from arbitrary addresses:"
53 { $subsection alien>string }
54 "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 } "." ;
55
56 ABOUT: "c-strings"