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


Golang utf8.DecodeLastRune函数代码示例

本文整理汇总了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]
	}
}
开发者ID:hagna,项目名称:prolix,代码行数:26,代码来源:utils.go

示例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
}
开发者ID:ekanite,项目名称:bleve,代码行数:14,代码来源:possessive_filter_en.go

示例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
}
开发者ID:naisanza,项目名称:gotty,代码行数:33,代码来源:utf8reader.go

示例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
}
开发者ID:flyingtime,项目名称:goback,代码行数:27,代码来源:node.go

示例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()
			}
		}
	}
}
开发者ID:hfurubotten,项目名称:kit,代码行数:35,代码来源:multiple_choice.go

示例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
}
开发者ID:xingskycn,项目名称:go,代码行数:27,代码来源:stringreader.go

示例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
}
开发者ID:vron,项目名称:sem,代码行数:27,代码来源:gap.go

示例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
	*/
}
开发者ID:hackintoshrao,项目名称:minio,代码行数:63,代码来源:match.go

示例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
}
开发者ID:keysonZZZ,项目名称:kmg,代码行数:8,代码来源:reader.go

示例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)))
	}
}
开发者ID:yyBeta,项目名称:gopl,代码行数:9,代码来源:ex7.go

示例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
}
开发者ID:jemoonkim,项目名称:golangbook,代码行数:18,代码来源:unicode_utf8_DecodeRune.go

示例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
}
开发者ID:wellington,项目名称:sass,代码行数:10,代码来源:scanner.go

示例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:]
	}
}
开发者ID:aoeu,项目名称:acme,代码行数:56,代码来源:win.go

示例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)
}
开发者ID:ds2dev,项目名称:gcc,代码行数:10,代码来源:regexp.go

示例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)
}
开发者ID:vron,项目名称:sem,代码行数:19,代码来源:gap.go


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