Esempio n. 1
0
func quotedWordCutter(reader *strings.Reader) (string, bool) {
	var buffer bytes.Buffer
	for {
		if reader.Len() <= 0 {
			return "", false
		}
		ch, _, _ := reader.ReadRune()
		if ch != ' ' {
			reader.UnreadRune()
			break
		}
	}
	quote := NULQUOTE
	yenCount := 0
	for reader.Len() > 0 {
		ch, _, _ := reader.ReadRune()
		if yenCount%2 == 0 {
			if quote == NULQUOTE && (ch == '"' || ch == '\'') {
				quote = ch
			} else if quote != NULQUOTE && ch == quote {
				quote = NULQUOTE
			}
		}
		if ch == ' ' && quote == NULQUOTE {
			break
		}
		if ch == '\\' {
			yenCount++
		} else {
			yenCount = 0
		}
		buffer.WriteRune(ch)
	}
	return buffer.String(), true
}
Esempio n. 2
0
func insertHistory(buffer *bytes.Buffer, reader *strings.Reader, historyNo int) {
	history1 := conio.DefaultEditor.Histories[historyNo]
	ch, siz, _ := reader.ReadRune()
	if siz > 0 && ch == '^' {
		if len(history1.Word) >= 2 {
			buffer.WriteString(history1.At(1))
		}
	} else if siz > 0 && ch == '$' {
		if len(history1.Word) >= 2 {
			buffer.WriteString(history1.At(-1))
		}
	} else if siz > 0 && ch == '*' {
		if len(history1.Word) >= 2 {
			buffer.WriteString(strings.Join(history1.Word[1:], " "))
		}
	} else if siz > 0 && ch == ':' {
		n, count := atoi_(reader)
		if count <= 0 {
			buffer.WriteRune(':')
		} else if n < len(history1.Word) {
			buffer.WriteString(history1.Word[n])
		}
	} else {
		if siz > 0 {
			reader.UnreadRune()
		}
		buffer.WriteString(history1.Line)
	}
}
Esempio n. 3
0
func nextRune(r *strings.Reader) rune {
	ch, _, err := r.ReadRune()
	if err != nil {
		return utf8.RuneError
	}
	return unicode.ToLower(ch)
}
Esempio n. 4
0
func (t *Tree) uiDraw() {
	t.ui.beginDraw()
	defer t.ui.endDraw()

	if t.lines == nil {
		t.Rebuild()
	}

	line := 0
	for y := t.y0; y < t.y1; y++ {
		var reader *strings.Reader
		var item TreeItem
		level := 0
		if line < len(t.lines) {
			item = t.lines[line].Item
			level = t.lines[line].Level
			reader = strings.NewReader(item.String())
		}
		for x := t.x0; x < t.x1; x++ {
			var chr rune = ' '
			fg := t.Fg
			bg := t.Bg
			dx := x - t.x0
			dy := y - t.y0
			if reader != nil && level*2 <= dx {
				if ch, _, err := reader.ReadRune(); err == nil {
					chr = ch
					fg, bg = item.TreeItemStyle(fg, bg, t.active && t.activeLine == dy)
				}
			}
			termbox.SetCell(x, y, chr, termbox.Attribute(fg), termbox.Attribute(bg))
		}
		line++
	}
}
Esempio n. 5
0
func sum(stream *strings.Reader) (s int) {
	v, err := stream.ReadByte()
	for ; v != 0 && err != os.EOF; v, err = stream.ReadByte() {
		s += int(v)
	}

	return
}
Esempio n. 6
0
func collectQuotation(r *strings.Reader) *Quotation {
	var buffer bytes.Buffer
	for ch, _, err := r.ReadRune(); err == nil && ch != ']'; ch, _, err = r.ReadRune() {
		buffer.WriteRune(ch)
	}

	parsedTokens := tokenize(buffer.String())

	return &Quotation{*parsedTokens}
}
Esempio n. 7
0
// Get the Trie at the end of a strings.Reader
func (t *Trie) Get(s *strings.Reader) *Trie {
	rune, _, err := s.ReadRune()
	if err != nil {
		// We have reached EOF
		return t
	}
	t = t.children[rune]
	if t == nil {
		return nil
	}
	return t.Get(s)
}
Esempio n. 8
0
func (s *GitUploadPackService) applyHeadersToRequest(req *http.Request, content *strings.Reader) {
	req.Header.Add("User-Agent", "git/1.0")
	req.Header.Add("Host", "github.com")

	if content == nil {
		req.Header.Add("Accept", "*/*")
	} else {
		req.Header.Add("Accept", "application/x-git-upload-pack-result")
		req.Header.Add("Content-Type", "application/x-git-upload-pack-request")
		req.Header.Add("Content-Length", string(content.Len()))
	}
}
Esempio n. 9
0
// In performance critical applications, Reset can be used to discard the
// current compressor or decompressor state and reinitialize them quickly
// by taking advantage of previously allocated memory.
func Example_reset() {
	proverbs := []string{
		"Don't communicate by sharing memory, share memory by communicating.\n",
		"Concurrency is not parallelism.\n",
		"The bigger the interface, the weaker the abstraction.\n",
		"Documentation is for users.\n",
	}

	var r strings.Reader
	var b bytes.Buffer
	buf := make([]byte, 32<<10)

	zw, err := flate.NewWriter(nil, flate.DefaultCompression)
	if err != nil {
		log.Fatal(err)
	}
	zr := flate.NewReader(nil)

	for _, s := range proverbs {
		r.Reset(s)
		b.Reset()

		// Reset the compressor and encode from some input stream.
		zw.Reset(&b)
		if _, err := io.CopyBuffer(zw, &r, buf); err != nil {
			log.Fatal(err)
		}
		if err := zw.Close(); err != nil {
			log.Fatal(err)
		}

		// Reset the decompressor and decode to some output stream.
		if err := zr.(flate.Resetter).Reset(&b, nil); err != nil {
			log.Fatal(err)
		}
		if _, err := io.CopyBuffer(os.Stdout, zr, buf); err != nil {
			log.Fatal(err)
		}
		if err := zr.Close(); err != nil {
			log.Fatal(err)
		}
	}

	// Output:
	// Don't communicate by sharing memory, share memory by communicating.
	// Concurrency is not parallelism.
	// The bigger the interface, the weaker the abstraction.
	// Documentation is for users.
}
Esempio n. 10
0
func tokenize(mode int, maxTokenSize int, str *strings.Reader, l int, token string, buffer []string) []string {
	ch, _, err := str.ReadRune()
	if err == io.EOF {
		if l > 0 {
			buffer = append(buffer, token)
		}
		return buffer
	} else {
		switch mode {
		case apostrophe:
			if isLetter(ch) {
				return tokenize(normal, maxTokenSize, str, l+2, token+"'"+string(ch), buffer)
			} else {
				buffer = append(buffer, token)
				return tokenize(start, maxTokenSize, str, 0, "", buffer)
			}
		case start:
			if isLetter(ch) {
				return tokenize(normal, maxTokenSize, str, l+1, token+string(ch), buffer)
			} else if isCJK(ch) {
				buffer = append(buffer, string(ch))
				return tokenize(start, maxTokenSize, str, 0, "", buffer)
			} else {
				return tokenize(start, maxTokenSize, str, 0, "", buffer)
			}
		case skip:
			if isLetter(ch) || ch == '\'' {
				return tokenize(skip, maxTokenSize, str, l+1, token, buffer)
			} else {
				return tokenize(start, maxTokenSize, str, 0, "", buffer)
			}
		default:
			if isLetter(ch) {
				if l+1 > maxTokenSize {
					log.Printf("Token is too long: “%v”, skipping it", token)
					return tokenize(skip, maxTokenSize, str, l+1, token, buffer)
				}
				return tokenize(normal, maxTokenSize, str, l+1, token+string(ch), buffer)
			} else if ch == '\'' && l > 0 {
				return tokenize(apostrophe, maxTokenSize, str, l, token, buffer)
			} else {
				if l > 0 {
					buffer = append(buffer, token)
				}
				return tokenize(start, maxTokenSize, str, 0, "", buffer)
			}
		}
	}
}
Esempio n. 11
0
// Insert a strings.Reader into the Trie
func (t *Trie) Insert(s *strings.Reader) {
	rune, _, err := s.ReadRune()
	if err != nil {
		// We have reached EOF
		t.leaf = true
		return
	}

	child := t.children[rune]
	if child == nil {
		child = NewTrie()
		t.children[rune] = child
	}
	child.Insert(s)
}
Esempio n. 12
0
// Internal function: adds items to the trie, reading runes from a strings.Reader.  It returns
// the leaf node at which the addition ends.
func (p *Trie) addRunes(r *strings.Reader) *Trie {
	rune, _, err := r.ReadRune()
	if err != nil {
		p.leaf = true
		return p
	}

	n := p.children[rune]
	if n == nil {
		n = NewTrie()
		p.children[rune] = n
	}

	// recurse to store sub-runes below the new node
	return n.addRunes(r)
}
Esempio n. 13
0
// Internal string removal function.  Returns true if this node is empty following the removal.
func (p *Trie) removeRunes(r *strings.Reader) bool {
	rune, _, err := r.ReadRune()
	if err != nil {
		// remove value, remove leaf flag
		p.value = nil
		p.leaf = false
		return len(p.children) == 0
	}

	child, ok := p.children[rune]
	if ok && child.removeRunes(r) {
		// the child is now empty following the removal, so prune it
		p.children[rune] = nil, false
	}

	return len(p.children) == 0
}
Esempio n. 14
0
// Internal string inclusion function.
func (p *Trie) includes(r *strings.Reader) *Trie {
	rune, _, err := r.ReadRune()
	if err != nil {
		if p.leaf {
			return p
		}
		return nil
	}

	child, ok := p.children[rune]
	if !ok {
		return nil // no node for this rune was in the trie
	}

	// recurse down to the next node with the remainder of the string
	return child.includes(r)
}
Esempio n. 15
0
func (n *Node) MatchReader(reader *strings.Reader) bool {
	if reader.Len() == 0 && n.Terminal {
		return true
	}

	r, _, err := reader.ReadRune()

	if err != nil {
		fmt.Println("Bad rune")
	}

	next, ok := n.Next(r)

	if !ok {
		return false
	}

	return next.MatchReader(reader)
}
Esempio n. 16
0
func (t *Textview) uiDraw() {
	t.ui.beginDraw()
	defer t.ui.endDraw()

	var reader *strings.Reader
	line := len(t.parsedLines) - 1 - t.CurrentLine
	if line < 0 {
		line = 0
	}
	totalLines := len(t.parsedLines)
	if totalLines == 0 {
		totalLines = 1
	}
	currentScrollLine := t.y1 - 1 - int((float32(t.CurrentLine)/float32(totalLines))*float32(t.y1-t.y0))
	for y := t.y1 - 1; y >= t.y0; y-- {
		if t.parsedLines != nil && line >= 0 {
			reader = strings.NewReader(t.parsedLines[line])
		} else {
			reader = nil
		}
		for x := t.x0; x < t.x1; x++ {
			var chr rune = ' '
			if x == t.x1-1 { // scrollbar
				if y == currentScrollLine {
					chr = '█'
				} else {
					chr = '░'
				}
			} else if x < t.x1-3 {
				if reader != nil {
					if ch, _, err := reader.ReadRune(); err == nil {
						chr = ch
					}
				}
			}
			termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
		}
		line--
	}
}
Esempio n. 17
0
// field is of form: key="value"
func lexField(r *strings.Reader) (string, string, error) {
	key := make([]byte, 0, 5)
	val := make([]byte, 0, 32)

	// read the key
	for {
		ch, _ := r.ReadByte()
		if ch == '=' {
			break
		}
		if ch < 'a' || ch > 'z' { // fail if not ASCII lowercase letter
			return "", "", AuthFormatError{"header", "cannot parse header field"}
		}
		key = append(key, ch)
	}
	if ch, _ := r.ReadByte(); ch != '"' {
		return "", "", AuthFormatError{string(key), "cannot parse value"}
	}
	// read the value
	for {
		ch, _ := r.ReadByte()
		if ch == '"' {
			break
		}
		// character class is ASCII printable [\x20-\x7E] without \ and "
		if ch < 0x20 || ch > 0x7E || ch == '\\' {
			return "", "", AuthFormatError{string(key), "cannot parse value"}
		}
		val = append(val, ch)
	}

	return string(key), string(val), nil
}
Esempio n. 18
0
// Looks ahead to see if the next character completes the entry. Does not read
// any characters from the reader.
func entryCompleted(r *strings.Reader) bool {
	if r.Len() > 0 {
		b, _ := r.ReadByte()
		r.UnreadByte()
		return b == lineSeparator || b == entrySeparator
	}
	return true
}
Esempio n. 19
0
func collectStringLiteral(r *strings.Reader) StringLiteral {
	// pick up the opening quotation mark
	for ch, _, err := r.ReadRune(); err == nil && ch != '"'; ch, _, err = r.ReadRune() {

	}

	var buffer bytes.Buffer

	// collect till we get the closing quotation mark
	for ch, _, err := r.ReadRune(); err == nil && ch != '"'; ch, _, err = r.ReadRune() {
		buffer.WriteRune(ch)
	}

	return StringLiteral{buffer.String()}
}
Esempio n. 20
0
// Attempts to consume the wanted character from the reader. Returns true if
// character was read successfully, false otherwise. If the character could
// not be read, then no characters are consumed from the reader.
func consume(r *strings.Reader, wanted byte) bool {
	if r.Len() > 0 {
		b, _ := r.ReadByte()
		if b == wanted {
			return true
		}
		r.UnreadByte()
	}
	return false
}
Esempio n. 21
0
func atoi_(reader *strings.Reader) (int, int) {
	n := 0
	count := 0
	for reader.Len() > 0 {
		ch, _, _ := reader.ReadRune()
		index := strings.IndexRune("0123456789", ch)
		if index >= 0 {
			n = n*10 + index
			count++
		} else {
			reader.UnreadRune()
			break
		}
	}
	return n, count
}
Esempio n. 22
0
func scanToken(rd *strings.Reader) (token string, err error) {
	r, _, err := rd.ReadRune()
	if err != nil {
		return "", err
	}
	if isSplitRune(r) {
		return string(r), nil
	}

	var tok []rune
	for {
		tok = append(tok, r)
		r, _, err = rd.ReadRune()
		if err != nil {
			return "", err
		}
		if isSplitRune(r) {
			rd.UnreadRune()
			return string(tok), nil
		}
	}
}
Esempio n. 23
0
func (c *Client) merchantUploadImageFromStringsReader(filename string, reader *strings.Reader) (imageURL string, err error) {
	originalOffset, err := reader.Seek(0, 1)
	if err != nil {
		return
	}

	FormDataFileName := escapeQuotes(filename)
	ContentLength := int64(multipart_constPartLen + len(FormDataFileName) + reader.Len())

	hasRetry := false
RETRY:
	token, err := c.Token()
	if err != nil {
		return
	}
	url_ := merchantUploadImageURL(token, filename)

	if hasRetry {
		if _, err = reader.Seek(originalOffset, 0); err != nil {
			return
		}
	}
	mr := io.MultiReader(
		strings.NewReader(multipart_formDataFront),
		strings.NewReader(FormDataFileName),
		strings.NewReader(multipart_formDataMiddle),
		reader,
		strings.NewReader(multipart_formDataEnd),
	)

	httpReq, err := http.NewRequest("POST", url_, mr)
	if err != nil {
		return
	}
	httpReq.Header.Set("Content-Type", multipart_ContentType)
	httpReq.ContentLength = ContentLength

	httpResp, err := c.httpClient.Do(httpReq)
	if err != nil {
		return
	}
	defer httpResp.Body.Close()

	if httpResp.StatusCode != http.StatusOK {
		err = fmt.Errorf("http.Status: %s", httpResp.Status)
		return
	}

	var result struct {
		Error
		ImageURL string `json:"image_url"`
	}
	if err = json.NewDecoder(httpResp.Body).Decode(&result); err != nil {
		return
	}

	switch result.ErrCode {
	case errCodeOK:
		imageURL = result.ImageURL
		return

	case errCodeTimeout:
		if !hasRetry {
			hasRetry = true
			timeoutRetryWait()
			goto RETRY
		}
		fallthrough

	default:
		err = &result.Error
		return
	}
}
Esempio n. 24
0
// Tear apart the Authorization header value.
// PAY ATTENTION: this is large and complicated relative to other ones I've seen
// based on Split() using ',', ' ', and '=' in various orders. It is also probably
// correct even if the realm contains a '=', ' ', or '"' character, or if the
// sender uses HT, CR, or LF in their whitespace.
//
// The map that comes back looks like { "qop": "auth", "ns":"00000001", etc... }
func parseAuthorization(auth *strings.Reader) (map[string]string, error) {
	parts := map[string]string{}

	skipLWS := func(r *strings.Reader) error {
		for {
			ch, err := r.ReadByte()
			if err == io.EOF {
				return nil
			}
			if err != nil {
				return err
			}
			if ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' {
				// its white, we skip it and stay in loop
			} else {
				if err := r.UnreadByte(); err != nil {
					return err
				}
				break
			}
		}
		return nil
	}

	readName := func(r *strings.Reader) (string, error) {
		name := []byte{}

		for {
			ch, err := r.ReadByte()
			if err == io.EOF {
				break
			}
			if err != nil {
				return "", err
			}

			if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-' {
				name = append(name, ch)
			} else {
				if err := r.UnreadByte(); err != nil {
					return "", err
				}
				break
			}
		}
		if len(name) == 0 {
			return "", fmt.Errorf("expected name, got didn't get one")
		}
		return string(name), nil
	}

	readValue := func(r *strings.Reader) (string, error) {
		ch, err := r.ReadByte()
		if err != nil {
			return "", err
		}

		if ch == '"' {
			v := []byte{}
			for {
				ch, err := r.ReadByte()
				if err != nil {
					return "", fmt.Errorf("premature end of value: %s", err.Error())
				}
				if ch == '\\' {
					ch2, err := r.ReadByte()
					if err != nil {
						return "", fmt.Errorf("premature end of value: %s", err.Error())
					}
					v = append(v, ch2)
				} else if ch == '"' {
					break
				} else {
					v = append(v, ch)
				}
			}
			return string(v), nil
		} else {
			r.UnreadByte()
			return readName(r) // handles unquoted values, like true/false in the "stale" paramter
		}
	}

	for {
		name, err := readName(auth)
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, err
		}

		if err := skipLWS(auth); err != nil {
			return nil, err
		}

		eq, err := auth.ReadByte()
		if err == io.EOF || eq != '=' {
			return nil, fmt.Errorf("Malformed %s parameter, no equals", name)
		}

		if err := skipLWS(auth); err != nil {
			return nil, err
		}

		val, err := readValue(auth)
		if err != nil {
			return nil, err
		}

		parts[name] = val

		comma, err := auth.ReadByte()
		if err == io.EOF {
			break // our exit
		}
		if err != nil {
			return nil, err
		}
		if comma != ',' {
			return nil, fmt.Errorf("expected comma, got %v", comma)
		}

		if err := skipLWS(auth); err != nil {
			if err == io.EOF {
				break // our exit, finding an EOF after a value and some whitespace
			}
			return nil, err
		}
	}

	if testing.Verbose() {
		log.Printf("auth header = %#v", parts)
	}

	return parts, nil
}
Esempio n. 25
0
File: life3.go Progetto: josejc/life
// initw read a file .LIF and configure the world with it
func initw(f *os.File) bool {
	var r *strings.Reader
	var b byte
	var x, y, oldy int

	input := bufio.NewScanner(f)
	input.Scan()
	if input.Text() != "#Life 1.05" {
		fmt.Fprintf(os.Stderr, "ERROR: The file for initialization the world is not a valid .LIF format\n")
		return false
	}
header:
	// Read header of .LIF
	for input.Scan() {
		r = strings.NewReader(input.Text())
		b, _ = r.ReadByte()
		if b != '#' {
			fmt.Println(input.Text())
		} else {
			b, _ = r.ReadByte()
			switch b {
			case 'D':
				{
					fmt.Println("Description")
				}
			case 'N':
				{
					fmt.Println("Rules Conway R 23/3")
				}
			case 'R':
				{
					fmt.Fprintf(os.Stderr, "ERROR: 'R' option not implemented\n")
					return false
				}
			case 'P':
				{
					s := strings.Split(input.Text(), " ")
					x, _ = strconv.Atoi(s[1])
					y, _ = strconv.Atoi(s[2])
					x += (M / 2)
					y += (N / 2)
					oldy = y
					break header // Exit loop, now only blocks of position and cells
				}
			default:
				{
					fmt.Fprintf(os.Stderr, "ERROR: Option in header not implemented\n")
					return false
				}
			}
		}
	}
	var p Point
	m := map[Point]int{}
	// Read patterns and positions
	for input.Scan() {
		r = strings.NewReader(input.Text())
		b, _ = r.ReadByte()
		if b == '#' {
			b, _ = r.ReadByte()
			if b == 'P' {
				s := strings.Split(input.Text(), " ")
				x, _ = strconv.Atoi(s[1])
				y, _ = strconv.Atoi(s[2])
				x += (M / 2)
				y += (N / 2)
				oldy = y
			} else {
				fmt.Fprintf(os.Stderr, "ERROR: Expected Position or blocks not config parameters\n")
				return false
			}
		} else {
			p.x = x
			for cells := int(r.Size()); cells > 0; cells-- {
				p.y = y
				switch b {
				case '.':
					{
						//m[p] = 0
					}
				case '*':
					{
						m[p] = 1
					}
				default:
					{
						fmt.Fprintf(os.Stderr, "ERROR: Character not valid, only '.' or '*'\n")
						return false
					}
				}
				b, _ = r.ReadByte()
				y++
			}
		}
		x++
		y = oldy
	}
	w.Matrix[0] = m
	return true
	// NOTE: ignoring potential errors from input.Err()
}