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

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

示例1: Read

// Read reads and decodes quoted-printable data from the underlying reader.
func (r *Reader) Read(p []byte) (int, error) {
    // Deviations from RFC 2045:
    // 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
    // 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
    //    with other broken QP encoders & decoders.
    var n int
    var err error
    for len(p) > 0 {
        if len(r.line) == 0 {
            if err = r.Fn(); err != nil {
                return n, err
            }
            r.line, r.rerr = r.br.ReadSlice('\n')
            r.gerr.addUnrecover(r.rerr)

            // Does the line end in CRLF instead of just LF?
            hasLF := bytes.HasSuffix(r.line, lf)
            hasCR := bytes.HasSuffix(r.line, crlf)
            wholeLine := r.line
            r.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
            if bytes.HasSuffix(r.line, softSuffix) {
                rightStripped := wholeLine[len(r.line):]
                r.line = r.line[:len(r.line)-1]
                if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
                    r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
                    r.gerr.add(r.rerr)
                }
            } else if hasLF {
                if hasCR {
                    r.line = append(r.line, '\r', '\n')
                } else {
                    r.line = append(r.line, '\n')
                }
            }
            continue
        }
        b := r.line[0]

        switch {
        case b == '=':
            b, err = readHexByte(r.line[1:])
            if err != nil {
                b = '='
                r.gerr.add(err)
                break // this modification allow bad email to be parsed too
                //return n, err
            }
            r.line = r.line[2:] // 2 of the 3; other 1 is done below
        case b == '\t' || b == '\r' || b == '\n':
        case b < ' ' || b > '~':
            //return n, fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b)
            r.gerr.add(fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b))
        }
        p[0] = b
        p = p[1:]
        r.line = r.line[1:]
        n++
    }
    return n, r.Fn()
}

开发者ID:cention-sany,项目名称:mime,代码行数:61,代码来源:reader.go

示例2: setKeys

// setKeys sets n random keys and values across each machine in a
// cluster and returns these values to later be checked with checkKeys.
// If all the values don't get set due to a machine that is down and
// error is NOT returned. An error is returned if no keys are able to be
// set.
func setKeys(cluster platform.Cluster, n int) (map[string]string, error) {
    var written = map[string]string{}
    for _, m := range cluster.Machines() {
        for i := 0; i < n; i++ {
            // random key and value, may overwrwite previous sets if
            // collision which is fine
            key := strconv.Itoa(rand.Int())[0:3]
            value := strconv.Itoa(rand.Int())[0:3]

            cmd := cluster.NewCommand("curl", "-w", "%{http_code}", "-s", fmt.Sprintf("http://%v:2379/v2/keys/%v", m.IP(), key), "-XPUT", "-d", "value="+value)
            b, err := cmd.Output()
            if err != nil {
                continue
            }

            // check for 201 or 200 resp header
            if !bytes.HasSuffix(b, []byte("200")) && !bytes.HasSuffix(b, []byte("201")) {
                continue
            }

            written[key] = value
        }
    }
    if len(written) == 0 {
        return nil, fmt.Errorf("failed to write any keys")
    }

    plog.Infof("wrote %v keys", len(written))
    return written, nil
}

开发者ID:hanscj1,项目名称:mantle,代码行数:35,代码来源:util.go

示例3: isDate

// isDate detects if we see one of the following formats:
// August 12, 2014
// Aug 10, 2014  1:02 PM EDT
// Sunday August 10 2014
// Sunday, August 10, 2014 2:36 PM EDT
// Monday, August 11, 2014 9:18:59 AM
// Sat., Feb. 7, 2015 04:35 PM
// Tue., Apr. 21, 2015 4:17 p.m.
func isDate(line []byte) bool {
    // Trim dots 'n periods
    line = bytes.Trim(line, "• .\u00a0")
    // check if it starts with a day or month
    dateStart := false
    for _, day := range daysOfWeek {
        if bytes.HasPrefix(line, day) {
            dateStart = true
            break
        }
    }
    if !dateStart {
        for _, day := range daysOfWeekShort {
            if bytes.HasPrefix(line, day) {
                dateStart = true
                break
            }
        }
    }
    if !dateStart {
        for _, month := range months {
            if bytes.HasPrefix(line, month) {
                dateStart = true
                break
            }
        }
    }

    if !dateStart {
        return false
    }

    // check if it ends with a timezone/daytime/year
    dateEnd := false
    for _, ap := range amPM {
        if bytes.HasSuffix(line, ap) {
            dateEnd = true
            break
        }
    }
    if !dateEnd {
        // newshound started in 2012. adjust if you want older data
        for i := 2012; i <= time.Now().Year(); i++ {
            if bytes.HasSuffix(line, []byte(strconv.Itoa(i))) {
                dateEnd = true
                break
            }
        }
    }
    if !dateEnd {
        for _, zone := range timezones {
            if bytes.HasSuffix(line, zone) {
                dateEnd = true
                break
            }
        }
    }

    return dateEnd
}

