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

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

示例1: contextAfterText

// contextAfterText starts in context c, consumes some tokens from the front of
// s, then returns the context after those tokens and the unprocessed suffix.
func contextAfterText(c context, s []byte) (context, int) {
    if c.delim == delimNone {
        c1, i := tSpecialTagEnd(c, s)
        if i == 0 {
            // A special end tag (`</script>`) has been seen and
            // all content preceding it has been consumed.
            return c1, 0
        }
        // Consider all content up to any end tag.
        return transitionFunc[c.state](c, s[:i])
    }

    // We are at the beginning of an attribute value.

    i := bytes.IndexAny(s, delimEnds[c.delim])
    if i == -1 {
        i = len(s)
    }
    if c.delim == delimSpaceOrTagEnd {
        // http://www.w3.org/TR/html5/syntax.html#attribute-value-(unquoted)-state
        // lists the runes below as error characters.
        // Error out because HTML parsers may differ on whether
        // "<a id= onclick=f("     ends inside id's or onclick's value,
        // "<a class=`foo "        ends inside a value,
        // "<a style=font:'Arial'" needs open-quote fixup.
        // IE treats '`' as a quotation character.
        if j := bytes.IndexAny(s[:i], "\"'<=`"); j >= 0 {
            return context{
                state: stateError,
                err:   errorf(ErrBadHTML, nil, 0, "%q in unquoted attr: %q", s[j:j+1], s[:i]),
            }, len(s)
        }
    }
    if i == len(s) {
        // Remain inside the attribute.
        // Decode the value so non-HTML rules can easily handle
        //     <button onclick="alert(&quot;Hi!&quot;)">
        // without having to entity decode token boundaries.
        for u := []byte(html.UnescapeString(string(s))); len(u) != 0; {
            c1, i1 := transitionFunc[c.state](c, u)
            c, u = c1, u[i1:]
        }
        return c, len(s)
    }

    element := c.element

    // If this is a non-JS "type" attribute inside "script" tag, do not treat the contents as JS.
    if c.state == stateAttr && c.element == elementScript && c.attr == attrScriptType && !isJSType(string(s[:i])) {
        element = elementNone
    }

    if c.delim != delimSpaceOrTagEnd {
        // Consume any quote.
        i++
    }
    // On exiting an attribute, we discard all state information
    // except the state and element.
    return context{state: stateTag, element: element}, i
}

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

示例2: ExampleIndexAny

func ExampleIndexAny() {
    fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
    fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
    // Output:
    // 2
    // -1
}

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

示例3: tabsToSpaces

func tabsToSpaces(in []byte, tabsize int) []byte {
    if bytes.IndexByte(in, '\t') == -1 {
        return in
    }

    spaces := bytes.Repeat([]byte(" "), tabsize)

    var out []byte
    i := bytes.IndexAny(in, "\n\r\f\t")
    col := 0

    for i != -1 {
        out = append(out, in[:i]...)
        col += utf8.RuneCount(in[:i])
        if in[i] == '\t' {
            nspaces := tabsize - (col % tabsize)
            out = append(out, spaces[:nspaces]...)
            col += nspaces
        } else {
            // line feed
            out = append(out, in[i])
            col = 0
        }

        in = in[i+1:]
        i = bytes.IndexAny(in, "\n\r\f\t")
    }

    return append(out, in...)
}

开发者ID:rygorous,项目名称:wp2block,代码行数:30,代码来源:html2markdown.go

示例4: parserank

func parserank(barr []byte) (int, error) {
    ndx1 := bytes.IndexAny(barr, ":")
    if ndx1 == -1 {
        return 0, errors.New("Malformed rank line, could not find rank")
    }
    barr = barr[ndx1+1:]
    ndx1 = bytes.IndexAny(barr, ":")
    if ndx1 == -1 {
        return 0, errors.New("Malformed rank line, could not find rank")
    }
    barr = barr[ndx1+1:]

    // Find rows
    ndx1 = bytes.Index(barr, []byte("row"))
    if ndx1 == -1 {
        return 0, errors.New("Malformed rank line, could not find row")
    }

    // Attempt to parse the row
    nobj, err := strconv.ParseInt(string(bytes.TrimSpace(barr[:ndx1])), 10, 32)
    if err != nil {
        return 0, err
    }
    return int(nobj), nil
}

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

