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


Golang utf8.RuneStart函数代码示例

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


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

示例1: skipContinuationBytes

func (in *input) skipContinuationBytes(p int) int {
	if in.bytes == nil {
		for ; p < len(in.str) && !utf8.RuneStart(in.str[p]); p++ {
		}
	} else {
		for ; p < len(in.bytes) && !utf8.RuneStart(in.bytes[p]); p++ {
		}
	}
	return p
}
开发者ID:BillTheBest,项目名称:syncthing,代码行数:10,代码来源:input.go

示例2: ExampleRuneStart

func ExampleRuneStart() {
	buf := []byte("a界")
	fmt.Println(utf8.RuneStart(buf[0]))
	fmt.Println(utf8.RuneStart(buf[1]))
	fmt.Println(utf8.RuneStart(buf[2]))
	// Output:
	// true
	// true
	// false
}
开发者ID:kostyll,项目名称:gccpy,代码行数:10,代码来源:example_test.go

示例3: skipNonStarter

func (in *input) skipNonStarter(p int) int {
	if in.bytes == nil {
		for ; p < len(in.str) && !utf8.RuneStart(in.str[p]); p++ {
		}
	} else {
		for ; p < len(in.bytes) && !utf8.RuneStart(in.bytes[p]); p++ {
		}
	}
	return p
}
开发者ID:rajasaur,项目名称:sync_gateway,代码行数:10,代码来源:input.go

示例4: Less

func (rs reverseStrings) Less(i, j int) bool {
	for m, n := len(rs[i])-1, len(rs[j])-1; m >= 0 && n >= 0; m, n = m-1, n-1 {
		if rs[i][m] != rs[j][n] {
			// We want to compare runes, not bytes. So find the start of the
			// current runes and decode them.
			for ; m > 0 && !utf8.RuneStart(rs[i][m]); m-- {
			}
			for ; n > 0 && !utf8.RuneStart(rs[j][n]); n-- {
			}
			ri, _ := utf8.DecodeRuneInString(rs[i][m:])
			rj, _ := utf8.DecodeRuneInString(rs[j][n:])
			return ri < rj
		}
	}
	return len(rs[i]) < len(rs[j])
}
开发者ID:geramirez,项目名称:compliance-masonry,代码行数:16,代码来源:regexp.go

示例5: truncate

// truncate returns s truncated to the given size,
// avoiding splitting a multibyte UTF-8 sequence.
func truncate(p []byte, size int) []byte {
	if len(p) <= size {
		return p
	}
	p = p[0:size]
	start := size - 1
	r := rune(p[start])
	if r < utf8.RuneSelf {
		return p
	}
	// Find the start of the last character and check
	// whether it's valid.
	lim := size - utf8.UTFMax
	if lim < 0 {
		lim = 0
	}
	for ; start >= lim; start-- {
		if utf8.RuneStart(p[start]) {
			break
		}
	}
	// If we can't find the start of the last character,
	// return the whole lot.
	if start < 0 {
		return p
	}
	r, rsize := utf8.DecodeRune(p[start:size])
	// The last rune was valid, so include it.
	if rsize > 1 {
		return p
	}
	// The last rune was invalid, so lose it.
	return p[0:start]
}
开发者ID:zxpbenson,项目名称:rog-go,代码行数:36,代码来源:lines.go

示例6: Read

func (b *Buffer) Read(p []byte) (int, error) {
	n := 0
	bl := len(b.buf)
	for {
		r, size := utf8.DecodeRune(p)
		if size == 0 {
			break
		}
		n += size
		p = p[size:]
		b.buf = append(b.buf, r)
	}
	err := b.feed(bl)
	if err != nil {
		return n, err
	}
	// Check if the bytes are utf8 encoded. This is difficult because we
	// can't tell if more runes are coming. E.g. p[0] could be a valid rune
	// start, but it could require another byte, which might never arrive.
	// Can we detect the end of file?
	if len(p) > 0 && !utf8.RuneStart(p[0]) {
		return n, fmt.Errorf("Not utf8 encoded. Invalid rune start %x.", p[0])
	}
	return n, nil
}
开发者ID:ahhoefel,项目名称:parser,代码行数:25,代码来源:buffer.go

示例7: runeLimitedRead

// Read valid UTF-8 content from provided io.Reader.
// If underlying reader starts in the middle of a rune, an error is returned.
// If reader ends in the middle of a rune, the last (invalid) rune is discarded. Note that the
// underlying reader will now start reading from the middle of a rune.
func runeLimitedRead(r io.Reader, p []byte) (int, error) {
	n, err := r.Read(p)
	if n == 0 {
		return n, err
	}

	// If first byte is not a valid rune starting byte, returned error
	if n > 0 && !utf8.RuneStart(p[0]) {
		return 0, errInvalidStartingRune
	}

	// The following code is a lightly modified version of utf8#Valid()
	for i := 0; i < n; {
		if p[i] < utf8.RuneSelf {
			// Skip single byte rune
			i++
			continue
		}

		r, size := utf8.DecodeRune(p[i:])
		if size == 1 && r == utf8.RuneError {
			return i, err
		}
		i += size
	}

	return n, err
}
开发者ID:dropbox,项目名称:changes-artifacts,代码行数:32,代码来源:logchunkreader.go

