Minor cleanup of CFString.odin

This commit is contained in:
gingerBill
2024-08-27 11:22:28 +01:00
parent fb248056c9
commit 8694fa5f78
+7 -14
View File
@@ -159,7 +159,7 @@ StringEncodings :: enum Index {
ShiftJIS_X0213_00 = 0x0628, // Deprecated. Use `ShiftJIS_X0213` instead. ShiftJIS_X0213_00 = 0x0628, // Deprecated. Use `ShiftJIS_X0213` instead.
} }
@(link_prefix = "CF", default_calling_convention = "c") @(link_prefix="CF", default_calling_convention="c")
foreign CoreFoundation { foreign CoreFoundation {
// Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding. // Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.
StringGetCString :: proc(theString: String, buffer: [^]byte, bufferSize: Index, encoding: StringEncoding) -> b8 --- StringGetCString :: proc(theString: String, buffer: [^]byte, bufferSize: Index, encoding: StringEncoding) -> b8 ---
@@ -181,23 +181,16 @@ foreign CoreFoundation {
STR :: StringMakeConstantString STR :: StringMakeConstantString
StringCopyToOdinString :: proc( StringCopyToOdinString :: proc(theString: String, allocator := context.allocator) -> (str: string, ok: bool) #optional_ok {
theString: String,
allocator := context.allocator,
) -> (
str: string,
ok: bool,
) #optional_ok {
length := StringGetLength(theString) length := StringGetLength(theString)
max := StringGetMaximumSizeForEncoding(length, StringEncoding(StringBuiltInEncodings.UTF8)) max := StringGetMaximumSizeForEncoding(length, StringEncoding(StringBuiltInEncodings.UTF8))
buf, err := make([]byte, max, allocator) buf, err := make([]byte, max, allocator)
if err != nil { return } if err != nil {
return
raw_str := runtime.Raw_String {
data = raw_data(buf),
} }
StringGetBytes(theString, {0, length}, StringEncoding(StringBuiltInEncodings.UTF8), 0, false, raw_data(buf), max, (^Index)(&raw_str.len))
return transmute(string)raw_str, true n: Index
StringGetBytes(theString, {0, length}, StringEncoding(StringBuiltInEncodings.UTF8), 0, false, raw_data(buf), Index(len(buf)), &n)
return string(buf[:n]), true
} }