http://d.puremagic.com/issues/show_bug.cgi?id=10868
Summary: std.string.translate should take an optional buffer
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: [email protected]
ReportedBy: [email protected]
--- Comment #0 from Andrej Mitrovic <[email protected]> 2013-08-21
18:46:42 PDT ---
translate is useful for interfacing with other languages that require special
string literal escaping rules (e.g. Tcl), however these escaped strings
themselves typically have to be wrapped in some outer block.
Currently translate doesn't allow a user-provided buffer, leading to writing
inefficient code like this:
-----
import std.array;
import std.string;
void main()
{
string[dchar] table = ['{' : `\{`, '}' : `\}`];
string input = `{ foobar }`;
// multiple allocation
auto res = format(`"%s"`, input.translate(table));
assert(res == `"\{ foobar \}"`);
}
-----
A more efficient approach is to allow passing a custom buffer to translate:
-----
import std.array;
import std.string;
void main()
{
string[dchar] table = ['{' : `\{`, '}' : `\}`];
auto buffer = appender!(dchar[])();
buffer ~= '{';
string input = `{ foobar }`;
input.translate(table, null, buffer);
buffer ~= '}';
assert(buffer.data == `"\{ foobar \}"`);
}
-----
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------