Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku equivalent of Java StringBuffer/StringBuilder?

Tags:

raku

I need to create a big string out of a complex structure and the runtime is way too long. Back in the Java times I solved similar problems using StringBuffer or StringBuilder which greatly reduce the number of intermediate String objects needed.

Is there something similar in Raku?

like image 293
Konrad Bucheli Avatar asked Jun 02 '20 19:06

Konrad Bucheli


People also ask

Which is better StringBuffer or StringBuilder?

StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer. String concatenation operator (+) internally uses StringBuffer or StringBuilder class.

Do StringBuffer and StringBuilder have the same API?

In contrast to Java's String class, StringBuffer and StringBuilder are both mutable classes. The StringBuilder class and the StringBuffer class have the same API. The sole distinction is that whereas StringBuilder is neither synchronized nor thread-safe, StringBuffer is.

Which is thread-safe StringBuffer or StringBuilder?

StringBuffer is synchronized and therefore thread-safe. StringBuilder is compatible with StringBuffer API but with no guarantee of synchronization. Because it's not a thread-safe implementation, it is faster and it is recommended to use it in places where there's no need for thread safety.

What is String StringBuilder and StringBuffer in Java?

String buffer and StringBuilder both are mutable classes which can be used to do operation on string objects such as reverse of string, concating string and etc. We can modify string without creating a new object of the string. A string buffer is thread-safe whereas string builder is not thread-safe.


1 Answers

Native string arrays pretty much allow you to do that.

my str @parts;
@parts.push("foo")
...
say @parts.join;

Is that what you're looking for?

Additionally: on the MoarVM backend, when you concat strings, they are not actually concatenated in memory, but rather just "linked" together into a single virtual string. Unfortunately, the moment you want to do a regular expression on a string, if does need to be flattened. Which is one of the reasons regular expressions are relatively expensive.

like image 77
Elizabeth Mattijsen Avatar answered Oct 16 '22 18:10

Elizabeth Mattijsen