示例5: singleline

func singleline(w *writer, prefix string, buf []byte) {
    w.WriteString(prefix)
    idx := bytes.IndexAny(buf, "\r\n")
    for idx != -1 {
        w.Write(buf[:idx])
        w.WriteByte(' ')
        buf = buf[idx+1:]
        idx = bytes.IndexAny(buf, "\r\n")
    }
    w.Write(buf)
}

开发者ID:rygorous,项目名称:wp2block,代码行数:11,代码来源:html2markdown.go

示例6: process_data

func process_data(data []byte) (result []byte, err error) {

    fmt.Println(len(data), string(data[:10]))
    current_start := 0
    switch data[current_start] {
    case '{': // Find the closing delimiter for a dictionary: '}'
        {
            delimiter_count := 1
            position := 6
            for ;position < len(data); position++ {
                switch data[position] {
                case '}': delimiter_count--
                case '{': delimiter_count++
                }
                if delimiter_count == 0 { // found the closing delimiter
                    break
                }
            }
        }
    case '[': // Starting a list: get first list element
        result, err = process_data(data[1:])
    default: // process series of one or more comma separated quoted strings or integers followed by '}' or ']'
        {
            series := []byte {}
            series_end := bytes.IndexAny(data, "]}")
            current_start := 0
            for current_start < series_end { // process string and integer tokens
                token := []byte {}
                switch data[current_start] {
                case '"': // string
                    token_end := bytes.IndexByte(data[1:], '"')
                    token = data[1:token_end]
                    current_start = bytes.IndexAny(data, ",]}")
                default: // integer
                    next_start := bytes.IndexAny(data, ",]}")
                    token = data[current_start:next_start]
                    current_start = next_start
                }
                series = append(series, token...)
                if data[current_start] == ',' { // another token to process
                    current_start++
                    series = append(series, byte(' '))
                }
                fmt.Println(string(series))
            }
            err = nil
            result = append([]byte(nil), series...)
        }
    }
    return result, err
}

开发者ID:pawingard,项目名称:AdventofCode-2015,代码行数:51,代码来源:AofCode_12.go

示例7: EnvSub

func EnvSub(r io.Reader) (io.Reader, error) {
    bufIn := bufio.NewReader(r)
    bufOut := new(bytes.Buffer)
    for {
        chunk, err := bufIn.ReadBytes(byte('%'))
        if err != nil {
            if err == io.EOF {
                // We're done.
                bufOut.Write(chunk)
                break
            }
            return nil, err
        }
        bufOut.Write(chunk[:len(chunk)-1])

        tmp := make([]byte, 4)
        tmp, err = bufIn.Peek(4)
        if err != nil {
            if err == io.EOF {
                // End of file, write the last few bytes out and exit.
                bufOut.WriteRune('%')
                bufOut.Write(tmp)
                break
            }
            return nil, err
        }

        if string(tmp) == "ENV[" {
            // Found opening delimiter, advance the read cursor and look for
            // closing delimiter.
            tmp, err = bufIn.ReadBytes(byte('['))
            if err != nil {
                // This shouldn't happen, since the Peek succeeded.
                return nil, err
            }
            chunk, err = bufIn.ReadBytes(byte(']'))
            if err != nil {
                if err == io.EOF {
                    // No closing delimiter, return an error
                    return nil, ErrMissingCloseDelim
                }
                return nil, err
            }
            // `chunk` is now holding var name + closing delimiter.
            // var name contains invalid characters, return an error
            if bytes.IndexAny(chunk, invalidEnvChars) != -1 ||
                bytes.Index(chunk, invalidEnvPrefix) != -1 {
                return nil, ErrInvalidChars
            }
            varName := string(chunk[:len(chunk)-1])
            varVal := os.Getenv(varName)
            bufOut.WriteString(varVal)
        } else {
            // Just a random '%', not an opening delimiter, write it out and
            // keep going.
            bufOut.WriteRune('%')
        }
    }
    return bufOut, nil
}

