package helpersimport ()func ( []rune, []rune) int {// special caseiflen() == 0 {return -1 }// naive versionfor , := range {ifslices.Contains(, ) {return } }return -1}func ( []rune, rune) int {//TODO: bytes optimization?returnslices.Index(, )}func ( []rune, , rune) int {for , := range {if == || == {return } }return -1}func ( []rune, , , rune) int {for , := range {if == || == || == {return } }return -1}func ( []rune, , rune) int {for , := range {if >= && <= {return } }return -1}func ( []rune, []rune) int {for , := range { := falsefor , := range {if == { = truebreak } }if ! {return } }return -1}func ( []rune, rune) int {for , := range {if != {return } }return -1}func ( []rune, , rune) int {for , := range {if != && != {return } }return -1}func ( []rune, , , rune) int {for , := range {if != && != && != {return } }return -1}func ( []rune, , rune) int {for , := range {if > {return }if < {return } }return -1}func ( []rune, func( rune) bool) int {for := range {if ([]) {return } }return -1}func ( []rune, syntax.CharSet) int {//TODO: thispanic("not implemented")}func ( []rune, []rune) int { := len() - len() := [0] := len() - 1 := []for := ; >= 0; -- {//TODO: check 2 chars needed? // match start and end...check the middleif [] == && [+] == {// found our first char // check if the rest are equalifbytesEqual([:+len()], ) {return } } }//not foundreturn -1}func ( []rune, rune) int {for := len() - 1; >= 0; -- {if [] != {return } }return -1}func ( []rune, rune) int {for := len() - 1; >= 0; -- {if [] == {// found our charreturn } }//not foundreturn -1}func ( []rune, , rune) int {for := len() - 1; >= 0; -- {if [] >= && [] <= {return } }return -1}//TODO: LastIndexOf methods//IndexOfAnyInRange//LastIndexOfAnyInRange//LastIndexOfAnyExceptInRange// find should always be sent in lower-casefunc ( []rune, []rune) int {// search the in slice for the "find" slice, ignoring case in the comparisons := len() - len() := [0]for := 0; <= ; ++ {if [] != && unicode.ToLower([]) != {continue } := truefor := 1; < len(); ++ { := [+]if != [] && unicode.ToLower() != [] { = falsebreak } }if {return } }return -1}func ( []rune, []rune) int {// search the in slice for the "find" slice, ignoring case in the comparisons // we can assume the find chars are ascii and do simple masks on themiflen() == 0 {return0 } := len() - len() := foldASCII(rune([0]))for := 0; <= ; ++ {iffoldASCII([]) != {continue } := truefor := 1; < len(); ++ {iffoldASCII([+]) != foldASCII([]) { = falsebreak } }if {return } }return -1}func (, string) int {iflen() == 0 {return0 }for , := 0, len()-len(); <= ; { := indexASCIIByteIgnoreCase([:], [0])if < 0 || + > {return -1 } := + ifEqualStringIgnoreCaseASCII([:+len()], ) {return } = + 1 }return -1}func (, string) bool {iflen() < len() {returnfalse }for := 0; < len(); ++ {iffoldASCII(rune([])) != foldASCII(rune([])) {returnfalse } }returntrue}func indexASCIIByteIgnoreCase( string, byte) int { = byte(foldASCII(rune())) := strings.IndexByte(, )if < 'a' || > 'z' {return } := strings.IndexByte(, -('a'-'A'))if < 0 {return }if >= 0 && < {return }return}func foldASCII( rune) rune {if'A' <= && <= 'Z' {return + ('a' - 'A') }return}func ( []rune, []rune) int {/* Since we auto-gen the find code this shouldn't happen if len(find) == 0 { //special case return -1 }*/ := len() - len() := [0]//TODO: benchmark checking last char too or first two charsfor := 0; <= ; ++ {// match start...check the restif [] == {// found our first char // check if the rest are equalifbytesEqual([:+len()], ) {return }/*if slices.Equal(in[i:i+len(find)], find) { return i }*/ } }//not foundreturn -1}func ( []rune, []rune) bool {// if text is less than our "begin" then can't find itiflen() < len() {returnfalse }returnbytesEqual([:len()], )/*for i := 0; i < len(find); i++ { if in[i] != find[i] { return false } } return true*/}//StartsWithIgnoreCaseAscii would be faster// find should always be sent in lower-casefunc ( []rune, []rune) bool {// if text is less than our "begin" then can't find itiflen() < len() {returnfalse }for := 0; < len(); ++ {if [] == [] {// if we match the char exactly then we're goodcontinue }// if the to-lower still doesn't match then it's not a matchifunicode.ToLower([]) != [] {returnfalse } }returntrue}// internal function, assumes the bounds are already set right on the slices for equality// casts the rune slices to bytes to use framework fast []byte comparisonfunc bytesEqual(, []rune) bool { := unsafe.Slice((*byte)(unsafe.Pointer(&[0])), len()*4) := unsafe.Slice((*byte)(unsafe.Pointer(&[0])), len()*4)returnbytes.Equal(, )}func ( []rune, int, int, []rune) bool {iflen() == 0 {returntrue }returnbytesEqual([:+], )}func ( []rune, int, int, []rune) bool {//fast path if case matchesifEquals(, , , ) {returntrue }// search the in slice for the "find" slice, ignoring case in the comparisons // we can't assume casing or ascii-ness for either letter, have to toLower them bothfor := 0; < len(); ++ { := [+] := []if != && unicode.ToLower() != unicode.ToLower() {returnfalse } }// we've checked all chars and found matches every timereturntrue}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.