internal/bytealg: add generic LastIndexByte{,String}

To avoid duplicating them in net/netip and os and to allow these
packages automatically benefiting from future performance improvements
when optimized native LastIndexByte{,String} implementations are added.

For #36891

Change-Id: I4905a4742273570c2c36b867df57762c5bfbe1e4
Reviewed-on: https://go-review.googlesource.com/c/go/+/522475
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Tobias Klauser 2023-08-24 15:10:39 +02:00 committed by Gopher Robot
parent 43559aa9a5
commit 890a62bf1b
7 changed files with 34 additions and 38 deletions

View file

@ -83,7 +83,7 @@ func LastIndex(s, substr string) int {
case n == 0:
return len(s)
case n == 1:
return LastIndexByte(s, substr[0])
return bytealg.LastIndexByteString(s, substr[0])
case n == len(s):
if substr == s {
return 0
@ -227,12 +227,7 @@ func LastIndexAny(s, chars string) int {
// LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
func LastIndexByte(s string, c byte) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == c {
return i
}
}
return -1
return bytealg.LastIndexByteString(s, c)
}
// Generic split: splits after each instance of sep,