Ejemplo n.º 1
0
func parseOrders(r *bufio.Reader) ([]*order, error) {
	lineno := 0
	var keys, actions []byte
	var o []*order
	var err error
	for err == nil {
		l, _, err := r.ReadLine()
		if err != nil {
			break
		}
		lineno++
		if len(l) == 0 || l[0] == COMMENT {
			continue
		}
		for l[len(l)-1] == LINECONTINUE {
			nl, _, err := r.ReadLine()
			if err != nil {
				return nil, ParseError(fmt.Sprintf("line %d -- %s", lineno, err.Error()))
			}
			l = append(l, bytes.TrimFunc(nl, unicode.IsSpace)...)
		}
		if l[0] != SPACE {
			keys = bytes.Join(bytes.Split(l, []byte{SPACE}), []byte(""))
		} else {
			actions = bytes.TrimFunc(l, unicode.IsSpace)
		}
		if keys != nil && actions != nil {
			o = append(o, neworder(keys, actions))
			keys = nil
			actions = nil
		}
	}
	return o, nil
}
Ejemplo n.º 2
0
// Parse parses the given array of bytes and returns key-value map.
func (p *LineParser) Parse(b []byte) (map[string][]byte, error) {
	m := make(map[string][]byte)
	for i := 0; i < len(b); {
		nextDelim := bytes.IndexRune(b[i:], p.Delimiter)
		if nextDelim < 0 {
			return nil, errors.New("failed to parse, the delimiter is not found")
		}
		key := b[i : i+nextDelim]
		i += nextDelim + 1

		nextLn := bytes.IndexByte(b[i:], '\n')
		if nextLn < 0 {
			return nil, errors.New("failed to parse, the end of line is not found")
		}
		val := b[i : i+nextLn]
		if p.TrimSpaces {
			val = bytes.TrimSpace(val)
		}
		if p.TrimQuotes {
			val = bytes.TrimFunc(val, isQuotationMark)
		}
		m[string(key)] = val
		i += nextLn + 1
	}
	return m, nil
}
Ejemplo n.º 3
0
func main() {
	s := []byte("12345678")
	f := func(r rune) bool {
		return r <= '3' || r >= '6'
	}
	fmt.Println(string(bytes.TrimFunc(s, f)))
}
Ejemplo n.º 4
0
func main() {
	whitespace := " \t\r\n"

	padded := []byte("  \t\r\n\r\n\r\n  hello!!!    \t\t\t\t")
	trimmed := bytes.Trim(padded, whitespace)
	log.Printf("Trim removed runes in %q from the ends of %q to produce %q", whitespace, padded, trimmed)

	rhyme := []byte("aabbccddee")
	trimFunced := bytes.TrimFunc(rhyme, trimOdd)
	log.Printf("TrimFunc removed 'odd' runes from %q to produce %q", rhyme, trimFunced)

	leftTrimmed := bytes.TrimLeft(padded, whitespace)
	log.Printf("TrimLeft removed runes in %q from the left side of %q to produce %q", whitespace, padded, leftTrimmed)

	leftTrimFunced := bytes.TrimLeftFunc(rhyme, trimOdd)
	log.Printf("TrimLeftFunc removed 'odd' runes from the left side of %q to produce %q", rhyme, leftTrimFunced)

	rightTrimmed := bytes.TrimRight(padded, whitespace)
	log.Printf("TrimRight removed runes in %q from the right side of %q to produce %q", whitespace, padded, rightTrimmed)

	rightTrimFunced := bytes.TrimRightFunc(rhyme, trimOdd)
	log.Printf("TrimRightFunc removed 'odd' runes from the right side of %q to produce %q", rhyme, rightTrimFunced)

	spaceTrimmed := bytes.TrimSpace(padded)
	log.Printf("TrimSpace trimmed all whitespace from the ends of %q to produce %q", padded, spaceTrimmed)
}
Ejemplo n.º 5
0
func firstToken(tokens [][]byte) (string, int) {
	for i := 0; i < len(tokens); i++ {
		first := stemmer.Stem(bytes.TrimFunc(tokens[i], notletter))
		if !IsStopWord(string(first)) && len(first) > 0 {
			return string(first), i
		}
	}
	return "", len(tokens)
}
Ejemplo n.º 6
0
func (this *Document) tokenize(sentance []byte) <-chan NGram {
	c := make(chan NGram)
	tokens := SplitFunc(sentance, notletter)
	go func() {
		first, i := firstToken(tokens)
		for j := i + 1; j < len(tokens); j++ {
			last := string(stemmer.Stem(bytes.TrimFunc(tokens[j], notletter)))
			if !IsStopWord(last) && len(last) > 0 {
				c <- NewNGram(first, string(last))
				first = string(last)
			}
		}
		close(c)
	}()
	return c
}
Ejemplo n.º 7
0
func readXorString(data []byte, key byte) (r string) {

	strip_garbo := func(r rune) bool {
		if r == 0 || r == 3 || r == 1 || r == 2 {
			return true
		} else {
			return false
		}
	}

	d := bytes.TrimFunc(data, strip_garbo)
	rBuf := make([]byte, len(d))

	for i := 0; i < len(d); i++ {
		rBuf[i] = d[i] ^ key
	}

	return string(rBuf)
}
Ejemplo n.º 8
0
func parseConfig(r *bufio.Reader) ([]string, error) {
	lineno := 0
	var err error
	var ret []string
	for err == nil {
		l, _, err := r.ReadLine()
		if err != nil {
			break
		}
		lineno++
		if len(l) == 0 || l[0] == COMMENT {
			continue
		}
		for l[len(l)-1] == LINECONTINUE {
			nl, _, err := r.ReadLine()
			if err != nil {
				break
			}
			l = append(l, bytes.TrimFunc(nl, unicode.IsSpace)...)
		}
		ret = append(ret, string(l))
	}
	return ret, err
}
Ejemplo n.º 9
0
func zeroUnPadding(origData []byte) []byte {
	return bytes.TrimFunc(origData,
		func(r rune) bool {
			return r == rune(0)
		})
}