本文整理汇总了Golang中bytes.IndexFunc函数的典型用法代码示例。如果您正苦于以下问题:Golang IndexFunc函数的具体用法?Golang IndexFunc怎么用?Golang IndexFunc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IndexFunc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleIndexFunc
func ExampleIndexFunc() {
f := func(c rune) bool {
return unicode.Is(unicode.Han, c)
}
fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
// Output:
// 7
// -1
}
示例2: OffsetLine
// TODO:
func (f *File) OffsetLine(ln, start int) (offset int, e error) {
if start < 0 || start > len(f.b) {
return 0, memfile.OutOfBounds
}
if ln == 0 {
i := bytes.LastIndex(f.b[:start], []byte("\n"))
return i + 1, nil
}
if ln < 0 {
i := 0
return bytes.LastIndexFunc(f.b[:start], func(r rune) bool {
if r == '\n' {
if i == ln {
return true
}
i--
}
return false
}) + 1, nil
}
i := 0
va := bytes.IndexFunc(f.b[start:], func(r rune) bool {
if r == '\n' {
i++
if i == ln {
return true
}
}
return false
})
if va != -1 {
return va + start + 1, nil
}
return len(f.b), nil
}
示例3: trimSpaceLeft
// Trim space from the left.
func (buf *parserBuf) trimSpaceLeft() {
n := bytes.IndexFunc(buf.bytes, func(r rune) bool { return !unicode.IsSpace(r) })
if n == -1 {
n = len(buf.bytes)
}
buf.trimBytesLeft(n)
}
示例4: Add
func (l *LogFile) Add(b []byte) error {
// Extract the first word --- which will be the command; the rest will be args
if (l.linenum % l.Size) == l.Rank {
ndx := bytes.IndexFunc(b, unicode.IsSpace)
var comm, args string
if ndx == -1 {
comm = string(b)
args = ""
} else {
comm = string(b[0:ndx])
args = string(b[ndx:])
}
fmt.Fprintln(l.F, "-->", string(b))
out, err := exec.Command(comm, args).CombinedOutput()
if err != nil {
fmt.Fprintln(l.F, "-->ERROR : ", err)
fmt.Fprintln(l.F, "-->Output follows :")
}
fmt.Fprintln(l.F, string(out))
fmt.Fprintln(l.F, "-->")
}
l.linenum += 1
return nil
}
示例5: writeBytesKey
func writeBytesKey(w io.Writer, key []byte) error {
if len(key) == 0 || bytes.IndexFunc(key, invalidKeyRune) != -1 {
return ErrInvalidKey
}
_, err := w.Write(key)
return err
}
示例6: indexFunc
func indexFunc(s []byte, f func(rune) bool) {
if i := bytes.IndexFunc(s, f); i == -1 {
log.Printf("Something controlled by %#v does NOT appear in %s", f, s)
} else {
log.Printf("Something controlled by %#v appears at index %d in %s", f, i, s)
}
}
示例7: IterWords
func IterWords(data []byte, cb func(word []byte)) {
for {
i := bytes.IndexFunc(data, IsWord)
if i == -1 {
return
}
data = data[i:]
i = bytes.IndexFunc(data, func(r rune) bool {
return !IsWord(r)
})
if i == -1 {
return
}
cb(data[:i])
data = data[i:]
}
}
示例8: lastContiguousIndexFunc
func lastContiguousIndexFunc(s []byte, f func(r rune) bool) int {
i := bytes.IndexFunc(s, func(r rune) bool {
return !f(r)
})
if i == -1 {
i = len(s)
}
return i - 1
}
示例9: writeBytesValue
func writeBytesValue(w io.Writer, value []byte) error {
var err error
if bytes.IndexFunc(value, needsQuotedValueRune) >= 0 {
_, err = writeQuotedBytes(w, value)
} else {
_, err = w.Write(value)
}
return err
}
示例10: consumeToken
func consumeToken(v []byte) (token, rest []byte) {
notPos := bytes.IndexFunc(v, isNotTokenChar)
if notPos == -1 {
return v, nil
}
if notPos == 0 {
return nil, v
}
return v[0:notPos], v[notPos:]
}
示例11: main
/*IndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
It returns the byte index in s of the first Unicode code point satisfying f(c), or -1 if none do.*/
func main() {
s := []byte("123456677")
f := func(a rune) bool {
if a > '6' {
return true
}
return false
}
fmt.Println(bytes.IndexFunc(s, f))
}
示例12: glogBody
// glogBody logs a body output that could be either JSON or protobuf. It explicitly guards against
// allocating a new string for the body output unless necessary. Uses a simple heuristic to determine
// whether the body is printable.
func glogBody(prefix string, body []byte) {
if glog.V(8) {
if bytes.IndexFunc(body, func(r rune) bool {
return r < 0x0a
}) != -1 {
glog.Infof("%s:\n%s", prefix, hex.Dump(body))
} else {
glog.Infof("%s: %s", prefix, string(body))
}
}
}
示例13: parseRule
func parseRule(sel []byte, in *bufio.Reader) Item {
// Clean up the selector.
sel = bytes.TrimSpace(sel)
sel = bytes.Replace(sel, []byte{'\t'}, []byte{' '}, -1)
sel = bytes.Replace(sel, []byte{'\n'}, []byte{' '}, -1)
for si := bytes.IndexByte(sel, ' '); si != -1; si = bytes.IndexByte(sel[si+2:], ' ') + si + 2 {
lsi := bytes.IndexFunc(sel[si+1:], func(c rune) bool { return c != ' ' })
if lsi == -1 {
// No non-space was found.
break
} else if lsi == 0 {
// The very next character was a non-space.
continue
}
copy(sel[si+1:], sel[si+lsi+1:])
sel = sel[:len(sel)-lsi]
}
sel = bytes.Replace(sel, []byte{',', ' '}, []byte{','}, -1)
sel = bytes.Replace(sel, []byte{' ', ','}, []byte{','}, -1)
sel = bytes.Replace(sel, []byte{'>', ' '}, []byte{'>'}, -1)
sel = bytes.Replace(sel, []byte{' ', '>'}, []byte{'>'}, -1)
// Read the body portion.
body, _ := in.ReadBytes('}')
// Clean up the body.
body = bytes.TrimSpace(body[:len(body)-1])
if len(body) == 0 {
// This rule doesn't do anything. It's useless. No need to
// include it in the output.
return nil
}
// Create the slice of pairs to store in the rule. (This slice will be
// extended as necessary.)
pairs := make([]pair, 0)
// Iterate over the directives in the body.
for _, p := range bytes.Split(body, []byte{';'}) {
// Clean up the pair.
p = bytes.TrimSpace(p)
i := bytes.Index(p, []byte{':'})
if i == -1 {
// Hmm. There's no colon in this pair. Something's wrong.
// We'll just silently omit it.
continue
}
// Extend our slice of pairs with the new directive.
pairs = append(pairs, pair{bytes.TrimSpace(p[:i]), bytes.TrimSpace(p[i+1:])})
}
return &rule{sel, pairs}
}
示例14: verifyBinary
// Charactor code 0x00 - 0x08 is control code (ASCII)
func verifyBinary(buf []byte) bool {
var b []byte
if len(buf) > 256 {
b = buf[:256]
} else {
b = buf
}
if bytes.IndexFunc(b, func(r rune) bool { return r < 0x09 }) != -1 {
return true
}
return false
}
示例15: StringCutRune
func StringCutRune(str string, n int) string {
b := []byte(str)
i := 0
index := bytes.IndexFunc(b, func(r rune) bool {
i++
if i > n {
return true
}
return false
})
if index < 0 {
return str
}
return string(b[:index])
}