Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 06, 2016 Ascii string literal. | ||||
---|---|---|---|---|
| ||||
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 Re: Ascii string literal. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandru Ermicioi | 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 Re: Ascii string literal. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: Ascii string literal. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | 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 07, 2016 Re: Ascii string literal. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 07, 2016 Re: Ascii string literal. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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...
|
Copyright © 1999-2021 by the D Language Foundation