开发者ID:jacqui,项目名称:newshound,代码行数:68,代码来源:alert.go

示例4: codeLines

// codeLines takes a source file and returns the lines that
// span the byte range specified by start and end.
// It discards lines that end in "OMIT" and in "OMIT -->"
func codeLines(src []byte, start, end int) (lines []byte) {
    startLine := 1
    for i, b := range src {
        if i == start {
            break
        }
        if b == '\n' {
            startLine++
        }
    }
    s := bufio.NewScanner(bytes.NewReader(src[start:end]))
    for n := startLine; s.Scan(); n++ {
        l := s.Bytes()
        if bytes.HasSuffix(l, []byte("OMIT")) {
            continue
        }
        if bytes.HasSuffix(l, []byte("OMIT -->")) {
            continue
        }
        lines = append(lines, l...)
        lines = append(lines, '\n')
    }
    // TODO(miek): trim leading and trailing blanklines
    return
}

开发者ID:prodigeni,项目名称:mmark,代码行数:28,代码来源:code.go

示例5: equivalent

// equivalent does a linewise comparison of a and b.
// For each line:
//    got exactly equals want OR
//    want ends in " //substr" and is a substring of got OR
//    want ends in " //slashes" and runtime.GOOS == "windows" and got equals want with its slashes swapped for backslashes
// Otherwise equivalent returns false.
func equivalent(got, want []byte) bool {
    var (
        gotLines  = bytes.Split(got, newline)
        wantLines = bytes.Split(want, newline)
        substr    = []byte(" //substr")
        slashes   = []byte(" //slashes")
        slash     = []byte{'/'}
        gg, ww    []byte
    )

    if len(gotLines) != len(wantLines) {
        return false
    }

    for i := range gotLines {
        gg, ww = gotLines[i], wantLines[i]
        if bytes.HasSuffix(ww, slashes) {
            ww = bytes.Replace(ww[:len(ww)-len(slashes)], slash, []byte{filepath.Separator}, -1)
        }
        if !(bytes.Equal(gg, ww) || bytes.HasSuffix(ww, substr) && bytes.Contains(gg, ww[:len(ww)-len(substr)])) {
            return false
        }
    }
    return true
}

开发者ID:philipmulcahy,项目名称:godebug,代码行数:31,代码来源:endtoend_cli_test.go

示例6: checkPreformatted

// Blocks of ``` need to have blank lines on both sides or they don't look
// right in HTML.
func checkPreformatted(filePath string, fileBytes []byte) ([]byte, error) {
    f := splitByPreformatted(fileBytes)
    f = append(fileBlocks{{false, []byte{}}}, f...)
    f = append(f, fileBlock{false, []byte{}})

    output := []byte(nil)
    for i := 1; i < len(f)-1; i++ {
        prev := &f[i-1]
        block := &f[i]
        next := &f[i+1]
        if !block.preformatted {
            continue
        }
        neededSuffix := []byte("\n\n")
        for !bytes.HasSuffix(prev.data, neededSuffix) {
            prev.data = append(prev.data, '\n')
        }
        for !bytes.HasSuffix(block.data, neededSuffix) {
            block.data = append(block.data, '\n')
            if bytes.HasPrefix(next.data, []byte("\n")) {
                // don't change the number of newlines unless needed.
                next.data = next.data[1:]
                if len(next.data) == 0 {
                    f = append(f[:i+1], f[i+2:]...)
                }
            }
        }
    }
    for _, block := range f {
        output = append(output, block.data...)
    }
    return output, nil
}

开发者ID:gabrielweyer,项目名称:kubernetes,代码行数:35,代码来源:preformatted.go

示例7: SetKeys

