本文整理汇总了Golang中unicode/utf8.DecodeLastRuneInString函数的典型用法代码示例。如果您正苦于以下问题:Golang DecodeLastRuneInString函数的具体用法?Golang DecodeLastRuneInString怎么用?Golang DecodeLastRuneInString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DecodeLastRuneInString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HasSuffixFold
// HasSuffixFold is like strings.HasPrefix but uses Unicode case-folding.
func HasSuffixFold(s, suffix string) bool {
if suffix == "" {
return true
}
// count the runes and bytes in s, but only till rune count of suffix
bo, so := len(s), len(suffix)
for bo > 0 && so > 0 {
r, size := utf8.DecodeLastRuneInString(s[:bo])
if r == utf8.RuneError {
return false
}
bo -= size
sr, size := utf8.DecodeLastRuneInString(suffix[:so])
if sr == utf8.RuneError {
return false
}
so -= size
if !equalFoldRune(r, sr) {
return false
}
}
return so == 0
}
示例2: top
// top returns offset to start of an match.
func (c *ctx) top(tail int, w string) int {
for len(w) > 0 {
if tail <= 0 {
debug.Printf("over backtrack: w=%q", w)
return -1
}
wr, wn := utf8.DecodeLastRuneInString(w)
cr, cn := utf8.DecodeLastRuneInString(c.content[:tail])
tail -= cn
if unicode.IsSpace(wr) {
if !unicode.IsSpace(cr) {
// no spaces which required.
debug.Printf("not space: tail=%d w=%q cr=%q", tail, w, cr)
return -1
}
w = w[:len(w)-wn]
continue
}
if unicode.IsSpace(cr) {
continue
}
w = w[:len(w)-wn]
if cr != wr {
// didn't match runes.
debug.Printf("not match: tail=%d w=%q cr=%q wr=%q",
tail, w, cr, wr)
return -1
}
}
return tail
}
示例3: skinkefy
func skinkefy(word string) string {
isNoun := foundWord(Nouns, word)
isVerb := foundWord(Verbs, word)
isAdjective := foundWord(Adjectives, word)
if UseWordList && !isNoun && !isVerb && !isAdjective {
return word
}
camelCaseCheckR, _ := regexp.Compile("[\\p{Ll}][\\p{Lu}][\\p{L}]+")
camelCaseR, _ := regexp.Compile("([\\p{Lu}][\\p{Ll}]+|[\\p{Lu}]+)")
if camelCaseCheckR.MatchString(word) {
word = camelCaseR.ReplaceAllStringFunc(word, skinkefy)
} else {
if (rand.Intn(100) <= skinkeFactor &&
!IkkeSkinker.Contains(word) &&
len(word) >= skinkeLength) ||
Skinker.Contains(word) {
// Yes, we got a skinke, let's add the word.
Skinker.Append(word)
var skinkeCondition int
first, _ := utf8.DecodeRuneInString(word)
switch {
case strings.ToUpper(word) == word:
skinkeCondition = scFullUpper
case unicode.ToUpper(first) == first:
skinkeCondition = scCapitalised
default:
skinkeCondition = scRegular
}
// Get the last two letters of the word.
end, size := utf8.DecodeLastRuneInString(word)
end2, _ := utf8.DecodeLastRuneInString(word[:len(word)-size])
end = unicode.ToUpper(end)
end2 = unicode.ToUpper(end2)
// If it ends on R, it's plural. That's our rule!
// If it ends on ET/EN, it's definitive.
if isNoun {
word = oneSkinkeNoun(skinkeCondition, end == 'R', end2 == 'E' && (end == 'T' || end == 'N'))
}
if isVerb {
word = oneSkinkeVerb(skinkeCondition, end == 'R', end2 == 'D' && end == 'E')
}
if isAdjective {
word = oneSkinkeAdjective(skinkeCondition)
}
} else {
// Not a skinke word. If it appears again, it should remain
// not skinke.
IkkeSkinker.Append(word)
}
}
return word
}
示例4: OperatorName
func (n *ProtoDecl) OperatorName() rune {
if !(n.IsUnaryOp() || n.IsBinaryOp()) {
panic("Not an operator")
}
r, _ := utf8.DecodeLastRuneInString(n.Name)
return r
}
示例5: main
func main() {
fmt.Println(strings.Index("chicken", "ken"))
fmt.Println(strings.Index("chicken", "dmr"))
fmt.Println(strings.ContainsAny("team", "e"))
fmt.Println(strings.ContainsAny("failure", "u & i"))
fmt.Println(strings.ContainsAny("foo", ""))
fmt.Println(strings.ContainsAny("", ""))
fmt.Println(1 & 1)
fmt.Println(2 & 1)
fmt.Println(4 & 1)
fmt.Println(5 & 1)
pow, sq := HashTest("1234567")
fmt.Println(pow)
fmt.Println(sq)
str := "df 世 界 asd"
s := "1界"
index := strings.IndexAny(str, "界")
fmt.Printf("%d......\n", index)
for i, c := range str {
fmt.Println(i)
for _, m := range s {
fmt.Printf("%c\t%c\n", c, m)
}
}
for i := len(str); i > 0; {
rune, size := utf8.DecodeLastRuneInString(str[0:i])
fmt.Printf("%v\t", i)
i -= size
for _, m := range s {
fmt.Printf("%c\t%v\t%v\n", rune, size, m)
}
}
fmt.Println("bytes =", len(str))
fmt.Println("runes =", utf8.RuneCountInString(str))
for _, ss := range strings.Fields(str) {
fmt.Println(ss)
}
fmt.Println(strings.ToTitle(str))
str = "Hello, 世界"
// for len(str) > 0 {
// r, size := utf8.DecodeRuneInString(str)
// fmt.Printf("%c %v\n", r, size)
// str = str[size:]
// }
fmt.Println(Replace(str, "", "f", -1))
hulu := new(huluwa)
hello(hulu)
}
示例6: rev
func rev(f *os.File) error {
r := bufio.NewReader(f)
for {
line, err := fgetln(r)
for len(line) > 0 {
r, size := utf8.DecodeLastRuneInString(line)
if size == 0 {
break
}
fmt.Printf("%c", r)
line = line[:len(line)-size]
}
if len(line) > 0 || (len(line) == 0 && err == nil) {
fmt.Println()
}
if err == io.EOF {
return nil
}
if err != nil {
return err
}
}
}
示例7: init
func init() {
if len(os.Args) > 1 &&
(os.Args[1] == "-a" || os.Args[1] == "--ascii") {
os.Args = append(os.Args[:1], os.Args[2:]...) // Strip out arg.
IsPalindrome = func(s string) bool { // Simple ASCII-only version
j := len(s) - 1
for i := 0; i < len(s)/2; i++ {
if s[i] != s[j] {
return false
}
j--
}
return true
}
} else {
IsPalindrome = func(s string) bool { // UTF-8 version
for len(s) > 0 {
first, sizeOfFirst := utf8.DecodeRuneInString(s)
if sizeOfFirst == len(s) {
break // s only has one character
}
last, sizeOfLast := utf8.DecodeLastRuneInString(s)
if first != last {
return false
}
s = s[sizeOfFirst : len(s)-sizeOfLast]
}
return true
}
}
}
示例8: init
func init() {
if len(os.Args) > 1 && (os.Args[1] == "-a" || os.Args[1] == "--ascii") {
os.Args = append(os.Args[:1], os.Args[2:]...)
IsPalindrome = func(str string) bool {
j := len(str) - 1
for i := 0; i < len(str)/2; i++ {
if str[i] != str[j] {
return false
}
j--
}
return true
}
} else {
IsPalindrome = func(str string) bool {
for len(str) > 0 {
first, sizeOfFirst := utf8.DecodeRuneInString(str)
if sizeOfFirst == len(str) {
break
}
last, sizeOfLast := utf8.DecodeLastRuneInString(str)
if first != last {
return false
}
str = str[sizeOfLast : len(str)-sizeOfLast]
}
return true
}
}
}
示例9: NewLabel
// NewLabel returns a new label whos name consists of a prefix followed by
// some digits which make it unique.
//
// Only the first length characters of the prefix are used, and the prefix must
// not end with a digit. If no prefix is given, then the default is used.
//
// Label is initially not initally placed.
func NewLabel(prefix string) *Label {
l := &Label{
count: count,
length: 5,
prefix: "label",
}
if utf8.RuneCountInString(prefix) > utf8.RuneCountInString(l.prefix) {
l.prefix = prefix[:utf8.RuneCountInString(prefix)-1]
}
lastRune, _ := utf8.DecodeLastRuneInString(l.prefix)
if prefix == "main" {
l.name = "main"
return l
}
if unicode.IsDigit(lastRune) {
log.Fatal("label ended with a digit")
}
l.name = l.prefix + strconv.Itoa(l.count)
count++
return l
}
示例10: init
func init() {
if len(os.Args) > 1 &&
(os.Args[1] == "-a" || os.Args[1] == "--ascii") {
os.Args = append(os.Args[:1], os.Args[2:]...) // Strip out arg.
IsPalindrome = func(s string) bool { // Simple ASCII-only version
if len(s) <= 1 {
return true
}
if s[0] != s[len(s)-1] {
return false
}
return IsPalindrome(s[1 : len(s)-1])
}
} else {
IsPalindrome = func(s string) bool { // UTF-8 version
if utf8.RuneCountInString(s) <= 1 {
return true
}
first, sizeOfFirst := utf8.DecodeRuneInString(s)
last, sizeOfLast := utf8.DecodeLastRuneInString(s)
if first != last {
return false
}
return IsPalindrome(s[sizeOfFirst : len(s)-sizeOfLast])
}
}
}
示例11: main
func main() {
phrase := "naïve\u2028🚀"
fmt.Printf("string: %s\n", phrase)
fmt.Println("len:", len(phrase),
"RuneCountInString:", utf8.RuneCountInString(phrase))
fmt.Println("index rune char bytes")
for index, char := range phrase {
fmt.Printf(" %2d %-8U %c % X\n",
index, char, char, []byte(string(char)))
}
last_rune, length := utf8.DecodeLastRuneInString(phrase)
fmt.Printf("last rune: 0x%x length: %d\n", last_rune, length)
fmt.Printf("length of string: %d length of []rune: %d\n",
len(phrase), len([]rune(phrase)))
fmt.Printf("line separator: %[1]U 0x%[1]x '%[1]c' %s\n", '\u2028', string('\u2028'))
fmt.Printf("paragraph separator: %[1]U 0x%[1]x '%[1]c' %s\n", '\u2029', string('\u2029'))
line := "rå tørt\u2028vær"
i := strings.IndexFunc(line, unicode.IsSpace)
firstWord := line[:i]
j := strings.LastIndexFunc(line, unicode.IsSpace)
_, size := utf8.DecodeRuneInString(line[j:])
lastWord := line[j+size:]
fmt.Println("size of space:", size)
fmt.Println(firstWord, lastWord)
}
示例12: NewStr
// pointer to a StringCell, set symbol value
func NewStr(s string) (*StringCell, error) {
if r, _ := utf8.DecodeLastRuneInString(s); r != '"' {
err := fmt.Errorf("syntax error: string \" %s \" does not end in \"", s)
return nil, err
}
return &StringCell{value: s}, nil
}
示例13: 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()
}
}
}
}
示例14: lexLiteral
// lexLiteral emits tokens after converting values of the forms:
// <binary>b
// <octal>o
// <hexadecimal>h
// to decimal form
func lexLiteral(l *lexer) lexFn {
parseInt := func(i, sz int) {
var base int
switch string(l.line[i]) {
case "b":
base = 2
case "o":
base = 8
case "h":
base = 16
default:
base = 10
i += sz
}
n, err := strconv.ParseInt(l.line[l.start:i], base, 64)
if err != nil {
l.lexErr(err.Error())
}
t := token{terminal: tLiteral}
t.lexeme = strconv.Itoa(int(n))
l.emit(t)
}
if l.cur == ':' || l.cur == '=' || unicode.IsSpace(l.cur) {
_, sz := utf8.DecodeLastRuneInString(l.line[:l.pos])
parseInt(l.pos-sz, sz)
return lexNext(l)
} else if l.isLast() {
parseInt(l.pos, l.width)
return lexNext
}
return lexLiteral
}
示例15: uiKeyEvent
func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
redraw := false
switch key {
case KeyCtrlC:
t.Text = ""
redraw = true
case KeyEnter:
if t.Input != nil {
t.Input(t.ui, t, t.Text)
}
t.Text = ""
redraw = true
case KeySpace:
t.Text = t.Text + " "
redraw = true
case KeyBackspace:
case KeyBackspace2:
if len(t.Text) > 0 {
if r, size := utf8.DecodeLastRuneInString(t.Text); r != utf8.RuneError {
t.Text = t.Text[:len(t.Text)-size]
redraw = true
}
}
}
if redraw {
t.uiDraw()
}
}