diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2023-08-24 15:10:39 +0200 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-08-25 15:08:28 +0000 |
commit | 890a62bf1b8022833d3cb616d3d7911b7f1d289a (patch) | |
tree | 342cf7087e823193b0092689329eb50ba43647ab /src/strings | |
parent | 43559aa9a5a550ba7dac224a174130e42f93de99 (diff) | |
download | go-890a62bf1b8022833d3cb616d3d7911b7f1d289a.tar.gz go-890a62bf1b8022833d3cb616d3d7911b7f1d289a.zip |
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>
Diffstat (limited to 'src/strings')
-rw-r--r-- | src/strings/strings.go | 9 |
1 files changed, 2 insertions, 7 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go index 2dd4321142..301cd8667e 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -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, |