Skip to content

Commit

Permalink
added an usage section to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
philiplb committed Sep 27, 2020
1 parent 96e3dd0 commit 8dd52da
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,42 @@ Just `go get` it:
go get github.com/philiplb/peekabuf
```

## Usage

First, import it:

```Go
import "github.com/philiplb/peekabuf"
```

Then, create a new RuneReader with a `io.Reader` as source, in this case, simply a `bytes.BufferString` is used:

```Go
r := peekabuf.NewRuneReader(bytes.NewBufferString("pab"))
```

Now, you can read, unread, peek on it. Note the returned `EOF` rune if the end of the buffer is reached:

```Go
// read a bit in the buffer
read := r.Read() // read == 'p'
read = r.Read() // read == 'a'

// unread the last rune
r.Unread()
read = r.Read() // read == 'a'

// peek a bit in the buffer
r.Unread() // unread the last rune so we have a bit to peek
peeked, err := r.Peek(2) // peeked == {'a', 'b'}
peeked, err := r.Peek(4) // peeked == {'a', 'b', peekabuf.EOF}, note that four runes were requested

// read the rest of the buffer
read = r.Read() // read == 'a'
read = r.Read() // read == 'b'
read = r.Read() // read == peekabuf.EOF
read = r.Read() // read == peekabuf.EOF
```

## License
[MIT](https://choosealicense.com/licenses/mit/)

0 comments on commit 8dd52da

Please sign in to comment.