diff --git a/lib/std/collections/enummap.c3 b/lib/std/collections/enummap.c3 index 60e7e3c31..7d411a619 100644 --- a/lib/std/collections/enummap.c3 +++ b/lib/std/collections/enummap.c3 @@ -7,7 +7,7 @@ struct EnumMap fn void EnumMap.init(&self, ValueType init_value) { - foreach(&a : self.values) + foreach (&a : self.values) { *a = init_value; } @@ -30,6 +30,22 @@ fn String EnumMap.to_string(&self, Allocator* using = mem::heap()) @dynamic return string::printf("%s", *self); } -fn uint EnumMap.len(&self) @operator(len) => self.values.len; -fn ValueType EnumMap.get(&self, Enum key) @operator([]) => self.values[key.ordinal]; -fn void EnumMap.set(&self, Enum key, ValueType value) @operator([]=) => self.values[key.ordinal] = value; +fn usz EnumMap.len(&self) @operator(len) @inline +{ + return self.values.len; +} + +fn ValueType EnumMap.get(&self, Enum key) @operator([]) @inline +{ + return self.values[key.ordinal]; +} + +fn ValueType* EnumMap.get_ref(&self, Enum key) @operator(&[]) @inline +{ + return &self.values[key.ordinal]; +} + +fn void EnumMap.set(&self, Enum key, ValueType value) @operator([]=) @inline +{ + self.values[key.ordinal] = value; +} \ No newline at end of file diff --git a/lib/std/core/string.c3 b/lib/std/core/string.c3 index 5868fc828..bc19d5bb4 100644 --- a/lib/std/core/string.c3 +++ b/lib/std/core/string.c3 @@ -252,24 +252,12 @@ fn usz! String.rindex_of_char(s, char needle) fn usz! String.index_of(s, String needle) { usz needed = needle.len; - char search = needle[0]; - if (needed == 1) return s.index_of_char(search); - usz match = 0; - usz index_start = 0; - foreach (usz i, char c : s) + if (needed > 0 && s.len >= needed) { - if (c == search) + char first = needle[0]; + foreach (i, c: s[..^needed]) { - if (!match) index_start = i; - match++; - if (match == needed) return index_start; - search = needle[match]; - continue; - } - if (match) - { - match = 0; - search = needle[0]; + if (c == first && s[i:needed] == needle) return i; } } return SearchResult.MISSING?; @@ -289,24 +277,12 @@ fn usz! String.index_of(s, String needle) fn usz! String.rindex_of(s, String needle) { usz needed = needle.len; - char search = needle[^1]; - if (needed == 1) return s.rindex_of_char(search); - usz match; - usz index_start; - foreach_r (usz i, char c : s) + if (needed > 0 && s.len >= needed) { - if (c == search) + char first = needle[0]; + foreach_r (i, c: s[..^needed]) { - if (!match) index_start = i; - match++; - if (match == needed) return index_start - needle.len + 1; - search = needle[^(match + 1)]; - continue; - } - if (match) - { - match = 0; - search = needle[^1]; + if (c == first && s[i:needed] == needle) return i; } } return SearchResult.MISSING?; diff --git a/test/unit/stdlib/core/string.c3 b/test/unit/stdlib/core/string.c3 index b02610730..04b8e816c 100644 --- a/test/unit/stdlib/core/string.c3 +++ b/test/unit/stdlib/core/string.c3 @@ -80,6 +80,7 @@ fn void! test_index_of() String test = "hello world hello"; assert(test.index_of("o")! == 4); assert(test.index_of("ll")! == 2); + assert(test.index_of(" hello")! == 11); assert(@catchof(test.index_of("wi"))); } @@ -90,6 +91,7 @@ fn void! test_rindex_of() assert(test.rindex_of("ll")! == 14); assert(test.rindex_of("he")! == 12); assert(test.rindex_of("world")! == 6); + assert(test.rindex_of("hello ")! == 0); assert(@catchof(test.rindex_of("wi"))); } @@ -98,6 +100,7 @@ fn void! test_index_of_char() String test = "hello world hello"; assert(test.index_of_char('o')! == 4); assert(test.index_of_char('l')! == 2); + assert(test.index_of_char('h')! == 0); assert(@catchof(test.index_of_char('x'))); } @@ -106,5 +109,6 @@ fn void! test_rindex_of_char() String test = "hello world hello"; assert(test.rindex_of_char('o')! == 16); assert(test.rindex_of_char('l')! == 15); + assert(test.rindex_of_char('h')! == 12); assert(@catchof(test.index_of_char('x'))); } \ No newline at end of file