diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2025-08-02 11:00:15 +0100 |
|---|---|---|
| committer | gingerBill <gingerBill@users.noreply.github.com> | 2025-08-02 11:00:15 +0100 |
| commit | 2561427dd396a69cd49eb02c0814c4e8e8b3a08f (patch) | |
| tree | d390c6fe5c43b9469c312ebb2af07215eaf92fe1 /core/fmt | |
| parent | 710203eadb605b41e652084297cde54754008b87 (diff) | |
Add `string16` and `cstring16` (UTF-16 based strings)
Diffstat (limited to 'core/fmt')
| -rw-r--r-- | core/fmt/fmt.odin | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 0f6470cca..7fe6287d4 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -1551,6 +1551,79 @@ fmt_string :: proc(fi: ^Info, s: string, verb: rune) { fmt_cstring :: proc(fi: ^Info, s: cstring, verb: rune) { fmt_string(fi, string(s), verb) } + +// Formats a string UTF-16 with a specific format. +// +// Inputs: +// - fi: Pointer to the Info struct containing format settings. +// - s: The string to format. +// - verb: The format specifier character (e.g. 's', 'v', 'q', 'x', 'X'). +// +fmt_string16 :: proc(fi: ^Info, s: string16, verb: rune) { + s, verb := s, verb + if ol, ok := fi.optional_len.?; ok { + s = s[:clamp(ol, 0, len(s))] + } + if !fi.in_bad && fi.record_level > 0 && verb == 'v' { + verb = 'q' + } + + switch verb { + case 's', 'v': + if fi.width_set { + if fi.width > len(s) { + if fi.minus { + io.write_string16(fi.writer, s, &fi.n) + } + + for _ in 0..<fi.width - len(s) { + io.write_byte(fi.writer, ' ', &fi.n) + } + + if !fi.minus { + io.write_string16(fi.writer, s, &fi.n) + } + } else { + io.write_string16(fi.writer, s, &fi.n) + } + } else { + io.write_string16(fi.writer, s, &fi.n) + } + + case 'q', 'w': // quoted string + io.write_quoted_string16(fi.writer, s, '"', &fi.n) + + case 'x', 'X': + space := fi.space + fi.space = false + defer fi.space = space + + for i in 0..<len(s) { + if i > 0 && space { + io.write_byte(fi.writer, ' ', &fi.n) + } + char_set := __DIGITS_UPPER + if verb == 'x' { + char_set = __DIGITS_LOWER + } + _fmt_int(fi, u64(s[i]), 16, false, bit_size=16, digits=char_set) + } + + case: + fmt_bad_verb(fi, verb) + } +} +// Formats a C-style UTF-16 string with a specific format. +// +// Inputs: +// - fi: Pointer to the Info struct containing format settings. +// - s: The C-style string to format. +// - verb: The format specifier character (Ref fmt_string). +// +fmt_cstring16 :: proc(fi: ^Info, s: cstring16, verb: rune) { + fmt_string16(fi, string16(s), verb) +} + // Formats a raw pointer with a specific format. // // Inputs: @@ -3210,6 +3283,9 @@ fmt_arg :: proc(fi: ^Info, arg: any, verb: rune) { case string: fmt_string(fi, a, verb) case cstring: fmt_cstring(fi, a, verb) + case string16: fmt_string16(fi, a, verb) + case cstring16: fmt_cstring16(fi, a, verb) + case typeid: reflect.write_typeid(fi.writer, a, &fi.n) case i16le: fmt_int(fi, u64(a), true, 16, verb) |