master
Jordan Orelli 11 years ago
parent 0f0d02a5e5
commit c1a55b99d2

@ -15,8 +15,8 @@ type statement struct {
func (s statement) String() string { func (s statement) String() string {
var buf bytes.Buffer var buf bytes.Buffer
s.each(func(n *node) error { s.each(func(n *node) error {
_, err := fmt.Fprint(&buf, n) _, err := fmt.Fprint(&buf, n)
return err return err
}) })
return buf.String() return buf.String()
} }
@ -24,20 +24,20 @@ func (s statement) String() string {
func (s *statement) each(fn func(*node) error) error { func (s *statement) each(fn func(*node) error) error {
curr := s.head curr := s.head
for curr != nil { for curr != nil {
err := fn(curr) err := fn(curr)
if err != nil { if err != nil {
return err return err
} }
curr = curr.next curr = curr.next
} }
return nil return nil
} }
func (s *statement) write(w io.Writer) error { func (s *statement) write(w io.Writer) error {
return s.each(func(n *node) error { return s.each(func(n *node) error {
_, err := w.Write(n.line) _, err := w.Write(n.line)
return err return err
}) })
} }
type node struct { type node struct {
@ -54,23 +54,23 @@ func (s *statement) add(line []byte) {
if s.head == nil { if s.head == nil {
s.head = n s.head = n
} else if s.head.next == nil { } else if s.head.next == nil {
s.head.next = n s.head.next = n
} }
if s.tail != nil { if s.tail != nil {
s.tail.next = n s.tail.next = n
} }
s.tail = n s.tail = n
} }
func whitespace(buf []byte) bool { func whitespace(buf []byte) bool {
for i, _ := range buf { for i, _ := range buf {
switch buf[i] { switch buf[i] {
case '\r', '\n': case '\r', '\n':
default: default:
return false return false
} }
} }
return true return true
} }
func split(r io.Reader, c chan statement) { func split(r io.Reader, c chan statement) {
@ -82,27 +82,27 @@ func split(r io.Reader, c chan statement) {
switch err { switch err {
case nil: case nil:
case io.EOF: case io.EOF:
if s != nil { if s != nil {
c <- *s c <- *s
} }
return return
default: default:
fmt.Printf("error on read: %v\n", err) fmt.Printf("error on read: %v\n", err)
return return
} }
if whitespace(line) { if whitespace(line) {
continue continue
} }
if line[len(line)-2] != '\r' { if line[len(line)-2] != '\r' {
fmt.Printf("bad line terminator") fmt.Printf("bad line terminator")
break break
} }
if line[0] == '*' { if line[0] == '*' {
if s != nil { if s != nil {
c <- *s c <- *s
} }
s = new(statement) s = new(statement)
s.add(line) s.add(line)
} else { } else {
if s == nil { if s == nil {
fmt.Println("ummm wut") fmt.Println("ummm wut")

Loading…
Cancel
Save