www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Translate for chars to strings

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
There's a really useful function 'translate' in std.string, used like this:

__gshared dchar[dchar] MangleTable;

shared static this()
{
    MangleTable =
    [
        '*':'p',  // ptr
        '&':'r',  // reference
        '<':'L',  // left angle
        '>':'R',  // right angle
        ' ':'_',  // space
    ];
}

string mangleType(string input)
{
    return input.translate(MangleTable);
}

However I'm looking for something which translates characters to
strings. Of course that also implies potential reallocation. Does
anyone have such a function in some library?

Note that I'm not looking for .mangleof, I need to work on strings of
C++ names, not D symbols.
Mar 05 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/05/2012 04:32 PM, Andrej Mitrovic wrote:
 There's a really useful function 'translate' in std.string, used like this:

 __gshared dchar[dchar] MangleTable;

 shared static this()
 {
      MangleTable =
      [
          '*':'p',  // ptr
          '&':'r',  // reference
          '<':'L',  // left angle
          '>':'R',  // right angle
          ' ':'_',  // space
      ];
 }

 string mangleType(string input)
 {
      return input.translate(MangleTable);
 }

 However I'm looking for something which translates characters to
 strings. Of course that also implies potential reallocation. Does
 anyone have such a function in some library?

 Note that I'm not looking for .mangleof, I need to work on strings of
 C++ names, not D symbols.
One of the translate() overloads can translate from dchar to string. The third example is exactly about that: http://dlang.org/phobos/std_string.html#translate string[dchar] transTable3 = ['e' : "5", 'o' : "orange"]; assert(translate("hello world", transTable3) == "h5llorange worangerld"); Ali
Mar 05 2012
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Hmm somehow I missed that. Thanks.

On 3/6/12, Ali =C7ehreli <acehreli yahoo.com> wrote:
 On 03/05/2012 04:32 PM, Andrej Mitrovic wrote:
 There's a really useful function 'translate' in std.string, used like
 this:

 __gshared dchar[dchar] MangleTable;

 shared static this()
 {
      MangleTable =3D
      [
          '*':'p',  // ptr
          '&':'r',  // reference
          '<':'L',  // left angle
          '>':'R',  // right angle
          ' ':'_',  // space
      ];
 }

 string mangleType(string input)
 {
      return input.translate(MangleTable);
 }

 However I'm looking for something which translates characters to
 strings. Of course that also implies potential reallocation. Does
 anyone have such a function in some library?

 Note that I'm not looking for .mangleof, I need to work on strings of
 C++ names, not D symbols.
One of the translate() overloads can translate from dchar to string. The third example is exactly about that: http://dlang.org/phobos/std_string.html#translate string[dchar] transTable3 =3D ['e' : "5", 'o' : "orange"]; assert(translate("hello world", transTable3) =3D=3D "h5llorange worangerl=
d");
 Ali
Mar 06 2012