]> Cypherpunks repositories - gostls13.git/commitdiff
strings: improve readability of IndexAny and LastIndexAny functions.
authorGabriel Aszalos <gabriel.aszalos@gmail.com>
Mon, 25 Sep 2017 12:54:37 +0000 (14:54 +0200)
committerIan Lance Taylor <iant@golang.org>
Mon, 25 Sep 2017 18:23:11 +0000 (18:23 +0000)
This change removes the check of len(chars) > 0 inside the Index and
IndexAny functions which was redundant.

Change-Id: Iffbc0f2b3332c6e31c7514b5f644b6fe7bdcfe0d
Reviewed-on: https://go-review.googlesource.com/65910
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
src/strings/strings.go

index caabc5affd62dc3e8daae6c5a8f5f360760b8c2b..a7941fbb90f42ed52f386bf5fddb03fe72363a3a 100644 (file)
@@ -166,22 +166,20 @@ func IndexRune(s string, r rune) int {
 // IndexAny returns the index of the first instance of any Unicode code point
 // from chars in s, or -1 if no Unicode code point from chars is present in s.
 func IndexAny(s, chars string) int {
-       if len(chars) > 0 {
-               if len(s) > 8 {
-                       if as, isASCII := makeASCIISet(chars); isASCII {
-                               for i := 0; i < len(s); i++ {
-                                       if as.contains(s[i]) {
-                                               return i
-                                       }
+       if len(s) > 8 {
+               if as, isASCII := makeASCIISet(chars); isASCII {
+                       for i := 0; i < len(s); i++ {
+                               if as.contains(s[i]) {
+                                       return i
                                }
-                               return -1
                        }
+                       return -1
                }
-               for i, c := range s {
-                       for _, m := range chars {
-                               if c == m {
-                                       return i
-                               }
+       }
+       for i, c := range s {
+               for _, m := range chars {
+                       if c == m {
+                               return i
                        }
                }
        }
@@ -192,24 +190,22 @@ func IndexAny(s, chars string) int {
 // point from chars in s, or -1 if no Unicode code point from chars is
 // present in s.
 func LastIndexAny(s, 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.DecodeLastRuneInString(s[:i])
-                       i -= size
-                       for _, c := range chars {
-                               if r == c {
-                                       return i
-                               }
+       }
+       for i := len(s); i > 0; {
+               r, size := utf8.DecodeLastRuneInString(s[:i])
+               i -= size
+               for _, c := range chars {
+                       if r == c {
+                               return i
                        }
                }
        }