From 8662df2b7fa6a45cd81941357a2fe96854b05336 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 26 Dec 2018 19:33:56 +0000 Subject: [PATCH] Update `package strings` --- core/fmt/fmt.odin | 8 +- core/runtime/internal.odin | 2 +- core/strings/strings.odin | 448 +++++++++++++++++++++++++++++++++++- core/unicode/utf8/utf8.odin | 6 +- 4 files changed, 452 insertions(+), 12 deletions(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 229e72fc3..73a848764 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -640,7 +640,7 @@ _pad :: proc(fi: ^Fmt_Info, s: string) { } - width := fi.width - utf8.rune_count_from_string(s); + width := fi.width - utf8.rune_count_in_string(s); if fi.minus { // right pad write_string(fi.buf, s); fmt_write_padding(fi, width); @@ -905,16 +905,16 @@ fmt_bit_set :: proc(fi: ^Fmt_Info, v: any, name: string = "") { if commas > 0 do write_string(fi.buf, ", "); - defer commas += 1; - if is_enum do for ev, evi in e.values { v := enum_value_to_u64(ev); if v == i { write_string(fi.buf, e.names[evi]); + commas += 1; continue loop; } } write_i64(fi.buf, i64(i), 10); + commas += 1; } } } @@ -1464,7 +1464,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ..any) -> string { break loop; } - verb, w := utf8.decode_rune_from_string(fmt[i:]); + verb, w := utf8.decode_rune_in_string(fmt[i:]); i += w; switch { diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 96ff861b2..5db1845f5 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -326,7 +326,7 @@ type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column } string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { - return utf8.decode_rune_from_string(s); + return utf8.decode_rune_in_string(s); } bounds_check_error_loc :: inline proc "contextless" (using loc := #caller_location, index, count: int) { diff --git a/core/strings/strings.odin b/core/strings/strings.odin index b3006f36f..20c4beb2b 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -1,16 +1,17 @@ package strings import "core:mem" +import "core:unicode/utf8" -new_string :: proc(s: string) -> string { - c := make([]byte, len(s)+1); +new_string :: proc(s: string, allocator := context.allocator) -> string { + c := make([]byte, len(s)+1, allocator); copy(c, cast([]byte)s); c[len(s)] = 0; return string(c[:len(s)]); } -new_cstring :: proc(s: string) -> cstring { - c := make([]byte, len(s)+1); +new_cstring :: proc(s: string, allocator := context.allocator) -> cstring { + c := make([]byte, len(s)+1, allocator); copy(c, cast([]byte)s); c[len(s)] = 0; return cstring(&c[0]); @@ -31,3 +32,442 @@ contains_rune :: proc(s: string, r: rune) -> int { } return -1; } + +contains :: proc(s, substr: string) -> bool { + return index(s, substr) >= 0; +} + +contains_any :: proc(s, chars: string) -> bool { + return index_any(s, chars) >= 0; +} + + +equal_fold :: proc(s, t: string) -> bool { + loop: for s != "" && t != "" { + sr, tr: rune; + if s[0] < utf8.RUNE_SELF { + sr, s = rune(s[0]), s[1:]; + } else { + r, size := utf8.decode_rune_in_string(s); + sr, s = r, s[size:]; + } + if t[0] < utf8.RUNE_SELF { + tr, t = rune(t[0]), t[1:]; + } else { + r, size := utf8.decode_rune_in_string(t); + tr, t = r, t[size:]; + } + + if tr == sr { // easy case + continue loop; + } + + if tr < sr { + tr, sr = sr, tr; + } + + if tr < utf8.RUNE_SELF { + switch sr { + case 'A'..'Z': + if tr == (sr+'a')-'A' { + continue loop; + } + } + return false; + } + + // TODO(bill): Unicode folding + + return false; + } + + return s == t; +} + +has_prefix :: proc(s, prefix: string) -> bool { + return len(s) >= len(prefix) && s[0:len(prefix)] == prefix; +} + +has_suffix :: proc(s, suffix: string) -> bool { + return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix; +} + + +join :: proc(a: []string, sep: string, allocator := context.allocator) -> string { + if len(a) == 0 { + return ""; + } + + n := len(sep) * (len(a) - 1); + for s in a { + n += len(s); + } + + b := make([]byte, n); + i := copy(b, cast([]byte)a[0]); + for s in a[1:] { + i += copy(b[i:], cast([]byte)sep); + i += copy(b[i:], cast([]byte)s); + } + return string(b); +} + +concatenate :: proc(a: []string, allocator := context.allocator) -> string { + if len(a) == 0 { + return ""; + } + + n := 0; + for s in a { + n += len(s); + } + b := make([]byte, n); + i := 0; + for s in a { + i += copy(b[i:], cast([]byte)s); + } + return string(b); +} + +index_byte :: proc(s: string, c: byte) -> int { + for i := 0; i < len(s); i += 1 { + if s[i] == c do return i; + } + return -1; +} + +// Returns i1 if c is not present +last_index_byte :: proc(s: string, c: byte) -> int { + for i := len(s)-1; i >= 0; i -= 1 { + if s[i] == c do return i; + } + return -1; +} + +index :: proc(s, substr: string) -> int { + n := len(substr); + switch { + case n == 0: + return 0; + case n == 1: + return index_byte(s, substr[0]); + case n == len(s): + if s == substr { + return 0; + } + return -1; + case n > len(s): + return -1; + } + + for i := 0; i < len(s)-n+1; i += 1 { + x := s[i:i+n]; + if x == substr { + return i; + } + } + return -1; +} + +index_any :: proc(s, chars: string) -> int { + if chars == "" { + return -1; + } + + // TODO(bill): Optimize + for r, i in s { + for c in chars { + if r == c { + return i; + } + } + } + return -1; +} + +last_index_any :: proc(s, chars: string) -> int { + if chars == "" { + return -1; + } + + for i := len(s); i > 0; { + r, w := utf8.decode_last_rune_in_string(s[:i]); + i -= w; + for c in chars { + if r == c { + return i; + } + } + } + return -1; +} + +count :: proc(s, substr: string) -> int { + if len(substr) == 0 { // special case + return utf8.rune_count_in_string(s) + 1; + } + if len(substr) == 1 { + c := substr[0]; + switch len(s) { + case 0: + return 0; + case 1: + return int(s[0] == c); + } + n := 0; + for i := 0; i < len(s); i += 1 { + if s[i] == c { + n += 1; + } + } + return n; + } + + // TODO(bill): Use a non-brute for approach + n := 0; + for { + i := index(s, substr); + if i == -1 { + return n; + } + n += 1; + s = s[i+len(substr):]; + } + return n; +} + + +repeat :: proc(s: string, count: int, allocator := context.allocator) -> string { + if count < 0 { + panic("strings: negative repeat count"); + } else if count > 0 && (len(s)*count)/count != len(s) { + panic("strings: repeat count will cause an overflow"); + } + + b := make([]byte, len(s)*count, allocator); + i := copy(b, cast([]byte)s); + for i < len(b) { // 2^N trick to reduce the need to copy + copy(b[i:], b[:i]); + i *= 2; + } + return string(b); +} + +replace_all :: proc(s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) { + return replace(s, old, new, -1, allocator); +} + +// if n < 0, no limit on the number of replacements +replace :: proc(s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) { + if old == new || n == 0 { + was_allocation = false; + output = s; + return; + } + + if m := count(s, old); m == 0 { + was_allocation = false; + output = s; + return; + } else if n < 0 || m < n { + n = m; + } + + + t := make([]byte, len(s) + n*(len(new) - len(old)), allocator); + was_allocation = true; + + w := 0; + start := 0; + for i := 0; i < n; i += 1 { + j := start; + if len(old) == 0 { + if i > 0 { + _, w := utf8.decode_rune_in_string(s[start:]); + j += w; + } + } else { + j += index(s[start:], old); + } + w += copy(t[w:], cast([]byte)s[start:j]); + w += copy(t[w:], cast([]byte)new); + start = j + len(old); + } + w += copy(t[w:], cast([]byte)s[start:]); + output = string(t[0:w]); + return; +} + +is_ascii_space :: proc(r: rune) -> bool { + switch r { + case '\t', '\n', '\v', '\f', '\r', ' ': + return true; + } + return false; +} + +is_space :: proc(r: rune) -> bool { + if r < 0x2000 { + switch r { + case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xa0, 0x1680: + return true; + } + } else { + if r <= 0x200a { + return true; + } + switch r { + case 0x2028, 0x2029, 0x202f, 0x205f, 0x3000: + return true; + } + } + return false; +} + +index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int { + for r, i in s { + if p(r) == truth { + return i; + } + } + return -1; +} + +index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int { + for r, i in s { + if p(state, r) == truth { + return i; + } + } + return -1; +} + +last_index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int { + // TODO(bill): Probably use Rabin-Karp Search + for i := len(s); i > 0; { + r, size := utf8.decode_last_rune_in_string(s[:i]); + i -= size; + if p(r) == truth { + return i; + } + } + return -1; +} + +last_index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int { + // TODO(bill): Probably use Rabin-Karp Search + for i := len(s); i > 0; { + r, size := utf8.decode_last_rune_in_string(s[:i]); + i -= size; + if p(state, r) == truth { + return i; + } + } + return -1; +} + +trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string { + i := index_proc(s, p, false); + if i == -1 { + return ""; + } + return s[i:]; +} + + +index_rune :: proc(s: string, r: rune) -> int { + switch { + case 0 <= r && r < utf8.RUNE_SELF: + return index_byte(s, byte(r)); + + case r == utf8.RUNE_ERROR: + for r, i in s { + if r == utf8.RUNE_ERROR { + return i; + } + } + return -1; + + case !utf8.valid_rune(r): + return -1; + } + + b, w := utf8.encode_rune(r); + return index(s, string(b[:w])); +} + + +trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string { + i := index_proc_with_state(s, p, state, false); + if i == -1 { + return ""; + } + return s[i:]; +} + +trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string { + i := last_index_proc(s, p, false); + if i >= 0 && s[i] >= utf8.RUNE_SELF { + _, w := utf8.decode_rune_in_string(s[i:]); + i += w; + } else { + i += 1; + } + return s[0:i]; +} + +trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string { + i := last_index_proc_with_state(s, p, state, false); + if i >= 0 && s[i] >= utf8.RUNE_SELF { + _, w := utf8.decode_rune_in_string(s[i:]); + i += w; + } else { + i += 1; + } + return s[0:i]; +} + + +is_in_cutset :: proc(state: rawptr, r: rune) -> bool { + if state == nil { + return false; + } + cutset := (^string)(state)^; + for c in cutset { + if r == c { + return true; + } + } + return false; +} + + +trim_left :: proc(s: string, cutset: string) -> string { + if s == "" || cutset == "" { + return s; + } + return trim_left_proc_with_state(s, is_in_cutset, &cutset); +} + +trim_right :: proc(s: string, cutset: string) -> string { + if s == "" || cutset == "" { + return s; + } + return trim_right_proc_with_state(s, is_in_cutset, &cutset); +} + +trim :: proc(s: string, cutset: string) -> string { + return trim_right(trim_left(s, cutset), cutset); +} + +trim_left_space :: proc(s: string) -> string { + return trim_left_proc(s, is_space); +} + +trim_right_space :: proc(s: string) -> string { + return trim_right_proc(s, is_space); +} + +trim_space :: proc(s: string) -> string { + return trim_right_space(trim_left_space(s)); +} diff --git a/core/unicode/utf8/utf8.odin b/core/unicode/utf8/utf8.odin index cf67d4655..ec2c8a1e0 100644 --- a/core/unicode/utf8/utf8.odin +++ b/core/unicode/utf8/utf8.odin @@ -94,7 +94,7 @@ encode_rune :: proc(r: rune) -> ([4]u8, int) { return buf, 4; } -decode_rune_from_string :: inline proc(s: string) -> (rune, int) do return decode_rune(cast([]u8)s); +decode_rune_in_string :: inline proc(s: string) -> (rune, int) do return decode_rune(cast([]u8)s); decode_rune :: proc(s: []u8) -> (rune, int) { n := len(s); if n < 1 { @@ -134,7 +134,7 @@ decode_rune :: proc(s: []u8) -> (rune, int) { -decode_last_rune_from_string :: inline proc(s: string) -> (rune, int) do return decode_last_rune(cast([]u8)s); +decode_last_rune_in_string :: inline proc(s: string) -> (rune, int) do return decode_last_rune(cast([]u8)s); decode_last_rune :: proc(s: []u8) -> (rune, int) { r: rune; size: int; @@ -215,7 +215,7 @@ valid_string :: proc(s: string) -> bool { rune_start :: inline proc(b: u8) -> bool do return b&0xc0 != 0x80; -rune_count_from_string :: inline proc(s: string) -> int do return rune_count(cast([]u8)s); +rune_count_in_string :: inline proc(s: string) -> int do return rune_count(cast([]u8)s); rune_count :: proc(s: []u8) -> int { count := 0; n := len(s);