]> gitweb.factorcode.org Git - factor.git/commitdiff
docs, ffi: more explicit c-string conversions docs
authorJon Harper <jon.harper87@gmail.com>
Sat, 9 Jan 2016 17:30:35 +0000 (18:30 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 9 Jan 2016 21:54:21 +0000 (13:54 -0800)
basis/alien/data/data-docs.factor

index e4fb14b4dde666641e4f5421fd68f58d5a85a90b..624b18230d77559de7998b3415df4cf65ff851ce 100644 (file)
@@ -181,7 +181,15 @@ HELP: <c-direct-array>
 ARTICLE: "c-strings" "C strings"
 "C string types are arrays with shape " { $snippet "{ c-string encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $link c-string } " is an alias for " { $snippet "{ c-string utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors. In " { $link POSTPONE: TYPEDEF: } ", " { $link POSTPONE: FUNCTION: } ", " { $link POSTPONE: CALLBACK: } ", and " { $link POSTPONE: STRUCT: } " definitions, the shorthand syntax " { $snippet "c-string[encoding]" } " can be used to specify the string encoding."
 $nl
-"Passing a Factor string to a C function expecting a " { $link 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."
+"Using C string types triggers automatic conversions:"
+{ $list
+    {
+    "Passing a Factor string to a C function expecting a " { $link c-string } " allocates a " { $link byte-array } " in the Factor heap; the string is then encoded to the requested encoding and a raw pointer is passed to the function."
+    "Passing an already encoded " { $link byte-array } " also works and performs no conversion."
+    }
+    { "Returning a C string from a C function allocates a Factor string in the Factor heap; the memory pointed to by the returned pointer is then decoded with the requested encoding into the Factor string." }
+    { "Reading " { $link c-string } " slots of "  { $link POSTPONE: STRUCT: } " or " { $link POSTPONE: UNION-STRUCT: } " returns Factor strings." }
+}
 $nl
 "Care must be taken if the C function expects a pointer to a string with its length represented by another parameter rather than a null terminator. 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."
 $nl