[squeak-dev] The Inbox: Regex-Core-ct.61.mcz

Levente Uzonyi leves at caesar.elte.hu
Fri Jul 9 15:09:47 UTC 2021


Hi Christoph,

On Thu, 8 Jul 2021, christoph.thiede at student.hpi.uni-potsdam.de wrote:

> Hi Levente,
> 
> two very fair points, thank you for the feedback! Revisiting #escapeString: again, we do not even need to compile a new regex, which is really expensive, but we can use a simple loop instead:

I thought you deliberately wanted to use regular expressions there.

> 
>     | special |
>     special := self specialCharacters.
>     ^ String streamContents: [:stream |
>         aString do: [:char |
>             (special includes: char) ifTrue: [stream nextPut: $\].
>             stream nextPut: char]]
> 
> Which is 90% faster than the original approach. :-)

If you want the "Best Performance (tm)", there is a Squeak-specific 
pattern for these kinds of string-rewrite methods, which consists of a 
precomputed character set and the use of #new:streamContents:, 
#indexOfAnyOf:startingAt: and #next:putAll:startingAt:.
String's #format:, #expandMacrosWithArguments:, #unescapePercentsRaw 
and #jsonWriteOn: (only if you have JSON-ul.56 in your image) all use that 
pattern.


Levente


More information about the Squeak-dev mailing list