// Reads a single word from a file, assuming space + tab + EOL to be word boundaries func ReadWord(fin *bufio.Reader) (word string, err error) { var a int = 0 var ch byte var buf bytes.Buffer for { ch, err = fin.ReadByte() if err == io.EOF { break } if ch == 13 { continue } if (ch == ' ') || (ch == '\t') || (ch == '\n') { if a > 0 { if ch == '\n' { fin.UnreadByte() } break } if ch == '\n' { word = "</s>" return } else { continue } } buf.WriteByte(ch) a++ } if a >= MAX_STRING { // Truncate too long words buf.Truncate(MAX_STRING) } word = buf.String() return }
func readHexInt(r *bufio.Reader) (int, error) { n := 0 i := 0 var k int for { c, err := r.ReadByte() if err != nil { if err == io.EOF && i > 0 { return n, nil } return -1, err } k = hexbyte2int(c) if k < 0 { if i == 0 { return -1, fmt.Errorf("cannot read hex num from empty string") } r.UnreadByte() return n, nil } if i >= maxHexIntChars { return -1, fmt.Errorf("cannot read hex num with more than %d digits", maxHexIntChars) } n = (n << 4) | k i++ } }
// read reads the next value, ignoring whitespace. func read(r *bufio.Reader) []byte { var comment bool space(r) buf := make([]byte, 0, 16) loop: for { b, err := r.ReadByte() check(err) switch { case b == '#': comment = true case (comment && isNewline(b)) || (!comment && isSpace(b)): check(r.UnreadByte()) break loop } if !comment { buf = append(buf, b) } } if len(buf) == 0 { return read(r) } return buf }
func readHexInt(r *bufio.Reader) (int, error) { n := 0 i := 0 var k int for { c, err := r.ReadByte() if err != nil { if err == io.EOF && i > 0 { return n, nil } return -1, err } k = hexbyte2int(c) if k < 0 { if i == 0 { return -1, errEmptyHexNum } r.UnreadByte() return n, nil } if i >= maxHexIntChars { return -1, errTooLargeHexNum } n = (n << 4) | k i++ } }
func read_symbol(b *bufio.Reader) (goscm.SCMT, error) { ret := "" var c byte var err error for { c, err = b.ReadByte() if err != nil { break } if c == ')' { b.UnreadByte() break } if c == ';' { read_comment(b) continue } if is_whitespace(c) { break } ret = string(append([]byte(ret), c)) } return goscm.NewSymbol(ret), err }
func (p *protocol) ReadParam(r *bufio.Reader) ([]byte, bool, error) { data, err := r.ReadSlice('\r') if err != nil { return nil, false, p.sanitizeErr(err) } err = p.assumeByte(r, '\n') if err != nil { return nil, false, err } next, err := r.ReadByte() if err != nil { return nil, false, p.sanitizeErr(err) } hasNext := true if next == '\r' { hasNext = false err = p.assumeByte(r, '\n') if err != nil { return nil, false, p.sanitizeErr(err) } } else { r.UnreadByte() } return data, hasNext, nil }
// skipComments skips all comments (and whitespace) that may occur between PNM // header tokens. // // The singleSpace argument is used to scan comments between the header and the // raster data where only a single whitespace delimiter is allowed. This // prevents scanning the image data. func skipComments(r *bufio.Reader, singleSpace bool) (err error) { var c byte for { // Skip whitespace c, err = r.ReadByte() for unicode.IsSpace(rune(c)) { if c, err = r.ReadByte(); err != nil { return err } if singleSpace { break } } // If there are no more comments, unread the last byte and return. if c != '#' { r.UnreadByte() return nil } // A comment ends with a newline or carriage return. for c != '\n' && c != '\r' { if c, err = r.ReadByte(); err != nil { return } } } return }
func isEndOfString(br *bufio.Reader) (ended bool, text string, err error) { var b byte w := bytes.NewBufferString("") for { switch b, err = br.ReadByte(); { case err == io.EOF: ended = true err = nil return case err != nil: return case b == ',' || b == ']': br.UnreadByte() ended = true return case !unicode.IsSpace(rune(b)): ended = false text = w.String() err = nil br.UnreadByte() return } w.WriteByte(b) } return }
func Decode(input *bufio.Reader) (_ string, err error) { buf := bytes.NewBufferString("") for { b, err := input.ReadByte() if err != nil { break } if b != '=' { err = buf.WriteByte(b) } else { input.UnreadByte() charset, err := readCharset(input) if err != nil { return "", err } encoding, err := readEncoding(input) if err != nil { return "", err } (&Decoder{encoding, charset}).Decode(input, buf) } } if err == io.EOF { return buf.String(), nil } return buf.String(), err }
func peek(r *bufio.Reader) (c byte, e error) { c, e = r.ReadByte() if e != io.EOF { r.UnreadByte() } return }
func expect(r *bufio.Reader, c byte) bool { ReadWhitespace(r) res, err := r.ReadByte() if res != c { r.UnreadByte() } return res == c && err != io.EOF }
func genericExpect(br *bufio.Reader, what string, predicate func(byte) bool) error { switch b, err := br.ReadByte(); { case err != nil: return err case !predicate(b): br.UnreadByte() return NewErrExpect(br, what) } return nil }
func skip(rd *bufio.Reader) { var b byte = ' ' var err error for b == ' ' || b == '\t' || b == '\n' { b, err = rd.ReadByte() if err != nil { return } } rd.UnreadByte() }
// space reads whitespace from the given reader, until // the stream ends or a non-whitespace character is found. func space(r *bufio.Reader) { for { b, err := r.ReadByte() check(err) if !isSpace(b) { check(r.UnreadByte()) return } } }
//Peek and check if packet is EOF func peekEOF(br *bufio.Reader) (bool, os.Error) { b, err := br.ReadByte() if err != nil { return false, err } err = br.UnreadByte() if b == 0xfe { return true, err } return false, err }
func readSymbol(self *LIPS, in *bufio.Reader, char byte) (cell Cell, e os.Error) { buffer := bytes.NewBuffer(nil) buffer.WriteByte(char) for char, e = in.ReadByte(); e == nil && (charReadsSymbol(self, char) || charReadsNumber(self, char) || charReadsSign(self, char)); char, e = in.ReadByte() { buffer.WriteByte(char) } in.UnreadByte() cell = self.Symbol(buffer.String()) return }
func readSign(self *LIPS, in *bufio.Reader, char byte) (cell Cell, e os.Error) { sign := char if char, e = in.ReadByte(); e == nil { in.UnreadByte() if charReadsNumber(self, char) { cell, e = readNumber(self, in, sign) } else { cell, e = readSymbol(self, in, sign) } } return }
func readNumber(self *LIPS, in *bufio.Reader, char byte) (cell Cell, e os.Error) { buffer := bytes.NewBuffer(nil) buffer.WriteByte(char) for char, e = in.ReadByte(); e == nil && charReadsNumber(self, char); char, e = in.ReadByte() { buffer.WriteByte(char) } in.UnreadByte() if number, e := strconv.Atoi(buffer.String()); e == nil { cell = Number(number) } return }
// Read a single S-expression from buffered IO r, returning any error // encountered. May return io.EOF if at end of r; may return a valid // S-expression and io.EOF if the EOF was encountered at the end of // parsing. func Read(r *bufio.Reader) (s Sexp, err error) { c, err := r.ReadByte() if err != nil { return nil, err } switch c { case '{': enc, err := r.ReadBytes('}') acc := make([]byte, 0, len(enc)-1) for _, c := range enc[:len(enc)-1] { if bytes.IndexByte(whitespaceChar, c) == -1 { acc = append(acc, c) } } str := make([]byte, base64.StdEncoding.DecodedLen(len(acc))) n, err := base64.StdEncoding.Decode(str, acc) if err != nil { return nil, err } s, err = Read(bufio.NewReader(bytes.NewReader(str[:n]))) if err == nil || err == io.EOF { return s, nil } else { return nil, err } case '(': l := List{} // skip whitespace for { c, err := r.ReadByte() switch { case c == ')': return l, err case bytes.IndexByte(whitespaceChar, c) == -1: r.UnreadByte() element, err := Read(r) if err != nil { return nil, err } l = append(l, element) } if err != nil { return nil, err } } default: return readString(r, c) } if err != nil { return s, err } panic("Can't reach here") }
func checkSeparator(reader *bufio.Reader) error { err := reader.UnreadByte() if err != nil { return err } delim, err := reader.ReadByte() if err == nil && delim != '\n' { err = ErrSeparatorMissed } return err }
func spaces(br *bufio.Reader) error { for { switch b, err := br.ReadByte(); { case err != nil: return err case !unicode.IsSpace(rune(b)): br.UnreadByte() return nil } } return nil }
func doByteRead(r *bufio.Reader) { b, err := r.ReadByte() if err != nil { log.Fatalf("failed reading a byte: %s", err) } log.Printf("Read a byte: %x", b) log.Printf("Didn't mean to read that either, putting it back") err = r.UnreadByte() if err != nil { log.Fatalf("failed urneading a byte: %s", err) } }
func readList(self *LIPS, in *bufio.Reader, char byte) (head Cell, e os.Error) { var tail Cell var cell Cell var end byte switch char { case '(': end = ')' case '[': end = ']' case '{': end = '}' } head = Cons(nil, nil) tail = head for char, e = skipBlanks(self, in); e == nil; char, e = skipBlanks(self, in) { if char == end { break } if char == ')' || char == ']' || char == '}' { e = NewError("Error: unmatched parentheses.") break } if char == '.' { if cell, e = self.ReadExpression(in); e == nil { Rplacd(tail, cell) } } else { in.UnreadByte() if cell, e = self.ReadExpression(in); e == nil { tail = Rplacd(tail, Cons(cell, nil)) } else if e == os.EOF { e = NewError("Error: EOF in list.") } } } head = Cdr(head) if _, is := Car(head).(*TypeSymbol); is { if expr := Assq(Car(head), Cdr(self.special)); expr != nil { head, e = self.Apply(Cdr(expr), Cdr(head), self.Globals) } if head == nil { return } } if head == nil { head = self.nothing } return }
func isDelim(r *bufio.Reader, delim byte) (bool, error) { c, err := r.ReadByte() if err != nil { return false, err } if c == delim { return true, nil } err = r.UnreadByte() if err != nil { return false, err } return false, nil }
func skipSpace(r *bufio.Reader) int { n := 0 for { c, err := r.ReadByte() if err != nil { // Bufio will keep err until next read. break } if c != ' ' && c != '\t' { r.UnreadByte() break } n++ } return n }
/** * Read row data packet */ func (pkt *packetRowData) read(reader *bufio.Reader) (err os.Error) { // Read (check if exists) null bit map c, err := reader.ReadByte() if err != nil { return } if c >= 0x40 && c <= 0x7f { pkt.nullBitMap = c } else { reader.UnreadByte() } // Allocate memory pkt.values = make([]interface{}, len(pkt.fields)) // Read data for each field for i, field := range pkt.fields { str, _, err := pkt.readlengthCodedString(reader) if err != nil { return } switch field.Type { // Strings and everythign else, keep as string default: pkt.values[i] = str // Tiny, small + med int convert into (u)int case FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG: if field.Flags.Unsigned { pkt.values[i], _ = strconv.Atoui(str) } else { pkt.values[i], _ = strconv.Atoi(str) } // Big int convert to (u)int64 case FIELD_TYPE_LONGLONG: if field.Flags.Unsigned { pkt.values[i], _ = strconv.Atoui64(str) } else { pkt.values[i], _ = strconv.Atoi64(str) } // Floats case FIELD_TYPE_FLOAT: pkt.values[i], _ = strconv.Atof32(str) // Double case FIELD_TYPE_DOUBLE: pkt.values[i], _ = strconv.Atof64(str) } } return }
func isEndOfSlice(br *bufio.Reader) (bool, error) { var ( b byte err error ) if err = spaces(br); err != nil { return false, err } switch b, err = br.ReadByte(); { case err != nil: return false, err case b == ']': return true, nil } br.UnreadByte() return false, nil }
// Hack to wait until read is available on a bufio.Reader func waitForRead(bufr *bufio.Reader) <-chan os.Error { ch := make(chan os.Error) go func() { _, err := bufr.ReadByte() if err != nil { ch <- err return } err = bufr.UnreadByte() if err != nil { ch <- err return } ch <- nil }() return ch }
func readUnquote(self *LIPS, in *bufio.Reader, char byte) (cell Cell, e os.Error) { if char, e = in.ReadByte(); e == nil { in.UnreadByte() cell, e = self.ReadExpression(in) switch e { case os.EOF: e = NewError("Error: EOF in quasiquoted literal.") case nil: cell = Cons(cell, nil) if char == '@' { cell = Cons(self.Symbol("unquote-splicing"), cell) } else { cell = Cons(self.Symbol("unquote"), cell) } } } return }
func (s *Scanner) lexElement(b *bufio.Reader) bool { // Spec says size will always fit into 20 bytes or less const maxSizeLength = 20 sizeBuf := make([]byte, maxSizeLength) i := 0 for i = range sizeBuf { c, err := b.ReadByte() if err != nil { return s.setError(err) } if !isDigit(c) { b.UnreadByte() break } sizeBuf[i] = c } // Turn the size into an int size, err := strconv.ParseInt(string(sizeBuf[:i]), 10, 64) if err != nil { return s.setError(ErrUnreadableSize) } c, err := b.ReadByte() if err != nil { return s.setError(err) } switch c { case '<': return s.lexData(b, size) case '[': return s.lexArrayOrDict(b, size, true) case '{': return s.lexArrayOrDict(b, size, false) } return s.setError(ErrUnexpectedChar) }