Strip Leading Whitespace from an io.Reader

367 views
Skip to first unread message

Jordan Wright

unread,
Feb 29, 2016, 1:56:21 PM2/29/16
to golang-nuts
I have a use case where I'm taking in an HTTP request with the raw content I'm looking for in ```r.Body```.

My goal is to then send this io.ReadCloser to another function that expects an io.Reader. The problem is that I need to first strip out any leading whitespace from the content in the HTTP request body.

Is there an easy way to strip leading whitespace from an io.Reader? Will I need to create another custom reader, or should I just convert to a string, strip the whitespace, and convert back to a reader?

Any help is much appreciated!

Konstantin Khomoutov

unread,
Feb 29, 2016, 2:33:35 PM2/29/16
to Jordan Wright, golang-nuts
That's a ten lines-worth custom reader [1]:

type SkipWsReader struct {
r io.Reader
pass bool
}

func NewSkipWsReader(r io.Reader) *SkipWsReader {
return &SkipWsReader{r: r}
}

func (swr *SkipWsReader) Read(b []byte) (n int, err error) {
for {
n, err = swr.r.Read(b)
if swr.pass || err != nil {
return
}
for i, c := range b[0:n] {
switch c {
case '\t', '\v', '\n', '\x20':
continue
default:
n = copy(b, b[i:n])
swr.pass = true
return
}
}
}
}

The downside of this code is that if the underlying reader fails while
reading leading whitespace, that whitespase will end up in the
client-supplied slice, but that would require a bit more advanced logic
which I don't have time to mess with at the moment ;-)

1. http://play.golang.org/p/n1-YcMkHr1

pierre...@gmail.com

unread,
Feb 29, 2016, 2:42:40 PM2/29/16
to golang-nuts
Simply wrap the reader with something like this:

John McKown

unread,
Feb 29, 2016, 2:57:17 PM2/29/16
to pierre...@gmail.com, golang-nuts
On Mon, Feb 29, 2016 at 1:42 PM, <pierre...@gmail.com> wrote:
Simply wrap the reader with something like this:


​Nice! I've squirrel'ed that away for later.​


Le lundi 29 février 2016 19:56:21 UTC+1, Jordan Wright a écrit :
I have a use case where I'm taking in an HTTP request with the raw content I'm looking for in ```r.Body```.

My goal is to then send this io.ReadCloser to another function that expects an io.Reader. The problem is that I need to first strip out any leading whitespace from the content in the HTTP request body.

Is there an easy way to strip leading whitespace from an io.Reader? Will I need to create another custom reader, or should I just convert to a string, strip the whitespace, and convert back to a reader?

Any help is much appreciated!


--
The man has the intellect of a lobotomized turtle.

Maranatha! <><
John McKown

Jordan Wright

unread,
Feb 29, 2016, 6:43:06 PM2/29/16
to golang-nuts, pierre...@gmail.com
Thanks for the great replies, everyone! Much appreciated!

Matt Harden

unread,
Feb 29, 2016, 11:31:00 PM2/29/16
to Jordan Wright, golang-nuts, pierre...@gmail.com
You can also use a bufio.Reader and read in the whitespace before passing the (buffered) reader on:


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

roger peppe

unread,
Mar 1, 2016, 4:18:26 AM3/1/16
to Matt Harden, Jordan Wright, golang-nuts, pierre...@gmail.com
On 1 March 2016 at 04:30, Matt Harden <matt....@gmail.com> wrote:
> You can also use a bufio.Reader and read in the whitespace before passing
> the (buffered) reader on:
>
> https://play.golang.org/p/SKC1Fs-Hug

That won't work well if there's a unicode whitespace character that
straddles a 1024-byte boundary.
I'd suggest a simpler approach:

https://play.golang.org/p/KFGby-fwMo
Reply all
Reply to author
Forward
0 new messages