Exemple #1
0
// 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
}
Exemple #2
0
// 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
}
Exemple #3
0
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
}
func (n *ProtoDecl) OperatorName() rune {
	if !(n.IsUnaryOp() || n.IsBinaryOp()) {
		panic("Not an operator")
	}
	r, _ := utf8.DecodeLastRuneInString(n.Name)
	return r
}
Exemple #5
0
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)
}
Exemple #6
0
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
		}
	}
}
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
		}
	}
}
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
		}
	}
}
Exemple #9
0
// 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
}
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])
		}
	}
}
Exemple #11
0
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)
}
Exemple #12
0
// 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
}
Exemple #13
0
// 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()
			}
		}
	}
}
Exemple #14
0
// 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
}
Exemple #15
0
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()
	}
}
Exemple #16
0
//Last returns the last rune in a string s, which must be non-empty.
func Last(s string) rune {
	if s == "" {
		panic("empty list")
	}

	r, _ := utf8.DecodeLastRuneInString(s)
	return r
}
Exemple #17
0
func (l *listing) backspace() bool {
	_, size := utf8.DecodeLastRuneInString(l.filter)
	if size > 0 {
		l.changeFilter(l.filter[:len(l.filter)-size])
		return true
	}
	return false
}
Exemple #18
0
// Add adds a string pattern with user value v.
func (m *Matcher) Add(pattern string, v interface{}) {
	_, n := utf8.DecodeLastRuneInString(pattern)
	m.trie.Put(pattern, &Data{
		Pattern: &pattern,
		Offset:  len(pattern) - n,
		Value:   v,
	})
}
Exemple #19
0
func (ps *parser) backup() {
	if ps.overEOF > 0 {
		ps.overEOF -= 1
		return
	}
	_, s := utf8.DecodeLastRuneInString(ps.src[:ps.pos])
	ps.pos -= s
}
Exemple #20
0
func (u URI) resolve(s string) URI {
	r, _ := utf8.DecodeLastRuneInString(s)
	switch r {
	case '/':
		return NewURI(strings.TrimSuffix(string(u), "/") + s)
	case '#':
		return NewURI(strings.TrimSuffix(string(u), "#") + s)
	default:
		r2, _ := utf8.DecodeLastRuneInString(string(u))
		switch r2 {
		case '/', '#':
			return NewURI(string(u) + s)
		default:
			return NewURI(string(u) + "/" + s)
		}
	}
}
Exemple #21
0
func makeString(token string) (*types.String, error) {
	lastRune, _ := utf8.DecodeLastRuneInString(token)
	if !isDoubleQuote(lastRune) {
		return nil, &parseError{"Unterminated string", token}
	}

	return &types.String{token[1 : len(token)-1]}, nil
}
Exemple #22
0
func main() {
	str := "чупа чупс"
	first, size := utf8.DecodeRuneInString(str)
	last, size2 := utf8.DecodeLastRuneInString(str)
	fmt.Println(string(first), "размер =", size, string(last), "размер =", size2)
	znak := []int32(str)
	fmt.Println(string(znak[0]))
	fmt.Printf("%U\n", znak[1])
}
Exemple #23
0
func killRuneLeft(ed *Editor) {
	if ed.dot > 0 {
		_, w := utf8.DecodeLastRuneInString(ed.line[:ed.dot])
		ed.line = ed.line[:ed.dot-w] + ed.line[ed.dot:]
		ed.dot -= w
	} else {
		ed.flash()
	}
}
Exemple #24
0
func fSay(wr io.Writer, msg string, trim bool) (int, error) {
	if trim {
		msg = strings.TrimRightFunc(msg, unicode.IsSpace)
	}
	if c, _ := utf8.DecodeLastRuneInString(msg); unicode.IsSpace(c) {
		return fmt.Fprint(wr, msg)
	}
	return fmt.Fprintln(wr, msg)
}
Exemple #25
0
// exactly16Bytes truncates the string if necessary so it is at most 16 bytes long,
// then pads the result with spaces to be exactly 16 bytes.
// Fmt uses runes for its width calculation, but we need bytes in the entry header.
func exactly16Bytes(s string) string {
	for len(s) > 16 {
		_, wid := utf8.DecodeLastRuneInString(s)
		s = s[:len(s)-wid]
	}
	const sixteenSpaces = "                "
	s += sixteenSpaces[:16-len(s)]
	return s
}
Exemple #26
0
func killRuneLeft(ed *Editor, k Key) *leReturn {
	if ed.dot > 0 {
		_, w := utf8.DecodeLastRuneInString(ed.line[:ed.dot])
		ed.line = ed.line[:ed.dot-w] + ed.line[ed.dot:]
		ed.dot -= w
	} else {
		ed.beep()
	}
	return nil
}
Exemple #27
0
func (i *inputString) context(pos int) syntax.EmptyOp {
	r1, r2 := endOfText, endOfText
	if pos > 0 && pos <= len(i.str) {
		r1, _ = utf8.DecodeLastRuneInString(i.str[:pos])
	}
	if pos < len(i.str) {
		r2, _ = utf8.DecodeRuneInString(i.str[pos:])
	}
	return syntax.EmptyOpContext(r1, r2)
}
func WithTrailingDot(s string) string {
	if s == "" {
		return s
	}
	lastRune, _ := utf8.DecodeLastRuneInString(s)
	if lastRune != rune('.') {
		return s + "."
	}
	return s
}
// lastIndexFunc is the same as LastIndexFunc except that if
// truth==false, the sense of the predicate function is
// inverted.
func lastIndexFunc(s string, f func(rune) bool, truth bool) int {
	for i := len(s); i > 0; {
		r, size := utf8.DecodeLastRuneInString(s[0:i])
		i -= size
		if f(r) == truth {
			return i
		}
	}
	return -1
}
Exemple #30
0
func ToAccusative(word string) string {
	_, last_rune_size := utf8.DecodeLastRuneInString(word)
	_, last_rune_size2 := utf8.DecodeLastRuneInString(word[0 : len(word)-last_rune_size])
	suffix2 := word[len(word)-(last_rune_size+last_rune_size2):]
	prefix2 := word[0 : len(word)-(last_rune_size+last_rune_size2)]

	// для прилагательных
	switch suffix2 {
	case "ая": // женский род
		return prefix2 + "ую"
	case "яя": // женский род
		return prefix2 + "юю"
	case "ое": // средний род
		return prefix2 + "ое"
	case "ее": // средний род
		return prefix2 + "ее"
	case "ой": // мужской род
		return prefix2 + "ого" // needs check
	case "ый": // мужской род
		return prefix2 + "ого" // needs check
	case "ий": // мужской род
		return prefix2 + "его" // needs check
	}

	suffix1 := word[len(word)-last_rune_size:]
	prefix1 := word[0 : len(word)-last_rune_size]

	declension := detectDeclension(word)
	switch {
	case suffix1 == "а" && declension == 1:
		return prefix1 + "у"
	case suffix1 == "я" && declension == 1:
		return prefix1 + "ю"
	case declension == 2 && suffix1 != "о" && suffix1 != "е":
		return word + "а"
	case declension == 2 && suffix1 == "о":
		return prefix1 + ""
	case declension == 2 && suffix1 == "е":
		return prefix1 + "я"
	}

	return word
}