aboutsummaryrefslogtreecommitdiff
path: root/src/bytes/bytes.go
diff options
context:
space:
mode:
authorGabriel Aszalos <gabriel.aszalos@gmail.com>2017-09-25 13:01:36 +0200
committerIan Lance Taylor <iant@golang.org>2017-09-25 18:05:01 +0000
commitb71f39612adb671c176586cd9b0018eb3c716f3c (patch)
treeff1ff6a3be6ba81da1731bc72f7741f9379980e9 /src/bytes/bytes.go
parentf22ba1f24786be600bfa3686a7ce5a318a96b9c9 (diff)
downloadgo-b71f39612adb671c176586cd9b0018eb3c716f3c.tar.xz
bytes: improve readability of IndexAny and LastIndexAny functions
This change removes the check of len(chars) > 0 inside the Index and IndexAny functions which was redundant. Change-Id: Ic4bf8b8a37d7f040d3ebd81b4fc45fcb386b639a Reviewed-on: https://go-review.googlesource.com/65851 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/bytes/bytes.go')
-rw-r--r--src/bytes/bytes.go66
1 files changed, 31 insertions, 35 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 446026233e..fe7c09fa9b 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -144,29 +144,27 @@ func IndexRune(s []byte, r rune) int {
// code points in chars. It returns -1 if chars is empty or if there is no code
// point in common.
func IndexAny(s []byte, chars string) int {
- if len(chars) > 0 {
- if len(s) > 8 {
- if as, isASCII := makeASCIISet(chars); isASCII {
- for i, c := range s {
- if as.contains(c) {
- return i
- }
+ if len(s) > 8 {
+ if as, isASCII := makeASCIISet(chars); isASCII {
+ for i, c := range s {
+ if as.contains(c) {
+ return i
}
- return -1
}
+ return -1
}
- var width int
- for i := 0; i < len(s); i += width {
- r := rune(s[i])
- if r < utf8.RuneSelf {
- width = 1
- } else {
- r, width = utf8.DecodeRune(s[i:])
- }
- for _, ch := range chars {
- if r == ch {
- return i
- }
+ }
+ var width int
+ for i := 0; i < len(s); i += width {
+ r := rune(s[i])
+ if r < utf8.RuneSelf {
+ width = 1
+ } else {
+ r, width = utf8.DecodeRune(s[i:])
+ }
+ for _, ch := range chars {
+ if r == ch {
+ return i
}
}
}
@@ -178,24 +176,22 @@ func IndexAny(s []byte, chars string) int {
// the Unicode code points in chars. It returns -1 if chars is empty or if
// there is no code point in common.
func LastIndexAny(s []byte, chars string) int {
- if len(chars) > 0 {
- if len(s) > 8 {
- if as, isASCII := makeASCIISet(chars); isASCII {
- for i := len(s) - 1; i >= 0; i-- {
- if as.contains(s[i]) {
- return i
- }
+ if len(s) > 8 {
+ if as, isASCII := makeASCIISet(chars); isASCII {
+ for i := len(s) - 1; i >= 0; i-- {
+ if as.contains(s[i]) {
+ return i
}
- return -1
}
+ return -1
}
- for i := len(s); i > 0; {
- r, size := utf8.DecodeLastRune(s[:i])
- i -= size
- for _, c := range chars {
- if r == c {
- return i
- }
+ }
+ for i := len(s); i > 0; {
+ r, size := utf8.DecodeLastRune(s[:i])
+ i -= size
+ for _, c := range chars {
+ if r == c {
+ return i
}
}
}