当前位置: 首页>>代码示例>>Golang>>正文


Golang Scanner.Bytes方法代码示例

本文整理汇总了Golang中bufio.Scanner.Bytes方法的典型用法代码示例。如果您正苦于以下问题:Golang Scanner.Bytes方法的具体用法?Golang Scanner.Bytes怎么用?Golang Scanner.Bytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bufio.Scanner的用法示例。


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

示例1: ProcessChanges

/*
ProcessChanges takes `git status -z` output and returns all status items.

(Note: in our case, we actually use `git status -bz` and remove branch header
when we process it earlier, but the results are binary identical.)

This is a complicated process because the format is weird. Each line is a
variable length number of columns (2-3), but the separator for 1-2 is a space
(but the content of columns can contain spaces too!), and the seperator for 2-3
is a NUL character (ASCII 0), *if* there is a third column. But here's where it
gets wacky: NUL is also the entry terminator (rather than a LF like in normal
porcelain mode)

Thankfully(?), column 1 which contains the status codes is a fixed length of two
bytes, and in theory the status codes contain enough secrets for us to determine
whether we should expect 2 or 3 columns (current hypothesis is we only get the
third column which is PATH2 when there is a "rename" operation). Sooo... we can
just read those two bytes and use that to determine how many NULs to scan to
until we have consumed a full entry.

We put up with this because it means no shell escaping, which should mean better
cross-platform support. Better hope some Windows people end up using it someday!
*/
func ProcessChanges(s *bufio.Scanner, root string) (results []*StatusItem) {

	// Before we process any changes, get the Current Working Directory.
	// We're going to need use to calculate absolute and relative filepaths for
	// every change, so we get it once now and pass it along.
	// If for some reason this fails (?), fallback to the git worktree root.
	wd, err := os.Getwd()
	if err != nil {
		wd = root
	}

	for s.Scan() {
		chunk := s.Bytes()
		// ...if chunk represents a rename or copy op, need to append another chunk
		// to get the full change item, with NUL manually reinserted because scanner
		// will extract past it.
		if (chunk[0] == 'R' || chunk[0] == 'C') && s.Scan() {
			chunk = append(chunk, '\x00')
			chunk = append(chunk, s.Bytes()...)
		}
		results = append(results, processChange(chunk, wd, root)...)
	}

	return
}
开发者ID:vyder,项目名称:scmpuff,代码行数:48,代码来源:process.go

示例2: scanBlock

