本文整理汇总了Golang中bytes.IndexFunc函数的典型用法代码### 示例。如果您正苦于以下问题:Golang IndexFunc函数的具体用法?Golang IndexFunc怎么用?Golang IndexFunc使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了IndexFunc函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}

开发者ID:achanda,项目名称:go,代码行数:10,代码来源:example_test.go

示例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
}

开发者ID:vron,项目名称:sem,代码行数:36,代码来源:gap.go

示例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)
}

开发者ID:johan-bolmsjo,项目名称:pot,代码行数:8,代码来源:parser_buf.go

示例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
}

开发者ID:npadmana,项目名称:npgo,代码行数:25,代码来源:main.go

示例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
}

开发者ID:qband,项目名称:down,代码行数:7,代码来源:encode.go

示例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)
    }
}

开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:7,代码来源:searching.go

示例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:]
    }
}

开发者ID:kisielk,项目名称:vigo,代码行数:17,代码来源:utils.go

示例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
}

开发者ID:pombredanne,项目名称:syntaxhighlight,代码行数:9,代码来源:highlight.go

示例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
}

开发者ID:qband,项目名称:down,代码行数:9,代码来源:encode.go

示例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:]
}

开发者ID:gitter-badger,项目名称:alkasir,代码行数:10,代码来源:link.go

示例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))
}

开发者ID:cwen-coder,项目名称:study-gopkg,代码行数:12,代码来源:IndexFunc.go

示例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))
        }
    }
}

开发者ID:johscheuer,项目名称:kubernetes,代码行数:14,代码来源:request.go

示例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}
}

开发者ID:AntiMS,项目名称:Simple-CSS-Shrinker,代码行数:54,代码来源:parsecss.go

示例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
}

开发者ID:ryochack,项目名称:gorep,代码行数:13,代码来源:gorep.go

示例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])
}

开发者ID:danuxguin,项目名称:dxnet,代码行数:19,代码来源:common.go

示例16: tokenizeXML

func tokenizeXML(data []byte, atEOF bool) (advance int, token []byte, err error) {
    var tcomment = []byte{'<', '!', '-', '-'}
    if bytes.HasPrefix(data, tcomment) {
        return len(tcomment), tcomment, nil
    }
    r, size := utf8.DecodeRune(data)
    if unicode.IsSpace(r) {
        return size, data[:size], nil
    }
    if data[0] == '<' || data[0] == '>' {
        return 1, data[:1], nil
    }
    if data[0] == '/' && data[1] == '>' {
        return 2, data[:2], nil
    }
    num := bytes.IndexFunc(data, nameboundary)
    if num > 0 {
        return num, data[:num], nil
    }
    return 1, data[:1], nil
}

开发者ID:speedata,项目名称:publisher,代码行数:21,代码来源:xml.go

示例17: Import

// Import parses the contents of a libotr private key file.
func (priv *PrivateKey) Import(in []byte) bool {
    mpiStart := []byte(" #")

    mpis := make([]*big.Int, 5)

    for i := 0; i < len(mpis); i++ {
        start := bytes.Index(in, mpiStart)
        if start == -1 {
            return false
        }
        in = in[start+len(mpiStart):]
        end := bytes.IndexFunc(in, notHex)
        if end == -1 {
            return false
        }
        hexBytes := in[:end]
        in = in[end:]

        if len(hexBytes)&1 != 0 {
            return false
        }

        mpiBytes := make([]byte, len(hexBytes)/2)
        if _, err := hex.Decode(mpiBytes, hexBytes); err != nil {
            return false
        }

        mpis[i] = new(big.Int).SetBytes(mpiBytes)
    }

    priv.PrivateKey.P = mpis[0]
    priv.PrivateKey.Q = mpis[1]
    priv.PrivateKey.G = mpis[2]
    priv.PrivateKey.Y = mpis[3]
    priv.PrivateKey.X = mpis[4]
    priv.PublicKey.PublicKey = priv.PrivateKey.PublicKey

    a := new(big.Int).Exp(priv.PrivateKey.G, priv.PrivateKey.X, priv.PrivateKey.P)
    return a.Cmp(priv.PrivateKey.Y) == 0
}

开发者ID:sneha29shukla,项目名称:mig,代码行数:41,代码来源:otr.go

