www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ascii string literal.

reply Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
Good day,

Is it possible somehow to convert implicitly a string literal 
into an ubyte array?
For example:

void do(immutable(ubyte)[] asciiString) {
// Do something with ascii string.
}

And from another section of code, calling it like:

do("Some ascii string");
-----------

If no, is there an us-ascii string literal that consists of 
ubytes and not chars?

It's just in some of my code should work only with ascii strings, 
and it will be cumbersome to convert to ubyte array a string 
literal each time a function accepting an ubyte array is called.

Thank you.
May 06 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 6 May 2016 at 20:01:27 UTC, Alexandru Ermicioi wrote:
 Is it possible somehow to convert implicitly a string literal
Not implicitly (well, unless you just use string, ascii is a strict subset of utf-8 anyway), but you could do it explicitly easily. immutable(ubyte)[] ascii(string s) { return cast(typeof(return)) s; } Then use it like ascii("your string") or make it a template and use ascii!"your string" or "your string".ascii whatever. You could (and imo should!) also make a struct to hold the new type.
May 06 2016
parent reply Anonymouse <asdf asdf.net> writes:
On Friday, 6 May 2016 at 20:29:35 UTC, Adam D. Ruppe wrote:
 On Friday, 6 May 2016 at 20:01:27 UTC, Alexandru Ermicioi wrote:
 Is it possible somehow to convert implicitly a string literal
Not implicitly (well, unless you just use string, ascii is a strict subset of utf-8 anyway), but you could do it explicitly easily. immutable(ubyte)[] ascii(string s) { return cast(typeof(return)) s; }
Is this different from what std.string.representation does?
May 06 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 6 May 2016 at 21:39:35 UTC, Anonymouse wrote:
 Is this different from what std.string.representation does?
No, it does the same thing, but with your own function the intention may be clearer (or you could do a template to avoid any function or custom types too)
May 06 2016
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Fri, 06 May 2016 21:57:22 +0000
"Adam D. Ruppe via Digitalmars-d-learn"
<digitalmars-d-learn puremagic.com> wrote:

 On Friday, 6 May 2016 at 21:39:35 UTC, Anonymouse wrote:
 Is this different from what std.string.representation does?
No, it does the same thing, but with your own function the intention may be clearer (or you could do a template to avoid any function or custom types too)
In general, it's better to use representation than to cast, because representation gets the constness right, whereas if you cast, there's always the risk that you won't. - Jonathan M Davis
May 06 2016
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 7 May 2016 at 01:37:30 UTC, Jonathan M Davis wrote:
 In general, it's better to use representation than to cast, 
 because representation gets the constness right, whereas if you 
 cast, there's always the risk that you won't.
Yeah, if it is a general thing, but here it is a simple function statically typed to string...
May 06 2016