...

Source file src/github.com/launchdarkly/eventsource/normalise.go

Documentation: github.com/launchdarkly/eventsource

     1  package eventsource
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // A reader which normalises line endings
     8  // "/r" and "/r/n" are converted to "/n"
     9  type normaliser struct {
    10  	r        io.Reader
    11  	lastChar byte
    12  }
    13  
    14  func newNormaliser(r io.Reader) *normaliser {
    15  	return &normaliser{r: r}
    16  }
    17  
    18  func (norm *normaliser) Read(p []byte) (n int, err error) {
    19  	n, err = norm.r.Read(p)
    20  	for i := 0; i < n; i++ {
    21  		switch {
    22  		case p[i] == '\n' && norm.lastChar == '\r':
    23  			copy(p[i:n], p[i+1:])
    24  			norm.lastChar = p[i]
    25  			n--
    26  			i--
    27  		case p[i] == '\r':
    28  			norm.lastChar = p[i]
    29  			p[i] = '\n'
    30  		default:
    31  			norm.lastChar = p[i]
    32  		}
    33  	}
    34  	return
    35  }
    36  

View as plain text