Bug 10379 – std.string.translate (and others) for a Range of characters

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-16T06:53:53Z
Last change time
2018-02-10T22:55:13Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-06-16T06:53:53Z
In Phobos I'd like a function like std.string.translate (or an overload of the same function) that works on a lazy Range of characters, instead of a string. This is useful to reduce the memory used when I have to process lot of data, to avoid the creation of many large intermediate strings. (One current workaround is to use chunks, and process each piece with the string functions). Probably it's useful for other similar string functions to accept a lazy Range of characters, like std.string.removechars, std.string.tr, std.array.replace, etc. (This also makes a function as std.string.tr even more similar to the Posix tr).
Comment #1 by andrej.mitrovich — 2013-06-17T16:38:19Z
I could imagine a generic version of it put into std.algorithm (where the translate hash table can have any Value type but the Key type would have to be the ElementType of the range), for example: ----- string[int] table = [1 : "one", 2 : "two"]; auto inputRange = ...; // some integer range inputRange.translate(table); ----- I'm not sure about the use-cases though.
Comment #2 by bearophile_hugs — 2013-06-17T17:20:41Z
(In reply to comment #1) > I'm not sure about the use-cases though. My request is about a translate (and other string functions) that works on a range of characters. Extending it to other kinds of data is possible, but fulfils different uses cases. Regarding my use case, if you have a very long string (multi megabytes) and you want to apply several string functions on it, every time they generate an intermediate very large string. This sometimes requires lot of memory. When there are even large streams of textual data you may not even have enough space to keep it whole in memory. In such cases I'd like lazy string functions.
Comment #3 by greensunny12 — 2018-02-10T22:55:13Z
2.079 will ship with substitute: https://dlang.org/changelog/pending.html#std-algorithm-iteration-substitute It doesn't use an internal table and is fully lazy and @nogc. Its generic and works with any kind of range (+strings) and single chars. Moreover, there's tr and translate in std.string: https://dlang.org/phobos/std_string.html#.tr https://dlang.org/phobos/std_string.html#.translate