示例8: highlightError

func highlightError(f io.Reader, pos int64) (line int, col int, highlight string) {
	line = 1
	br := bufio.NewReader(f)
	lastLine := ""
	thisLine := new(bytes.Buffer)
	for n := int64(0); n < pos; n++ {
		b, err := br.ReadByte()
		if err != nil {
			break
		}
		if b == '\n' {
			lastLine = thisLine.String()
			thisLine.Reset()
			line++
			col = 1
		} else {
			if utf8.RuneStart(b) {
				col++
			}
			thisLine.WriteByte(b)
		}
	}
	if line > 1 {
		highlight += fmt.Sprintf("%5d: %s\n", line-1, lastLine)
	}
	highlight += fmt.Sprintf("%5d: %s\n", line, thisLine.String())
	highlight += fmt.Sprintf("%s^\n", strings.Repeat(" ", col+5))
	return
}
开发者ID:ctchen,项目名称:government-service-design-manual,代码行数:29,代码来源:mdlint.go

示例9: redirToWs

func redirToWs(fd int, ws *websocket.Conn) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Fprintf(os.Stderr, "Error occured: %s\n", r)
			runtime.Goexit()
		}
	}()

	var buf [8192]byte
	start, end, buflen := 0, 0, 0
	for {
		switch nr, er := syscall.Read(fd, buf[start:]); {
		case nr < 0:
			fmt.Fprintf(os.Stderr, "error reading from websocket %d with code %d\n", fd, er)
			return
		case nr == 0: // EOF
			return
		case nr > 0:
			buflen = start + nr
			for end = buflen - 1; end >= 0; end-- {
				if utf8.RuneStart(buf[end]) {
					ch, width := utf8.DecodeRune(buf[end:buflen])
					if ch != utf8.RuneError {
						end += width
					}
					break
				}

				if buflen-end >= 6 {
					fmt.Fprintf(os.Stderr, "Invalid UTF-8 sequence in output")
					end = nr
					break
				}
			}

			runes := bytes.Runes(buf[0:end])
			buf_clean := []byte(string(runes))

			nw, ew := ws.Write(buf_clean[:])
			if ew != nil {
				fmt.Fprintf(os.Stderr, "error writing to websocket with code %s\n", ew)
				return
			}

			if nw != len(buf_clean) {
				fmt.Fprintf(os.Stderr, "Written %d instead of expected %d\n", nw, end)
			}

			start = buflen - end

			if start > 0 {
				// copy remaning read bytes from the end to the beginning of a buffer
				// so that we will get normal bytes
				for i := 0; i < start; i++ {
					buf[i] = buf[end+i]
				}
			}
		}
	}
}
开发者ID:Jacke,项目名称:WebTerm,代码行数:60,代码来源:ws.go

示例10: runeToByteOffset

func runeToByteOffset(s []byte, offset_c int) (offset_b int) {
	for offset_b = 0; offset_c > 0 && offset_b < len(s); offset_b++ {
		if utf8.RuneStart(s[offset_b]) {
			offset_c--
		}
	}
	return offset_b
}
开发者ID:trevordixon,项目名称:gocode,代码行数:8,代码来源:utils.go

示例11: char_to_byte_offset

func char_to_byte_offset(s []byte, offset_c int) (offset_b int) {
	for offset_b = 0; offset_c > 0 && offset_b < len(s); offset_b++ {
		if utf8.RuneStart(s[offset_b]) {
			offset_c--
		}
	}
	return offset_b
}
开发者ID:zaphar,项目名称:gocode,代码行数:8,代码来源:utils.go

示例12: getRuneSize

func getRuneSize(s string, i int) int {
	runeSize := 1
	for i+runeSize < len(s) && !utf8.RuneStart(s[i+runeSize]) {
		runeSize++
	}

	return runeSize
}
开发者ID:dieucao,项目名称:notifications,代码行数:8,代码来源:header.go

示例13: move_backwards

// move cursor backwards to the next valid utf8 rune start, or 0
func (this *bytes_iterator) move_backwards() {
	for this.cursor != 0 {
		this.cursor--
		if utf8.RuneStart(this.char()) {
			return
		}
	}
}
开发者ID:rjmcguire,项目名称:gocode,代码行数:9,代码来源:cursorcontext.go

示例14: charToByteOffset

func charToByteOffset(s []byte, offsetC int) (offsetB int) {
	for offsetB = 0; offsetC > 0 && offsetB < len(s); offsetB++ {
		if utf8.RuneStart(s[offsetB]) {
			offsetC--
		}
	}
	return offsetB
}
开发者ID:Luit-IT,项目名称:gocode,代码行数:8,代码来源:gocode.go

示例15: lastRuneStart

// lastRuneStart returns the runeInfo and position of the last
// rune in buf or the zero runeInfo and -1 if no rune was found.
func lastRuneStart(fd *formInfo, buf []byte) (runeInfo, int) {
	p := len(buf) - 1
	for ; p >= 0 && !utf8.RuneStart(buf[p]); p-- {
	}
	if p < 0 {
		return runeInfo{0, 0, 0, 0}, -1
	}
	return fd.info(inputBytes(buf), p), p
}
开发者ID:krasin,项目名称:go-deflate,代码行数:11,代码来源:normalize.go


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