开发者ID:Nitro,项目名称:heka,代码行数:60,代码来源:config.go

示例8: Parse

// Parse a file, based on lines. Note that arr may be modified even
// if an error occured. arr.Add is called with a single line in it, with
// any comments and leading and trailing whitespace removed.
// Subsequent parsing is left up to arr.Add
func (l LineIOParams) Parse(fn string, arr LineIOType) error {
    // Open the file
    ff, err := os.Open(fn)
    if err != nil {
        return err
    }
    defer ff.Close()

    scan := bufio.NewScanner(ff)
    scan.Split(bufio.ScanLines)
    var bb []byte
    var n int
    for scan.Scan() {
        // Trim out leading and trailing whitespace
        bb = scan.Bytes()
        bb = bytes.TrimSpace(bb)
        if n = bytes.IndexAny(bb, l.Comment); n != -1 {
            bb = bb[0:n]
        }
        if len(bb) > 0 {
            err = arr.Add(bb)
            if err != nil {
                return err
            }
        }
    }
    return scan.Err()
}

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

示例9: validate

// validate validates the options.
func (opts *PUBLISHOptions) validate() error {
    // Check the QoS.
    if !mqtt.ValidQoS(opts.QoS) {
        return ErrInvalidQoS
    }

    // Check the length of the Topic Name.
    if len(opts.TopicName) > maxStringsLen {
        return ErrTopicNameExceedsMaxStringsLen
    }

    // Check if the Topic Name contains the wildcard characters.
    if bytes.IndexAny(opts.TopicName, wildcards) != -1 {
        return ErrTopicNameContainsWildcards
    }

    // Check the length of the Application Message.
    if len(opts.Message) > maxStringsLen {
        return ErrMessageExceedsMaxStringsLen
    }

    // End the validation if the QoS equals to QoS 0.
    if opts.QoS == mqtt.QoS0 {
        return nil
    }

    // Check the Packet Identifier.
    if opts.PacketID == 0 {
        return ErrInvalidPacketID
    }

    return nil
}

开发者ID:mezzato,项目名称:gmq,代码行数:34,代码来源:publish_options.go

示例10: parsehdr

func parsehdr(barr []byte, fn string) (int, error) {
    barr = bytes.TrimSpace(barr)
    // First character is a #
    if barr[0] != '#' {
        return 0, errors.New("Missing # to start header")
    }
    barr = barr[1:]

    // Second elt is the filename
    ndx1 := bytes.IndexAny(barr, ":")
    if ndx1 == -1 {
        return 0, errors.New("Malformed header, couldn't find filename")
    }
    if bytes.Compare(bytes.TrimSpace(barr[:ndx1]), []byte(fn)) != 0 {
        return 0, errors.New("Malformed header, unable to find filename in header")
    }

    // Find ranks
    barr = barr[ndx1+1:]
    ndx1 = bytes.Index(barr, []byte("rank"))
    if ndx1 == -1 {
        return 0, errors.New("Malformed header, could not find rank")
    }

    // Attempt to parse the rank
    rank64, err := strconv.ParseInt(string(bytes.TrimSpace(barr[:ndx1])), 10, 32)
    if err != nil {
        return 0, err
    }
    return int(rank64), nil
}

开发者ID:npadmana,项目名称:go-bigsims,代码行数:31,代码来源:gioprint.go

示例11: header

func (r *Reader) header(line []byte) (seqio.SequenceAppender, error) {
    s := r.t.Clone().(seqio.SequenceAppender)
    fieldMark := bytes.IndexAny(line, " \t")
    var err error
    if fieldMark < 0 {
        err = s.SetName(string(line[len(r.IDPrefix):]))
        return s, err
    } else {
        err = s.SetName(string(line[len(r.IDPrefix):fieldMark]))
        _err := s.SetDescription(string(line[fieldMark+1:]))
        if err != nil || _err != nil {
            switch {
            case err == _err:
                return s, err
            case err != nil && _err != nil:
                return s, fmt.Errorf("fasta: multiple errors: name: %s, desc:%s", err, _err)
            case err != nil:
                return s, err
            case _err != nil:
                return s, _err
            }
        }
    }

    return s, nil
}

