[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Proposal: improvement of string.gsub()
- From: Gavin Wraith <gavin@...>
- Date: Mon, 18 Apr 2005 10:55:16 +0100
In message <BE56D841E332474FA850E9049569ADCCE5EDC5@mucse204.muc.infineon.com>
> For seperating strings into blocks for iteration, it could be useful to
> replace certain key words by characters, which are easier to match with
> Lua-patterns. I sometimes use string.gsub to change key-words in XML
> files into a frame for text segments which I want to capture. (I don't
> use any Lua extensions for XML as I want to stick with the small Lua
> original as close as possible.)
> Disadvantage: Replacing substrings in very large strings takes lots of
> Solution (in implementation of string.gsub): In case the replacement has
> the same size as the key word (which is to be replaced), the
> search&replace could be done directly in the original string without the
> need to create new strings. This would increase speed significantly and
> decrease memory usage.
> Sorry for not implementing that myself, but unfortunately I cannot
> tackle this issue right now.
Unfortunately this breaks referential transparency - a bad idea.
The usual remedy for string manipulation is never to concatenate
strings, but instead to use "stringles" - lists whose components
are either strings or stringles - i.e trees whose leaves are strings.
Output of a stringle is just a tree walk - no extra space is required
for concatenation. This is often a much more appropriate datatype
than strings when it comes to text manipulation for languages in
which strings are immutable objects. Use string.gsub to insert
the captured text into a stringle.
Gavin Wraith (email@example.com)
Home page: http://www.wra1th.plus.com/