www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unicode exception raise when replacing underscore with space

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
I get

     core.exception.UnicodeException src/rt/util/utf.d(290):

in a call to

     std.string.tr(x, `_`, ` `)

for a badly encode string x. Is it really needed to do 
auto-decoding here?

Isn't the encoding of underscore and space uniquely one by byte 
in UTF-8?

What do I need to do/add to avoid auto-decoding here?
Jan 13 2015
parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
V Tue, 13 Jan 2015 12:32:15 +0000
"Nordlöw" via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
napsáno:

 I get
 
      core.exception.UnicodeException src/rt/util/utf.d(290):
 
 in a call to
 
      std.string.tr(x, `_`, ` `)
 
 for a badly encode string x. Is it really needed to do 
 auto-decoding here?
 
 Isn't the encoding of underscore and space uniquely one by byte 
 in UTF-8?
 
 What do I need to do/add to avoid auto-decoding here?
std.array.replace(x, `_`, ` `);
Jan 13 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Tuesday, 13 January 2015 at 13:01:56 UTC, Daniel Kozák via 
Digitalmars-d-learn wrote:
 What do I need to do/add to avoid auto-decoding here?
std.array.replace(x, `_`, ` `);
Thanks! What about adding See alsos in the docs that relate these two with respect to auto-decoding?
Jan 13 2015
parent "Daniel Kozak" <kozzi11 gmail.com> writes:
On Tuesday, 13 January 2015 at 20:30:16 UTC, Nordlöw wrote:
 On Tuesday, 13 January 2015 at 13:01:56 UTC, Daniel Kozák via 
 Digitalmars-d-learn wrote:
 What do I need to do/add to avoid auto-decoding here?
std.array.replace(x, `_`, ` `);
Thanks! What about adding See alsos in the docs that relate these two with respect to auto-decoding?
I am not sure, it doesn`t exactly do the same. And to be fair std.array.replace use internaly std.algorithm.find which use in some scenario auto-decoding. So to be sure no autodecoding occured you must used something like that: string x = "some_text"; auto res = std.array.replace(cast(byte[])x, [byte('_')], [byte(' ')]); writeln(cast(string)res);
Jan 13 2015