//receive from client func receive(in string) (ret []string) { fmt.Printf("Received: %s\n", in) var response string var answer = make(chan string, 3) sf := func(c rune) bool { return c == ',' || c == ',' || c == ';' || c == '。' || c == '.' || c == '?' || c == '?' } if chinese(in) { go func() { if ret := iceAI(in); ret != "" { answer <- ret } }() go func() { if ret := tlAI(in); ret != "" { answer <- ret } }() go func() { if ret := qinAI(in); ret != "" { answer <- strings.Replace(ret, "Jarvis", "samaritan", -1) } }() response = <-answer // accept the first reply // Separate into fields with func. ret = strings.FieldsFunc(response, sf) } else { response = mitAI(in) ret = strings.FieldsFunc(response, sf) } return }
// isSameOrNewer go version (goA.B) // go1.6 >= go1.6 == true // go1.5 >= go1.6 == false func isSameOrNewer(base, check string) bool { if base == check { return true } if strings.HasPrefix(check, "devel-") { return true } bp := strings.FieldsFunc(base, GoVersionFields) cp := strings.FieldsFunc(check, GoVersionFields) if len(bp) < 2 || len(cp) < 2 { log.Fatalf("Error comparing %s to %s\n", base, check) } if bp[0] == cp[0] { // We only have go version 1 right now bm, err := strconv.Atoi(bp[1]) // These errors are unlikely and there is nothing nice to do here anyway if err != nil { panic(err) } cm, err := strconv.Atoi(cp[1]) if err != nil { panic(err) } return cm >= bm } return false }
// parse and filter list of platforms func ApplyBuildConstraints(buildConstraints string, unfilteredPlatforms []Platform) []Platform { ret := []Platform{} items := strings.FieldsFunc(buildConstraints, func(r rune) bool { return r == ' ' }) if len(items) == 0 { return unfilteredPlatforms } for _, item := range items { parts := strings.FieldsFunc(item, func(r rune) bool { return r == ',' }) itemOs := []string{} itemNegOs := []string{} itemArch := []string{} itemNegArch := []string{} for _, part := range parts { isNeg, modulus := isNegative(part) if IsOs(modulus) { if isNeg { itemNegOs = append(itemNegOs, modulus) } else { itemOs = append(itemOs, modulus) } } else if IsArch(modulus) { if isNeg { itemNegArch = append(itemNegArch, modulus) } else { itemArch = append(itemArch, modulus) } } else { log.Printf("Unrecognised build constraint! Ignoring '%s'", part) } } ret = append(ret, resolveItem(itemOs, itemNegOs, itemArch, itemNegArch, unfilteredPlatforms)...) } return ret }
func (r *Rules) parseRules(expression string, onRule func(functionName string, function interface{}, arguments []string) error) error { functions := map[string]interface{}{ "Host": r.host, "HostRegexp": r.hostRegexp, "Path": r.path, "PathStrip": r.pathStrip, "PathPrefix": r.pathPrefix, "PathPrefixStrip": r.pathPrefixStrip, "Method": r.methods, "Headers": r.headers, "HeadersRegexp": r.headersRegexp, } if len(expression) == 0 { return errors.New("Empty rule") } f := func(c rune) bool { return c == ':' } // Allow multiple rules separated by ; splitRule := func(c rune) bool { return c == ';' } parsedRules := strings.FieldsFunc(expression, splitRule) for _, rule := range parsedRules { // get function parsedFunctions := strings.FieldsFunc(rule, f) if len(parsedFunctions) == 0 { return errors.New("Error parsing rule: '" + rule + "'") } functionName := strings.TrimSpace(parsedFunctions[0]) parsedFunction, ok := functions[functionName] if !ok { return errors.New("Error parsing rule: '" + rule + "'. Unknown function: '" + parsedFunctions[0] + "'") } parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...) fargs := func(c rune) bool { return c == ',' } // get function parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs) if len(parsedArgs) == 0 { return errors.New("Error parsing args from rule: '" + rule + "'") } for i := range parsedArgs { parsedArgs[i] = strings.TrimSpace(parsedArgs[i]) } err := onRule(functionName, parsedFunction, parsedArgs) if err != nil { return fmt.Errorf("Parsing error on rule: %v", err) } } return nil }
func openID(w http.ResponseWriter, r *http.Request) { r.ParseForm() url := *r.URL url.Scheme = "http" url.Host = "localhost:8080" if r.Form.Get("openid.ns") == "" { req := openid.Request{ ReturnTo: url.String(), Teams: strings.FieldsFunc(*teams, isComma), SRegRequired: strings.FieldsFunc(*required, isComma), SRegOptional: strings.FieldsFunc(*optional, isComma), } url := client.RedirectURL(&req) http.Redirect(w, r, url, http.StatusFound) return } resp, err := client.Verify(url.String()) w.Header().Set("ContentType", "text/html") if err != nil { w.WriteHeader(http.StatusBadRequest) errorTemplate.Execute(w, err) return } loginTemplate.Execute(w, resp) }
func parseParameters(params string) Params { pairs := strings.FieldsFunc(params, splitQuotedOn(';')) r := make(map[string][]string, len(pairs)) for _, pair := range pairs { values := strings.FieldsFunc(pair, splitQuotedOn('=', ',')) key := values[0] if len(pair) == 1 { r[key] = append(r[key], "") continue } for i := 1; i < len(values); i++ { val := values[i] if len(val) > 0 && val[0] == '"' { var err error val, err = strconv.Unquote(val) if err != nil { panic(err) } } r[key] = append(r[key], val) } } return Params(r) }
// FieldsFunc splits the string s at each run of Unicode code c satisfying f(c) and returns an array of slices of s. // if all code points in s satisfy f(c) or the string is empty an empty slice is returned. FieldsFunc makes no guarantees // about the order in which is calls f(c). if f does not return consistent results for given c FieldsFunc may crash func FieldsFunc(s string, f func(rune) bool) []string { ft := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Field are: %q", strings.FieldsFunc(" foo1;bar2*baz3...", ft)) // Fields are: ["foo1" "bar2" "baz3"] return strings.FieldsFunc(s, f) }
func ImageLessBySemanticVersion(a, b interface{}) bool { imageA, ok := a.(string) if !ok { return false } imageB, ok := b.(string) if !ok { return false } repoA, tagA, err := ParseDockerImage(imageA) if err != nil { return false } repoB, tagB, err := ParseDockerImage(imageB) if err != nil { return false } if repoA != repoB { return false } sep := func(c rune) bool { return c == '.' || c == ',' || c == '-' } min := func(a, b int) int { if a < b { return a } else { return b } } // compare the tags... we tokenize the tags by delimiters such as . and - fieldsA := strings.FieldsFunc(tagA, sep) fieldsB := strings.FieldsFunc(tagB, sep) for i := 0; i < min(len(fieldsA), len(fieldsB)); i++ { a, erra := strconv.Atoi(fieldsA[i]) b, errb := strconv.Atoi(fieldsB[i]) switch { case erra != nil && errb != nil: if fieldsA[i] == fieldsB[i] { continue } else { return fieldsA[i] < fieldsB[i] } case erra == nil && errb == nil: if a == b { continue } else { return a < b } case erra != nil || errb != nil: return false } } return false }
func GetLastWord(linesrc string, off int) ([]string, []string) { wordsLeft := strings.FieldsFunc(linesrc[:off], func(r rune) bool { return !(unicode.IsLetter(r) || unicode.IsDigit(r)) }) wordsRight := strings.FieldsFunc(linesrc[off:], func(r rune) bool { return !(unicode.IsLetter(r) || unicode.IsDigit(r)) }) return wordsLeft, wordsRight }
// interpret list of destination platforms (based on os & arch settings) //0.5 add support for space delimiters (similar to BuildConstraints) //0.5 add support for different oses/services func GetDestPlatforms(specifiedOses string, specifiedArches string) []Platform { destOses := strings.FieldsFunc(specifiedOses, func(r rune) bool { return r == ',' || r == ' ' }) destArchs := strings.FieldsFunc(specifiedArches, func(r rune) bool { return r == ',' || r == ' ' }) for _, o := range destOses { supported := false for _, supportedPlatformArr := range getSupportedPlatforms() { supportedOs := supportedPlatformArr.Os if o == supportedOs { supported = true } } if !supported { log.Printf("WARNING: Operating System '%s' is unsupported", o) } } for _, o := range destArchs { supported := false for _, supportedPlatformArr := range getSupportedPlatforms() { supportedArch := supportedPlatformArr.Arch if o == supportedArch { supported = true } } if !supported { log.Printf("WARNING: Architecture '%s' is unsupported", o) } } if len(destOses) == 0 { destOses = []string{""} } if len(destArchs) == 0 { destArchs = []string{""} } var destPlatforms []Platform for _, supportedPlatformArr := range getSupportedPlatforms() { supportedOs := supportedPlatformArr.Os supportedArch := supportedPlatformArr.Arch for _, destOs := range destOses { if destOs == "" || supportedOs == destOs { for _, destArch := range destArchs { if destArch == "" || supportedArch == destArch { destPlatforms = append(destPlatforms, supportedPlatformArr) } } } } } if len(destPlatforms) < 1 { log.Printf("WARNING: no valid platforms specified") } return destPlatforms }
// Parse parses rules expressions func (r *Rules) Parse(expression string) (*mux.Route, error) { functions := map[string]interface{}{ "Host": r.host, "HostRegexp": r.hostRegexp, "Path": r.path, "PathStrip": r.pathStrip, "PathPrefix": r.pathPrefix, "PathPrefixStrip": r.pathPrefixStrip, "Method": r.methods, "Headers": r.headers, "HeadersRegexp": r.headersRegexp, } f := func(c rune) bool { return c == ':' } // get function parsedFunctions := strings.FieldsFunc(expression, f) if len(parsedFunctions) == 0 { return nil, errors.New("Error parsing rule: " + expression) } parsedFunction, ok := functions[parsedFunctions[0]] if !ok { return nil, errors.New("Error parsing rule: " + expression + ". Unknow function: " + parsedFunctions[0]) } parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...) fargs := func(c rune) bool { return c == ',' || c == ';' } // get function parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs) if len(parsedArgs) == 0 { return nil, errors.New("Error parsing args from rule: " + expression) } inputs := make([]reflect.Value, len(parsedArgs)) for i := range parsedArgs { inputs[i] = reflect.ValueOf(parsedArgs[i]) } method := reflect.ValueOf(parsedFunction) if method.IsValid() { resultRoute := method.Call(inputs)[0].Interface().(*mux.Route) if r.err != nil { return nil, r.err } if resultRoute.GetError() != nil { return nil, resultRoute.GetError() } return resultRoute, nil } return nil, errors.New("Method not found: " + parsedFunctions[0]) }
func node_style_to_attribute(n *html.Node) { style := node_get_attribute(n, "style") lines := strings.FieldsFunc(style, func(r rune) bool { return strings.ContainsRune(";\n", r) }) for _, line := range lines { fields := strings.FieldsFunc(line, func(r rune) bool { return strings.ContainsRune(": ", r) }) if len(fields) == 2 { node_set_attribute(n, fields[0], fields[1]) } } }
// Recursively query a decoded json blob func rquery(blob interface{}, s ...string) (interface{}, error) { var ( val interface{} err error ) // If there is only a single string argument and if that single string argument has either a "." or a "[" in it // the assume it is a path specification and disagregate it into an array of indexes. terms := s if len(s) == 1 && strings.IndexAny(s[0], ".[]") != -1 { terms = strings.FieldsFunc(s[0], func(c rune) bool { return c == '.' || c == '[' || c == ']' }) } val = blob for _, q := range terms { val, err = query(val, q) if err != nil { return nil, err } } switch val.(type) { case nil: return nil, fmt.Errorf("Nil value found at %s\n", s[len(s)-1]) } return val, nil }
// Attempt to read up to a new line, skipping any comment lines found in // the process. Return a Row object containing the fields read and any // error encountered. func (csvr *Reader) ReadRow() Row { var ( r Row line string ) // Read lines until a non-comment line is found. for true { if line, r.Error = csvr.readLine(); r.Error != nil { return r } csvr.lineNum++ if !csvr.Comments { break } else if !csvr.LooksLikeComment(line) { break } else if csvr.pastHeader && !csvr.CommentsInBody { break } } csvr.pastHeader = true // Break the line up into fields. r.Fields = strings.FieldsFunc(line, func(c rune) bool { return csvr.IsSep(c) }) // Trim any unwanted characters. if csvr.Trim { for i := 0; i < len(r.Fields); i++ { r.Fields[i] = strings.Trim(r.Fields[i], csvr.Cutset) } } return r }
func Args(s string) (result []string) { defer func() { if recover() != nil { result = make([]string, 0, 0) } }() inStr := false escape := false return strings.FieldsFunc(s, func(r rune) bool { if escape { escape = false return false } switch r { case '\\': escape = true return false case ' ', '\n', '\t': return !inStr case '"': inStr = !inStr return true default: return false } }) }
// Headings returns subject headings. func (doc Document) Headings() []string { var headings []string fields := strings.FieldsFunc(doc.Descriptors, func(r rune) bool { return r == ';' || r == '/' }) // refs. #8009 if len(fields) == 1 { fields = strings.FieldsFunc(doc.Descriptors, func(r rune) bool { return r == ',' }) } for _, f := range fields { headings = append(headings, strings.TrimSpace(f)) } return headings }
func nodeDefToMetadata(nodeDef string) (string, graph.Metadata, error) { f := strings.FieldsFunc(nodeDef, func(r rune) bool { return r == '[' || r == ']' }) if len(f) > 2 { return "", nil, fmt.Errorf("Unable to parse node definition: %s", nodeDef) } // by default use the node name as Name metadata attribute metadata := graph.Metadata{ "Name": f[0], "Type": "device", } // parse attributes metadata given if len(f) > 1 { for _, pair := range strings.Split(f[1], ",") { pair = strings.TrimSpace(pair) kv := strings.Split(pair, "=") if len(kv)%2 != 0 { return "", nil, fmt.Errorf("attributes must be defined by pair k=v: %v", nodeDef) } key := strings.Trim(kv[0], `"`) value := strings.Trim(kv[1], `"`) metadata[key] = value } } return f[0], metadata, nil }
func ReadInTrainData( trainString string ) ([]IncrementTable) { // =======> NumVariables & NumRows linesFromTrain, err := readLines( trainString ) fmt.Printf("linesFromTrain is %v, error is %v \n", linesFromTrain, err ) numVariables, numRows := ReturnFileDetails( linesFromTrain ) fmt.Printf("numVariables is %v, numRows is %v \n", numVariables, numRows ) // =======> Read in the values into IncrementTables array trainData := make([]IncrementTable, numVariables) fmt.Printf("trainData is %v \n", trainData ) for i := 2; i < int(numRows) + 2; i ++ { currentRow := linesFromTrain[i] tokenizedString := strings.FieldsFunc( currentRow, delimiter ) yValue, yErr := strconv.ParseInt(tokenizedString[numVariables], 0, 32) fmt.Printf("yValue is %v, yErr is %v \n", yValue, yErr ) for j := 0; j < int(numVariables); j ++ { xValue, xErr := strconv.ParseInt(tokenizedString[j], 0, 32) fmt.Printf("xValue is %v, xErr is %v \n", xValue, xErr ) IncrementInputVariable( &trainData, j, xValue, yValue ) } } return trainData }
func makeWords(q string) []string { words := strings.FieldsFunc(q, nonword) for i, w := range words { words[i] = strings.ToLower(w) } return words }
// Parses a request like "GET /200/500/1000/1500 HTTP/1.1" into 200, 500, 1000, 1500 // Parse errors or missing values are silently ignored and replaced with defaults. func parseRequest(request string) (responseCode, headerSendTime, bodySendTime int) { requestParts := strings.Fields(request) requestPathParts := strings.FieldsFunc(requestParts[1], func(r rune) bool { return r == '/' }) responseCode = DefaultResponseCode headerSendTime = DefaultHeaderSendTime bodySendTime = DefaultBodySendTime if (len(requestPathParts)) >= 1 { v, err := strconv.Atoi(requestPathParts[0]) if err == nil && v >= 0 && v < 1000 { responseCode = v } } if (len(requestPathParts)) >= 2 { v, err := strconv.Atoi(requestPathParts[1]) if err == nil && v >= 0 { headerSendTime = v } } if (len(requestPathParts)) >= 3 { v, err := strconv.Atoi(requestPathParts[2]) if err == nil && v >= 0 { bodySendTime = v } } return // Seriously, Go? }
// SentimentAnalysis takes in a (possibly 'dirty') // sentence (or any block of text,) cleans the // text, finds the sentiment of each word in the // text, finds the sentiment of the sentence as // a whole, adn returns an Analysis struct func (m Models) SentimentAnalysis(sentence string, lang Language) *Analysis { if _, ok := m[lang]; !ok { lang = English } analysis := &Analysis{ Language: lang, Words: []Score{}, Score: uint8(0), } sentences := strings.FieldsFunc(sentence, SplitSentences) if len(sentences) > 1 { analysis.Sentences = []SentenceScore{} for _, s := range sentences { analysis.Sentences = append(analysis.Sentences, SentenceScore{ Sentence: s, Score: m[lang].Predict(s), }) } } w := strings.Split(sentence, " ") for _, word := range w { analysis.Words = append(analysis.Words, Score{ Word: word, Score: m[lang].Predict(word), }) } analysis.Score = m[lang].Predict(sentence) return analysis }
// GetVersion collects version information about current instance // version is of the form '1.2.34-56.7' or '9.8.76a-54.3-log' // want to represent version in form '1.234567' or '9.876543' func (s *MysqlStatDBs) GetVersion() { res, err := s.Db.QueryReturnColumnDict(versionQuery) if err != nil { s.Db.Log(err) return } if len(res["VERSION()"]) == 0 { return } version := res["VERSION()"][0] //filter out letters f := func(r rune) bool { if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') { return true } return false } version = strings.Join(strings.FieldsFunc(version, f), "") //filters out letters from string version = strings.Replace(strings.Replace(version, "-", ".", -1), "_", ".", -1) //replaces "_" and "-" with "." leading := float64(len(strings.Split(version, ".")[0])) version = strings.Replace(version, ".", "", -1) ver, err := strconv.ParseFloat(version, 64) if err != nil { s.Db.Log(err) } ver /= math.Pow(10.0, (float64(len(version)) - leading)) s.Metrics.Version.Set(ver) return }
func hackTranslateVersion(pkgname string, ver string) string { if !strings.HasPrefix(pkgname, "linux-image-generic") { return ver } ffunc := func(c rune) bool { if c == '-' { return true } return false } f := strings.FieldsFunc(ver, ffunc) if len(f) != 2 { return ver } ret := f[1] idx := strings.Index(ret, ".") if idx == -1 { return ver } idx2 := idx + 1 for _, c := range ret[idx2:] { if !unicode.IsDigit(c) { break } idx2++ } ret = f[0] + "." + ret[:idx] + ret[idx2:] return ret }
func init() { cfgFile, err := os.Open("./goconf.cfg") if err != nil { fmt.Println("read config file failed.") return } defer cfgFile.Close() br := bufio.NewReader(cfgFile) for { buff, _, err := br.ReadLine() if err != nil { if err == io.EOF { break } fmt.Println("ReadLine failed, " + err.Error()) return } isSeparator := func(r rune) bool { return r == ':' } ss := strings.FieldsFunc(string(buff), isSeparator) if ss[0][0] == '"' { continue } fmt.Println(ss) switch ss[0] { case "proc": proc = ss[1] /* case "env": env = ss[1] case "match": match = ss[1] */ case "logName": logName = ss[1] case "cmd": command = ss[1] case "dirpath": dirpath = ss[1] + ":" + ss[2] default: fmt.Println("Invalid config parameter !") return } } if proc == "" || /* env == "" || match == "" || */ logName == "" || command == "" || dirpath == "" { fmt.Println("Parameters configured incomplete !") return } fmt.Println() }
func parseScript(s string) (script.Script, error) { m := nameMap() scr := script.NewScript() words := strings.FieldsFunc(s, func(r rune) bool { return r == ' ' || r == '\n' || r == '\t' }) for _, w := range words { if allDigit(w) || (w[0] == '-' && allDigit(w[1:])) { n, err := strconv.Atoi(w) if err != nil { return nil, errors.New(fmt.Sprintf("Error parsing integer: %s", w)) } scr.AppendPushInt(int64(n)) } else if len(w) > 2 && w[:2] == "0x" { data, err := hex.DecodeString(w[2:]) if err != nil { return nil, errors.New(fmt.Sprintf("Error parsing hex: %s", w)) } scr.AppendData(data) } else if len(w) >= 2 && w[0] == '\'' && w[len(w)-1] == '\'' { scr.AppendPushData([]byte(w[1 : len(w)-1])) } else if op, ok := m[w]; ok { scr.AppendOp(op) } else { return nil, errors.New(fmt.Sprintf("Error parsing unknown word: %s", w)) } } return *scr, nil }
func normalize(name string) string { fargs := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } // get function return strings.Join(strings.FieldsFunc(name, fargs), "-") }
func parseTime(in string) (time.Time, error) { parts := strings.FieldsFunc(in, func(r rune) bool { return r == '/' || r == '-' || r == ':' || r == 'T' || r == ' ' || r == '.' }) if len(parts) < 6 { return time.Time{}, fmt.Errorf("Incorrect number of fields: %#v", parts) } np := []int{} for _, p := range parts { x, err := strconv.Atoi(p) if err != nil { return time.Time{}, errors.New("Unparsable time") } np = append(np, x) } nsec := 0 if len(np) > 6 { nsec = np[6] * 1000 } return time.Date(np[0], time.Month(np[1]), np[2], np[3], np[4], np[5], nsec, time.Local), nil }
func parseLine(input string) (line Line) { line.Raw = input line.Time = time.Now() // quick sanity check if len(input) == 0 || input[0] == ' ' { return } // split input, first into "prefix :suffix", and then tokenize prefix comps := strings.SplitN(input, " :", 2) input = comps[0] words := strings.FieldsFunc(input, func(r rune) bool { return r == ' ' }) if len(words) == 0 { // where's my prefix/command? return } else if words[0][0] == ':' { // it has the expected sender prefix line.Src = parseUser(words[0][1:]) words = words[1:] } if len(words) == 0 { // where's my command? return } line.Command = words[0] words = words[1:] if len(comps) > 1 { words = append(words, comps[1]) } line.Args = words return }
func getPeerFromChannel(channel string) (peer string) { if strings.Contains(channel, "IAX2/trunk_") { return "" } //try to find the destination from channel delim := '-' if strings.Contains(channel, "@") { delim = '@' } w := strings.FieldsFunc(channel, func(r rune) bool { switch r { case '/', delim: return true } return false }) if len(w) >= 3 { return w[len(w)-2] } else { return channel } }
// our simplified version of MapReduce does not supply a // key to the Map function, as in the paper; only a value, // which is a part of the input file content. the return // value should be a list of key/value pairs, each represented // by a mapreduce.KeyValue. func Map(value string) *list.List { f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } s := strings.FieldsFunc(value, f) l := list.New() m := make(map[string]string) for _, k := range s { if v, exsits := m[k]; exsits != false { v += "1" m[k] = v } else { m[k] = "1" } } for k, v := range m { //fmt.Println(v)//XXX for test kv := mapreduce.NewKeyValue(k, v) l.PushBack(kv) } return l }