www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does std.string.munch take a string ref?

reply "Sean Kelly" <sean invisibleduck.org> writes:
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
next sibling parent "w0rp" <devw0rp gmail.com> writes:
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
prev sibling next sibling parent "Sean Kelly" <sean invisibleduck.org> writes:
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:
 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.

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?
Jun 11 2014
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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:
 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.

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?

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.
Jun 11 2014
prev sibling next sibling parent "Sean Kelly" <sean invisibleduck.org> writes:
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
prev sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
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