Merge pull request #3 from ajhager/master

Fix Writers returning n-1 bytes written.
pull/4/head
jimt 2012-07-14 11:20:48 -07:00
commit fea0373680
2 changed files with 74 additions and 70 deletions

View File

@ -1,35 +1,37 @@
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at: // license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/ // http://creativecommons.org/publicdomain/zero/1.0/
package main package main
import ( import (
"fmt" "fmt"
"io" "io"
) )
var newline = []byte{'\n'} var newline = []byte{'\n'}
type ByteWriter struct { type ByteWriter struct {
io.Writer io.Writer
c int c int
} }
func (w *ByteWriter) Write(p []byte) (n int, err error) { func (w *ByteWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 { if len(p) == 0 {
return return
} }
for n = range p { for n = range p {
if w.c%12 == 0 { if w.c%12 == 0 {
w.Writer.Write(newline) w.Writer.Write(newline)
w.c = 0 w.c = 0
} }
fmt.Fprintf(w.Writer, "0x%02x,", p[n]) fmt.Fprintf(w.Writer, "0x%02x,", p[n])
w.c++ w.c++
} }
return n++
}
return
}

View File

@ -1,35 +1,37 @@
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at: // license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/ // http://creativecommons.org/publicdomain/zero/1.0/
package main package main
import ( import (
"fmt" "fmt"
"io" "io"
) )
var line = []byte("\"+\n\"") var line = []byte("\"+\n\"")
type StringWriter struct { type StringWriter struct {
io.Writer io.Writer
c int c int
} }
func (w *StringWriter) Write(p []byte) (n int, err error) { func (w *StringWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 { if len(p) == 0 {
return return
} }
for n = range p { for n = range p {
if w.c%16 == 0 { if w.c%16 == 0 {
w.Writer.Write(line) w.Writer.Write(line)
w.c = 0 w.c = 0
} }
fmt.Fprintf(w.Writer, "\\x%02x", p[n]) fmt.Fprintf(w.Writer, "\\x%02x", p[n])
w.c++ w.c++
} }
return n++
}
return
}