replace_string & buffers?

Okay, I've been searching for some instruction on the replace_string function and haven't got it figured out yet. I found this:

ASH strings are immutable; it's not possible for replace_string() to modify the string in place... Instead, it returns a buffer containing the modified text (and as a buffer, it's more suitable for any further modifications you want to make to the text).
from here http://kolmafia.us/showthread.php?t=2226

That's very helpful, but I guess I not sure how to use buffers. When replace_string, or any function for that matter, makes a buffer how do you call that buffer in a script?

As an example how can I change teststring "xyz" to string "123"?

This:
Code:
string teststring = "xyz";
print(teststring);
print(replace_string(teststring,"x","1"));
print(replace_string(teststring,"y","2"));
as expected returns this:
Code:
xyz
1yz
x2z

I don't know how to pass the buffer from the first replace_string to the second replace_string?
 

Alhifar

Member
Simply convert it to a string with to_string().
For example:
Code:
string teststring = "xyz";
print( teststring );
teststring = replace_string( teststring , "x" , "1" ).to_string();
print( teststring );
teststring = replace_string( teststring , "y" , "2" ).to_string();
print( teststring );
 

Grotfang

Developer
Thanks Alhifar! My method previously had been to define and use a new string instead:

Code:
string teststring = "xyz";
print( teststring );
string teststring_one = replace_string( teststring , "x" , "1" );
print( teststring_one );
string teststring_two = replace_string( teststring_one , "y" , "2" );
print( teststring_two );
 

holatuwol

Developer
Alternatively, you could declare a variable to be of 'buffer' type and then work with it directly.

Code:
buffer teststring;
teststring.append("xyz");
teststring.print();
teststring.replace_string("x","1").print();
teststring.replace_string("y","2").print();
 

dj_d

Member
I guess this begs the question - what are the differences between a string and a buffer?

(nice to see you around here, hola!)
 

holatuwol

Developer
I guess this begs the question - what are the differences between a string and a buffer?
The main difference is described by Jason (buffers are mutable, while strings are immutable), which is mainly relevant because most things in ASH are passed by reference. An example should help if you don't understand that terminology:

Code:
buffer test1;
buffer test2 = test1;

test1.append("abc");
test2.append("xyz");

test1.print();
test2.print();

Because buffers are mutable, buffers are also more memory and CPU efficient for common string operations (concatenation, replacement) than strings, which is why we return them from ASH methods that do those operations.
 

Catch-22

Active member
Aha! Thanks.

Yes and the reason it works out quicker for concatenation is because if you concatenate two strings, a buffer is being created internally anyway. Each string is then appended to the buffer, performing the concatenation, then the result of that buffer is converted back to a string. It gets worse the more string concatenations you do.

Check out this article for more info.
 
Top