digitalmars.D - Why does std.string.munch take a string ref?
- Sean Kelly (3/3) Jun 11 2014 It's the only function in std.string that takes a string by ref
- w0rp (2/5) Jun 11 2014 munch modifies the string you give it.
- Sean Kelly (4/9) Jun 11 2014 Yes, but why does it do this when it could leave the string as-is
- monarch_dodra (9/19) Jun 11 2014 I think it's because it "returns" both the munched data, and the
- Sean Kelly (5/13) Jun 11 2014 Right. The problem I ran into was that because munch takes a
- Jonathan M Davis via Digitalmars-d (9/12) Jun 11 2014 It looks like it's trying to act like std.conv.parse, though that behavi...
It's the only function in std.string that takes a string by ref instead of by value, and this screws up call chaining. What's the reason for this?
Jun 11 2014
On Wednesday, 11 June 2014 at 20:27:41 UTC, Sean Kelly wrote:It's the only function in std.string that takes a string by ref instead of by value, and this screws up call chaining. What's the reason for this?munch modifies the string you give it.
Jun 11 2014
On Wednesday, 11 June 2014 at 21:07:18 UTC, w0rp wrote:On Wednesday, 11 June 2014 at 20:27:41 UTC, Sean Kelly wrote:Yes, but why does it do this when it could leave the string as-is and return a modified slice instead, like all the other routines in std.string?It's the only function in std.string that takes a string by ref instead of by value, and this screws up call chaining. What's the reason for this?munch modifies the string you give it.
Jun 11 2014
On Wednesday, 11 June 2014 at 21:10:42 UTC, Sean Kelly wrote:On Wednesday, 11 June 2014 at 21:07:18 UTC, w0rp wrote:I think it's because it "returns" both the munched data, and the modified string: string s = "123abc"; string t = munch(s, "0123456789"); assert(t == "123" && s == "abc"); But it would indeed be more natural to simply return the updated "s". It's what things like "find" or "stripLeft" do anyways, and that works fine.On Wednesday, 11 June 2014 at 20:27:41 UTC, Sean Kelly wrote:Yes, but why does it do this when it could leave the string as-is and return a modified slice instead, like all the other routines in std.string?It's the only function in std.string that takes a string by ref instead of by value, and this screws up call chaining. What's the reason for this?munch modifies the string you give it.
Jun 11 2014
On Wednesday, 11 June 2014 at 21:18:29 UTC, monarch_dodra wrote:I think it's because it "returns" both the munched data, and the modified string: string s = "123abc"; string t = munch(s, "0123456789"); assert(t == "123" && s == "abc"); But it would indeed be more natural to simply return the updated "s". It's what things like "find" or "stripLeft" do anyways, and that works fine.Right. The problem I ran into was that because munch takes a reference, I can't chain it with other functions in std.string because they all return rvalues. So it makes for some unnecessarily awkward code.
Jun 11 2014
On Wed, 11 Jun 2014 20:27:40 +0000 Sean Kelly via Digitalmars-d <digitalmars-d puremagic.com> wrote:It's the only function in std.string that takes a string by ref instead of by value, and this screws up call chaining. What's the reason for this?It looks like it's trying to act like std.conv.parse, though that behavior isn't useful in quite the same way when what's being returned is a string. However, it _is_ among the std.string functions which take patterns and would ideally be changed to take regexes, so if that ever/finally gets done, then that would be a prime time to change the function's behavior if its current behavior is undesirable. - Jonathan M Davis
Jun 11 2014