Skip to content
Snippets Groups Projects
bytes.go 29.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Russ Cox's avatar
    Russ Cox committed
    			j += Index(s[start:], old)
    		}
    		w += copy(t[w:], s[start:j])
    		w += copy(t[w:], new)
    		start = j + len(old)
    	}
    	w += copy(t[w:], s[start:])
    	return t[0:w]
    }
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    // ReplaceAll returns a copy of the slice s with all
    // non-overlapping instances of old replaced by new.
    // If old is empty, it matches at the beginning of the slice
    // and after each UTF-8 sequence, yielding up to k+1 replacements
    // for a k-rune slice.
    func ReplaceAll(s, old, new []byte) []byte {
    	return Replace(s, old, new, -1)
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // EqualFold reports whether s and t, interpreted as UTF-8 strings,
    
    // are equal under Unicode case-folding, which is a more general
    // form of case-insensitivity.
    
    Russ Cox's avatar
    Russ Cox committed
    func EqualFold(s, t []byte) bool {
    	for len(s) != 0 && len(t) != 0 {
    		// Extract first rune from each.
    
    Russ Cox's avatar
    Russ Cox committed
    		var sr, tr rune
    
    Russ Cox's avatar
    Russ Cox committed
    		if s[0] < utf8.RuneSelf {
    
    Russ Cox's avatar
    Russ Cox committed
    			sr, s = rune(s[0]), s[1:]
    
    Russ Cox's avatar
    Russ Cox committed
    		} else {
    			r, size := utf8.DecodeRune(s)
    			sr, s = r, s[size:]
    		}
    		if t[0] < utf8.RuneSelf {
    
    Russ Cox's avatar
    Russ Cox committed
    			tr, t = rune(t[0]), t[1:]
    
    Russ Cox's avatar
    Russ Cox committed
    		} else {
    			r, size := utf8.DecodeRune(t)
    			tr, t = r, t[size:]
    		}
    
    		// If they match, keep going; if not, return false.
    
    		// Easy case.
    		if tr == sr {
    			continue
    		}
    
    		// Make sr < tr to simplify what follows.
    		if tr < sr {
    			tr, sr = sr, tr
    		}
    		// Fast check for ASCII.
    
    		if tr < utf8.RuneSelf {
    			// ASCII only, sr/tr must be upper/lower case
    			if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
    
    Russ Cox's avatar
    Russ Cox committed
    				continue
    			}
    			return false
    		}
    
    
    		// General case. SimpleFold(x) returns the next equivalent rune > x
    
    Russ Cox's avatar
    Russ Cox committed
    		// or wraps around to smaller values.
    		r := unicode.SimpleFold(sr)
    		for r != sr && r < tr {
    			r = unicode.SimpleFold(r)
    		}
    		if r == tr {
    			continue
    		}
    		return false
    	}
    
    
    	// One string is empty. Are both?
    
    Russ Cox's avatar
    Russ Cox committed
    	return len(s) == len(t)
    }
    
    // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
    func Index(s, sep []byte) int {
    	n := len(sep)
    	switch {
    	case n == 0:
    		return 0
    	case n == 1:
    		return IndexByte(s, sep[0])
    	case n == len(s):
    		if Equal(sep, s) {
    			return 0
    		}
    		return -1
    	case n > len(s):
    		return -1
    	case n <= bytealg.MaxLen:
    		// Use brute force when s and sep both are small
    		if len(s) <= bytealg.MaxBruteForce {
    			return bytealg.Index(s, sep)
    		}
    
    		c0 := sep[0]
    		c1 := sep[1]
    
    		t := len(s) - n + 1
    
    		for i < t {
    			if s[i] != c0 {
    
    				// IndexByte is faster than bytealg.Index, so use it as long as
    				// we're not getting lots of false positives.
    
    				o := IndexByte(s[i+1:t], c0)
    
    			if s[i+1] == c1 && Equal(s[i:i+n], sep) {
    
    				return i
    			}
    			fails++
    			i++
    			// Switch to bytealg.Index when IndexByte produces too many false positives.
    			if fails > bytealg.Cutover(i) {
    				r := bytealg.Index(s[i:], sep)
    				if r >= 0 {
    					return r + i
    				}
    				return -1
    			}
    		}
    		return -1
    	}
    
    	c0 := sep[0]
    	c1 := sep[1]
    
    	t := len(s) - n + 1
    	for i < t {
    		if s[i] != c0 {
    
    			o := IndexByte(s[i+1:t], c0)
    
    		if s[i+1] == c1 && Equal(s[i:i+n], sep) {
    
    		if fails >= 4+i>>4 && i < t {
    
    			// Give up on IndexByte, it isn't skipping ahead
    			// far enough to be better than Rabin-Karp.
    			// Experiments (using IndexPeriodic) suggest
    			// the cutover is about 16 byte skips.
    			// TODO: if large prefixes of sep are matching
    			// we should cutover at even larger average skips,
    			// because Equal becomes that much more expensive.
    			// This code does not take that effect into account.
    
    			j := bytealg.IndexRabinKarpBytes(s[i:], sep)