Beispiel #1
0
// TrimFunc returns a slice of the string s with all leading adn trailing Unicode code point
// c satisfying f(c) removed
func TrimFunc(s string, f func(rune) bool) string {
	inner_func := func(r rune) bool {
		return r <= 'c' || r >= 'i'
	}
	fmt.Println(strings.TrimFunc("abcdefghijk", inner_func)) // defgh
	return strings.TrimFunc(s, f)
}
Beispiel #2
0
func (s Store) parse(reader *bufio.Reader, filename string) (err error) {
	lineno := 0
	section := ""
	for err == nil {
		l, _, err := reader.ReadLine()
		if err != nil {
			break
		}
		lineno++
		if len(l) == 0 {
			continue
		}
		line := strings.TrimFunc(string(l), unicode.IsSpace)
		for line[len(line)-1] == '\\' {
			line = line[:len(line)-1]
			l, _, err := reader.ReadLine()
			if err != nil {
				return err
			}
			line += strings.TrimFunc(string(l), unicode.IsSpace)
		}
		section, err = s.parseLine(section, line)
		if err != nil {
			return StoreParseError(filename, lineno)
		}
	}
	return err
}
Beispiel #3
0
func (q *Query) matchImportance(i int) bool {

	if q.Importance == "" {
		return true
	}

	val, err := strconv.Atoi(strings.TrimFunc(q.Importance, unicode.IsLetter))
	if err != nil {
		//maybe don't fatal
		logger.Criticalf("%+v", err)
		return false
	}

	switch strings.TrimFunc(q.Importance, unicode.IsDigit) {
	case "gte":
		return (i >= val)
	case "gt":
		return (i > val)
	case "lte":
		return (i <= val)
	case "lt":
		return (i < val)
	}
	return (val == i)

}
Beispiel #4
0
Datei: ini.go Projekt: tpot/goini
func LoadReader(reader *bufio.Reader) (dict Dict, err error) {
	dict = make(map[string]map[string]string)
	lineno := 0
	section := ""
	dict[section] = make(map[string]string)

	for err == nil {
		l, _, err := reader.ReadLine()
		if err != nil {
			break
		}
		lineno++
		if len(l) == 0 {
			continue
		}
		line := strings.TrimFunc(string(l), unicode.IsSpace)

		for line[len(line)-1] == '\\' {
			line = line[:len(line)-1]
			l, _, err := reader.ReadLine()
			if err != nil {
				return nil, err
			}
			line += strings.TrimFunc(string(l), unicode.IsSpace)
		}

		section, err = dict.parseLine(section, line)
		if err != nil {
			return nil, newError(
				err.Error() + fmt.Sprintf("':%d'.", lineno))
		}
	}

	return
}
Beispiel #5
0
Datei: ini.go Projekt: giel/goini
func (dict Dict) parseLine(section, line string) (string, error) {
	// comments
	if line[0] == '#' || line[0] == ';' {
		return section, nil
	}

	// section name
	if line[0] == '[' && line[len(line)-1] == ']' {
		sec := strings.TrimFunc(line[1:len(line)-1], unicode.IsSpace)
		sec = strings.ToLower(sec)
		dict[sec] = make(map[string]string)
		return sec, nil
	}

	// key = value
	if m := regDoubleQuote.FindAllStringSubmatch(line, 1); m != nil {
		dict.add(section, m[0][1], m[0][2])
		return section, nil
	} else if m = regSingleQuote.FindAllStringSubmatch(line, 1); m != nil {
		dict.add(section, m[0][1], m[0][2])
		return section, nil
	} else if m = regNoQuote.FindAllStringSubmatch(line, 1); m != nil {
		dict.add(section, m[0][1], strings.TrimFunc(m[0][2], unicode.IsSpace))
		return section, nil
	} else if m = regNoValue.FindAllStringSubmatch(line, 1); m != nil {
		dict.add(section, m[0][1], "")
		return section, nil
	}

	return section, newError("iniparser: syntax error at ")
}
Beispiel #6
0
func (s Store) parseLine(section, line string) (string, error) {
	if line[0] == '#' || line[0] == ';' {
		return section, nil
	}

	if line[0] == '[' && line[len(line)-1] == ']' {
		section := strings.TrimFunc(line[1:len(line)-1], unicode.IsSpace)
		section = strings.ToLower(section)
		return section, nil
	}

	if m := regDoubleQuote.FindAllStringSubmatch(line, 1); m != nil {
		s.add(section, m[0][1], m[0][2])
		return section, nil
	} else if m = regSingleQuote.FindAllStringSubmatch(line, 1); m != nil {
		s.add(section, m[0][1], m[0][2])
		return section, nil
	} else if m = regNoQuote.FindAllStringSubmatch(line, 1); m != nil {
		s.add(section, m[0][1], strings.TrimFunc(m[0][2], unicode.IsSpace))
		return section, nil
	} else if m = regNoValue.FindAllStringSubmatch(line, 1); m != nil {
		s.add(section, m[0][1], "")
		return section, nil
	}
	return section, errors.New("conf parse error")
}
Beispiel #7
0
// command executes an external command in the given directory.
// The command's standard out and error are trimmed and returned as strings
// It may return the type *GitError if the command itself fails.
func command(w io.Writer, name, dir string, args ...string) (stdout, stderr string, err error) {
	cmdOut := &bytes.Buffer{}
	cmdErr := &bytes.Buffer{}

	cmd := exec.Command(name, args...)
	cmd.Dir = dir
	if w != nil {
		cmd.Stdout = w
	} else {
		cmd.Stdout = cmdOut
	}
	cmd.Stderr = cmdErr

	err = cmd.Run()
	if w == nil {
		stdout = strings.TrimFunc(cmdOut.String(), unicode.IsSpace)
	}
	stderr = strings.TrimFunc(cmdErr.String(), unicode.IsSpace)
	if exitErr, ok := err.(*exec.ExitError); ok {
		err = &GitError{
			Err:    exitErr,
			Stdout: stdout,
			Stderr: stderr,
		}
	}
	return
}
Beispiel #8
0
func parseMetadata(line string) *metadata {
	index := strings.Index(line, ":")
	if index == -1 {
		return nil
	}

	return &metadata{
		k: strings.TrimFunc(line[:index], unicode.IsSpace),
		v: strings.TrimFunc(line[index+1:], unicode.IsSpace),
	}
}
Beispiel #9
0
func (mp *mesosPorts) UnmarshalJSON(b []byte) error {
	p := strings.TrimFunc(string(b), func(r rune) bool {
		return r == '[' || r == ']' || r == '"'
	})

	ports := []int{}

	for _, s := range strings.Split(p, " ") {
		r := strings.Split(s, "-")
		if len(r) != 2 {
			return fmt.Errorf("expected port range to be in formate XXXX-YYYY, got %s", r)
		}

		start, err := strconv.Atoi(r[0])
		if err != nil {
			return err
		}

		end, err := strconv.Atoi(r[1])
		if err != nil {
			return err
		}

		for i := start; i <= end; i++ {
			ports = append(ports, i)
		}
	}

	*mp = mesosPorts(ports)

	return nil
}
Beispiel #10
0
func SearchJSON(ctx *middleware.Context) {
	q := ctx.Query("q")

	// Clean up keyword.
	q = strings.TrimFunc(q, func(c rune) bool {
		return unicode.IsSpace(c) || c == '"'
	})
	fmt.Println(q)

	pinfos, err := models.SearchPkgInfo(7, q)
	if err != nil {
		log.ErrorD(4, "SearchPkgInfo '%s': %v", q, err)
		return
	}

	results := make([]*searchResult, len(pinfos))
	for i := range pinfos {
		results[i] = &searchResult{
			Title:       pinfos[i].ImportPath,
			Description: pinfos[i].Synopsis,
		}
	}

	ctx.JSON(200, map[string]interface{}{
		"results": results,
	})
}
Beispiel #11
0
Datei: irc.go Projekt: qbit/goirc
// Recv listens for incoming messages. Two constants have been provided for
// use: IRC_READ_CHUNK should be used in almost all cases, as it listens for
// a fixed size message; IRC_READ_ALL will read until the connection closes.
// The block parameter, if false, will set a timeout on the socket (this
// timeout can be changed by modifying the constant IRC_TIMEOUT), resetting
// the socket to blocking mode after the receive.
func (irc *Irc) Recv(size int, block bool) (reply string, err error) {
	buf := make([]byte, size)

	if !block {
		dur, _ := time.ParseDuration(IRC_TIMEOUT)
		timeo := time.Now().Add(dur)
		irc.conn.SetDeadline(timeo)
	}

	if size == 0 {
		buf, err = ioutil.ReadAll(irc.conn)
	} else {
		_, err = irc.conn.Read(buf)
	}

	if err != nil {
		if err == io.EOF {
			fmt.Println("[+] connection closed.")
			irc.Quit(0)
		} else if err != nil && timeout(err) {
			fmt.Println("[-] timeout: resetting err")
			err = nil
		}
	}
	reply = strings.TrimFunc(string(buf), TrimReply)
	irc.conn.SetDeadline(time.Unix(0, 0))
	return
}
Beispiel #12
0
// readData constructs an in-memory vector with all input strings.
func readData(fn string) []string {
	f, err := os.Open(fn)
	if nil != err {
		fmt.Fprintf(os.Stderr, "!! Unable to open the input file: %s; %s\n", fn, err)
		os.Exit(-1)
	}
	defer f.Close()

	r := bufio.NewReaderSize(f, 4*1024)

	v := make([]string, 0, INIT_INPUT_SIZE)

	for {
		by, _, err := r.ReadLine()
		if nil != err {
			break
		}
		s := strings.TrimFunc(string(by), func(c rune) bool {
			if (' ' == c) || ('\t' == c) {
				return true
			}
			return false
		})
		if len(s) > 0 {
			v = append(v, s)
		}
	}

	return v
}
Beispiel #13
0
func addNodeLinks(nodes map[string]*Node, line string) []byte {
	var buf bytes.Buffer
	scan := bufio.NewScanner(strings.NewReader(line))
	scan.Split(bufio.ScanWords)
	for scan.Scan() {
		word := strings.TrimFunc(scan.Text(), unicode.IsPunct)
		parts := strings.Split(word, ".")
		node := word
		method := ""
		if len(parts) == 2 {
			node = parts[0]
			method = parts[1]
		}
		if nodes[node] != nil && ast.IsExported(node) {
			buf.Write([]byte("["))
			buf.Write(scan.Bytes())
			buf.Write([]byte("]("))
			if method == "" {
				buf.Write([]byte(nodeNameToLink(node)))
			} else {
				buf.Write([]byte(methodNameToLink(node, method)))
			}
			buf.Write([]byte(") "))
		} else {
			buf.Write(scan.Bytes())
			buf.Write([]byte(" "))
		}
	}
	return buf.Bytes()
}
Beispiel #14
0
func parseConfigFile(rd io.Reader) (*configFile, error) {
	scanner := bufio.NewScanner(rd)

	cfg := configFile{[]configFileHost{}}

	for scanner.Scan() {
		fields := strings.Fields(scanner.Text())

		if len(fields) == 2 {
			fields[1] = strings.TrimFunc(fields[1], func(r rune) bool {
				return r == '"'
			})

			if fields[0] == "Host" {
				pattern, err := parsePattern(fields[1])
				if err != nil {
					return nil, err
				}

				cfg.hosts = append(cfg.hosts, configFileHost{
					pattern: pattern,
					options: []configFileOption{},
				})
			} else if len(cfg.hosts) > 0 {
				cfg.hosts[len(cfg.hosts)-1].options = append(
					cfg.hosts[len(cfg.hosts)-1].options,
					configFileOption{fields[0], fields[1]},
				)
			}
		}
	}

	return &cfg, nil
}
Beispiel #15
0
func (lt *logTuple) Units() string {
	_, err := lt.Float64()
	if err != nil {
		return ""
	}
	return strings.TrimFunc(string(lt.Val), trimToChar)
}
Beispiel #16
0
// 兼容之前的写法
func (this *Dao) _set(clause string) {
	clauses := strings.Split(clause, ",")
	if len(clauses) >= len(strings.Split(clause, "=")) {
		this.columns = nil
		return
	}

	for _, clause := range clauses {
		parts := strings.Split(clause, "=")
		// 如果参数不合法,让执行的sql报错
		if len(parts) != 2 {
			this.columns = nil
			return
		}
		parts[0] = strings.TrimFunc(parts[0], func(r rune) bool {
			switch r {
			case ' ', '`':
				return true
			}
			return false
		})
		this.columns = append(this.columns, "`"+parts[0]+"`=?")
		this.colValues = append(this.colValues, strings.TrimSpace(parts[1]))
	}
}
Beispiel #17
0
// trim whitespace from a name and return true if ok
// currently names must be at least 1 character long.
func validateName(name string) (string, bool) {
	name = strings.TrimFunc(name, unicode.IsSpace)
	if len(name) < 1 {
		return name, false
	}
	return name, true
}
Beispiel #18
0
func (t *tuple) Units() string {
	_, err := t.Float64()
	if err != nil {
		return ""
	}
	return strings.TrimFunc(string(t.Val), trimToChar)
}
Beispiel #19
0
// SnakeCase converst camel case to snake case
func SnakeCase(s string) string {
	res := upper.ReplaceAllString(s, "_$1")
	res = strings.ToLower(res)
	res = under.ReplaceAllString(res, "_")
	return strings.TrimFunc(res, func(r rune) bool {
		return r == '_'
	})
}
func SplitIntoWords(input string) []Word {
	fields := strings.Fields(input)

	// split words like '2-point', '1-inch' into two words ('2', 'pound')
	mapped := make([]string, 0, len(fields))
	for _, word := range fields {
		trimmed := strings.TrimFunc(word, split.IsTrimmableRune)
		if dash := strings.IndexRune(trimmed, '-'); dash >= 0 {
			left := trimmed[:dash]
			right := trimmed[dash+1:]

			// fmt.Printf("SplitIntoWords dashed %#v, left = %#v, right = %#v, reg(left) = %#v, reg(right) = %#v, num(left) = %#v\n", word, left, right, split.IsRegularWord(left), split.IsRegularWord(right), ClassifyNumberString(left))

			if !split.IsRegularWord(left) && split.IsRegularWord(right) && ClassifyNumberString(left) != NumClassNone {
				idx := strings.Index(word, left)
				if idx < 0 {
					panic(fmt.Sprintf("Cannot find %#v in %#v", left, word))
				}
				dash := idx + len(left)
				fullLeft := word[:dash]
				fullRight := word[dash+1:]

				mapped = append(mapped, fullLeft)
				mapped = append(mapped, fullRight)
				continue
			}
		}
		mapped = append(mapped, word)
	}

	words := make([]Word, 0, len(mapped))
	for _, word := range mapped {
		trimmed := strings.TrimFunc(word, split.IsTrimmableRune)
		trimmed = cleanupWord(trimmed)
		if len(trimmed) == 0 {
			continue
		}

		norm := Normalize(trimmed)

		stem := Stem(trimmed)
		// println(trimmed, "=>", stem)
		words = append(words, Word{Raw: word, Trimmed: trimmed, Stem: stem, Normalized: norm})
	}
	return words
}
Beispiel #21
0
func keys(s string) []string {
	words := strings.Fields(s)
	for i, w := range words {
		w = strings.ToLower(w)
		words[i] = strings.TrimFunc(w, unicode.IsPunct)
	}
	return words
}
Beispiel #22
0
// CleanString cleans a string from new lines and caret returns, un-escapes HTML entities and trims spaces.
func CleanString(str string, unescape bool) string {
	if unescape {
		str = html.UnescapeString(str)
	}
	str = strings.Replace(str, "\n", "", -1)
	str = strings.Replace(str, "\r", "", -1)
	return strings.TrimFunc(str, unicode.IsSpace)
}
Beispiel #23
0
func main() {
	fmt.Println(strings.TrimFunc("&&&&nihao&&&&", func(r rune) bool {
		if r == '&' {
			return true
		}
		return false
	})) //nihao
}
Beispiel #24
0
// command executes an external command in the given directory.
// The command's standard out and error are trimmed and returned as strings
// It may return the type *GitError if the command itself fails.
func command(w io.Writer, name, dir string, env []string, args ...string) (stdout, stderr string, err error) {
	cmdOut := &bytes.Buffer{}
	cmdErr := &bytes.Buffer{}

	glog.V(4).Infof("Executing %s %s", name, strings.Join(args, " "))
	cmd := exec.Command(name, args...)
	cmd.Dir = dir
	if w != nil {
		cmd.Stdout = w
	} else {
		cmd.Stdout = cmdOut
	}
	cmd.Stderr = cmdErr
	cmd.Env = env

	if glog.V(8) && env != nil {
		glog.Infof("Environment:\n")
		for _, e := range env {
			glog.Infof("- %s", e)
		}
	}

	err = cmd.Run()
	if err != nil {
		glog.V(4).Infof("Exec error: %v", err)
	}
	if w == nil {
		stdout = strings.TrimFunc(cmdOut.String(), unicode.IsSpace)
		if len(stdout) > 0 {
			glog.V(4).Infof("Out: %s", stdout)
		}
	}
	stderr = strings.TrimFunc(cmdErr.String(), unicode.IsSpace)
	if len(stderr) > 0 {
		glog.V(4).Infof("Err: %s", cmdErr.String())
	}
	if exitErr, ok := err.(*exec.ExitError); ok {
		err = &GitError{
			Err:    exitErr,
			Stdout: stdout,
			Stderr: stderr,
		}
	}
	return
}
Beispiel #25
0
// MakeSimpleName strips any non-alphanumeric characters out of a string and returns
// either an empty string or a string which is valid for most Kubernetes resources.
func MakeSimpleName(name string) string {
	name = strings.ToLower(name)
	name = invalidServiceChars.ReplaceAllString(name, "")
	name = strings.TrimFunc(name, func(r rune) bool { return r == '-' })
	if len(name) > kuval.DNS1035LabelMaxLength {
		name = name[:kuval.DNS1035LabelMaxLength]
	}
	return name
}
Beispiel #26
0
func slugTrim(s string) string {
	return strings.TrimFunc(s, func(r rune) bool {
		switch r {
		case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0, '-':
			return true
		}
		return false
	})
}
Beispiel #27
0
func countWords(line string, partCounts chan int, wg *sync.WaitGroup) {
	cnt := 0
	for _, word := range strings.Split(line, " ", -1) {
		if strings.TrimFunc(word, cutSet_func) != "" {
			cnt++
		}
	}
	partCounts <- cnt
	wg.Done()
}
Beispiel #28
0
func keywordNormalize(s string) string {
	var fields []string
	for _, s := range strings.FieldsFunc(s, isUserDelim) {
		s = strings.ToLower(strings.TrimFunc(s, isUserDelim))
		if len(s) > 2 {
			fields = append(fields, s)
		}
	}
	return strings.Join(fields, " ")
}
Beispiel #29
0
func cleanupId(msgid string, escapedAt bool) string {
	s := strings.TrimFunc(msgid, func(r rune) bool {
		return r == ' ' || r == '<' || r == '>'
	})
	qe := url.QueryEscape(s)
	if escapedAt {
		return qe
	}
	return strings.Replace(qe, "%40", "@", -1)
}
Beispiel #30
0
func countWords(line string) {
	cnt := 0
	for _, word := range strings.Split(line, " ", -1) {
		if strings.TrimFunc(word, cutSet_func) != "" {
			cnt++
		}
	}
	partCounts <- cnt
	waitGrp.Done()
}