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/internal/bytealg | |
| parent | 43559aa9a5a550ba7dac224a174130e42f93de99 (diff) | |
| download | go-890a62bf1b8022833d3cb616d3d7911b7f1d289a.tar.xz | |
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/internal/bytealg')
| -rw-r--r-- | src/internal/bytealg/lastindexbyte_generic.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/internal/bytealg/lastindexbyte_generic.go b/src/internal/bytealg/lastindexbyte_generic.go new file mode 100644 index 0000000000..b905f53c2b --- /dev/null +++ b/src/internal/bytealg/lastindexbyte_generic.go @@ -0,0 +1,23 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package bytealg + +func LastIndexByte(s []byte, c byte) int { + for i := len(s) - 1; i >= 0; i-- { + if s[i] == c { + return i + } + } + return -1 +} + +func LastIndexByteString(s string, c byte) int { + for i := len(s) - 1; i >= 0; i-- { + if s[i] == c { + return i + } + } + return -1 +} |
