| 1 |
package algo |
| 2 |
|
| 3 |
// Reference scanners over a []rune, with no representation tricks. |
| 4 |
// |
| 5 |
// They have two roles. Where reinterpreting a []rune as little-endian bytes is |
| 6 |
// not valid, they are the shipped implementation, via runeindex_others.go. |
| 7 |
// Everywhere else, the tests feed the same inputs to these and to the byte-view |
| 8 |
// scanners in runeindex_x86.go and require identical answers. |
| 9 |
// |
| 10 |
// They carry no build tag so that both roles hold on every platform. Otherwise |
| 11 |
// the portable build would be code that nothing here ever runs. |
| 12 |
|
| 13 |
func indexAsciiRuneRef(runes []rune, caseSensitive bool, b byte, from int) int { |
| 14 |
lower, upper := rune(b), rune(-1) |
| 15 |
if !caseSensitive && b >= 'a' && b <= 'z' { |
| 16 |
upper = rune(b - 32) |
| 17 |
} |
| 18 |
for i := from; i < len(runes); i++ { |
| 19 |
if runes[i] == lower || runes[i] == upper { |
| 20 |
return i |
| 21 |
} |
| 22 |
} |
| 23 |
return -1 |
| 24 |
} |
| 25 |
|
| 26 |
func lastIndexAsciiRuneRef(runes []rune, caseSensitive bool, b byte, from int) int { |
| 27 |
lower, upper := rune(b), rune(-1) |
| 28 |
if !caseSensitive && b >= 'a' && b <= 'z' { |
| 29 |
upper = rune(b - 32) |
| 30 |
} |
| 31 |
for i := len(runes) - 1; i >= from; i-- { |
| 32 |
if runes[i] == lower || runes[i] == upper { |
| 33 |
return i |
| 34 |
} |
| 35 |
} |
| 36 |
return -1 |
| 37 |
} |
| 38 |
|
| 39 |
func indexRuneRef(runes []rune, r rune, from int) int { |
| 40 |
for i := from; i < len(runes); i++ { |
| 41 |
if runes[i] == r { |
| 42 |
return i |
| 43 |
} |
| 44 |
} |
| 45 |
return -1 |
| 46 |
} |
| 47 |
|
| 48 |
func lastIndexRuneRef(runes []rune, r rune, from int) int { |
| 49 |
for i := len(runes) - 1; i >= from; i-- { |
| 50 |
if runes[i] == r { |
| 51 |
return i |
| 52 |
} |
| 53 |
} |
| 54 |
return -1 |
| 55 |
} |