[Issue 10868] New: std.string.translate should take an optional buffer
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Aug 21 18:46:43 PDT 2013
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: andrej.mitrovich at gmail.com
ReportedBy: andrej.mitrovich at gmail.com
--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 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: -------
More information about the Digitalmars-d-bugs
mailing list