Overview
Comment: | Integrate ioutil.ReadByte from the bpe branch into trunk. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
00c4e0e4487b7a17ffa2a0db8312c7c2 |
User & Date: | spaskalev on 2014-12-28 17:19:36 |
Other Links: | manifest | tags |
Context
2014-12-28
| ||
18:06 | [mtf] Removed :to indices from copy(..) destinations. check-in: c07658474d user: spaskalev tags: trunk | |
17:19 | Integrate ioutil.ReadByte from the bpe branch into trunk. check-in: 00c4e0e448 user: spaskalev tags: trunk | |
14:14 | Added 0dev.org/ioutil.ReadByte() function and a test for it. CC at 100% check-in: 879630c89c user: spaskalev tags: bpe | |
2014-12-26
| ||
21:35 | Added 0dev.org/types, providing aliases that implement sort.Interface for [u]int{8|16|32|64} check-in: 129d90b4a8 user: spaskalev tags: trunk | |
Changes
Modified src/0dev.org/ioutil/ioutil.go from [c11fdb5a53] to [83fc64874b].
16 16 // An function alias type that implements io.Reader. 17 17 type ReaderFunc func([]byte) (int, error) 18 18 19 19 // Delegates the call to the WriterFunc while implementing io.Reader. 20 20 func (r ReaderFunc) Read(b []byte) (int, error) { 21 21 return r(b) 22 22 } 23 + 24 +// Reads a single byte from the provided io.Reader 25 +func ReadByte(reader io.Reader) (byte, error) { 26 + var ( 27 + arr [1]byte 28 + err error 29 + ) 30 + _, err = reader.Read(arr[:]) 31 + return arr[0], err 32 +} 23 33 24 34 // Returns a writer that delegates calls to Write(...) while ensuring 25 35 // that it is never called with less bytes than the specified amount. 26 36 // 27 37 // Calls with fewer bytes are buffered while a call with a nil slice 28 38 // causes the buffer to be flushed to the underlying writer. 29 39 func SizedWriter(writer io.Writer, size int) io.Writer {
Modified src/0dev.org/ioutil/ioutil_test.go from [91aaac4713] to [514a112f00].
43 43 // Diff the result against the initial input 44 44 delta := diff.Diff(diff.D{len(input), len(output), 45 45 func(i, j int) bool { return input[i] == output[j] }}) 46 46 if len(delta.Added) > 0 || len(delta.Removed) > 0 { 47 47 t.Error("Differences detected ", delta) 48 48 } 49 49 } 50 + 51 +func TestReadByte(t *testing.T) { 52 + var ( 53 + input []byte = []byte{255} 54 + reader *bytes.Reader = bytes.NewReader(input) 55 + ) 56 + 57 + result, err := ReadByte(reader) 58 + if result != input[0] { 59 + t.Error("Unexpected read result from ReadByte", result) 60 + } 61 + if err != nil { 62 + t.Error("Unexpected error from ReadByte", err) 63 + } 64 + 65 + result, err = ReadByte(reader) 66 + if err != io.EOF { 67 + t.Error("Unexpected nil error from ReadByte, read value:", result) 68 + } 69 +} 50 70 51 71 func TestSizedWriter(t *testing.T) { 52 72 var ( 53 73 buffer bytes.Buffer 54 74 writer io.Writer = SizedWriter(&buffer, 4) 55 75 ) 56 76