本文整理汇总了Golang中unicode/utf8.DecodeLastRune函数的典型用法代码示例。如果您正苦于以下问题:Golang DecodeLastRune函数的具体用法?Golang DecodeLastRune怎么用?Golang DecodeLastRune使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DecodeLastRune函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: iter_words_backward
func iter_words_backward(data []byte, cb func(word []byte)) {
for {
if len(data) == 0 {
return
}
r, rlen := utf8.DecodeLastRune(data)
// skip non-word runes
for !is_word(r) {
data = data[:len(data)-rlen]
if len(data) == 0 {
return
}
r, rlen = utf8.DecodeLastRune(data)
}
// must be on a word rune
i := len(data)
for is_word(r) && i > 0 {
i -= rlen
r, rlen = utf8.DecodeLastRune(data[:i])
}
cb(data[i:])
data = data[:i]
}
}
示例2: Filter
func (s *PossessiveFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
for _, token := range input {
lastRune, lastRuneSize := utf8.DecodeLastRune(token.Term)
if lastRune == 's' || lastRune == 'S' {
nextLastRune, nextLastRuneSize := utf8.DecodeLastRune(token.Term[:len(token.Term)-lastRuneSize])
if nextLastRune == rightSingleQuotationMark ||
nextLastRune == apostrophe ||
nextLastRune == fullWidthApostrophe {
token.Term = token.Term[:len(token.Term)-lastRuneSize-nextLastRuneSize]
}
}
}
return input
}
示例3: Read
func (r *UTF8Reader) Read(p []byte) (n int, err error) {
size := 0
if cap(p) < utf8.UTFMax {
return size, SmallBufferError
}
if r.buffer.Len() > 0 {
n, err = r.buffer.Read(p)
size += n
if err != nil {
return size, err
}
}
n, err = r.reader.Read(p[size:])
size += n
if err != nil {
return size, err
}
leftOver := 0
for ; leftOver < utf8.UTFMax && size-leftOver > 0; leftOver++ {
rune, _ := utf8.DecodeLastRune(p[:size-leftOver])
if rune != utf8.RuneError {
break
}
}
r.buffer.Write(p[size-leftOver : size])
return size - leftOver, nil
}
示例4: Resume
func (f *wordBoundaryFiber) Resume() (output, error) {
if f.cnt == 0 {
f.cnt++
match := false
if len(f.I.b) > 0 {
if f.I.begin > 0 && isASCIIWord(rune(f.I.b[0])) != isASCIIWord(rune(f.I.o[f.I.begin-1])) {
match = true
}
if f.I.begin == 0 && isASCIIWord(rune(f.I.b[0])) {
match = true
}
}
if len(f.I.o) > 0 && f.I.begin == len(f.I.o) {
r, _ := utf8.DecodeLastRune(f.I.o)
if isASCIIWord(r) {
match = true
}
}
if f.node.Reversed {
match = !match
}
if match {
return output{offset: 0}, nil
}
}
return output{}, errDeadFiber
}
示例5: MultipleChoice
// MultipleChoice computes the score of a multiple choice exercise
// with student answers provided in fileName, and the answers provided
// in the answerKey object. The function requires a Score object, and
// will produce both string output and JSON output.
func MultipleChoice(t *testing.T, sc *score.Score, fileName string, answers Choices) {
defer sc.WriteString(os.Stdout)
defer sc.WriteJSON(os.Stdout)
// Read the whole file
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
sc.Score = 0
t.Fatalf(fmt.Sprintf("%v: error reading the file: %v", fileName, err))
return
}
for i := range answers {
// Find the user's answer to the corresponding question number
regexStr := "\n" + strconv.Itoa(answers[i].Number) + "[.)]*[ \t\v\r\n\f]*[A-Za-z]*"
regex := regexp.MustCompile(regexStr)
userAnswer := regex.Find(bytes)
if userAnswer == nil {
t.Errorf("%v %d: Answer not found.\n", sc.TestName, answers[i].Number)
sc.Dec()
} else {
r, _ := utf8.DecodeLastRune(userAnswer)
got, _ := utf8.DecodeLastRuneInString(strings.ToUpper(string(r)))
if got != answers[i].Want {
t.Errorf("%v %d: %q is incorrect.\n", sc.TestName, answers[i].Number, got)
sc.Dec()
}
}
}
}
示例6: Read
func (sr *stringReader) Read(p []byte) (int, error) {
if sr.remLen > 0 {
copy(p, sr.rem[:sr.remLen])
}
n, err := sr.r.Read(p[sr.remLen:])
n += int(sr.remLen)
sr.remLen = 0
if err != nil {
return n, err
}
// find the last full rune
var i int
for i = n; i >= 0; {
r, size := utf8.DecodeLastRune(p[:i])
if !(size == 1 && r == utf8.RuneError) {
break
}
i -= size
}
if i == n {
return n, err
}
sr.remLen = uint8(n - i)
log.Printf("remlLen=%d i=%d n=%d", sr.remLen, i, n)
copy(sr.rem[:sr.remLen], p[i:n])
return i, err
}
示例7: OffsetRune
// TODO
// Got to hate utf8 for making it complicated... Guess there is not much to do..
func (f *File) OffsetRune(cn, start int) (offset int, e error) {
if start < 0 || start > len(f.b) {
return 0, memfile.OutOfBounds
}
if cn <= 0 {
ind := start
for ; cn < 0; cn++ {
if ind < 1 {
return 0, nil
}
_, s := utf8.DecodeLastRune(f.b[:ind])
ind -= s
}
return ind, nil
}
ind := start
for ; cn > 0; cn-- {
if ind >= len(f.b)-1 {
return len(f.b), nil
}
_, s := utf8.DecodeRune(f.b[ind:])
ind += s
}
return ind, nil
}
示例8: Allowable
// Allowable parses the pattern and determines the minimum and maximum allowable
// values that the pattern can represent.
// When the max cannot be determined, 'true' will be returned
// for infinite.
func Allowable(pattern string) (min, max string) {
if pattern == "" || pattern[0] == '*' {
return "", ""
}
minb := make([]byte, 0, len(pattern))
maxb := make([]byte, 0, len(pattern))
var wild bool
for i := 0; i < len(pattern); i++ {
if pattern[i] == '*' {
wild = true
break
}
if pattern[i] == '?' {
minb = append(minb, 0)
maxb = append(maxb, maxRuneBytes...)
} else {
minb = append(minb, pattern[i])
maxb = append(maxb, pattern[i])
}
}
if wild {
r, n := utf8.DecodeLastRune(maxb)
if r != utf8.RuneError {
if r < utf8.MaxRune {
r++
if r > 0x7f {
b := make([]byte, 4)
nn := utf8.EncodeRune(b, r)
maxb = append(maxb[:len(maxb)-n], b[:nn]...)
} else {
maxb = append(maxb[:len(maxb)-n], byte(r))
}
}
}
}
return string(minb), string(maxb)
/*
return
if wild {
r, n := utf8.DecodeLastRune(maxb)
if r != utf8.RuneError {
if r < utf8.MaxRune {
infinite = true
} else {
r++
if r > 0x7f {
b := make([]byte, 4)
nn := utf8.EncodeRune(b, r)
maxb = append(maxb[:len(maxb)-n], b[:nn]...)
} else {
maxb = append(maxb[:len(maxb)-n], byte(r))
}
}
}
}
return string(minb), string(maxb), infinite
*/
}
示例9: UnreadRune
func (r *Reader) UnreadRune() rune {
run, size := utf8.DecodeLastRune(r.buf[:r.pos])
if size == 0 {
panic(r.GetFileLineInfo() + " [UnreadRune] last is not valid utf8 code.")
}
r.pos -= size
return run
}
示例10: reverse
func reverse(str []byte) {
for i, j, s1, s2, len1, len2 := 0, len(str), rune(0), rune(0), 0, 0; i < j-1; i, j = i+len2, j-len1 {
s1, len1 = utf8.DecodeRune(str[i:])
s2, len2 = utf8.DecodeLastRune(str[:j])
copy(str[i+len2:j-len1], str[i+len1:j-len2])
copy(str[j-len1:j], []byte(string(s1)))
copy(str[i:i+len2], []byte(string(s2)))
}
}
示例11: main
func main() {
b := []byte("안녕하세요")
r, size := utf8.DecodeRune(b)
fmt.Printf("%c %d\n", r, size) // 안 3: "안녕하세요"의 첫 글자를 디코딩하여 '안', 바이트 수 3
r, size = utf8.DecodeRune(b[3:]) // '안'의 길이가 3이므로 인덱스 3부터 부분 슬라이스를 만들면 "녕하세요"가 됨
fmt.Printf("%c %d\n", r, size) // 녕 3: "녕하세요"를 첫 글자를 디코딩하여 '녕', 바이트 수 3
r, size = utf8.DecodeLastRune(b)
fmt.Printf("%c %d\n", r, size) // 요 3: "안녕하세요"의 마지막 글자를 디코딩하여 '요', 바이트 수 3
// '요'의 길이가 3이므로 // 문자열 길이-3을 하여 부분 슬라이스를 만들면
// "안녕하세"가 됨
r, size = utf8.DecodeLastRune(b[:len(b)-3])
fmt.Printf("%c %d\n", r, size) // 세 3: "안녕하세"의 마지막 글자를 디코딩하여 '세', 바이트 수 3
}
示例12: backup
// backup one rune
func (s *Scanner) backup() {
w := utf8.RuneLen(s.ch)
s.rdOffset -= w
// Copy of slice, this is expensive
r, w := utf8.DecodeLastRune(s.src[:s.rdOffset])
s.offset = s.rdOffset - w
s.ch = r
}
示例13: typing
func (w *win) typing(q0, q1 int) {
if *debug {
defer func(p, e int) {
w.Addr("#%d", w.eAddr)
text, err := w.ReadAll("data")
if err != nil {
panic(err)
}
w.Addr("#%d", w.pAddr)
log.Printf("typing pAddr before: %d, pAddr after: %d, eAddr before: %d, eAddr after: %d [%s]\n", p, w.pAddr, e, w.eAddr, text)
}(w.pAddr, w.eAddr)
}
if q0 < w.pAddr {
d("typing before prompt")
w.pAddr += q1 - q0
}
if q0 < w.eAddr {
d("typing before entry")
w.eAddr += q1 - q0
return
}
if q0 < w.pAddr {
return
}
defer w.Addr("#%d", w.pAddr)
w.Addr("#%d", w.eAddr)
text, err := w.ReadAll("data")
if err != nil {
panic("Failed to read from window: " + err.Error())
}
// If the last character after the prompt isn't a newline then
// wait. This fixes a bug where Send sends two typing
// events, the sent text and a new line. The text won't
// be issued to w.send() until the final newline is received.
// Otherwise the first insert event messes up the
// addresses and the subsequent event (with the newline)
// appears to have inserted a newline before pAddr.
if r, _ := utf8.DecodeLastRune(text); r != '\n' {
return
}
for {
i := bytes.IndexRune(text, '\n')
if i < 0 {
break
}
t := string(text[:i+1])
w.Addr("#%d,#%d", w.pAddr, w.eAddr+utf8.RuneCountInString(t))
w.send(t)
text = text[i+1:]
}
}
示例14: context
func (i *inputBytes) context(pos int) syntax.EmptyOp {
r1, r2 := endOfText, endOfText
if pos > 0 && pos <= len(i.str) {
r1, _ = utf8.DecodeLastRune(i.str[:pos])
}
if pos < len(i.str) {
r2, _ = utf8.DecodeRune(i.str[pos:])
}
return syntax.EmptyOpContext(r1, r2)
}
示例15: Context
func (f *File) Context(pos int) syntax.EmptyOp {
pos += f.pos
r1, r2 := endOfText, endOfText
if pos > 0 && pos <= f.Length() {
if pos < f.gapStart {
r1, _ = utf8.DecodeLastRune(f.b[:pos])
} else {
r1, _ = utf8.DecodeLastRune(f.b[:pos+(f.gapEnd-f.gapStart)])
}
}
if pos < f.Length() {
if pos < f.gapStart {
r2, _ = utf8.DecodeRune(f.b[pos:])
} else {
r2, _ = utf8.DecodeRune(f.b[pos+(f.gapEnd-f.gapStart):])
}
}
return syntax.EmptyOpContext(r1, r2)
}