开发者ID:cgravill,项目名称:antha,代码行数:26,代码来源:fasta.go

示例12: GetInfo

func GetInfo(pid int) (info Info, err error) {
    info.Pid = pid
    var bs []byte
    bs, err = ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
    if err != nil {
        return
    }
    var comm = string(bs)
    if strings.HasSuffix(comm, "\x00") {
        comm = comm[:len(comm)-1]
    }
    info.Comm = strings.Replace(comm, "\x00", " ", -1)
    bs, err = ioutil.ReadFile(fmt.Sprintf("/proc/%d/smaps", pid))
    if err != nil {
        return
    }
    var total int64
    for _, line := range bytes.Split(bs, []byte("\n")) {
        if bytes.HasPrefix(line, []byte("Swap:")) {
            start := bytes.IndexAny(line, "0123456789")
            end := bytes.Index(line[start:], []byte(" "))
            size, err := strconv.ParseInt(string(line[start:start+end]), 10, 0)
            if err != nil {
                continue
            }
            total += size
        }
    }
    info.Size = total
    return
}

开发者ID:Ma233,项目名称:swapview,代码行数:31,代码来源:swapview.go

示例13: lex

func (l *lexer) lex() {
    var err error
    defer func() {
        close(l.optchan)
        close(l.errchan)
    }()
    next := l.lexNextSection
    for next != nil {
        if l.buf.Buffered() >= SYSTEMD_LINE_MAX {
            // systemd truncates lines longer than LINE_MAX
            // https://bugs.freedesktop.org/show_bug.cgi?id=85308
            // Rather than allowing this to pass silently, let's
            // explicitly gate people from encountering this
            line, err := l.buf.Peek(SYSTEMD_LINE_MAX)
            if err != nil {
                l.errchan <- err
                return
            }
            if bytes.IndexAny(line, SYSTEMD_NEWLINE) == -1 {
                l.errchan <- ErrLineTooLong
                return
            }
        }

        next, err = next()
        if err != nil {
            l.errchan <- err
            return
        }
    }
}

开发者ID:pmoust,项目名称:go-systemd,代码行数:31,代码来源:deserialize.go

示例14: ParseMatches

func ParseMatches(r io.Reader) ([]Match, error) {
    s := bufio.NewScanner(os.Stdin)
    s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
        i := bytes.IndexAny(data, "\n \t\r")
        if len(data) == 0 {
            return 0, nil, nil
        } else if i == -1 && !atEOF {
            return 0, nil, nil
        } else if i == -1 && atEOF {
            return len(data), bytes.TrimSpace(data), nil
        }

        token = bytes.TrimSpace(data[:i])
        if len(token) == 0 {
            token = nil
        }
        return i + 1, token, nil
    })

    matches := []Match{}
    for {
        if !s.Scan() {
            break
        }
        winner := s.Text()
        if !s.Scan() {
            return nil, errors.New("odd number of players causes opponentless match")
        }
        loser := s.Text()

        matches = append(matches, Match{winner, loser})
    }
    return matches, s.Err()
}

开发者ID:rwcarlsen,项目名称:tournament,代码行数:34,代码来源:main.go

示例15: censorPasswords

func censorPasswords(m *HttpMessage, msg []byte) {

    keywords := _Config.Passwords.Hide_keywords

    if m.IsRequest && m.ContentLength > 0 &&
        strings.Contains(m.ContentType, "urlencoded") {
        for _, keyword := range keywords {
            index := bytes.Index(msg[m.bodyOffset:], []byte(keyword))
            if index > 0 {
                start_index := m.bodyOffset + index + len(keyword)
                end_index := bytes.IndexAny(msg[m.bodyOffset+index+len(keyword):], "& \r\n")
                if end_index > 0 {
                    end_index += m.bodyOffset + index
                    if end_index > m.end {
                        end_index = m.end
                    }
                } else {
                    end_index = m.end
                }

                if end_index-start_index < 120 {
                    for i := start_index; i < end_index; i++ {
                        msg[i] = byte('*')
                    }
                }
            }
        }
    }
}

开发者ID:mk-qi,项目名称:packetbeat,代码行数:29,代码来源:http.go

