std.string.translate
- multiple declarations
- Function translate
- Function translate
- Function translate
- Function translate
Function translate
This is an overload of
which takes an existing translate
buffer
to write the contents to.
Prototypes
void translate(C1, C2, Buffer)( C1[] str, dchar[dchar] transTable, const(C2)[] toRemove, Buffer buffer ) if (isSomeChar!C1 && isSomeChar!C2 && isOutputRange!(Buffer, C1)); void translate(C1, S, C2, Buffer)( C1[] str, S[dchar] transTable, const(C2)[] toRemove, Buffer buffer ) if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2 && isOutputRange!(Buffer, S));
Parameters
Name | Description |
---|---|
str | The original string. |
transTable | The AA indicating which characters to replace and what to replace them with. |
toRemove | The characters to remove from the string. |
buffer | An output range to write the contents to. |
Example
import std.array : appender; dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q']; auto buffer = appender!(dchar[])(); translate("hello world", transTable1, null, buffer); assert(buffer.data == "h5ll7 w7rld"); buffer.clear(); translate("hello world", transTable1, "low", buffer); assert(buffer.data == "h5 rd"); buffer.clear(); string[dchar] transTable2 = ['e' : "5", 'o' : "orange"]; translate("hello world", transTable2, null, buffer); assert(buffer.data == "h5llorange worangerld");
Function translate
This is an ASCII-only overload of
which takes an existing translate
buffer
to write the contents to.
Prototype
void translate(C, Buffer)( char[] str, char[] transTable, char[] toRemove, Buffer buffer ) pure @trusted if (is(Unqual!C == char) && isOutputRange!(Buffer, char));
Parameters
Name | Description |
---|---|
str | The original string. |
transTable | The string indicating which characters to replace and what
to replace them with. It is generated by makeTransTable . |
toRemove | The characters to remove from the string. |
buffer | An output range to write the contents to. |
Example
import std.array : appender; auto buffer = appender!(char[])(); auto transTable1 = makeTransTable("eo5", "57q"); translate("hello world", transTable1, null, buffer); assert(buffer.data == "h5ll7 w7rld"); buffer.clear(); translate("hello world", transTable1, "low", buffer); assert(buffer.data == "h5 rd");
Function translate
Replaces the characters in
which are keys in str
with
their corresponding values in transTable
. transTable
is an AA
where its keys are transTable
dchar
and its values are either dchar
or some
type of string. Also, if
is given, the characters in it are
removed from toRemove
prior to translation. str
itself is unaltered.
A copy with the changes is returned.
str
Prototypes
C1[] translate(C1, C2)( C1[] str, dchar[dchar] transTable, const(C2)[] toRemove = null ) pure @safe if (isSomeChar!C1 && isSomeChar!C2); C1[] translate(C1, S, C2)( C1[] str, S[dchar] transTable, const(C2)[] toRemove = null ) pure @safe if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2);
See Also
Parameters
Name | Description |
---|---|
str | The original string. |
transTable | The AA indicating which characters to replace and what to replace them with. |
toRemove | The characters to remove from the string. |
Example
dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q']; assert(translate("hello world", transTable1) == "h5ll7 w7rld"); assert(translate("hello world", transTable1, "low") == "h5 rd"); string[dchar] transTable2 = ['e' : "5", 'o' : "orange"]; assert(translate("hello world", transTable2) == "h5llorange worangerld");
Function translate
This is an ASCII-only overload of translate. It will not work with Unicode. It exists as an optimization for the cases where Unicode processing is not necessary.
Unlike the other overloads of translate, this one does not take
an AA. Rather, it takes a string
generated by makeTransTable
.
The array generated by
is makeTransTable
256
elements long such that
the index is equal to the ASCII character being replaced and the value is
equal to the character that it's being replaced with. Note that translate
does not decode any of the characters, so you can actually pass it Extended
ASCII characters if you want to (ASCII only actually uses 128
characters), but be warned that Extended ASCII characters are not valid
Unicode and therefore will result in a UTFException
being thrown from
most other Phobos functions.
Also, because no decoding occurs, it is possible to use this overload to
translate
ASCII characters within a proper UTF-8 string without altering the
other, non-ASCII characters. It's replacing any code unit greater than
127
with another code unit or replacing any code unit with another code
unit greater than 127
which will cause UTF validation issues.
Prototype
C[] translate(C)( char[] str, char[] transTable, char[] toRemove = null ) pure nothrow @trusted if (is(Unqual!C == char));
See Also
Parameters
Name | Description |
---|---|
str | The original string. |
transTable | The string indicating which characters to replace and what
to replace them with. It is generated by makeTransTable . |
toRemove | The characters to remove from the string. |
Authors
Walter Bright, Andrei Alexandrescu, and Jonathan M Davis