rename countBytes -> countUtf8Bytes

This commit is contained in:
Folkert 2022-07-03 21:42:32 +02:00
parent 5f2c7c5b5e
commit 5904934887
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
10 changed files with 27 additions and 25 deletions

View file

@ -146,7 +146,7 @@ comptime {
exportStrFn(str.strSplitInPlaceC, "str_split_in_place");
exportStrFn(str.countSegments, "count_segments");
exportStrFn(str.countGraphemeClusters, "count_grapheme_clusters");
exportStrFn(str.countBytes, "count_bytes");
exportStrFn(str.countUtf8Bytes, "count_utf8_bytes");
exportStrFn(str.startsWith, "starts_with");
exportStrFn(str.startsWithScalar, "starts_with_scalar");
exportStrFn(str.endsWith, "ends_with");

View file

@ -1184,7 +1184,7 @@ test "countGraphemeClusters: emojis, ut8, and ascii characters" {
try expectEqual(count, 10);
}
pub fn countBytes(string: RocStr) callconv(.C) usize {
pub fn countUtf8Bytes(string: RocStr) callconv(.C) usize {
return string.len();
}

View file

@ -229,7 +229,7 @@ toI8 : Str -> Result I8 [InvalidNumStr]*
getUnsafe : Str, Nat -> U8
## gives the number of string bytes
countBytes : Str -> Nat
countUtf8Bytes : Str -> Nat
## string slice that does not do bounds checking or utf-8 verification
substringUnsafe : Str, Nat, Nat -> Str
@ -242,10 +242,10 @@ splitFirst : Str, Str -> Result { before : Str, after : Str } [NotFound]*
splitFirst = \haystack, needle ->
when firstMatch haystack needle is
Some index ->
remaining = Str.countBytes haystack - Str.countBytes needle - index
remaining = Str.countUtf8Bytes haystack - Str.countUtf8Bytes needle - index
before = Str.substringUnsafe haystack 0 index
after = Str.substringUnsafe haystack (index + Str.countBytes needle) remaining
after = Str.substringUnsafe haystack (index + Str.countUtf8Bytes needle) remaining
Ok { before, after }
None ->
@ -253,8 +253,8 @@ splitFirst = \haystack, needle ->
firstMatch : Str, Str -> [Some Nat, None]
firstMatch = \haystack, needle ->
haystackLength = Str.countBytes haystack
needleLength = Str.countBytes needle
haystackLength = Str.countUtf8Bytes haystack
needleLength = Str.countUtf8Bytes needle
lastPossible = Num.subSaturated haystackLength needleLength
firstMatchHelp haystack needle 0 lastPossible
@ -277,10 +277,10 @@ splitLast : Str, Str -> Result { before : Str, after : Str } [NotFound]*
splitLast = \haystack, needle ->
when lastMatch haystack needle is
Some index ->
remaining = Str.countBytes haystack - Str.countBytes needle - index
remaining = Str.countUtf8Bytes haystack - Str.countUtf8Bytes needle - index
before = Str.substringUnsafe haystack 0 index
after = Str.substringUnsafe haystack (index + Str.countBytes needle) remaining
after = Str.substringUnsafe haystack (index + Str.countUtf8Bytes needle) remaining
Ok { before, after }
None ->
@ -288,8 +288,8 @@ splitLast = \haystack, needle ->
lastMatch : Str, Str -> [Some Nat, None]
lastMatch = \haystack, needle ->
haystackLength = Str.countBytes haystack
needleLength = Str.countBytes needle
haystackLength = Str.countUtf8Bytes haystack
needleLength = Str.countUtf8Bytes needle
lastPossibleIndex = Num.subSaturated haystackLength (needleLength + 1)
lastMatchHelp haystack needle lastPossibleIndex
@ -309,8 +309,8 @@ min = \x, y -> if x < y then x else y
matchesAt : Str, Nat, Str -> Bool
matchesAt = \haystack, haystackIndex, needle ->
haystackLength = Str.countBytes haystack
needleLength = Str.countBytes needle
haystackLength = Str.countUtf8Bytes haystack
needleLength = Str.countUtf8Bytes needle
endIndex = min (haystackIndex + needleLength) haystackLength
matchesAtHelp haystack haystackIndex needle 0 endIndex
@ -329,7 +329,7 @@ matchesAtHelp = \haystack, haystackIndex, needle, needleIndex, endIndex ->
## UTF-8 `U8` byte as well as the index of that byte within the string.
walkUtf8WithIndex : Str, state, (state, U8, Nat -> state) -> state
walkUtf8WithIndex = \string, state, step ->
walkUtf8WithIndexHelp string state step 0 (Str.countBytes string)
walkUtf8WithIndexHelp string state step 0 (Str.countUtf8Bytes string)
walkUtf8WithIndexHelp : Str, state, (state, U8, Nat -> state), Nat, Nat -> state
walkUtf8WithIndexHelp = \string, state, step, index, length ->

View file

@ -313,7 +313,7 @@ pub const STR_JOIN_WITH: &str = "roc_builtins.str.joinWith";
pub const STR_STR_SPLIT_IN_PLACE: &str = "roc_builtins.str.str_split_in_place";
pub const STR_TO_SCALARS: &str = "roc_builtins.str.to_scalars";
pub const STR_COUNT_GRAPEHEME_CLUSTERS: &str = "roc_builtins.str.count_grapheme_clusters";
pub const STR_COUNT_BYTES: &str = "roc_builtins.str.count_bytes";
pub const STR_COUNT_UTF8_BYTES: &str = "roc_builtins.str.count_utf8_bytes";
pub const STR_STARTS_WITH: &str = "roc_builtins.str.starts_with";
pub const STR_STARTS_WITH_SCALAR: &str = "roc_builtins.str.starts_with_scalar";
pub const STR_ENDS_WITH: &str = "roc_builtins.str.ends_with";