mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
strings.write_quoted_string
This commit is contained in:
+1
-20
@@ -601,26 +601,7 @@ fmt_string :: proc(fi: ^Info, s: string, verb: rune) {
|
|||||||
strings.write_string(fi.buf, s);
|
strings.write_string(fi.buf, s);
|
||||||
|
|
||||||
case 'q': // quoted string
|
case 'q': // quoted string
|
||||||
quote: byte = '"';
|
strings.write_quoted_string(fi.buf, s, '"');
|
||||||
strings.write_byte(fi.buf, quote);
|
|
||||||
for width := 0; len(s) > 0; s = s[width:] {
|
|
||||||
r := rune(s[0]);
|
|
||||||
width = 1;
|
|
||||||
if r >= utf8.RUNE_SELF {
|
|
||||||
r, width = utf8.decode_rune_in_string(s);
|
|
||||||
}
|
|
||||||
if width == 1 && r == utf8.RUNE_ERROR {
|
|
||||||
strings.write_byte(fi.buf, '\\');
|
|
||||||
strings.write_byte(fi.buf, 'x');
|
|
||||||
strings.write_byte(fi.buf, __DIGITS_LOWER[s[0]>>4]);
|
|
||||||
strings.write_byte(fi.buf, __DIGITS_LOWER[s[0]&0xf]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
strings.write_escaped_rune(fi.buf, r, quote);
|
|
||||||
|
|
||||||
}
|
|
||||||
strings.write_byte(fi.buf, quote);
|
|
||||||
|
|
||||||
case 'x', 'X':
|
case 'x', 'X':
|
||||||
space := fi.space;
|
space := fi.space;
|
||||||
|
|||||||
@@ -61,6 +61,31 @@ write_bytes :: proc(b: ^Builder, x: []byte) {
|
|||||||
append(&b.buf, ..x);
|
append(&b.buf, ..x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
static DIGITS_LOWER := "0123456789abcdefx";
|
||||||
|
|
||||||
|
write_quoted_string :: proc(b: ^Builder, s: string, quote: byte = '"') {
|
||||||
|
write_byte(b, quote);
|
||||||
|
for width := 0; len(s) > 0; s = s[width:] {
|
||||||
|
r := rune(s[0]);
|
||||||
|
width = 1;
|
||||||
|
if r >= utf8.RUNE_SELF {
|
||||||
|
r, width = utf8.decode_rune_in_string(s);
|
||||||
|
}
|
||||||
|
if width == 1 && r == utf8.RUNE_ERROR {
|
||||||
|
write_byte(b, '\\');
|
||||||
|
write_byte(b, 'x');
|
||||||
|
write_byte(b, DIGITS_LOWER[s[0]>>4]);
|
||||||
|
write_byte(b, DIGITS_LOWER[s[0]&0xf]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
write_escaped_rune(b, r, quote);
|
||||||
|
|
||||||
|
}
|
||||||
|
write_byte(b, quote);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) {
|
write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) {
|
||||||
if write_quote do write_byte(b, '\'');
|
if write_quote do write_byte(b, '\'');
|
||||||
@@ -93,8 +118,6 @@ write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) {
|
|||||||
|
|
||||||
|
|
||||||
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte) {
|
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte) {
|
||||||
static DIGITS_LOWER := "0123456789abcdefx";
|
|
||||||
|
|
||||||
is_printable :: proc(r: rune) -> bool {
|
is_printable :: proc(r: rune) -> bool {
|
||||||
if r <= 0xff {
|
if r <= 0xff {
|
||||||
switch r {
|
switch r {
|
||||||
|
|||||||
Reference in New Issue
Block a user