Christopher
Christopher

Reputation: 421

bufio.Reader ReadRune - size 0 (return value) possible?

Can ReadRune really ever have a size 0 return value when an error is nil?

I'm curious because I've seen some online examples with the following code:

//assuming input = *bufio.Reader
r, size, err := input.ReadRune()
if size == 0 && err == nil {
    return 0, nil
} else if err != nil {
    return 0, err
}
return r, nil

However, according to the go documentation:

If the encoded rune is invalid, it consumes one byte and returns unicode.ReplacementChar (U+FFFD) with a size of 1.

So in what case would a size 0 rune be returned when the error is nil?

Upvotes: 0

Views: 284

Answers (1)

Thundercat
Thundercat

Reputation: 121009

There is no case where the bufio.Reader ReadRune method returns size == 0 and err == nil.

The method reads a rune or a single byte in the case where a valid rune cannot be read. In both cases, the size returned is greater than zero.

Upvotes: 1

Related Questions