本文整理汇总了Golang中unicode.ToLower函数的典型用法代码示例。如果您正苦于以下问题:Golang ToLower函数的具体用法?Golang ToLower怎么用?Golang ToLower使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToLower函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ToSnake
// 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)
}
示例2: rangematch
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
}
示例3: SnakeCase
// 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()
}
示例4: SortAlphabetic
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
}
示例5: IndexFold
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
}
示例6: accept
// 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
}
示例7: ConvertToUnderscore
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
}
示例8: ToSnakeCase
/*
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()
}
示例9: hash
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))
}
示例10: scanIdentifier
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())
}
示例11: ValueEq
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
}
示例12: Score
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
}
示例13: Init
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)
}
示例14: runeFoldData
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
}
示例15: ToSnakeCase
// 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()
}