本文整理汇总了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
}
示例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
}
示例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
}
示例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])
}
示例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]
}
示例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
}
示例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
}
示例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
}
示例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]
}
}
}
}
}
示例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
}
示例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
}
示例12: getRuneSize
func getRuneSize(s string, i int) int {
runeSize := 1
for i+runeSize < len(s) && !utf8.RuneStart(s[i+runeSize]) {
runeSize++
}
return runeSize
}
示例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
}
}
}
示例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
}
示例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
}