mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 20:11:17 +00:00
std/lib: simplify String.{,r}index_of and improve speed for the index… (#907)
* std/lib: simplify String.{,r}index_of and improve speed for the index_of one
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
* lib/std/collections: add EnumMap.get_ref
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
---------
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
@@ -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?;
|
||||
|
||||
Reference in New Issue
Block a user