github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/src/bytes/bytes_generic.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !amd64,!s390x
     6  
     7  package bytes
     8  
     9  // TODO: implements short string optimization on non amd64 platforms
    10  // and get rid of bytes_amd64.go
    11  
    12  // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
    13  func Index(s, sep []byte) int {
    14  	n := len(sep)
    15  	if n == 0 {
    16  		return 0
    17  	}
    18  	if n > len(s) {
    19  		return -1
    20  	}
    21  	c := sep[0]
    22  	if n == 1 {
    23  		return IndexByte(s, c)
    24  	}
    25  	i := 0
    26  	t := s[:len(s)-n+1]
    27  	for i < len(t) {
    28  		if t[i] != c {
    29  			o := IndexByte(t[i:], c)
    30  			if o < 0 {
    31  				break
    32  			}
    33  			i += o
    34  		}
    35  		if Equal(s[i:i+n], sep) {
    36  			return i
    37  		}
    38  		i++
    39  	}
    40  	return -1
    41  }