// 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 "" }
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) }
// 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) }
// 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 Rename(filename string, line int, column int, newName string) (ok bool, err *errors.GoRefactorError) { if ok, err = CheckRenameParameters(filename, line, column, newName); !ok { return } programTree := parseProgram(filename) var sym st.Symbol if sym, err = programTree.FindSymbolByPosition(filename, line, column); err == nil { if _, ok := sym.(*st.PointerTypeSymbol); ok { panic("find by position returned pointer type!!!") } if st.IsPredeclaredIdentifier(sym.Name()) { return false, errors.UnrenamableIdentifierError(sym.Name(), " It's a basic language symbol") } if sym.PackageFrom().IsGoPackage { return false, errors.UnrenamableIdentifierError(sym.Name(), " It's a symbol,imported from go library") } if unicode.IsUpper(int(sym.Name()[0])) && !unicode.IsUpper(int(newName[0])) || unicode.IsLower(int(sym.Name()[0])) && !unicode.IsLower(int(newName[0])) { return false, &errors.GoRefactorError{ErrorType: "Can't rename identifier", Message: "can't change access modifier. Changing first letter's case can cause errors."} } if _, ok := sym.Scope().LookUp(newName, filename); ok { return false, errors.IdentifierAlreadyExistsError(newName) } if meth, ok := sym.(*st.FunctionSymbol); ok { if meth.IsInterfaceMethod { return false, errors.UnrenamableIdentifierError(sym.Name(), " It's an interface method") } } if ps, ok := sym.(*st.PackageSymbol); ok { pack, file := programTree.FindPackageAndFileByFilename(filename) impDecl := findImportDecl(pack, file, ps) if impDecl == nil { panic("couldn't find import decl") } if impDecl.Name == nil || impDecl.Name.Name == "" { impDecl.Name = ast.NewIdent(newName) } } fnames, fsets, files, err := renameSymbol(sym, newName, programTree) if err == nil { for i, f := range fnames { programTree.SaveFileExplicit(f, fsets[i], files[i]) } } return err == nil, err } else { return false, err } panic("unreachable code") }
func toSnake(s string) string { in := []rune(s) out := make([]rune, 0, len(in)) for i, c := range in { if i > 0 && unicode.IsUpper(c) && ((i+1 < len(in) && unicode.IsLower(in[i+1])) || unicode.IsLower(in[i-1])) { out = append(out, '_') } out = append(out, unicode.ToLower(c)) } return string(out) }
func (f *Function) TestName() string { if f.Receiver != nil { receiverType := f.Receiver.Type.Value if unicode.IsLower([]rune(receiverType)[0]) { receiverType = "_" + receiverType } return "Test" + receiverType + "_" + f.Name } if unicode.IsLower([]rune(f.Name)[0]) { return "Test_" + f.Name } return "Test" + f.Name }
func test_password(pass string) bool { // Windows AD password needs at leat 7 characters password, and must contain characters from three of the following five categories: // uppercase character // lowercase character // digit character // nonalphanumeric characters // any Unicode character that is categorized as an alphabetic character but is not uppercase or lowercase if len(pass) < 7 { return false } d := 0 l := 0 u := 0 p := 0 o := 0 for _, c := range pass { if unicode.IsDigit(c) { // check digit character d = 1 } else if unicode.IsLower(c) { // check lowercase character l = 1 } else if unicode.IsUpper(c) { // check uppercase character u = 1 } else if unicode.IsPunct(c) { // check nonalphanumeric character p = 1 } else { // other unicode character o = 1 } } if d+l+u+p+o < 3 { return false } return true }
// Stat calculates statistics for all runes read from r. func (m *Main) Stat(r io.RuneReader) (Stats, error) { var stats Stats for { // Read next character. ch, sz, err := r.ReadRune() if err == io.EOF { break } else if err != nil { return stats, err } // Calculate stats. stats.TotalN++ if unicode.IsControl(ch) { stats.ControlN++ } if unicode.IsDigit(ch) { stats.DigitN++ } if unicode.IsGraphic(ch) { stats.GraphicN++ } if unicode.IsLetter(ch) { stats.LetterN++ } if unicode.IsLower(ch) { stats.LowerN++ } if unicode.IsMark(ch) { stats.MarkN++ } if unicode.IsNumber(ch) { stats.NumberN++ } if unicode.IsPrint(ch) { stats.PrintN++ } if unicode.IsPunct(ch) { stats.PunctN++ } if unicode.IsSpace(ch) { stats.SpaceN++ } if unicode.IsSymbol(ch) { stats.SymbolN++ } if unicode.IsTitle(ch) { stats.TitleN++ } if unicode.IsUpper(ch) { stats.UpperN++ } if sz > 1 { stats.MultiByteN++ } } return stats, nil }
// 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() }
func lexTopLevel(l *lexer) stateFn { for l.pos < len(l.input) { r, rlen := l.runeAt(l.start) l.pos += rlen simpleType, isSimple := simpleTokens[r] switch { case isSimple: l.emit(simpleType) case unicode.IsSpace(r): l.start += rlen case unicode.IsUpper(r): return lexVariable case unicode.IsLower(r): return lexSimpleAtom case r == ':': next, _ := l.runeAt(l.pos) if next == '-' { l.pos += 1 l.emit(TknColonDash) continue } fallthrough } } return nil }
func encodeStruct(t reflect.Type) interface{} { var fields []eField for i := 0; i < t.NumField(); i++ { f := t.Field(i) if len(f.Name) > 0 { r, _ := utf8.DecodeRuneInString(f.Name) if unicode.IsLower(r) { continue } } if f.Tag.Get("msgpack") == "ignore" { continue } if f.Anonymous { f.Name = f.Type.Name() } e := eField{ offset: f.Offset, name: f.Name, encode: getTypeEncoder(f.Type), index: f.Index, } _, e.needsReflect = e.encode.(encodeReflectFunc) fields = append(fields, e) } var b []byte l := uint64(len(fields)) switch { case l <= 15: b = make([]byte, 1) b[0] = 0x80 | byte(l) case l <= 0xFFFF: b = make([]byte, 3) b[0] = 0xDE binary.BigEndian.PutUint16(b[1:], uint16(l)) case l <= 0xFFFFFFFF: b = make([]byte, 5) b[0] = 0xDF binary.BigEndian.PutUint32(b[1:], uint32(l)) } return encodeReflectFunc(func(enc *Encoder, v reflect.Value) error { p := v.UnsafeAddr() enc.w.Write(b) for _, e := range fields { _encodeString(enc, unsafe.Pointer(&e.name)) if !e.needsReflect { encode := e.encode.(encodeFunc) if err := encode(enc, unsafe.Pointer(p+e.offset)); err != nil { return err } } else { encode := e.encode.(encodeReflectFunc) if err := encode(enc, v.FieldByIndex(e.index)); err != nil { return err } } } return nil }) }
// processPackageTestFiles runs through ast of each test // file pack and looks for godog suite contexts to register // on run func processPackageTestFiles(packs ...[]string) ([]string, error) { var ctxs []string fset := token.NewFileSet() for _, pack := range packs { for _, testFile := range pack { node, err := parser.ParseFile(fset, testFile, nil, 0) if err != nil { return ctxs, err } ctxs = append(ctxs, astContexts(node)...) } } var failed []string for _, ctx := range ctxs { runes := []rune(ctx) if unicode.IsLower(runes[0]) { expected := append([]rune{unicode.ToUpper(runes[0])}, runes[1:]...) failed = append(failed, fmt.Sprintf("%s - should be: %s", ctx, string(expected))) } } if len(failed) > 0 { return ctxs, fmt.Errorf("godog contexts must be exported:\n\t%s", strings.Join(failed, "\n\t")) } return ctxs, nil }
func camelCaseToSnakeCase(name string) string { buf := new(bytes.Buffer) runes := []rune(name) for i := 0; i < len(runes); i++ { buf.WriteRune(unicode.ToLower(runes[i])) if i != len(runes)-1 && unicode.IsUpper(runes[i+1]) && (unicode.IsLower(runes[i]) || unicode.IsDigit(runes[i]) || (i != len(runes)-2 && unicode.IsLower(runes[i+2]))) { buf.WriteRune('_') } } return buf.String() }
// UnderscoreMapper converts CamelCase strings to their camel_case // counterpart func UnderscoreMapper(str string) string { var ( parts = []string{} cur = []rune{} last2 = [2]rune{} ) for _, c := range str { if unicode.IsUpper(c) { if last2[1] != 0 && unicode.IsLower(last2[1]) { parts = append(parts, string(cur)) cur = nil } cur = append(cur, unicode.ToLower(c)) } else { if last2[0] != 0 && last2[1] != 0 && unicode.IsUpper(last2[0]) && unicode.IsUpper(last2[1]) { parts = append(parts, string(cur[:len(cur)-1])) cur = []rune{cur[len(cur)-1]} } cur = append(cur, c) } last2[0] = last2[1] last2[1] = c } if len(cur) > 0 { parts = append(parts, string(cur)) } return strings.Join(parts, "_") }
func main() { s := "" lower := 0.0 upper := 0.0 underscore := 0.0 symbol := 0.0 fmt.Scanf("%s\n", &s) rs := []rune(s) for _, r := range rs { if unicode.IsLower(r) { lower++ } else if unicode.IsUpper(r) { upper++ } else if isUnderscore(r) { underscore++ } else { symbol++ } } sum := lower + upper + underscore + symbol fmt.Println(underscore / sum) fmt.Println(lower / sum) fmt.Println(upper / sum) fmt.Println(symbol / sum) }
func ToSnakeCase(sep string, s string) string { rsep := []rune(sep) out := make([]rune, 0, len(s)) lastLower := false lastLetter := false for _, r := range s { if lastLower = unicode.IsLower(r); lastLower || r == '_' || r == '-' || r == ' ' { if !lastLower { if lastLetter { out = append(out, rsep...) } goto skip } goto next } else if unicode.IsUpper(r) { if lastLower { log.Println(r, "lastLower + upper(r)") out = append(out, rsep...) } r = unicode.ToLower(r) } lastLower = false next: out = append(out, r) skip: lastLower = false lastLetter = unicode.IsLetter(r) } return string(out) }
// Converts a position in algebraic notation (e.g., "c4" or "a8") to a Position func AlgPos(pos string) (Position, error) { row, err := strconv.Atoi(pos[1:]) if len(pos) != 2 || !unicode.IsLower(rune(pos[0])) || !unicode.IsDigit(rune(pos[1])) || err != nil { return nil, errors.New(fmt.Sprintf("%v is an invalid algebraic notation position", pos)) } return CoordPos(Column(int(pos[0])-int('a')), Row(row-1)), nil }
func commandStart(s *scanner, r rune) error { if unicode.IsSpace(r) { return nil } if unicode.IsLower(r) { if err := s.r.UnreadByte(); err != nil { return err } s.next = keyStart s.command.Name = "SAY" return nil } switch r { case 'J': // JOIN s.commandState = []rune(`OIN`) case 'L': // LEAVE s.commandState = []rune(`EAVE`) case 'S': // SAY s.commandState = []rune(`AY`) case 'E': // ERROR s.commandState = []rune(`RROR`) case 'P': // PASS s.commandState = []rune(`ASS`) default: return fmt.Errorf("parser: invalid command") } s.next = commandEnd s.pushState(commandState) return nil }
// buildFields builds the fields of a struct into a list. func buildFields(structType *ast.StructType, varName string) (fields []*FieldInfo, err error) { for _, field := range structType.Fields.List { if field.Names == nil { return nil, fmt.Errorf("anonymous embeds not supported: %+v", field.Type) } for _, name := range field.Names { var tag string if field.Tag != nil { values := tagRE.FindStringSubmatch(field.Tag.Value) if len(values) >= 2 { tag = values[1] } } if tag == "" { if unicode.IsLower(rune(name.Name[0])) { continue } // Use var name as tag. tag = "\"" + name.Name + "\"" } fullName := varName + "." + name.Name fieldInfo, err := buildField(field.Type, tag, fullName) if err != nil { return nil, err } fields = append(fields, fieldInfo) } } return fields, nil }
// NewFuncReferenceType returns a TypeFactory that returns a method of another type as method value (function). // // Goldigen yaml syntax example: // my_func_type: // func: "@some_type::FancyAction" func NewFuncReferenceType(typeID, functionName string) TypeFactory { if functionName == "" || unicode.IsLower(rune(functionName[0])) { return newInvalidType(fmt.Errorf("can not use unexported method %q as second argument to NewFuncReferenceType", functionName)) } return &funcReferenceType{NewTypeID("@" + typeID + "::" + functionName)} }
func validateHarborPassword(password string) bool { correct := true number := false upper := false lower := false count := 0 for _, letter := range password { switch { case unicode.IsNumber(letter): number = true count++ case unicode.IsUpper(letter): upper = true count++ case unicode.IsLower(letter): lower = true count++ case letter == ' ': correct = false default: count++ } } return correct && number && upper && lower && (count >= 7) }
// 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() }
// CheckPasswordStrength : VErify that the given password strength is good enougth func CheckPasswordStrength(pass string) error { extraCnt := 0 digitCnt := 0 upperCaseCnt := 0 lowerCaseCnt := 0 for _, c := range defs.ExtraCharStr { extraCnt += strings.Count(pass, string(c)) } for _, c := range pass { if unicode.IsUpper(c) { upperCaseCnt++ } else if unicode.IsLower(c) { lowerCaseCnt++ } else if unicode.IsDigit(c) { digitCnt++ } } if len(pass) < MinPasswordLength || extraCnt < minExtraChars || digitCnt < minDigits || upperCaseCnt < minUpperCase || lowerCaseCnt < minLowerCase { return fmt.Errorf("The checked password does not pass the password strength test. In order to be strong, the password must adhere to all of the following:\nContains at least %v characters\nIncludes at least %v digits\nIncludes at least %v letters, where at least %v must be uppercase and at least %v must be lowercase\nIncludes at least %v special characters from the following list:\nSpecial characters: '%v'", MinPasswordLength, minDigits, minUpperCase+minLowerCase, minUpperCase, minLowerCase, minExtraChars, defs.ExtraCharStr) } return nil }
func isSecretStrengthOk(pass string) error { extraCnt := 0 digitCnt := 0 upperCaseCnt := 0 lowerCaseCnt := 0 for _, c := range extraCharStr { extraCnt += strings.Count(pass, string(c)) } for _, c := range pass { if unicode.IsUpper(c) { upperCaseCnt++ } else if unicode.IsLower(c) { lowerCaseCnt++ } else if unicode.IsDigit(c) { digitCnt++ } } if len(pass) < minSecretLen || extraCnt < minExtraChars || digitCnt < minDigits || upperCaseCnt < minUpperCase || lowerCaseCnt < minLowerCase { return fmt.Errorf("The secure storage secret does not pass the secret strength test. In order to be strong, the password must adhere to all of the following:\nContains at least %v characters\nInclude at least: %v digits\nInclude at least %v letters where at least %v must be upper-case and at least %v must be lower-case\nInclude at least %v special characters from the following list:\nSpecial characters: '%v'", minSecretLen, minDigits, minUpperCase+minLowerCase, minUpperCase, minLowerCase, minExtraChars, extraCharStr) } return nil }
/* * Password rules: * at least 7 letters * at least 1 number * at least 1 upper case * at least 1 special character */ func ValidatePassword(value, local string) error { fmt.Println("Validate password", value) if len(value) < 7 { return errors.New(i18n.Translate(local, i18nSec, "text03")) } var num, lower, upper, spec bool for _, r := range value { switch { case unicode.IsDigit(r): num = true case unicode.IsUpper(r): upper = true case unicode.IsLower(r): lower = true case unicode.IsSymbol(r), unicode.IsPunct(r): spec = true } } if num && lower && upper && spec { return nil } return errors.New(i18n.Translate(local, i18nSec, "text03")) }
func orbitCase(c byte) byte { if unicode.IsLower(rune(c)) { return byte(unicode.ToUpper(rune(c))) } else { return byte(unicode.ToLower(rune(c))) } }
func isTestSuffix(name string) bool { if len(name) == 0 { // "Test" is ok. return true } r, _ := utf8.DecodeRuneInString(name) return !unicode.IsLower(r) }
func makeAllPossibleKeys(ctx *context) (res []string) { if ctx.customName != "" { return []string{ctx.customName} } tmp := make(map[string]struct{}) { n := []rune(ctx.name) var buf bytes.Buffer // this is the buffer where we put extra underscores on "word" boundaries var buf2 bytes.Buffer // this is the buffer with the standard naming scheme wroteUnderscore := false for i, r := range ctx.name { if r == '.' { buf.WriteRune('_') buf2.WriteRune('_') wroteUnderscore = true continue } prevOrNextLower := i+1 < len(n) && i-1 > 0 && (unicode.IsLower(n[i+1]) || unicode.IsLower(n[i-1])) if i > 0 && unicode.IsUpper(r) && prevOrNextLower && !wroteUnderscore { buf.WriteRune('_') } buf.WriteRune(r) buf2.WriteRune(r) wroteUnderscore = false } tmp[strings.ToLower(buf.String())] = struct{}{} tmp[strings.ToUpper(buf.String())] = struct{}{} tmp[strings.ToLower(buf2.String())] = struct{}{} tmp[strings.ToUpper(buf2.String())] = struct{}{} } for k := range tmp { res = append(res, k) } sort.Strings(res) return }
// True if the token's first character is lowercase func (p *DefaultWordTokenizer) FirstLower(t *Token) bool { if t.Tok == "" { return false } runes := []rune(t.Tok) return unicode.IsLower(runes[0]) }