Exemplo n.º 1
1
Arquivo: gas.go Projeto: moshee/gas
// ToSnake is a utility function that converts from camelCase to snake_case.
func ToSnake(in string) string {
	if len(in) == 0 {
		return ""
	}

	out := make([]rune, 0, len(in))
	foundUpper := false
	r := []rune(in)

	for i := 0; i < len(r); i++ {
		ch := r[i]
		if unicode.IsUpper(ch) {
			if i > 0 && i < len(in)-1 && !unicode.IsUpper(r[i+1]) {
				out = append(out, '_', unicode.ToLower(ch), r[i+1])
				i++
				continue
				foundUpper = false
			}
			if i > 0 && !foundUpper {
				out = append(out, '_')
			}
			out = append(out, unicode.ToLower(ch))
			foundUpper = true
		} else {
			foundUpper = false
			out = append(out, ch)
		}
	}
	return string(out)
}
Exemplo n.º 2
0
func rangematch(pattern *string, test rune, flags int) bool {
	if len(*pattern) == 0 {
		return false
	}
	casefold := (flags&FNM_CASEFOLD != 0)
	noescape := (flags&FNM_NOESCAPE != 0)
	if casefold {
		test = unicode.ToLower(test)
	}
	var negate, matched bool
	if (*pattern)[0] == '^' || (*pattern)[0] == '!' {
		negate = true
		(*pattern) = (*pattern)[1:]
	}
	for !matched && len(*pattern) > 1 && (*pattern)[0] != ']' {
		c := unpackRune(pattern)
		if !noescape && c == '\\' {
			if len(*pattern) > 1 {
				c = unpackRune(pattern)
			} else {
				return false
			}
		}
		if casefold {
			c = unicode.ToLower(c)
		}
		if (*pattern)[0] == '-' && len(*pattern) > 1 && (*pattern)[1] != ']' {
			unpackRune(pattern) // skip the -
			c2 := unpackRune(pattern)
			if !noescape && c2 == '\\' {
				if len(*pattern) > 0 {
					c2 = unpackRune(pattern)
				} else {
					return false
				}
			}
			if casefold {
				c2 = unicode.ToLower(c2)
			}
			// this really should be more intelligent, but it looks like
			// fnmatch.c does simple int comparisons, therefore we will as well
			if c <= test && test <= c2 {
				matched = true
			}
		} else if c == test {
			matched = true
		}
	}
	// skip past the rest of the pattern
	ok := false
	for !ok && len(*pattern) > 0 {
		c := unpackRune(pattern)
		if c == '\\' && len(*pattern) > 0 {
			unpackRune(pattern)
		} else if c == ']' {
			ok = true
		}
	}
	return ok && matched != negate
}
Exemplo n.º 3
0
// SnakeCase produces the snake_case version of the given CamelCase string.
func SnakeCase(name string) string {
	for u, l := range toLower {
		name = strings.Replace(name, u, l, -1)
	}
	var b bytes.Buffer
	var lastUnderscore bool
	ln := len(name)
	if ln == 0 {
		return ""
	}
	b.WriteRune(unicode.ToLower(rune(name[0])))
	for i := 1; i < ln; i++ {
		r := rune(name[i])
		nextIsLower := false
		if i < ln-1 {
			n := rune(name[i+1])
			nextIsLower = unicode.IsLower(n) && unicode.IsLetter(n)
		}
		if unicode.IsUpper(r) {
			if !lastUnderscore && nextIsLower {
				b.WriteRune('_')
				lastUnderscore = true
			}
			b.WriteRune(unicode.ToLower(r))
		} else {
			b.WriteRune(r)
			lastUnderscore = false
		}
	}
	return b.String()
}
Exemplo n.º 4
0
func SortAlphabetic(a string, b string) bool {
	iRunes := []rune(a)
	jRunes := []rune(b)

	max := len(iRunes)
	if max > len(jRunes) {
		max = len(jRunes)
	}

	for idx := 0; idx < max; idx++ {
		ir := iRunes[idx]
		jr := jRunes[idx]

		lir := unicode.ToLower(ir)
		ljr := unicode.ToLower(jr)

		if lir != ljr {
			return lir < ljr
		}

		// the lowercase runes are the same, so compare the original
		if ir != jr {
			return ir < jr
		}
	}

	return false
}
Exemplo n.º 5
0
func IndexFold(s, sub string, offset int) (int, int) {
	sub = strings.ToLower(sub)
	n := len(sub)
	if n == 0 {
		return 0, 0
	}
	sc, _ := utf8.DecodeRuneInString(sub)
	for i := offset; i+n <= len(s); {
		c, size := utf8.DecodeRuneInString(s[i:])
		if unicode.ToLower(c) == sc {
			fail := false
			var i_, j int
			for i_, j = i, 0; j < n; {
				c, size := utf8.DecodeRuneInString(s[i_:])
				c2, size2 := utf8.DecodeRuneInString(sub[j:])
				if unicode.ToLower(c) != c2 {
					fail = true
					break
				}
				i_ += size
				j += size2
			}
			if !fail {
				return i, i_
			}
		}
		i += size
	}
	return -1, -1
}
Exemplo n.º 6
0
// accept consumes the next rune if it's equal to the one provided.
func (l *lexer) accept(r rune) bool {
	if unicode.ToLower(l.next()) == unicode.ToLower(r) {
		return true
	}
	l.backup()
	return false
}
Exemplo n.º 7
0
func ConvertToUnderscore(camel string) (string, error) {
	var prevRune rune
	var underscore []rune
	for index, runeChar := range camel {
		if index == 0 {
			if !unicode.IsLetter(runeChar) {
				return "", fmt.Errorf("Table and column names can't start with a character other than a letter.")
			}
			underscore = append(underscore, unicode.ToLower(runeChar))
			prevRune = runeChar
		} else {
			if runeChar == '_' || unicode.IsLetter(runeChar) || unicode.IsDigit(runeChar) {
				//Look for Upper case letters, append _ and make character lower case
				if unicode.IsUpper(runeChar) {
					if !unicode.IsUpper(prevRune) {
						underscore = append(underscore, '_')
					}
					underscore = append(underscore, unicode.ToLower(runeChar))
				} else {
					underscore = append(underscore, runeChar)
				}
			} else {
				return "", fmt.Errorf("Table and column names can't contain non-alphanumeric characters.")
			}
		}
		prevRune = runeChar
	}
	return string(underscore), nil
}
Exemplo n.º 8
0
/*
	Converts the given target string to snake_case, e.g.
	"somethingQuiteFine" becomes "something_quite_fine"
*/
func ToSnakeCase(target string) string {

	var ret bytes.Buffer
	var channel chan rune
	var convertedCharacter string
	var character rune
	var previous bool

	channel = make(chan rune)
	go getCharacterChannel(target, channel)

	character = <-channel
	convertedCharacter = removeInvalidCharacters(string(unicode.ToLower(character)))
	ret.Write([]byte(convertedCharacter))
	previous = false

	for character = range channel {

		if unicode.IsUpper(character) {

			if !previous {
				ret.WriteRune('_')

				previous = true
			}

			ret.WriteRune(unicode.ToLower(character))
		} else {
			ret.WriteRune(character)
			previous = false
		}
	}

	return ret.String()
}
Exemplo n.º 9
0
func hash(rs []rune) string {
	if len(rs) > 2 {
		return fmt.Sprintf("%s%s%s%03d", string(unicode.ToLower(rs[0])), string(unicode.ToLower(rs[1])), string(unicode.ToLower(rs[2])), len(rs))
	}
	if len(rs) > 1 {
		return fmt.Sprintf("%s%s%03d", string(unicode.ToLower(rs[0])), string(unicode.ToLower(rs[1])), len(rs))
	}
	return fmt.Sprintf("%s%03d", string(unicode.ToLower(rs[0])), len(rs))
}
Exemplo n.º 10
0
func (tkn *Tokenizer) scanIdentifier(Type int) *Node {
	buffer := bytes.NewBuffer(make([]byte, 0, 8))
	buffer.WriteByte(byte(unicode.ToLower(rune(tkn.lastChar))))
	for tkn.Next(); isLetter(tkn.lastChar) || isDigit(tkn.lastChar); tkn.Next() {
		buffer.WriteByte(byte(unicode.ToLower(rune(tkn.lastChar))))
	}
	if keywordId, found := keywords[buffer.String()]; found {
		return NewParseNode(keywordId, buffer.Bytes())
	}
	return NewParseNode(Type, buffer.Bytes())
}
Exemplo n.º 11
0
func (lf *File) ValueEq(tk *Token, s []rune) bool {
	v := lf.TokenValue(tk)
	if len(v) != len(s) {
		return false
	}
	for i, r := range v {
		if unicode.ToLower(r) != unicode.ToLower(s[i]) {
			return false
		}
	}
	return true
}
Exemplo n.º 12
0
func Score(suggestion, partial []rune) float64 {
	best := math.MaxFloat64
	for i := len(suggestion) - 1; i >= 0; i-- {
		current := suggestion[i:]
		difficulty := float64(i) * prepend
		missed := float64(0)
		matched := 0
		for _, r := range partial {
			idx := -1
			for k, h := range current {
				if r == h {
					idx = k
					break
				}
				if unicode.ToLower(r) == unicode.ToLower(h) {
					idx = k
					difficulty += changeCase
					break
				}
			}
			if idx == -1 {
				missed++
				continue
			}
			matched++
			switch {
			case matched == 1 && missed < float64(idx):
				difficulty += missed*substitution + (float64(idx)-missed)*prepend
			case missed <= float64(idx):
				difficulty += missed*substitution + (float64(idx)-missed)*insertion
			case missed > float64(idx):
				difficulty += float64(idx)*substitution + (missed-float64(idx))*deletion
			}
			missed = 0
			current = current[idx+1:]
		}
		if float64(matched)/float64(len(partial)) < minMatchPercent {
			continue
		}
		remainder := float64(len(current))
		switch {
		case missed > 0 && missed <= remainder:
			difficulty += missed*substitution + (remainder-missed)*append
		case missed > remainder:
			difficulty += remainder*substitution + (missed-remainder)*deletion
		}
		difficulty += float64(len(current)) * append
		if difficulty < best {
			best = difficulty
		}
	}
	return best
}
Exemplo n.º 13
0
func (s *BigramScoringFunc) Init(mapping *kbdlayout.KeyboardMapping) {
	file, err := os.Open("bigrams.txt")
	if err != nil {
		log.Fatal(err)
	}
	scanner := bufio.NewScanner(file)

	s.bigrams = make([][]uint64, len(mapping.ID2Rune))
	for i := 0; i < len(mapping.ID2Rune); i++ {
		s.bigrams[i] = make([]uint64, len(mapping.ID2Rune))
	}

	for scanner.Scan() {
		line := scanner.Text()

		// read unicode letter
		letter1, size := utf8.DecodeRuneInString(line)
		// remove it from the line
		line = line[size:]

		letter2, size := utf8.DecodeRuneInString(line)
		// remove it and space from line
		line = line[size+1:]

		// make letters lowercase
		letter1 = unicode.ToLower(letter1)
		letter2 = unicode.ToLower(letter2)

		characterId1, ok := mapping.Rune2ID[letter1]
		if !ok {
			// there is no need for this letter, as there is no mapping for it
			continue
		}
		characterId2, ok := mapping.Rune2ID[letter2]
		if !ok {
			// there is no need for this letter, as there is no mapping for it
			continue
		}

		// parse count from the line
		count, err := strconv.ParseUint(line, 10, 64)
		if err != nil {
			log.Fatal(err)
		}
		s.bigrams[characterId1][characterId2] = count
	}

	prepareWeights()

	qwerty := kbdlayout.NewLayout(kbdlayout.Qwerty, mapping)
	s.qwertyScore = s.CalculateScore(&qwerty)
}
Exemplo n.º 14
0
func runeFoldData(r rune) (x struct{ simple, full, special string }) {
	x = foldMap[r]
	if x.simple == "" {
		x.simple = string(unicode.ToLower(r))
	}
	if x.full == "" {
		x.full = string(unicode.ToLower(r))
	}
	if x.special == "" {
		x.special = x.full
	}
	return
}
Exemplo n.º 15
0
// ToSnakeCase returns a copy of the string s with all Unicode letters mapped to their snake case.
// It will insert letter of '_' at position of previous letter of uppercase and all
// letters convert to lower case.
func ToSnakeCase(s string) string {
	if s == "" {
		return ""
	}
	var result bytes.Buffer
	result.WriteRune(unicode.ToLower(rune(s[0])))
	for _, c := range s[1:] {
		if unicode.IsUpper(c) {
			result.WriteRune('_')
		}
		result.WriteRune(unicode.ToLower(c))
	}
	return result.String()
}
Exemplo n.º 16
0
func DbMapper(n string) string {
	var buf bytes.Buffer
	for i, it := range n {
		if i == 0 {
			buf.WriteRune(unicode.ToLower(it))
		} else if unicode.IsUpper(it) {
			buf.WriteString("_")
			buf.WriteRune(unicode.ToLower(it))
		} else {
			buf.WriteRune(it)
		}
	}
	return buf.String()
}
Exemplo n.º 17
0
func (r *runner) refmatch(index, len int) bool {
	var c, pos, cmpos int

	if !r.rightToLeft {
		if r.runtextend-r.runtextpos < len {
			return false
		}

		pos = r.runtextpos + len
	} else {
		if r.runtextpos-0 < len {
			return false
		}

		pos = r.runtextpos
	}
	cmpos = index + len

	c = len

	if !r.caseInsensitive {
		for c != 0 {
			c--
			cmpos--
			pos--
			if r.runtext[cmpos] != r.runtext[pos] {
				return false
			}

		}
	} else {
		for c != 0 {
			c--
			cmpos--
			pos--

			if unicode.ToLower(r.runtext[cmpos]) != unicode.ToLower(r.runtext[pos]) {
				return false
			}
		}
	}

	if !r.rightToLeft {
		pos += len
	}

	r.runtextpos = pos

	return true
}
Exemplo n.º 18
0
func camelToUnderscore(name string) string {
	rv := ""
	for i, r := range name {
		if unicode.IsUpper(r) && i != 0 {
			rv += "_"
			rv += string(unicode.ToLower(r))
		} else if unicode.IsUpper(r) && i == 0 {
			rv += string(unicode.ToLower(r))
		} else {
			rv += string(r)
		}
	}
	return rv
}
Exemplo n.º 19
0
Arquivo: regexp.go Projeto: h12w/gombi
func singleLiteralToCharClass(rx *syntax.Regexp) {
	if rx.Op == syntax.OpLiteral && len(rx.Rune) == 1 {
		char := rx.Rune[0]
		if rx.Flags&syntax.FoldCase != 0 && unicode.ToLower(char) != unicode.ToUpper(char) {
			l, h := unicode.ToLower(char), unicode.ToUpper(char)
			rx.Rune = []rune{h, h, l, l}
			rx.Rune0 = [...]rune{h, h}
		} else {
			rx.Rune = []rune{char, char}
			rx.Rune0 = [...]rune{char, char}
		}
		rx.Op = syntax.OpCharClass
	}
}
Exemplo n.º 20
0
// camelToSnakeCase converts a string containing one or more camel
// cased words into their snake cased equivalents. For example,
// "CamelCase" results in "camel_case".
func camelToSnakeCase(s string) string {
	result := ""
	boundary := true // Are we on a word boundary?
	for _, r := range s {
		if unicode.IsUpper(r) && !boundary {
			result += "_" + string(unicode.ToLower(r))
		} else if unicode.IsUpper(r) {
			result += string(unicode.ToLower(r))
		} else {
			result += string(r)
		}
		boundary = !(unicode.IsLetter(r) || unicode.IsDigit(r))
	}
	return result
}
Exemplo n.º 21
0
func (s *State) printedTabs(items []string) func(tabDirection) (string, error) {
	numTabs := 1
	prefix := longestCommonPrefix(items)
	return func(direction tabDirection) (string, error) {
		if len(items) == 1 {
			return items[0], nil
		}

		if numTabs == 2 {
			if len(items) > 100 {
				fmt.Printf("\nDisplay all %d possibilities? (y or n) ", len(items))
				for {
					next, err := s.readNext()
					if err != nil {
						return prefix, err
					}

					if key, ok := next.(rune); ok {
						if unicode.ToLower(key) == 'n' {
							return prefix, nil
						} else if unicode.ToLower(key) == 'y' {
							break
						}
					}
				}
			}
			fmt.Println("")

			numColumns, numRows, maxWidth := calculateColumns(s.columns, items)

			for i := 0; i < numRows; i++ {
				for j := 0; j < numColumns*numRows; j += numRows {
					if i+j < len(items) {
						if maxWidth > 0 {
							fmt.Printf("%-*.[1]*s", maxWidth, items[i+j])
						} else {
							fmt.Printf("%v ", items[i+j])
						}
					}
				}
				fmt.Println("")
			}
		} else {
			numTabs++
		}
		return prefix, nil
	}
}
Exemplo n.º 22
0
//ifollows is basically case insensitive HasPrefix starting at
//current position to see if input contains the expected string
func (l *lexer) ifollows(es string) bool {
	var pos = l.pos
	//range on string returns runes
	for _, runeValue := range es {
		if pos >= len(l.input) {
			return false
		}
		inpRune, wdth := utf8.DecodeRuneInString(l.input[pos:])
		pos += wdth
		//compare runes if not match return false
		if unicode.ToLower(inpRune) != unicode.ToLower(runeValue) {
			return false
		}
	}
	return true
}
Exemplo n.º 23
0
func (v *lexer) recognizeNumberToken() {
	v.consume()

	if v.peek(0) == 'x' || v.peek(0) == 'X' {
		// Hexadecimal
		v.consume()
		v.lexNumberWithValidator(isHexDigit)
	} else if v.peek(0) == 'b' {
		// Binary
		v.consume()
		v.lexNumberWithValidator(isBinaryDigit)
	} else if v.peek(0) == 'o' {
		// Octal
		v.consume()
		v.lexNumberWithValidator(isOctalDigit)
	} else {
		// Decimal or floating
		v.lexNumberWithValidator(func(r rune) bool {
			if isDecimalDigit(r) || r == '.' {
				return true
			}
			peek := unicode.ToLower(r)
			return peek == 'f' || peek == 'd' || peek == 'q'
		})
	}
}
Exemplo n.º 24
0
// LimitedSepIdentifier builds an identifier out of multiple parts,
// all as lowercase strings and concatenated with the separator
// Non letters and digits are exchanged with dashes and
// reduced to a maximum of one each. If limit is true only
// 'a' to 'z' and '0' to '9' are allowed.
func LimitedSepIdentifier(sep string, limit bool, parts ...interface{}) string {
	iparts := make([]string, 0)
	for _, p := range parts {
		tmp := strings.Map(func(r rune) rune {
			// Check letter and digit.
			if unicode.IsLetter(r) || unicode.IsDigit(r) {
				lcr := unicode.ToLower(r)
				if limit {
					// Only 'a' to 'z' and '0' to '9'.
					if lcr <= unicode.MaxASCII {
						return lcr
					} else {
						return ' '
					}
				} else {
					// Every char is allowed.
					return lcr
				}
			}
			return ' '
		}, fmt.Sprintf("%v", p))
		// Only use non-empty identifier parts.
		if ipart := strings.Join(strings.Fields(tmp), "-"); len(ipart) > 0 {
			iparts = append(iparts, ipart)
		}
	}
	return strings.Join(iparts, sep)
}
Exemplo n.º 25
0
Arquivo: util.go Projeto: ARodri/hume
// parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes
func parseSizeInBytes(sizeStr string) uint {
	sizeStr = strings.TrimSpace(sizeStr)
	lastChar := len(sizeStr) - 1
	multiplier := uint(1)

	if lastChar > 0 {
		if sizeStr[lastChar] == 'b' || sizeStr[lastChar] == 'B' {
			if lastChar > 1 {
				switch unicode.ToLower(rune(sizeStr[lastChar-1])) {
				case 'k':
					multiplier = 1 << 10
					sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
				case 'm':
					multiplier = 1 << 20
					sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
				case 'g':
					multiplier = 1 << 30
					sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
				default:
					multiplier = 1
					sizeStr = strings.TrimSpace(sizeStr[:lastChar])
				}
			}
		}
	}

	size := cast.ToInt(sizeStr)
	if size < 0 {
		size = 0
	}

	return safeMul(uint(size), multiplier)
}
Exemplo n.º 26
0
// key HandleFunc for the http /key endpoint. This only happens if the client
// doesn't support websockets.
func (t *tbfe) key(w http.ResponseWriter, req *http.Request) {
	log.Debug("key: %s", req)
	kc := req.FormValue("keyCode")
	var kp keys.KeyPress
	v, _ := strconv.ParseInt(kc, 10, 32)

	if req.FormValue("altKey") == "true" {
		kp.Alt = true
	}
	if req.FormValue("ctrlKey") == "true" {
		kp.Ctrl = true
	}
	if req.FormValue("metaKey") == "true" {
		kp.Super = true
	}
	if req.FormValue("shiftKey") == "true" {
		kp.Shift = true
	}

	if !kp.Shift {
		v = int64(unicode.ToLower(rune(v)))
	}
	kp.Key = keys.Key(v)
	kp.Text = string(v)
	backend.GetEditor().HandleInput(kp)
}
Exemplo n.º 27
0
// formatName will convert to first character to lower case
func formatName(name string) string {
	ret := []rune(name)
	if len(ret) > 0 {
		ret[0] = unicode.ToLower(ret[0])
	}
	return string(ret)
}
Exemplo n.º 28
0
// toLowerDeferredCopy will function exactly like
// bytes.ToLower() only it will reuse (overwrite)
// the original byte array when possible
// NOTE: because its possible that the lower-case
// form of a rune has a different utf-8 encoded
// length, in these cases a new byte array is allocated
func toLowerDeferredCopy(s []byte) []byte {
	j := 0
	for i := 0; i < len(s); {
		wid := 1
		r := rune(s[i])
		if r >= utf8.RuneSelf {
			r, wid = utf8.DecodeRune(s[i:])
		}
		l := unicode.ToLower(r)
		lwid := utf8.RuneLen(l)
		if lwid > wid {
			// utf-8 encoded replacement is wider
			// for now, punt and defer
			// to bytes.ToLower() for the remainder
			// only known to happen with chars
			//   Rune Ⱥ(570) width 2 - Lower ⱥ(11365) width 3
			//   Rune Ⱦ(574) width 2 - Lower ⱦ(11366) width 3
			rest := bytes.ToLower(s[i:])
			rv := make([]byte, j+len(rest))
			copy(rv[:j], s[:j])
			copy(rv[j:], rest)
			return rv
		} else {
			utf8.EncodeRune(s[j:], l)
		}
		i += wid
		j += lwid
	}
	return s[:j]
}
Exemplo n.º 29
0
func lowerFirst(s string) string {
	if s == "" {
		return ""
	}
	r, n := utf8.DecodeRuneInString(s)
	return string(unicode.ToLower(r)) + s[n:]
}
Exemplo n.º 30
0
func methodQtSignature(method reflect.Method) (signature, result string) {
	var buf bytes.Buffer
	for i, rune := range method.Name {
		if i == 0 {
			buf.WriteRune(unicode.ToLower(rune))
		} else {
			buf.WriteString(method.Name[i:])
			break
		}
	}
	buf.WriteByte('(')
	n := method.Type.NumIn()
	for i := 1; i < n; i++ {
		if i > 1 {
			buf.WriteByte(',')
		}
		buf.WriteString("QVariant")
	}
	buf.WriteByte(')')
	signature = buf.String()

	switch method.Type.NumOut() {
	case 0:
		// keep it as ""
	case 1:
		result = "QVariant"
	default:
		result = "QVariantList"
	}
	return
}