mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-03 22:28:46 +00:00
Add string16 and cstring16 (UTF-16 based strings)
This commit is contained in:
@@ -5,6 +5,7 @@ package io
|
||||
|
||||
import "base:intrinsics"
|
||||
import "core:unicode/utf8"
|
||||
import "core:unicode/utf16"
|
||||
|
||||
// Seek whence values
|
||||
Seek_From :: enum {
|
||||
@@ -314,6 +315,29 @@ write_string :: proc(s: Writer, str: string, n_written: ^int = nil) -> (n: int,
|
||||
return write(s, transmute([]byte)str, n_written)
|
||||
}
|
||||
|
||||
// write_string16 writes the contents of the string16 s to w reencoded as utf-8
|
||||
write_string16 :: proc(s: Writer, str: string16, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
for i := 0; i < len(str); i += 1 {
|
||||
r := rune(utf16.REPLACEMENT_CHAR)
|
||||
|
||||
switch c := str[i]; {
|
||||
case c < utf16._surr1, utf16._surr3 <= c:
|
||||
r = rune(c)
|
||||
case utf16._surr1 <= c && c < utf16._surr2 && i+1 < len(str) &&
|
||||
utf16._surr2 <= str[i+1] && str[i+1] < utf16._surr3:
|
||||
r = utf16.decode_surrogate_pair(rune(c), rune(str[i+1]))
|
||||
i += 1
|
||||
}
|
||||
|
||||
w, err := write_rune(s, r, n_written)
|
||||
n += w
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// write_rune writes a UTF-8 encoded rune to w.
|
||||
write_rune :: proc(s: Writer, r: rune, n_written: ^int = nil) -> (size: int, err: Error) {
|
||||
defer if err == nil && n_written != nil {
|
||||
|
||||
@@ -264,6 +264,33 @@ write_quoted_string :: proc(w: Writer, str: string, quote: byte = '"', n_written
|
||||
return
|
||||
}
|
||||
|
||||
write_quoted_string16 :: proc(w: Writer, str: string16, quote: byte = '"', n_written: ^int = nil, for_json := false) -> (n: int, err: Error) {
|
||||
defer if n_written != nil {
|
||||
n_written^ += n
|
||||
}
|
||||
write_byte(w, quote, &n) or_return
|
||||
for width, s := 0, str; len(s) > 0; s = s[width:] {
|
||||
r := rune(s[0])
|
||||
width = 1
|
||||
if r >= utf8.RUNE_SELF {
|
||||
r, width = utf16.decode_rune_in_string(s)
|
||||
}
|
||||
if width == 1 && r == utf8.RUNE_ERROR {
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, 'x', &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[s[0]>>4], &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[s[0]&0xf], &n) or_return
|
||||
continue
|
||||
}
|
||||
|
||||
n_wrapper(write_escaped_rune(w, r, quote, false, nil, for_json), &n) or_return
|
||||
|
||||
}
|
||||
write_byte(w, quote, &n) or_return
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// writer append a quoted rune into the byte buffer, return the written size
|
||||
write_quoted_rune :: proc(w: Writer, r: rune) -> (n: int) {
|
||||
_write_byte :: #force_inline proc(w: Writer, c: byte) -> int {
|
||||
|
||||
Reference in New Issue
Block a user