// setKeys sets n random keys and values across each machine in a
// cluster and returns these values to later be checked with checkKeys.
// If all the values don't get set due to a machine that is down and
// error is NOT returned. An error is returned if no keys are able to be
// set.
func SetKeys(cluster platform.Cluster, n int) (map[string]string, error) {
    var written = map[string]string{}
    for _, m := range cluster.Machines() {
        for i := 0; i < n; i++ {
            // random key and value, may overwrwite previous sets if
            // collision which is fine
            key := strconv.Itoa(rand.Int())[0:3]
            value := strconv.Itoa(rand.Int())[0:3]

            b, err := m.SSH(fmt.Sprintf("curl -s -w %%{http_code} -s http://127.0.0.1:2379/v2/keys/%v -XPUT -d value=%v", key, value))
            if err != nil {
                return nil, err
            }

            // check for 201 or 200 resp header
            if !bytes.HasSuffix(b, []byte("200")) && !bytes.HasSuffix(b, []byte("201")) {
                continue
            }

            written[key] = value
        }
    }
    if len(written) == 0 {
        return nil, fmt.Errorf("failed to write any keys")
    }

    plog.Infof("wrote %v keys", len(written))
    return written, nil
}

开发者ID:pwaller,项目名称:mantle,代码行数:34,代码来源:util.go

示例8: PutItem

func (m *fakeDDB) PutItem(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) {
    m.assert.NotNil(input.Item[refAttr], "%s should have been present", refAttr)
    m.assert.NotNil(input.Item[refAttr].B, "key should have been a blob: %+v", input.Item[refAttr])
    key := input.Item[refAttr].B
    if bytes.HasSuffix(key, dynamoVersionKey) {
        m.assert.NotNil(input.Item[numAttr], "%s should have been present", numAttr)
        m.assert.NotNil(input.Item[numAttr].S, "vers should have been a string: %+v", input.Item[numAttr])
        m.version = aws.StringValue(input.Item[numAttr].S)
        return &dynamodb.PutItemOutput{}, nil
    }
    m.assert.NotNil(input.Item[chunkAttr], "%s should have present", chunkAttr)
    m.assert.NotNil(input.Item[chunkAttr].B, "value should have been a blob: %+v", input.Item[chunkAttr])
    value := input.Item[chunkAttr].B

    mustNotExist := *(input.ConditionExpression) == valueNotExistsExpression
    current, present := m.data[string(key)]

    if mustNotExist && present {
        return nil, mockAWSError("ConditionalCheckFailedException")
    } else if !mustNotExist && !bytes.Equal(current.chunk, input.ExpressionAttributeValues[":prev"].B) {
        return nil, mockAWSError("ConditionalCheckFailedException")
    }

    m.put(key, value, noneValue)
    if !bytes.HasSuffix(key, dynamoRootKey) {
        m.numPuts++
    }

    return &dynamodb.PutItemOutput{}, nil
}

开发者ID:Richardphp,项目名称:noms,代码行数:30,代码来源:dynamo_store_fake.go

示例9: parseSessionFromBytes

func parseSessionFromBytes(b []byte) *session {
    var s session

    b = normalizeCRLF(b)

    if bytes.HasSuffix(b, newline) {
        b = b[:len(b)-1]
    }

    lines := bytes.Split(b, newline)
    lines = removeSessionComment(lines)

    for _, line := range lines {
        if bytes.HasSuffix(line, []byte{'\r'}) { // convert CRLF to LF
            line = line[:len(line)-1]
        }
        line = append(line, '\n')

        if bytes.HasPrefix(line, prompt) {
            s.input = append(s.input, line[len(prompt):]...)
        }
        s.fullSession = append(s.fullSession, line...)
    }

    return &s
}

开发者ID:ricardo-rossi,项目名称:godebug,代码行数:26,代码来源:endtoend_test.go

示例10: five_a

func five_a(body []byte) []byte {
    if bytes.HasSuffix(body, []byte("e")) && Measure(body[:len(body)-1]) > 1 {
        return body[:len(body)-1]
    } else if bytes.HasSuffix(body, []byte("e")) && Measure(body[:len(body)-1]) == 1 && !star_o(body[:len(body)-1]) {
        return body[:len(body)-1]
    }
    return body
}

开发者ID:solidfox,项目名称:gopher,代码行数:8,代码来源:stemmer.go

示例11: periodCheck

func periodCheck(line []byte) []byte {
    if len(line) > 0 &&
        !bytes.HasSuffix(bytes.TrimSpace(line), period) &&
        !bytes.HasSuffix(bytes.TrimSpace(line), comma) {
        line = append(line, periodWithSpace...)
    }
    return line
}

开发者ID:jacqui,项目名称:newshound,代码行数:8,代码来源:alert.go

示例12: Read

