-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: strings: add (*Reader).String() #36290
Comments
I find it counterintuitive that calling On the other hand, the string can be invalid if it begins at any arbitrary index, so if returning a valid string must be guaranteed, the closest behavior I can think of is func (r *Reader) String() string {
return ToValidUTF8(r.s[r.i:], "")
} |
@smasher164 I don't think there is the need to worry about returning an invalid string, as there is precedence in (*bytes.Buffer).String(). To be consistent with (*bytes.Buffer).String() however, (*strings.Reader).String() could return the unread portion of the string. To get the whole string, use Seek(). |
@pierrec Sorry, which
Should it advance pointer then? So that second call to |
@RodionGork Well it can't be I agree with @smasher164 in that it should return the underlying string, however not all strings are utf8 strings (blog), so I disagree that it should be wrapped in |
@deanveloper thanks, I believe you are right (just had a momentary suspicion it is about extending interface and implementing method everywhere). But then, if the Reader should resemble bytes.Buffer more, then besides
yep, it would be confusing to receive empty string when reader is not at end (or error which is not part of behavior in bytes.Buffer) |
@RodionGork this proposal is not about making it resemble bytes.Buffer. It is about being able to easily recover the string that the reader is based on. I have been running into this a few times and think it would be a straight forward addition. |
I see your point, but the package header specifically states, "package strings implements simple functions to manipulate UTF-8 encoded strings." That being said, since strings.Builder allows one to construct a malformed string using the package, I don't see why strings.Reader shouldn't be allowed to, so I'm open to simply returning @pierrec I am still trying to understand the use case of this proposal. Whenever I use |
That's true, I can also see it going either way. Just seems weird to me that |
strings.Reader and bytes.Reader were meant to be the same API, just with different input types in the constructor. They're all about the streaming calls, not the bulk data of, say, bytes.Buffer. There's also ambiguity about which string should be returned - the remaining input or the original. So adding the API might confuse half the users anyway. And for bytes.Reader it would have to make a copy. As @smasher164 says, usually you use strings.Reader because you already have the string and want to pass it to a function as an io.Reader (or one of the other interfaces). What is the use case where you are passed a *strings.Reader and want to get the string back? How does that come up? |
I had to convert a type which was initially an io.ReadCloser to an io.ReadWriteCloser. That type was implemented using, you guessed it, a strings.Reader, and the hack I mention in the proposal, which seemed bad. |
For the record, you can retrieve the remainder of the string in a type returnedString string
type fetchString struct{}
func (fetchString) WriteString(s string) (int, error) {
panic(returnedString(s))
}
func (fetchString) Write([]byte) (int, error) {
panic("can't happen")
}
func FetchString(sr *strings.Reader) (ret string) {
defer func() {
if s, ok := recover().(returnedString); ok {
ret = string(s)
} else {
panic(recover())
}
}()
sr.WriteTo(fetchString{})
panic("can't happen")
} |
@ianlancetaylor yikes! |
|
Yep. I meant this:
as looking at the strings.Reader.WriteTo method code, it returns before calling WriteString if there is nothing to read. |
Ahh I see that now - my bad. Misinterpreted your initial comment and I thought you meant inside the |
@pierrec Yes, I think you're right. Oh well. |
Although it is possible to retrieve the string that a Reader contains, it would be nice to avoid this and have direct access to the string that a Reader contains.
Therefore I propose the following:
So the code required to get access to a Reader's content becomes:
Before:
After:
The text was updated successfully, but these errors were encountered: