Re: print ubyte[] as (ascii) string

2022-01-02 Thread eugene via Digitalmars-d-learn
On Friday, 31 December 2021 at 14:45:41 UTC, Steven Schveighoffer wrote: Unless I'm misunderstanding: ```d import std.algorithm : until; writefln("got '%s' from '%s:%d'", (cast(char[])ioCtx.buf[]).until('\n'), client.addr, client.port); ``` ```d import std.stdio; import std.string; void m

Re: print ubyte[] as (ascii) string

2022-01-02 Thread eugene via Digitalmars-d-learn
On Saturday, 1 January 2022 at 09:34:10 UTC, Jack Applegame wrote: On Thursday, 30 December 2021 at 18:07:15 UTC, eugene wrote: On Thursday, 30 December 2021 at 17:52:20 UTC, eugene wrote: much better than my initial You can also write ```d auto s = cast(string)b; // or cast(string)(b) ``` ins

Re: print ubyte[] as (ascii) string

2022-01-02 Thread eugene via Digitalmars-d-learn
On Sunday, 2 January 2022 at 08:39:57 UTC, eugene wrote: ```d import std.stdio; import std.string; ``` oops... ```d import std.stdio; import std.string; import std.algorithm : until; void main() { ubyte[8] b = [0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00]; /* "hello\n\0\0" */ w

Re: print ubyte[] as (ascii) string

2022-01-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 January 2022 at 09:15:32 UTC, eugene wrote: ``` p1.d(9): Error: cannot cast expression `until(b[], '\x0a', Flag.yes)` of type `Until!("a == b", ubyte[], char)` to `char[]` ``` Here's a working version: ```d import std.stdio; import std.string; import std.algorithm : until, map

Printing a quoted string

2022-01-02 Thread Amit via Digitalmars-d-learn
Hi! I would like to print a string in the same format that I would write it in the code (with quotes and with special characters escaped). Similar to [Go's %q format](https://pkg.go.dev/fmt#hdr-Printing). Is there a safe, built-in way to do that? For example: ``` string s = "one \"two\"\nt

Re: Printing a quoted string

2022-01-02 Thread Ali Çehreli via Digitalmars-d-learn
On 1/2/22 9:27 AM, Amit wrote: > string s = "one \"two\"\nthree four"; The issue there is that the string does not contain the two characters \" but the single character ". So, that's a syntax issue. The solution is to use back ticks to tell the compiler what you really mean. > And get as ou

Re: Printing a quoted string

2022-01-02 Thread Amit via Digitalmars-d-learn
On Sunday, 2 January 2022 at 17:33:22 UTC, Ali Çehreli wrote: The issue there is that the string does not contain the two characters \" but the single character ". So, that's a syntax issue. The solution is to use back ticks to tell the compiler what you really mean. Thank you! I think my que

Re: Printing a quoted string

2022-01-02 Thread WebFreak001 via Digitalmars-d-learn
On Sunday, 2 January 2022 at 17:27:53 UTC, Amit wrote: Hi! I would like to print a string in the same format that I would write it in the code (with quotes and with special characters escaped). Similar to [Go's %q format](https://pkg.go.dev/fmt#hdr-Printing). Is there a safe, built-in way to

Re: Printing a quoted string

2022-01-02 Thread JG via Digitalmars-d-learn
On Sunday, 2 January 2022 at 17:27:53 UTC, Amit wrote: Hi! I would like to print a string in the same format that I would write it in the code (with quotes and with special characters escaped). Similar to [Go's %q format](https://pkg.go.dev/fmt#hdr-Printing). Is there a safe, built-in way to

Re: Printing a quoted string

2022-01-02 Thread Amit via Digitalmars-d-learn
On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote: as a hack I always do: ```d writeln([s]); ``` because arrays get serialized like D strings, there will be additional `[` and `]` though. Sample output: ``` ["Hello there \"uwu\" ``\x1B[Dabc\n"] ``` On Sunday, 2 January 2022 at 19:

Re: print ubyte[] as (ascii) string

2022-01-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/2/22 4:15 AM, eugene wrote: On Sunday, 2 January 2022 at 08:39:57 UTC, eugene wrote: ```d import std.stdio; import std.string; ``` oops... ```d import std.stdio; import std.string; import std.algorithm : until; void main() {     ubyte[8] b = [0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0