// Read reads and decodes quoted-printable data from the underlying reader.
func (r *Reader) Read(p []byte) (n int, err error) {
    // Deviations from RFC 2045:
    // 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
    // 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
    //    with other broken QP encoders & decoders.
    // 3. it accepts soft line-break (=) at end of message (issue 15486); i.e.
    //    the final byte read from the underlying reader is allowed to be '=',
    //    and it will be silently ignored.
    for len(p) > 0 {
        if len(r.line) == 0 {
            if r.rerr != nil {
                return n, r.rerr
            }
            r.line, r.rerr = r.br.ReadSlice('\n')

            // Does the line end in CRLF instead of just LF?
            hasLF := bytes.HasSuffix(r.line, lf)
            hasCR := bytes.HasSuffix(r.line, crlf)
            wholeLine := r.line
            r.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
            if bytes.HasSuffix(r.line, softSuffix) {
                rightStripped := wholeLine[len(r.line):]
                r.line = r.line[:len(r.line)-1]
                if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) &&
                    !(len(rightStripped) == 0 && len(r.line) > 0 && r.rerr == io.EOF) {
                    r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
                }
            } else if hasLF {
                if hasCR {
                    r.line = append(r.line, '\r', '\n')
                } else {
                    r.line = append(r.line, '\n')
                }
            }
            continue
        }
        b := r.line[0]

        switch {
        case b == '=':
            b, err = readHexByte(r.line[1:])
            if err != nil {
                return n, err
            }
            r.line = r.line[2:] // 2 of the 3; other 1 is done below
        case b == '\t' || b == '\r' || b == '\n':
            break
        case b < ' ' || b > '~':
            return n, fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b)
        }
        p[0] = b
        p = p[1:]
        r.line = r.line[1:]
        n++
    }
    return n, nil
}

开发者ID:kuangchanglang,项目名称:go,代码行数:58,代码来源:reader.go

示例13: one_a

func one_a(body []byte) []byte {
    if bytes.HasSuffix(body, []byte("sses")) || bytes.HasSuffix(body, []byte("ies")) {
        return body[:len(body)-2]
    } else if bytes.HasSuffix(body, []byte("ss")) {
        return body
    } else if bytes.HasSuffix(body, []byte("s")) {
        return body[:len(body)-1]
    }
    return body
}

开发者ID:solidfox,项目名称:gopher,代码行数:10,代码来源:stemmer.go

示例14: Read

func (q *qpReader) Read(p []byte) (n int, err error) {
    for len(p) > 0 {
        if len(q.line) == 0 {
            if q.rerr != nil {
                return n, q.rerr
            }
            q.skipWhite = true
            q.line, q.rerr = q.br.ReadSlice('\n')

            // Does the line end in CRLF instead of just LF?
            hasLF := bytes.HasSuffix(q.line, lf)
            hasCR := bytes.HasSuffix(q.line, crlf)
            wholeLine := q.line
            q.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
            if bytes.HasSuffix(q.line, softSuffix) {
                rightStripped := wholeLine[len(q.line):]
                q.line = q.line[:len(q.line)-1]
                if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
                    q.rerr = fmt.Errorf("multipart: invalid bytes after =: %q", rightStripped)
                }
            } else if hasLF {
                if hasCR {
                    q.line = append(q.line, '\r', '\n')
                } else {
                    q.line = append(q.line, '\n')
                }
            }
            continue
        }
        b := q.line[0]
        if q.skipWhite && isQPSkipWhiteByte(b) {
            q.line = q.line[1:]
            continue
        }
        q.skipWhite = false

        switch {
        case b == '=':
            b, err = q.readHexByte(q.line[1:])
            if err != nil {
                return n, err
            }
            q.line = q.line[2:] // 2 of the 3; other 1 is done below
        case b == '\t' || b == '\r' || b == '\n':
            break
        case b < ' ' || b > '~':
            return n, fmt.Errorf("multipart: invalid unescaped byte 0x%02x in quoted-printable body", b)
        }
        p[0] = b
        p = p[1:]
        q.line = q.line[1:]
        n++
    }
    return n, nil
}

开发者ID:serge-hulne,项目名称:golang,代码行数:55,代码来源:quotedprintable.go

示例15: ExampleHasSuffix

func ExampleHasSuffix() {
    fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
    fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
    fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
    fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
    // Output:
    // true
    // false
    // false
    // true
}

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

示例16: IsContainerized

// IsContainerized returns true if we are running inside a container.
func IsContainerized() (bool, error) {
    b, err := ioutil.ReadFile(proc1Cgroup)
    if err != nil {
        return false, err
    }
    for _, line := range bytes.Split(b, []byte{'\n'}) {
        if len(line) > 0 && !bytes.HasSuffix(line, []byte{'/'}) && !bytes.HasSuffix(line, []byte("init.scope")) {
            return true, nil
        }
    }
    return false, nil
}

