func canonicalCase(field string) string { upper := strings.ToUpper(field) switch upper { case "SHA1", "SHA256", "SHA512": return upper case "MD5SUM": return "MD5Sum" case "NOTAUTOMATIC": return "NotAutomatic" case "BUTAUTOMATICUPGRADES": return "ButAutomaticUpgrades" } startOfWord := true return strings.Map(func(r rune) rune { if startOfWord { startOfWord = false return unicode.ToUpper(r) } if r == '-' { startOfWord = true } return unicode.ToLower(r) }, field) }
func standardizedLocale(s string) string { localeParts := strings.Split(s, "-") language := localeParts[0] regionOrScript := localeParts[1] switch len(s) { case 5: newRegion := "" for _, v := range regionOrScript { newRegion += string(unicode.ToUpper(v)) } return language + "_" + newRegion case 7: newScript := "" for i, v := range regionOrScript { if i == 0 { newScript += string(unicode.ToUpper(v)) } else { newScript += string(v) } } return language + "-" + newScript } return s }
// ToUpperCamelCase returns a copy of the string s with all Unicode letters mapped to their camel case. // It will convert to upper case previous letter of '_' and first letter, and remove letter of '_'. func ToUpperCamelCase(s string) string { if s == "" { return "" } upper := true start := 0 result := make([]byte, 0, len(s)) var runeBuf [utf8.UTFMax]byte var initialism []byte for _, c := range s { if c == '_' { upper = true candidate := string(result[start:]) initialism = initialism[:0] for _, r := range candidate { if r < utf8.RuneSelf { initialism = append(initialism, toUpperASCII(byte(r))) } else { n := utf8.EncodeRune(runeBuf[:], unicode.ToUpper(r)) initialism = append(initialism, runeBuf[:n]...) } } if length := commonInitialism.LookupByBytes(initialism); length > 0 { result = append(result[:start], initialism...) } start = len(result) continue } if upper { if c < utf8.RuneSelf { result = append(result, toUpperASCII(byte(c))) } else { n := utf8.EncodeRune(runeBuf[:], unicode.ToUpper(c)) result = append(result, runeBuf[:n]...) } upper = false continue } if c < utf8.RuneSelf { result = append(result, byte(c)) } else { n := utf8.EncodeRune(runeBuf[:], c) result = append(result, runeBuf[:n]...) } } candidate := string(result[start:]) initialism = initialism[:0] for _, r := range candidate { if r < utf8.RuneSelf { initialism = append(initialism, toUpperASCII(byte(r))) } else { n := utf8.EncodeRune(runeBuf[:], unicode.ToUpper(r)) initialism = append(initialism, runeBuf[:n]...) } } if length := commonInitialism.LookupByBytes(initialism); length > 0 { result = append(result[:start], initialism...) } return string(result) }
func (g Garbler) uppercase(p string, numUppercase int) string { if numUppercase <= 0 { return p } b := []byte(p) numsDone := 0 for i := 0; i < len(b); i++ { //play nice with environ sequence, //just uppercase 1st and 2nd consonants, //which should make the whole thing more readable if i%5 == 0 || i%5 == 2 { b[i] = byte(unicode.ToUpper(rune(b[i]))) numsDone++ if numsDone >= numUppercase { return string(b) } } } //playing nice didn't work out so do the other letters too //in no particular order for i := 0; i < len(b); i++ { if !(i%5 == 0 || i%5 == 2) { b[i] = byte(unicode.ToUpper(rune(b[i]))) numsDone++ if numsDone >= numUppercase { return string(b) } } } //still here? then numUppercase was too large, panic panic("ouch") }
// This is what generates the actual markov chain func markov(channel string, conn *irc.Conn) { markovData.mutex.RLock() var markovchain string messageLength := random(50) + 10 for i := 0; i < messageLength; i++ { splitchain := strings.Split(markovchain, " ") if len(splitchain) < 2 { s := []rune(markovData.keys[random(len(markovData.keys))]) s[0] = unicode.ToUpper(s[0]) markovchain = string(s) continue } chainlength := len(splitchain) searchfor := strings.ToLower(splitchain[chainlength-2] + " " + splitchain[chainlength-1]) if len(markovData.bigmap[searchfor]) == 0 || strings.LastIndex(markovchain, ".") < len(markovchain)-50 { s := []rune(markovData.keys[random(len(markovData.keys))]) s[0] = unicode.ToUpper(s[0]) markovchain = markovchain + ". " + string(s) continue } randnext := random(len(markovData.bigmap[searchfor])) markovchain = markovchain + " " + markovData.bigmap[searchfor][randnext] } conn.Privmsg(channel, markovchain+".") markovData.mutex.RUnlock() }
func upperWordLetterPairs(runes []rune) ([]runeBigram, int) { limit := len(runes) - 1 if limit < 1 { return make([]runeBigram, 0), 0 } bigrams := make([]runeBigram, limit) var a rune var b rune numPairs := 0 for i := 0; i < limit; i++ { a = runes[i] b = runes[i+1] if unicode.IsSpace(b) { i++ continue } if unicode.IsSpace(a) { continue } bigrams[numPairs] = runeBigram{rA: unicode.ToUpper(a), rB: unicode.ToUpper(b)} numPairs++ } bigrams = bigrams[0:numPairs] return bigrams, numPairs }
func defaultFileName(lang string, pkg *types.Package) string { switch lang { case "java": if pkg == nil { return "Universe.java" } firstRune, size := utf8.DecodeRuneInString(pkg.Name()) className := string(unicode.ToUpper(firstRune)) + pkg.Name()[size:] return className + ".java" case "go": if pkg == nil { return "go_universe.go" } return "go_" + pkg.Name() + ".go" case "objc": if pkg == nil { return "GoUniverse.m" } firstRune, size := utf8.DecodeRuneInString(pkg.Name()) className := string(unicode.ToUpper(firstRune)) + pkg.Name()[size:] return "Go" + className + ".m" } errorf("unknown target language: %q", lang) os.Exit(exitStatus) return "" }
// Goify makes a valid Go identifier out of any string. // It does that by removing any non letter and non digit character and by making sure the first // character is a letter or "_". // Goify produces a "CamelCase" version of the string, if firstUpper is true the first character // of the identifier is uppercase otherwise it's lowercase. func Goify(str string, firstUpper bool) string { runes := []rune(str) w, i := 0, 0 // index of start of word, scan for i+1 <= len(runes) { eow := false // whether we hit the end of a word if i+1 == len(runes) { eow = true } else if !validIdentifier(runes[i]) { // get rid of it runes = append(runes[:i], runes[i+1:]...) } else if runes[i+1] == '_' { // underscore; shift the remainder forward over any run of underscores eow = true n := 1 for i+n+1 < len(runes) && runes[i+n+1] == '_' { n++ } copy(runes[i+1:], runes[i+n+1:]) runes = runes[:len(runes)-n] } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) { // lower->non-lower eow = true } i++ if !eow { continue } // [w,i] is a word. word := string(runes[w:i]) // is it one of our initialisms? if u := strings.ToUpper(word); commonInitialisms[u] { if firstUpper { u = strings.ToUpper(u) } // All the common initialisms are ASCII, // so we can replace the bytes exactly. copy(runes[w:], []rune(u)) } else if w > 0 && strings.ToLower(word) == word { // already all lowercase, and not the first word, so uppercase the first character. runes[w] = unicode.ToUpper(runes[w]) } else if w == 0 && strings.ToLower(word) == word && firstUpper { runes[w] = unicode.ToUpper(runes[w]) } if w == 0 && !firstUpper { runes[w] = unicode.ToLower(runes[w]) } //advance to next word w = i } res := string(runes) if _, ok := reserved[res]; ok { res += "_" } return res }
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 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 } }
// Goify makes a valid Go identifier out of any string. // It does that by removing any non letter and non digit character and by making sure the first // character is a letter or "_". // Goify produces a "CamelCase" version of the string, if firstUpper is true the first character // of the identifier is uppercase otherwise it's lowercase. func Goify(str string, firstUpper bool) string { if firstUpper { if val, ok := acros[str]; ok { return val } for a, u := range acros { p := a + "_" if strings.HasPrefix(str, p) { rest := strings.TrimPrefix(str, p) res := Goify(rest, true) return u + res } } } if str == "ok" && firstUpper { return "OK" } else if str == "id" && firstUpper { return "ID" } var b bytes.Buffer var firstWritten, nextUpper bool for i := 0; i < len(str); i++ { r := rune(str[i]) if r == '_' { nextUpper = true } else if unicode.IsLetter(r) || unicode.IsDigit(r) { if !firstWritten { if firstUpper { r = unicode.ToUpper(r) } else { r = unicode.ToLower(r) } firstWritten = true nextUpper = false } else if nextUpper { r = unicode.ToUpper(r) nextUpper = false } b.WriteRune(r) } } if b.Len() == 0 { return "_v" // you have a better idea? } res := b.String() if _, ok := reserved[res]; ok { res += "_" } return res }
func upperFirst(s string) string { if s == "" { return "" } r, n := utf8.DecodeRuneInString(s) return string(unicode.ToUpper(r)) + s[n:] }
func GenSentence(wordCnt int) string { initWords() words := genWordSlice(wordCnt) firstWord := []rune(words[0]) words[0] = string(unicode.ToUpper(firstWord[0])) + string(firstWord[1:]) return strings.Join(words, " ") + "." }
func New(file string, args []string) (*Script, error) { if len(args)&1 == 1 { return nil, errors.New("number of arguments for template script must be even") } sc := &Script{} if len(args) != 0 { sc.args = make(map[string]string) for i := 0; i < len(args); i += 2 { if len(args[i]) < 2 || args[i][0] != '-' { return nil, errors.New("invalid flag name: " + args[i]) } r, n := utf8.DecodeRuneInString(args[i][1:]) if r == utf8.RuneError { return nil, errors.New("invalid flag name: " + args[i]) } sc.args[string(unicode.ToUpper(r))+args[i][1+n:]] = args[i+1] } } tmpl, err := template.New(filepath.Base(file)).Funcs(sc.funcs()).ParseFiles(file) if err != nil { return nil, err } sc.tmpl = tmpl return sc, nil }
func helpDocumentation(path string) { w := new(bytes.Buffer) w.WriteString(documentationHeader) w.WriteString("\n/*\n") if err := usageTmpl.Execute(w, commands); err != nil { log.Fatal(err) } for _, cmd := range commands { r, rlen := utf8.DecodeRuneInString(cmd.Short) w.WriteString("\n\n") w.WriteRune(unicode.ToUpper(r)) w.WriteString(cmd.Short[rlen:]) w.WriteString("\n\nUsage:\n\n\tgomobile " + cmd.Name) if cmd.Usage != "" { w.WriteRune(' ') w.WriteString(cmd.Usage) } w.WriteRune('\n') w.WriteString(cmd.Long) } w.WriteString("*/\npackage main // import \"golang.org/x/mobile/cmd/gomobile\"\n") if err := ioutil.WriteFile(path, w.Bytes(), 0666); err != nil { log.Fatal(err) } }
func toCamelCase(x string) string { if len(x) == 0 { return "" } output := make([]byte, 0) uppercase := true for len(x) > 0 { v, size := utf8.DecodeRuneInString(x) // If underscore, append and keep going. if v == '_' { uppercase = true } else if unicode.IsLetter(v) { if uppercase { uppercase = false buf := make([]byte, size) utf8.EncodeRune(buf, unicode.ToUpper(v)) output = bytes.Join([][]byte{output, buf}, nil) } else if unicode.IsUpper(v) { buf := make([]byte, size) utf8.EncodeRune(buf, v) output = bytes.Join([][]byte{output, buf}, []byte("_")) } } x = x[size:] } return string(output) }
func isRegister(ident string) bool { if len(ident) != 1 { return false } ch := unicode.ToUpper(rune(ident[0])) return (ch >= 'A' && ch <= 'C' || ch >= 'X' && ch <= 'Z' || ch == 'I' || ch == 'J') }
func UpperFirstCharacter(s string) string { r := []rune(s) r[0] = unicode.ToUpper(r[0]) return string(r) }
func camelize(str string) string { runes := []rune(str) w, i := 0, 0 for i+1 <= len(runes) { eow := false if i+1 == len(runes) { eow = true } else if !validIdentifier(runes[i]) { runes = append(runes[:i], runes[i+1:]...) } else if spacer(runes[i+1]) { eow = true n := 1 for i+n+1 < len(runes) && spacer(runes[i+n+1]) { n++ } copy(runes[i+1:], runes[i+n+1:]) runes = runes[:len(runes)-n] } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) { eow = true } i++ if !eow { continue } runes[w] = unicode.ToUpper(runes[w]) w = i } return string(runes) }
// capitalize changes the first letter of a string to upper case. func capitalize(s string) string { if len(s) == 0 { return s } x, i := utf8.DecodeRuneInString(s) return string(unicode.ToUpper(x)) + string(s[i:]) }
// ToLatin returns a string transliterated using provided mapping table. // Runes that cannot be transliterated inserted as-is. func ToLatin(s string, table map[int]string) string { runes := []int(s) out := make([]int, 0, len(s)) for i, rune := range runes { if tr, ok := table[unicode.ToLower(rune)]; ok { if tr == "" { continue } if unicode.IsUpper(rune) { // Correctly translate case of successive characters: // ЩИ -> SCHI // Щи -> Schi if i+1 < len(runes) && !unicode.IsUpper(runes[i+1]) { t := []int(tr) t[0] = unicode.ToUpper(t[0]) out = append(out, t...) continue } out = append(out, []int(strings.ToUpper(tr))...) continue } out = append(out, []int(tr)...) } else { out = append(out, rune) } } return string(out) }
// SwapCase will swap characters case from upper to lower or lower to upper. func SwapCase(str string) string { var r rune var size int buf := &bytes.Buffer{} for len(str) > 0 { r, size = utf8.DecodeRuneInString(str) switch { case unicode.IsUpper(r): buf.WriteRune(unicode.ToLower(r)) case unicode.IsLower(r): buf.WriteRune(unicode.ToUpper(r)) default: buf.WriteRune(r) } str = str[size:] } return buf.String() }
func (t *Textonym) ReadFrom(r io.Reader) (n int64, err error) { t.numberMap = make(map[string][]string) buf := make([]byte, 0, 32) sc := bufio.NewScanner(r) sc.Split(bufio.ScanWords) scan: for sc.Scan() { buf = buf[:0] word := sc.Text() // XXX we only bother approximating the number of bytes // consumed. This isn't used in the calling code and was // only included to match the io.ReaderFrom interface. n += int64(len(word)) + 1 for _, r := range word { d, ok := t.letterMap[unicode.ToUpper(r)] if !ok { //log.Printf("ignoring %q\n", word) continue scan } buf = append(buf, d) } //log.Printf("scanned %q\n", word) num := string(buf) t.numberMap[num] = append(t.numberMap[num], word) t.count++ if len(t.numberMap[num]) == 2 { t.textonyms++ } //log.Printf("%q → %v\t→ %v\n", word, num, t.numberMap[num]) } return n, sc.Err() }
// pascalCase combines the given words using PascalCase. // // If allowAllCaps is true, when an all-caps word that is not a known // abbreviation is encountered, it is left unchanged. Otherwise, it is // Titlecased. func pascalCase(allowAllCaps bool, words ...string) string { for i, chunk := range words { if len(chunk) == 0 { // foo__bar continue } // known initalism init := strings.ToUpper(chunk) if _, ok := commonInitialisms[init]; ok { words[i] = init continue } // Was SCREAMING_SNAKE_CASE and not a known initialism so Titlecase it. if isAllCaps(chunk) && !allowAllCaps { // A single ALLCAPS word does not count as SCREAMING_SNAKE_CASE. // There must be at least one underscore. words[i] = strings.Title(strings.ToLower(chunk)) continue } // Just another word, but could already be camelCased somehow, so just // change the first letter. head, headIndex := utf8.DecodeRuneInString(chunk) words[i] = string(unicode.ToUpper(head)) + string(chunk[headIndex:]) } return strings.Join(words, "") }
func Capitalize(s string) string { if s == "" { return "" } r, n := utf8.DecodeRuneInString(s) return string(unicode.ToUpper(r)) + strings.ToLower(s[n:]) }
// Converts a string (e.g. a line from a file) into an array of tokens. func TokenizeLine(line string, lowerCase bool, returnChars bool) (tokens []string) { // Lower case everything if required. if lowerCase { line = strings.ToLower(line) } // Split line into characters. if returnChars { // Create a map of acceptable characters. var okChars = make(map[rune]bool) for _, rn := range "abcdefghijklmnopqrstuvwxyz0123456789 ,;:." { okChars[rn] = true okChars[unicode.ToUpper(rn)] = true } // Add rune to tokens if it is acceptable. for _, rn := range line { if okChars[rn] { tokens = append(tokens, string(rn)) } else { tokens = append(tokens, "XXX") } } // Or else split line into "words" by splitting on space. } else { // Insert spaces around break character, remove repeated spaces, and then split on individual spaces. splitchars, _ := regexp.Compile(`\b`) multiplespaces, _ := regexp.Compile(` +`) line = splitchars.ReplaceAllString(line, " $1 ") line = multiplespaces.ReplaceAllString(line, " ") tokens = strings.Split(line, " ") } return }
// lintName returns a different name if it should be different. func lintName(name string) (should string) { // Fast path for simple cases: "_" and all lowercase. if name == "_" { return name } allLower := true for _, r := range name { if !unicode.IsLower(r) { allLower = false break } } if allLower { return name } // Split camelCase at any lower->upper transition, and split on underscores. // Check each word for common initialisms. runes := []rune(name) w, i := 0, 0 // index of start of word, scan for i+1 <= len(runes) { eow := false // whether we hit the end of a word if i+1 == len(runes) { eow = true } else if runes[i+1] == '_' { // underscore; shift the remainder forward over any run of underscores eow = true n := 1 for i+n+1 < len(runes) && runes[i+n+1] == '_' { n++ } copy(runes[i+1:], runes[i+n+1:]) runes = runes[:len(runes)-n] } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) { // lower->non-lower eow = true } i++ if !eow { continue } // [w,i) is a word. word := string(runes[w:i]) if u := strings.ToUpper(word); commonInitialisms[u] { // Keep consistent case, which is lowercase only at the start. if w == 0 && unicode.IsLower(runes[w]) { u = strings.ToLower(u) } // All the common initialisms are ASCII, // so we can replace the bytes exactly. copy(runes[w:], []rune(u)) } else if w > 0 && strings.ToLower(word) == word { // already all lowercase, and not the first word, so uppercase the first character. runes[w] = unicode.ToUpper(runes[w]) } w = i } return string(runes) }
func removeInvalidCharacters(target string) string { var ret bytes.Buffer var channel chan rune var previousInvalid bool channel = make(chan rune) previousInvalid = false go getCharacterChannel(target, channel) for character := range channel { if previousInvalid { character = unicode.ToUpper(character) } previousInvalid = !unicode.IsLetter(character) && !unicode.IsDigit(character) if !previousInvalid { ret.WriteRune(character) } } return ret.String() }
// LookupString attempts to convert a (modifiers, keycode) to an english string. // It essentially implements the rules described at http://goo.gl/qum9q // Namely, the bulleted list that describes how key syms should be interpreted // when various modifiers are pressed. // Note that we ignore the logic that asks us to check if particular key codes // are mapped to particular modifiers (i.e., "XK_Caps_Lock" to "Lock" modifier). // We just check if the modifiers are activated. That's good enough for me. // XXX: We ignore num lock stuff. // XXX: We ignore MODE SWITCH stuff. (i.e., we don't use group 2 key syms.) func LookupString(xu *xgbutil.XUtil, mods uint16, keycode xproto.Keycode) string { k1, k2, _, _ := interpretSymList(xu, keycode) shift := mods&xproto.ModMaskShift > 0 lock := mods&xproto.ModMaskLock > 0 switch { case !shift && !lock: return k1 case !shift && lock: if len(k1) == 1 && unicode.IsLower(rune(k1[0])) { return k2 } else { return k1 } case shift && lock: if len(k2) == 1 && unicode.IsLower(rune(k2[0])) { return string(unicode.ToUpper(rune(k2[0]))) } else { return k2 } case shift: return k2 } return "" }
// Extract a sensible template name from the file path func templateNameFromFilePath(p string) string { name := path.Base(p) if ext := path.Ext(name); ext != "" { name = name[:len(name)-len(ext)] } return string(unicode.ToUpper(rune(name[0]))) + name[1:] }