digitalmars.D.learn - Ascii string literal.
- Alexandru Ermicioi (16/16) May 06 2016 Good day,
- Adam D. Ruppe (12/13) May 06 2016 Not implicitly (well, unless you just use string, ascii is a
- Anonymouse (2/9) May 06 2016 Is this different from what std.string.representation does?
- Adam D. Ruppe (4/5) May 06 2016 No, it does the same thing, but with your own function the
- Jonathan M Davis via Digitalmars-d-learn (7/12) May 06 2016 On Fri, 06 May 2016 21:57:22 +0000
- Adam D. Ruppe (3/6) May 06 2016 Yeah, if it is a general thing, but here it is a simple function
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
On Friday, 6 May 2016 at 20:01:27 UTC, Alexandru Ermicioi wrote:Is it possible somehow to convert implicitly a string literalNot 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
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 this different from what std.string.representation does?Is it possible somehow to convert implicitly a string literalNot 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; }
May 06 2016
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
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: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 DavisIs 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
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