开发者ID:CadeLaRen,项目名称:docker-3,代码行数:13,代码来源:operatingsystem_linux.go

示例17: TestReservedKeys

func (suite *LevelDBStoreTestSuite) TestReservedKeys() {
    // Apparently, the following:
    //  s := []byte("")
    //  s = append(s, 1, 2, 3)
    //  f := append(s, 10, 20, 30)
    //  g := append(s, 4, 5, 6)
    //
    // Results in both f and g being [1, 2, 3, 4, 5, 6]
    // This was happening to us here, so ldb.chunkPrefix was "/chunk/" and ldb.rootKey was "/chun" instead of "/root"
    ldb := suite.factory.CreateStore("").(*LevelDBStore)
    suite.True(bytes.HasSuffix(ldb.rootKey, []byte(rootKeyConst)))
    suite.True(bytes.HasSuffix(ldb.chunkPrefix, []byte(chunkPrefixConst)))
}

开发者ID:willhite,项目名称:noms-old,代码行数:13,代码来源:leveldb_store_test.go

示例18: deleteOldAddrIndex

// deleteOldAddrIndex deletes the entire addrindex stored within the DB for a
// 2-byte addrIndexKeyPrefix. It also resets the cached in-memory metadata about
// the addr index.
func (db *LevelDb) deleteOldAddrIndex() error {
    db.dbLock.Lock()
    defer db.dbLock.Unlock()

    batch := db.lBatch()
    defer batch.Reset()

    // Delete the entire index along with any metadata about it.
    iter := db.lDb.NewIterator(bytesPrefix([]byte("a-")), db.ro)
    numInBatch := 0
    for iter.Next() {
        key := iter.Key()
        // With a 24-bit index key prefix, 1 in every 2^24 keys is a collision.
        // We check the length to make sure we only delete address index keys.
        // We also check the last two bytes to make sure the suffix doesn't
        // match other types of index that are 34 bytes long.
        if len(key) == 34 && !bytes.HasSuffix(key, recordSuffixTx) &&
            !bytes.HasSuffix(key, recordSuffixSpentTx) {
            batch.Delete(key)
            numInBatch++
        }

        // Delete in chunks to potentially avoid very large batches.
        if numInBatch >= batchDeleteThreshold {
            if err := db.lDb.Write(batch, db.wo); err != nil {
                iter.Release()
                return err
            }
            batch.Reset()
            numInBatch = 0
        }
    }
    iter.Release()
    if err := iter.Error(); err != nil {
        return err
    }

    batch.Delete(addrIndexMetaDataKey)
    batch.Delete(addrIndexVersionKey)

    if err := db.lDb.Write(batch, db.wo); err != nil {
        return err
    }

    db.lastAddrIndexBlkIdx = -1
    db.lastAddrIndexBlkSha = wire.ShaHash{}

    return nil
}

开发者ID:dan-da,项目名称:btcd,代码行数:52,代码来源:tx.go

示例19: readLine

// read one line from input and strip off terminating LF or terminating CR-LF
func (r *Reader) readLine() (line []byte, err error) {
    line, err = r.reader.ReadBytes(newline)
    if err != nil {
        return
    }

    switch {
    case bytes.HasSuffix(line, crlfSlice):
        line = line[0 : len(line)-len(crlfSlice)]
    case bytes.HasSuffix(line, newlineSlice):
        line = line[0 : len(line)-len(newlineSlice)]
    }

    return
}

开发者ID:semog,项目名称:stomp,代码行数:16,代码来源:reader.go

示例20: HandleLogfmt

func (dm *dynoLoadMsg) HandleLogfmt(key, val []byte) error {
    switch {
    case bytes.Equal(key, keySource):
        dm.Source = string(val)
    case bytes.Equal(key, keyDyno):
        dm.Dyno = string(val)
    case bytes.HasSuffix(key, keyLoadAvg1Min):
        dm.LoadAvg1Min, _ = strconv.ParseFloat(string(val), 64)
    case bytes.HasSuffix(key, keyLoadAvg5Min):
        dm.LoadAvg5Min, _ = strconv.ParseFloat(string(val), 64)
    case bytes.HasSuffix(key, keyLoadAvg15Min):
        dm.LoadAvg15Min, _ = strconv.ParseFloat(string(val), 64)
    }
    return nil
}

开发者ID:akkakks,项目名称:lumbermill,代码行数:15,代码来源:dyno_msg.go

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