本文整理汇总了Golang中utf8.DecodeRune函数的典型用法代码示例。如果您正苦于以下问题:Golang DecodeRune函数的具体用法?Golang DecodeRune怎么用?Golang DecodeRune使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DecodeRune函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TrimSpace
// Trim returns a slice of the string s, with all leading and trailing white space
// removed, as defined by Unicode.
func TrimSpace(s []byte) []byte {
start, end := 0, len(s)
for start < end {
wid := 1
rune := int(s[start])
if rune >= utf8.RuneSelf {
rune, wid = utf8.DecodeRune(s[start:end])
}
if !unicode.IsSpace(rune) {
break
}
start += wid
}
for start < end {
wid := 1
rune := int(s[end-1])
if rune >= utf8.RuneSelf {
// Back up carefully looking for beginning of rune. Mustn't pass start.
for wid = 2; start <= end-wid && !utf8.RuneStart(s[end-wid]); wid++ {
}
if start > end-wid { // invalid UTF-8 sequence; stop processing
return s[start:end]
}
rune, wid = utf8.DecodeRune(s[end-wid : end])
}
if !unicode.IsSpace(rune) {
break
}
end -= wid
}
return s[start:end]
}
示例2: FieldsFunc
// FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
// It splits the array s at each run of code points c satisfying f(c) and
// returns a slice of subarrays of s. If no code points in s satisfy f(c), an
// empty slice is returned.
func FieldsFunc(s []byte, f func(int) bool) [][]byte {
n := 0
inField := false
for i := 0; i < len(s); {
rune, size := utf8.DecodeRune(s[i:])
wasInField := inField
inField = !f(rune)
if inField && !wasInField {
n++
}
i += size
}
a := make([][]byte, n)
na := 0
fieldStart := -1
for i := 0; i <= len(s) && na < n; {
rune, size := utf8.DecodeRune(s[i:])
if fieldStart < 0 && size > 0 && !f(rune) {
fieldStart = i
i += size
continue
}
if fieldStart >= 0 && (size == 0 || f(rune)) {
a[na] = s[fieldStart:i]
na++
fieldStart = -1
}
if size == 0 {
break
}
i += size
}
return a[0:na]
}
示例3: nextWord
func (self *scanner) nextWord() (word tok, err os.Error) {
if self.index >= len(self.content) {
err = os.NewError("EOF")
return
}
for self.index < len(self.content) {
r, l := utf8.DecodeRune(self.content[self.index:])
if !unicode.IsSpace(r) || r == '\n' {
break
}
self.index += l
}
j, ttype, inchar, incode := self.index, other, false, 0
for self.index < len(self.content) {
r, l := utf8.DecodeRune(self.content[self.index:])
if r == '\'' {
inchar = !inchar
}
if self.index == j {
switch {
case unicode.IsUpper(r):
ttype = nonterm
case r == '\n':
self.index++
ttype = newline
break
case r == ':':
ttype = begindef
case r == ';':
ttype = enddef
case r == '|':
ttype = alternate
case r == '{' && memorizeTerms:
incode++
ttype = code
default:
ttype = term
}
} else if incode > 0 && r == '{' {
incode++
} else if incode > 0 && r == '}' {
incode--
}
if incode == 0 && !inchar && unicode.IsSpace(r) {
break
}
self.index += l
}
token := string(self.content[j:self.index])
if ttype == newline {
token = ""
}
word = tok{token, ttype}
return
}
示例4: EqualFold
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func EqualFold(s, t []byte) bool {
for len(s) != 0 && len(t) != 0 {
// Extract first rune from each.
var sr, tr rune
if s[0] < utf8.RuneSelf {
sr, s = rune(s[0]), s[1:]
} else {
r, size := utf8.DecodeRune(s)
sr, s = r, s[size:]
}
if t[0] < utf8.RuneSelf {
tr, t = rune(t[0]), t[1:]
} else {
r, size := utf8.DecodeRune(t)
tr, t = r, t[size:]
}
// If they match, keep going; if not, return false.
// Easy case.
if tr == sr {
continue
}
// Make sr < tr to simplify what follows.
if tr < sr {
tr, sr = sr, tr
}
// Fast check for ASCII.
if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
// ASCII, and sr is upper case. tr must be lower case.
if tr == sr+'a'-'A' {
continue
}
return false
}
// General case. SimpleFold(x) returns the next equivalent rune > x
// or wraps around to smaller values.
r := unicode.SimpleFold(sr)
for r != sr && r < tr {
r = unicode.SimpleFold(r)
}
if r == tr {
continue
}
return false
}
// One string is empty. Are both?
return len(s) == len(t)
}
示例5: hangul
func (s inputBytes) hangul(p int) uint32 {
if !isHangul(s[p:]) {
return 0
}
rune, _ := utf8.DecodeRune(s[p:])
return uint32(rune)
}
示例6: insert
// insert inserts the given rune in the buffer ordered by CCC.
// It returns true if the buffer was large enough to hold the decomposed rune.
func (rb *reorderBuffer) insert(src []byte, info runeInfo) bool {
if info.size == 3 && isHangul(src) {
rune, _ := utf8.DecodeRune(src)
return rb.decomposeHangul(uint32(rune))
}
if info.flags.hasDecomposition() {
dcomp := rb.f.decompose(src)
for i := 0; i < len(dcomp); {
info = rb.f.info(dcomp[i:])
pos := rb.nbyte
if !rb.insertOrdered(info) {
return false
}
end := i + int(info.size)
copy(rb.byte[pos:], dcomp[i:end])
i = end
}
} else {
pos := rb.nbyte
if !rb.insertOrdered(info) {
return false
}
copy(rb.byte[pos:], src[:info.size])
}
return true
}
示例7: _peek_char
func _peek_char(port Obj) Obj {
if is_immediate(port) {
panic("bad type")
}
switch v := (*port).(type) {
case *InputPort:
if v.is_binary {
panic("bad port type")
}
for !utf8.FullRune(v.lookahead[0:v.lookahead_valid]) {
n, err := io.ReadFull(v.r,
v.lookahead[v.lookahead_valid:v.lookahead_valid+1])
v.lookahead_valid += n
switch {
case err == os.EOF:
return Eof
case err != nil:
panic("I/O read error")
}
}
cp, _ := utf8.DecodeRune(v.lookahead[0:v.lookahead_valid])
return Make_char(cp)
}
panic("bad type")
}
示例8: next
// Read the next Unicode char into S.ch.
// S.ch < 0 means end-of-file.
//
func (S *Scanner) next() {
if S.rdOffset < len(S.src) {
S.offset = S.rdOffset
if S.ch == '\n' {
S.lineOffset = S.offset
S.file.AddLine(S.offset)
}
r, w := int(S.src[S.rdOffset]), 1
switch {
case r == 0:
S.error(S.offset, "illegal character NUL")
case r >= 0x80:
// not ASCII
r, w = utf8.DecodeRune(S.src[S.rdOffset:])
if r == utf8.RuneError && w == 1 {
S.error(S.offset, "illegal UTF-8 encoding")
}
}
S.rdOffset += w
S.ch = r
} else {
S.offset = len(S.src)
if S.ch == '\n' {
S.lineOffset = S.offset
S.file.AddLine(S.offset)
}
S.ch = -1 // eof
}
}
示例9: Map
// Map returns a copy of the byte array s with all its characters modified
// according to the mapping function. If mapping returns a negative value, the character is
// dropped from the string with no replacement. The characters in s and the
// output are interpreted as UTF-8-encoded Unicode code points.
func Map(mapping func(rune int) int, s []byte) []byte {
// In the worst case, the array can grow when mapped, making
// things unpleasant. But it's so rare we barge in assuming it's
// fine. It could also shrink but that falls out naturally.
maxbytes := len(s) // length of b
nbytes := 0 // number of bytes encoded in b
b := make([]byte, maxbytes)
for i := 0; i < len(s); {
wid := 1
rune := int(s[i])
if rune >= utf8.RuneSelf {
rune, wid = utf8.DecodeRune(s[i:])
}
rune = mapping(rune)
if rune >= 0 {
if nbytes+utf8.RuneLen(rune) > maxbytes {
// Grow the buffer.
maxbytes = maxbytes*2 + utf8.UTFMax
nb := make([]byte, maxbytes)
copy(nb, b[0:nbytes])
b = nb
}
nbytes += utf8.EncodeRune(b[nbytes:maxbytes], rune)
}
i += wid
}
return b[0:nbytes]
}
示例10: deduceDecl
func (c *AutoCompleteContext) deduceDecl(file []byte, cursor int) *DeclApropos {
orig := cursor
if cursor < 0 {
return nil
}
if cursor == 0 {
return &DeclApropos{nil, ""}
}
// figure out what is just before the cursor
cursor = utf8MoveBackwards(file, cursor)
if file[cursor] == '.' {
// we're '<whatever>.'
// figure out decl, Parital is ""
return c.deduceExpr(file[:cursor], "")
} else {
letter, _ := utf8.DecodeRune(file[cursor:])
if isIdent(letter) {
// we're '<whatever>.<ident>'
// parse <ident> as Partial and figure out decl
cursor = skipIdent(file, cursor)
partial := string(file[cursor+1 : orig])
if file[cursor] == '.' {
return c.deduceExpr(file[:cursor], partial)
} else {
return &DeclApropos{nil, partial}
}
}
}
return &DeclApropos{nil, ""}
}
示例11: next
// Read the next Unicode char into S.ch.
// S.ch < 0 means end-of-file.
//
func (S *Scanner) next() {
if S.offset < len(S.src) {
S.pos.Offset = S.offset
S.pos.Column++
if S.ch == '\n' {
// next character starts a new line
S.pos.Line++
S.pos.Column = 1
}
r, w := int(S.src[S.offset]), 1
switch {
case r == 0:
S.error(S.pos, "illegal character NUL")
case r >= 0x80:
// not ASCII
r, w = utf8.DecodeRune(S.src[S.offset:])
if r == utf8.RuneError && w == 1 {
S.error(S.pos, "illegal UTF-8 encoding")
}
}
S.offset += w
S.ch = r
} else {
S.pos.Offset = len(S.src)
S.ch = -1 // eof
}
}
示例12: findExpr
func findExpr(file []byte) []byte {
const (
LAST_NONE = iota
LAST_DOT
LAST_PAREN
LAST_IDENT
)
last := LAST_NONE
cursor := len(file)
cursor = utf8MoveBackwards(file, cursor)
loop:
for {
c := file[cursor]
letter, _ := utf8.DecodeRune(file[cursor:])
switch c {
case '.':
cursor = utf8MoveBackwards(file, cursor)
last = LAST_DOT
case ')', ']':
if last == LAST_IDENT {
break loop
}
cursor = utf8MoveBackwards(file, skipToPair(file, cursor))
last = LAST_PAREN
default:
if isIdent(letter) {
cursor = skipIdent(file, cursor)
last = LAST_IDENT
} else {
break loop
}
}
}
return file[cursor+1:]
}
示例13: Replace
// Replace returns a copy of the slice s with the first n
// non-overlapping instances of old replaced by new.
// If n < 0, there is no limit on the number of replacements.
func Replace(s, old, new []byte, n int) []byte {
if n == 0 {
return s // avoid allocation
}
// Compute number of replacements.
if m := Count(s, old); m == 0 {
return s // avoid allocation
} else if n <= 0 || m < n {
n = m
}
// Apply replacements to buffer.
t := make([]byte, len(s)+n*(len(new)-len(old)))
w := 0
start := 0
for i := 0; i < n; i++ {
j := start
if len(old) == 0 {
if i > 0 {
_, wid := utf8.DecodeRune(s[start:])
j += wid
}
} else {
j += Index(s[start:], old)
}
w += copy(t[w:], s[start:j])
w += copy(t[w:], new)
start = j + len(old)
}
w += copy(t[w:], s[start:])
return t[0:w]
}
示例14: ReadRune
// ReadRune returns the next UTF-8 encoded code point from the
// io.Reader inside r.
func (r *readRune) ReadRune() (rune int, size int, err os.Error) {
r.buf[0], err = r.readByte()
if err != nil {
return 0, 0, err
}
if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
rune = int(r.buf[0])
return
}
var n int
for n = 1; !utf8.FullRune(r.buf[0:n]); n++ {
r.buf[n], err = r.readByte()
if err != nil {
if err == os.EOF {
err = nil
break
}
return
}
}
rune, size = utf8.DecodeRune(r.buf[0:n])
if size < n { // an error
r.unread(r.buf[size:n])
}
return
}
示例15: next
// next reads and returns the next Unicode character. It is designed such
// that only a minimal amount of work needs to be done in the common ASCII
// case (one test to check for both ASCII and end-of-buffer, and one test
// to check for newlines).
func (s *Scanner) next() int {
ch := int(s.srcBuf[s.srcPos])
if ch >= utf8.RuneSelf {
// uncommon case: not ASCII or not enough bytes
for s.srcPos+utf8.UTFMax > s.srcEnd && !utf8.FullRune(s.srcBuf[s.srcPos:s.srcEnd]) {
// not enough bytes: read some more, but first
// save away token text if any
if s.tokPos >= 0 {
s.tokBuf.Write(s.srcBuf[s.tokPos:s.srcPos])
s.tokPos = 0
}
// move unread bytes to beginning of buffer
copy(s.srcBuf[0:], s.srcBuf[s.srcPos:s.srcEnd])
s.srcBufOffset += s.srcPos
// read more bytes
i := s.srcEnd - s.srcPos
n, err := s.src.Read(s.srcBuf[i:bufLen])
s.srcEnd = i + n
s.srcPos = 0
s.srcBuf[s.srcEnd] = utf8.RuneSelf // sentinel
if err != nil {
if s.srcEnd == 0 {
return EOF
}
if err != os.EOF {
s.error(err.String())
break
}
}
}
// at least one byte
ch = int(s.srcBuf[s.srcPos])
if ch >= utf8.RuneSelf {
// uncommon case: not ASCII
var width int
ch, width = utf8.DecodeRune(s.srcBuf[s.srcPos:s.srcEnd])
if ch == utf8.RuneError && width == 1 {
s.error("illegal UTF-8 encoding")
}
s.srcPos += width - 1
}
}
s.srcPos++
s.column++
switch ch {
case 0:
// implementation restriction for compatibility with other tools
s.error("illegal character NUL")
case '\n':
s.line++
s.column = 0
}
return ch
}