示例16: tLineCmt

// tLineCmt is the context transition function for //comment states.
func tLineCmt(c context, s []byte) (context, int) {
    var lineTerminators string
    var endState state
    switch c.state {
    case stateJSLineCmt:
        lineTerminators, endState = "\n\r\u2028\u2029", stateJS
    case stateCSSLineCmt:
        lineTerminators, endState = "\n\f\r", stateCSS
        // Line comments are not part of any published CSS standard but
        // are supported by the 4 major browsers.
        // This defines line comments as
        //     LINECOMMENT ::= "//" [^\n\f\d]*
        // since http://www.w3.org/TR/css3-syntax/#SUBTOK-nl defines
        // newlines:
        //     nl ::= #xA | #xD #xA | #xD | #xC
    default:
        panic(c.state.String())
    }

    i := bytes.IndexAny(s, lineTerminators)
    if i == -1 {
        return c, len(s)
    }
    c.state = endState
    // Per section 7.4 of EcmaScript 5 : http://es5.github.com/#x7.4
    // "However, the LineTerminator at the end of the line is not
    // considered to be part of the single-line comment; it is
    // recognized separately by the lexical grammar and becomes part
    // of the stream of input elements for the syntactic grammar."
    return c, i
}

开发者ID:strongo,项目名称:templates,代码行数:32,代码来源:transition.go

示例17: indexAny

func indexAny(s []byte, chars string) {
    if i := bytes.IndexAny(s, chars); i == -1 {
        log.Printf("No unicode characters in %q appear in %s", chars, s)
    } else {
        log.Printf("A unicode character in %q appears at index %d in %s", chars, i, s)
    }
}

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

示例18: Scan

func (s *Statements) Scan() {
    go s.Parser.Parse()

    scanner := bufio.NewScanner(s.Parser)

    depth := 0
    scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
        if i := bytes.IndexAny(data, ";{}"); i >= 0 {
            if string(data[i]) == "{" {
                depth++
            } else if string(data[i]) == "}" {
                depth--
            }
            return i + 1, data[0:i], nil
        } else if atEOF {
            return len(data), nil, nil
        }
        return 0, nil, nil
    })

    for scanner.Scan() {
        stmt := string(bytes.Join(bytes.Fields(scanner.Bytes()), []byte{' '}))
        s.stmts <- &stmtMsg{stmt, depth}
    }
}

开发者ID:skanjo,项目名称:jag,代码行数:25,代码来源:parser.go

示例19: tJS

// tJS is the context transition function for the JS state.
func tJS(c context, s []byte) (context, int) {
    i := bytes.IndexAny(s, `"'/`)
    if i == -1 {
        // Entire input is non string, comment, regexp tokens.
        c.jsCtx = nextJSCtx(s, c.jsCtx)
        return c, len(s)
    }
    c.jsCtx = nextJSCtx(s[:i], c.jsCtx)
    switch s[i] {
    case '"':
        c.state, c.jsCtx = stateJSDqStr, jsCtxRegexp
    case '\'':
        c.state, c.jsCtx = stateJSSqStr, jsCtxRegexp
    case '/':
        switch {
        case i+1 < len(s) && s[i+1] == '/':
            c.state, i = stateJSLineCmt, i+1
        case i+1 < len(s) && s[i+1] == '*':
            c.state, i = stateJSBlockCmt, i+1
        case c.jsCtx == jsCtxRegexp:
            c.state = stateJSRegexp
        case c.jsCtx == jsCtxDivOp:
            c.jsCtx = jsCtxRegexp
        default:
            return context{
                state: stateError,
                err:   errorf(ErrSlashAmbig, 0, "'/' could start a division or regexp: %.32q", s[i:]),
            }, len(s)
        }
    default:
        panic("unreachable")
    }
    return c, i + 1
}

开发者ID:strongo,项目名称:templates,代码行数:35,代码来源:transition.go

示例20: main

func main() {
    s := []byte("大家好大家早")
    index := bytes.IndexAny(s, "好早")
    if index >= 0 {
        fmt.Printf("%d: %s\n", index, string(s[index:]))
    }
}

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

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