當前位置: 首頁>>代碼示例>>Golang>>正文


Golang strings.TrimFunc函數代碼示例

本文整理匯總了Golang中strings.TrimFunc函數的典型用法代碼示例。如果您正苦於以下問題:Golang TrimFunc函數的具體用法?Golang TrimFunc怎麽用?Golang TrimFunc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了TrimFunc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TrimFunc

// 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)
}
開發者ID:upccup,項目名稱:cuplearn,代碼行數:9,代碼來源:stringsTest.go

示例2: parse

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
}
開發者ID:maiconio,項目名稱:flotilla,代碼行數:28,代碼來源:store.go

示例3: matchImportance

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)

}
開發者ID:straumur,項目名稱:straumur,代碼行數:26,代碼來源:query.go

示例4: LoadReader

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
}
開發者ID:tpot,項目名稱:goini,代碼行數:35,代碼來源:ini.go

示例5: parseLine

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 ")
}
開發者ID:giel,項目名稱:goini,代碼行數:31,代碼來源:ini.go

示例6: parseLine

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")
}
開發者ID:maiconio,項目名稱:flotilla,代碼行數:26,代碼來源:store.go

示例7: command

// 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
}
開發者ID:hloganathan,項目名稱:origin,代碼行數:30,代碼來源:repository.go

示例8: parseMetadata

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),
	}
}
開發者ID:devsmt,項目名稱:blog,代碼行數:11,代碼來源:parser.go

示例9: UnmarshalJSON

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
}
開發者ID:dec5e,項目名稱:zoidberg,代碼行數:32,代碼來源:mesos.go

示例10: SearchJSON

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,
	})
}
開發者ID:hexiaochun,項目名稱:gowalker,代碼行數:27,代碼來源:search.go

示例11: Recv

// 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
}
開發者ID:qbit,項目名稱:goirc,代碼行數:34,代碼來源:irc.go

示例12: readData

// 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
}
開發者ID:js-ojus,項目名稱:damlevdist.go,代碼行數:31,代碼來源:similarity.go

示例13: addNodeLinks

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()
}
開發者ID:md14454,項目名稱:kapacitor,代碼行數:30,代碼來源:main.go

示例14: parseConfigFile

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
}
開發者ID:postfix,項目名稱:sshutil,代碼行數:34,代碼來源:config.go

示例15: Units

func (lt *logTuple) Units() string {
	_, err := lt.Float64()
	if err != nil {
		return ""
	}
	return strings.TrimFunc(string(lt.Val), trimToChar)
}
開發者ID:pjvds,項目名稱:l2met,代碼行數:7,代碼來源:log_tuple.go


注:本文中的strings.TrimFunc函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。