示例18: getWord

func getWord(b []byte, pos *filePos) (string, []byte) {
    // Skip over leading whitespace
    i := 0
    for i < len(b) {
        r, size := utf8.DecodeRune(b[i:])
        if r == '\n' {
            pos.line++
        }
        if !unicode.IsSpace(r) {
            break
        }
        i += size
    }
    b = b[i:]

    // Find end of word
    i = bytes.IndexFunc(b, unicode.IsSpace)
    if i < 0 {
        i = len(b)
    }
    return string(b[0:i]), b[i:]
}

开发者ID:thotanagaraju,项目名称:hub,代码行数:22,代码来源:netrc.go

示例19: comment_format

func (p *PackageInfo) comment_format(comment, indent, preIndent string) string {
    containsOnlySpace := func(buf []byte) bool {
        isNotSpace := func(r rune) bool { return !unicode.IsSpace(r) }
        return bytes.IndexFunc(buf, isNotSpace) == -1
    }
    var buf bytes.Buffer
    const punchCardWidth = 80
    ToText(&buf, comment, indent, preIndent, punchCardWidth-2*len(indent))
    if containsOnlySpace(buf.Bytes()) {
        return ""
    }
    lines := strings.Split(buf.String(), "\n")
    if len(lines) > 0 && lines[len(lines)-1] == "" {
        lines = lines[:len(lines)-1]
    }
    for i := 0; i < len(lines); i++ {
        if lines[i] == "" || lines[i][0] != '\t' {
            lines[i] = "// " + lines[i]
        } else {
            lines[i] = "//" + lines[i]
        }
    }
    return strings.Join(lines, "\n")
}

开发者ID:iolg,项目名称:golangdoc,代码行数:24,代码来源:main.go

示例20: whitespace

func whitespace(src []byte) []byte {
    // remove needless comments
    for {
        pos := bytes.IndexFunc(src, func(r rune) bool {
            return r != ' ' && r != '\t' && r != '\n'
        })
        if pos < 0 {
            break
        }
        if pos == 0 {
            src = src[1:]
        } else {
            src = append(src[:pos], src[pos+1:]...)
        }
    }

    // parse whitespace into tokens
    tokens := opcodes{}
    for len(src) > 0 {
        op := ""
        code := Nop
        for k, v := range optable {
            if bytes.HasPrefix(src, []byte(k)) {
                op = k
                code = v
                break
            }
        }
        if op == "" {
            src = src[1:]
            continue
        }
        src = src[len(op):]
        var arg int
        switch code {
        case Push:
            // handle argument
        handle_signed_arg:
            for i := 1; i < len(src); i++ {
                switch src[i] {
                case ' ':
                    arg = (arg << 1) | 0
                case '\t':
                    arg = (arg << 1) | 1
                case '\n':
                    // Push take singed argument
                    if src[0] == '\t' {
                        arg = -arg
                    }
                    src = src[i+1:]
                    break handle_signed_arg
                }
            }
        case Mark, Call, Jump, Jz, Jn:
            // handle argument
        handle_unsigned_arg:
            for i := 0; i < len(src); i++ {
                switch src[i] {
                case ' ':
                    arg = (arg << 1) | 0
                case '\t':
                    arg = (arg << 1) | 1
                case '\n':
                    src = src[i+1:]
                    break handle_unsigned_arg
                }
            }
        }
        tokens = append(tokens, opcode{code, arg})
    }

    pc := 0
    ps := stack{}
    cs := stack{}
    heap := map[int]int{}
    for {
        token := tokens[pc]

        code, arg := token.code, token.arg
        //fmt.Println(pc, code, arg)
        pc++
        switch code {
        case Push:
            ps.push(arg)
        case Mark:
        case Dup:
            ps.dup()
        case OutN:
            fmt.Print(ps.pop())
        case OutC:
            fmt.Print(string(rune(ps.pop())))
        case Add:
            rhs := ps.pop()
            lhs := ps.pop()
            ps.push(lhs + rhs)
        case Sub:
            rhs := ps.pop()
            lhs := ps.pop()
            ps.push(lhs - rhs)
        case Mul:
//.........这里部分代码省略.........

开发者ID:mattn,项目名称:ws,代码行数:101,代码来源:ws.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng