Skip to content
Myr

runeindex_ref.go

This file

Path

src/algo/runeindex_ref.go

55 lines · 1.4 KiB · Go

Last changed by

Prefilter rune-mode input for non-ASCII patternsJunegunn Choibefore adoption9dfdba4

History of this file

Take it

1package algo23// Reference scanners over a []rune, with no representation tricks.4//5// They have two roles. Where reinterpreting a []rune as little-endian bytes is6// 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-view8// scanners in runeindex_x86.go and require identical answers.9//10// They carry no build tag so that both roles hold on every platform. Otherwise11// the portable build would be code that nothing here ever runs.1213func 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 i21		}22	}23	return -124}2526func 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 i34		}35	}36	return -137}3839func indexRuneRef(runes []rune, r rune, from int) int {40	for i := from; i < len(runes); i++ {41		if runes[i] == r {42			return i43		}44	}45	return -146}4748func lastIndexRuneRef(runes []rune, r rune, from int) int {49	for i := len(runes) - 1; i >= from; i-- {50		if runes[i] == r {51			return i52		}53	}54	return -155}