... Sorry for the publishing issues..

// BytesByLine emits chunks of []byte splitted by line (\n).
func BytesByLine() *Transform {
  var buf []byte
  var EOL = []byte("\n")

  byLine := func () [][]byte {
    ret := make([][]byte, 0)
    lines := bytes.Split(buf, EOL)
    isEOL := len(buf)>0 && buf[len(buf)-1:][0]==EOL[0]
    for i, line := range lines {
      if i==len(lines)-1 {
        if isEOL && len(line)>0 {
          buf=buf[:0]
          ret = append(ret, line)
        } else {
          buf = line
        }
      } else {
        ret = append(ret, line)
      }
    }
    return ret
  }

  var TT TransformFunc
  TT = func (s Stream, data interface{}) {
    if val, ok := data.([]byte); ok {
      buf = append(buf, val...)
      for _, line := range byLine() {
        s.Push(line)
      }
    } else {
      s.Emit(errors.New("Value must be of type []byte"))
    }
  }

  var FF FlushFunc
  FF = func (s Stream) {
    for _, line := range byLine() {
      s.Push(line)
    }
    if len(buf)>0 {
      s.Push(buf)
    }
  }

  return NewTransformFlush(TT, FF)
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to