Feature [ASH] Change replace_all, replace_first to return buffers

heeheehee

Developer
Staff member
The current state of ASH functions:
Code:
string replace_all( matcher, string )
string replace_first( matcher, string )
buffer replace_string( buffer, string, string )
buffer replace_string( string, string, string )

The only way to use regular expressions in buffer replacements is via replace_all and replace_first. However, since those return strings, not buffers, we currently need to do something along the lines of
Code:
buffer body;
...
matcher m = create_matcher(pattern, body);
string s = match.replace_first(replacement_text);
body.set_length(0);
body.append(s);

I would find it more convenient if ASH permitted something like
Code:
buffer body;
...
matcher m = create_matcher(pattern, body);
body = match.replace_first(replacement_text);
 

Veracity

Developer
Staff member
How about:

Code:
matcher m = create_matcher(pattern, body);
buffer body = match.replace_first(replacement_text).to_buffer();
or even:

Code:
matcher m = create_matcher(pattern, body);
buffer body = match.replace_first(replacement_text);
... with implicit coercion.

I.e., to_buffer( string) is the inverse of to_string( buffer ).

Otherwise, you are forcing the unnecessary creation of a buffer for whose who want a string.
 

Veracity

Developer
Staff member
That's an implementation artifact. Java's Matcher.replaceAll() and Matcher.replaceFirst() take strings and return strings. There is no Java "replaceString", so we call StringUtilities.globalStringReplace which takes a buffer and returns a buffer.

I believe we automatically coerce a buffer into a string, so the caller who doesn't want buffer as the return value never sees it.

If we could auto-coerce both from string to buffer and buffer to string, you wouldn't even care whether any of the functions returned a string vs. a buffer, although the "asymmetry", as you put it, would still exist for the purpose of eliminating unnecessary memory allocation.
 

heeheehee

Developer
Staff member
That's an implementation artifact. Java's Matcher.replaceAll() and Matcher.replaceFirst() take strings and return strings. There is no Java "replaceString", so we call StringUtilities.globalStringReplace which takes a buffer and returns a buffer.
Makes sense.

I believe we automatically coerce a buffer into a string, so the caller who doesn't want buffer as the return value never sees it.
Right.

If we could auto-coerce both from string to buffer and buffer to string, you wouldn't even care whether any of the functions returned a string vs. a buffer, although the "asymmetry", as you put it, would still exist for the purpose of eliminating unnecessary memory allocation.
Also a fair point.

Also, apparently ASH no longer supports to_buffer (as of the great datatype conversion purge of... I don't remember when). Perhaps that would be a more pertinent feature request, in this context?
 

Veracity

Developer
Staff member
Also, apparently ASH no longer supports to_buffer (as of the great datatype conversion purge of... I don't remember when). Perhaps that would be a more pertinent feature request, in this context?
I think that would give you what you really want and would also be generally useful elsewhere.

I have no idea why to_buffer() was removed.
 
Top