func scanBlock(scanner *bufio.Scanner) (*pfs.BlockRef, []byte, error) {
	var buffer bytes.Buffer
	var bytesWritten int
	hash := newHash()
	for scanner.Scan() {
		// they take out the newline, put it back
		bytes := append(scanner.Bytes(), '\n')
		buffer.Write(bytes)
		hash.Write(bytes)
		bytesWritten += len(bytes)
		if bytesWritten > blockSize {
			break
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, nil, err
	}
	return &pfs.BlockRef{
		Block: getBlock(hash),
		Range: &pfs.ByteRange{
			Lower: 0,
			Upper: uint64(buffer.Len()),
		},
	}, buffer.Bytes(), nil
}
开发者ID:alamehor,项目名称:pachyderm,代码行数:25,代码来源:local_block_api_server.go

示例3: ReadBlock

func ReadBlock(sc *bufio.Scanner) (*Block, error) {
	// Ignore empty lines
	for {
		if len(sc.Bytes()) != 0 {
			break
		}

		if ok := sc.Scan(); !ok && sc.Err() != nil {
			return nil, fmt.Errorf("Error occured while reading file: %v", sc.Err())
		} else if !ok && sc.Err() == nil {
			return nil, nil
		}
	}

	// Read the index
	indexStr := sc.Text()
	index, err := strconv.Atoi(indexStr)
	if err != nil {
		return nil, fmt.Errorf("Could not parse block index: %v", err)
	}

	// Read the timestamp
	if ok := sc.Scan(); !ok {
		return nil, fmt.Errorf("Could not read timestamp: %v", err)
	}

	// Parse the timestamps
	from, to, posInfo, err := ParseTimestamp(sc.Text())
	if err != nil {
		return nil, fmt.Errorf("Could not parse timestamp: %v", err)
	}

	// Read the actual subtitle text
	text := []*string{}
	for {
		if ok := sc.Scan(); !ok && sc.Err() != nil {
			return nil, fmt.Errorf("Could not read text: %v", sc.Err())
		} else if !ok && sc.Err() == nil {
			// EOF
			break
		}
		if len(sc.Bytes()) == 0 {
			break
		}

		str := sc.Text()
		text = append(text, &str)
	}

	return &Block{
		Index:   index,
		From:    *from,
		To:      *to,
		PosInfo: posInfo,
		Text:    text,
	}, nil
}
开发者ID:CrawX,项目名称:go-srttools,代码行数:57,代码来源:main.go

示例4: parseBody

func parseBody(scanner *bufio.Scanner, contentLength int64) []byte {
	bytes := make([]byte, contentLength)
	scanner.Split(bufio.ScanBytes)
	var i int64
	for i = 0; i < contentLength; i++ {
		scanner.Scan()
		bytes[i] = scanner.Bytes()[0]
	}
	return bytes
}
开发者ID:ben-mays,项目名称:http-server-go,代码行数:10,代码来源:request.go

示例5: putOneBlock

func (s *localBlockAPIServer) putOneBlock(scanner *bufio.Scanner) (result *pfs.BlockRef, retErr error) {
	hash := newHash()
	tmp, err := ioutil.TempFile(s.tmpDir(), "block")
	if err != nil {
		return nil, err
	}
	defer func() {
		if err := tmp.Close(); err != nil && retErr == nil {
			retErr = err
			return
		}
		if result == nil {
			return
		}
		// check if it's a new block
		if _, err := os.Stat(s.blockPath(result.Block)); !os.IsNotExist(err) {
			// already have this block, remove tmp
			if err := os.Remove(tmp.Name()); err != nil && retErr == nil {
				retErr = err
				return
			}
			return
		}
		// it's a new block, rename it accordingly
		if err := os.Rename(tmp.Name(), s.blockPath(result.Block)); err != nil && retErr == nil {
			retErr = err
			return
		}
	}()
	var bytesWritten int
	for scanner.Scan() {
		// they take out the newline, put it back
		bytes := append(scanner.Bytes(), '\n')
		if _, err := hash.Write(bytes); err != nil {
			return nil, err
		}
		if _, err := tmp.Write(bytes); err != nil {
			return nil, err
		}
		bytesWritten += len(bytes)
		if bytesWritten > blockSize {
			break
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return &pfs.BlockRef{
		Block: getBlock(hash),
		Range: &pfs.ByteRange{
			Lower: 0,
			Upper: uint64(bytesWritten),
		},
	}, nil
}
开发者ID:Manikandan-Selvaganesh,项目名称:pachyderm,代码行数:55,代码来源:local_block_api_server.go

示例6: scanPara

func scanPara(r *bufio.Scanner) ([][]byte, bool) {
	var para [][]byte
	for r.Scan() {
		line := r.Bytes()
		if len(bytes.TrimSpace(line)) == 0 {
			return para, true
		}
		para = append(para, append([]byte(nil), line...))
	}
	return para, false
}
开发者ID:mccoyst,项目名称:formate,代码行数:11,代码来源:main.go

示例7: scanValue

func scanValue(scanner *bufio.Scanner, pos int) ([]byte, string, int, error) {
	if scanner.Scan() {
		raw := scanner.Bytes()
		pos += bytes.Count(raw, []byte{'\n'})
		return raw, strings.TrimSpace(string(raw)), pos, nil
	}
	if err := scanner.Err(); err != nil {
		return nil, "", pos, &Error{pos, err.Error()}
	}
	return nil, "", pos, nil
}
开发者ID:istrategylabs,项目名称:heroku-cli,代码行数:11,代码来源:netrc.go

示例8: handle

func (w *Worker) handle(scanner *bufio.Scanner) error {
	for scanner.Scan() {
		p, err := PacketFromBytes(scanner.Bytes())
		if err != nil {
			return err
		}
		if err := w.handlePacket(p); err != nil {
			return err
		}
	}
	return scanner.Err()
}
开发者ID:azylman,项目名称:gearman,代码行数:12,代码来源:worker.go

示例9: assertNextChunk

func assertNextChunk(t *testing.T, r *bufio.Scanner, expected string) {
	if !r.Scan() {
		t.Fatalf("Expected chunk, but ran out early: %v", r.Err())
	}
	if r.Err() != nil {
		t.Fatalf("Error reading chunk: %q", r.Err())
	}
	data := r.Bytes()
	if string(data) != expected {
		t.Errorf("chunk reader read %q; want %q", data, expected)
	}
}
开发者ID:paladin74,项目名称:weave,代码行数:12,代码来源:proxy_intercept_test.go

示例10: eatLines

func eatLines(scan *bufio.Scanner, prefix string, n int) error {
	bprefix := []byte(prefix)
	for i := 0; i < n; i++ {
		if !scan.Scan() {
			return io.ErrUnexpectedEOF
		}
		if !bytes.HasPrefix(scan.Bytes(), bprefix) {
			return fmt.Errorf("line %q does not have expected prefix %q", scan.Bytes(), bprefix)
		}
	}
	return nil
}
开发者ID:zxpbenson,项目名称:rog-go,代码行数:12,代码来源:apply.go

示例11: fillArr

func fillArr(s *bufio.Scanner) (l []loc) {
	var err error
	for s.Scan() {
		if bytes.Count(s.Bytes(), []byte{','}) != 1 {
			if len(l) == 0 {
				ln, err := strconv.ParseInt(string(bytes.TrimSpace(s.Bytes())), 10, 0)
				check(err)
				l = make([]loc, 0, ln+10)
			}
			continue
		}

		t1 := make([]byte, len(s.Bytes()))
		copy(t1, s.Bytes())
		tmploc := loc{t1, 0, 0}
		tmp := bytes.SplitN(bytes.Trim(tmploc.pts, "() "), []byte{','}, 3)
		tmploc.x, err = strconv.ParseFloat(string(bytes.TrimSpace(tmp[0])), 64)
		check(err)
		tmploc.y, err = strconv.ParseFloat(string(bytes.TrimSpace(tmp[1])), 64)
		check(err)
		l = append(l, tmploc)
	}
	if s.Err() != nil {
		log.Fatal(s.Err())
	}
	sort.Sort(locA(l))
	return
}
开发者ID:Kunde21,项目名称:DailyProgrammer,代码行数:28,代码来源:c232I.go

示例12: dovecot

func dovecot(db *HTTPDB, decoder *bufio.Scanner, encoder func([]byte)) error {
	for decoder.Scan() {
		data := decoder.Bytes()

		if data[0] == 'H' {
			continue
		}
		if data[0] != 'L' {
			encoder([]byte{'F'})
			continue
		}

		msg := bytes.SplitN(data[1:], []byte{'/'}, 3)

		res, err := db.Request(&Query{
			Verb: string(msg[1]),
			Object: map[string]string{
				"context": string(msg[0]),
				"object":  string(msg[2]),
			},
		})

		if err != nil {
			encoder([]byte{'F'})
			panic(err)
		}

		switch res.Status {
		case "OK":
			data, err := json.Marshal(res.Data)
			if err != nil {
				encoder([]byte{'F'})
				panic(errors.New(fmt.Sprintf("strange Resp %+v", res)))
			}

			encoder(append([]byte{'O'}, data...))

		case "KO":
			encoder([]byte{'N'})

		default:
			encoder([]byte{'F'})
			panic(errors.New(fmt.Sprintf("strange Resp %+v", res)))
		}

	}

	return decoder.Err()
}
开发者ID:nathanaelle,项目名称:shitenno,代码行数:49,代码来源:handler.go

示例13: handle

func (c *Client) handle(first Packet, scanner *bufio.Scanner) error {
	if err := c.handlePacket(first); err != nil {
		return err
	}
	for scanner.Scan() {
		p, err := PacketFromBytes(scanner.Bytes())
		if err != nil {
			return err
		}
		if err := c.handlePacket(p); err != nil {
			return err
		}
	}
	return scanner.Err()
}
开发者ID:azylman,项目名称:gearman,代码行数:15,代码来源:client.go

示例14: doLoad

func (conf *Config) doLoad(scanner *bufio.Scanner) (err error) {
	var read bool
	for {
		read = scanner.Scan()
		if !read {
			break
		}

		re := configLine.FindSubmatch(scanner.Bytes())
		if re != nil {
			conf.values[string(re[1])] = re[2]
		}
	}

	return
}
开发者ID:gudTECH,项目名称:scamp-go,代码行数:16,代码来源:config.go

示例15: getChapterHeader

func getChapterHeader(scanner *bufio.Scanner) ([]byte, error) {
	buf := new(bytes.Buffer)

	for scanner.Scan() {
		l := scanner.Bytes()
		buf.Write(l)
		buf.WriteByte('\n')
		if reBody.Match(l) {
			break
		}
	}
	if e := scanner.Err(); e != nil {
		return nil, e
	}

	return removeUtf8Bom(buf.Bytes()), nil
}
开发者ID:haifengwang,项目名称:makeepub,代码行数:17,代码来源:make.go


注:本文中的bufio.Scanner.Bytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。