Jump to page: 1 24  
Page
Thread overview
print ubyte[] as (ascii) string
Dec 30, 2021
eugene
Dec 30, 2021
Era Scarecrow
Dec 30, 2021
eugene
Dec 30, 2021
Tejas
Dec 30, 2021
eugene
Dec 30, 2021
Tejas
Dec 30, 2021
eugene
Dec 30, 2021
eugene
Dec 30, 2021
Tejas
Dec 30, 2021
eugene
Dec 30, 2021
eugene
Jan 01, 2022
Jack Applegame
Jan 02, 2022
eugene
Dec 30, 2021
eugene
Jan 02, 2022
eugene
Jan 02, 2022
eugene
Jan 02, 2022
Paul Backus
Jan 07, 2022
eugene
Jan 07, 2022
eugene
Jan 07, 2022
H. S. Teoh
Jan 07, 2022
eugene
Jan 07, 2022
eugene
Jan 07, 2022
Adam D Ruppe
Jan 07, 2022
eugene
Jan 07, 2022
eugene
Jan 08, 2022
frame
Jan 07, 2022
Dukc
December 30, 2021

I suspect the question was asked somewhere before.
If so just give a link.

Anyway:


class IoContext {
    ...
    ubyte[] buf;
    ...
    this(uint bufSize) {
        buf = new ubyte[bufSize];
    }
}

The buffer contains (ascii) string terminated with '\n'.
In order to print it not as an array of numbers (buf is 1024 bytes long),
but as usual string I do

char[] s = cast(char[])ioCtx.buf[0 .. strlen(cast(char*)ioCtx.buf.ptr) - 1];
// -1 is to eliminate terminating '\n'
writefln("got '%s' from '%s:%d'", s, client.addr, client.port);

Is there some more concise/elegant way to do that?

Of course, I could use old good printf() instead:

printf(
    "got '%s' from '%s:%d'\n",
    ioCtx.buf.ptr,            // '\n' still there
    toStringz(client.addr),
    client.port
);

but I want to use D stdlib, not libc.

December 30, 2021

On Thursday, 30 December 2021 at 09:34:27 UTC, eugene wrote:

>

The buffer contains (ascii) string terminated with '\n'. In order to print it not as an array of numbers (buf is 1024 bytes long), but as usual string I do

Few years ago i asked a similar question, not to do UTF-8 but to do Ascii. I was working on a tool for Morrowind after determining the strings were not variable in length and instead much like ascii fixed at 256 combinations.

The answer i ended up with was a quick conversion to a UTF in order to print it. Seems you might have to convert to Latin-1.

Here's the old thread.

https://forum.dlang.org/thread/lehgyzmwewgvkdgraizv@forum.dlang.org

December 30, 2021

On Thursday, 30 December 2021 at 16:00:59 UTC, Era Scarecrow wrote:

>

The answer i ended up with was a quick conversion to a UTF in order to print it. Seems you might have to convert to Latin-1.

For a moment I only have symbols from the lower half of ASCII table.
I meant - can that be done as simple as in C, i.e:

u8 *buf;
...
printf("%s", (char*)buf);

without any twists and turns.

December 30, 2021

On Thursday, 30 December 2021 at 09:34:27 UTC, eugene wrote:

>

I suspect the question was asked somewhere before.
If so just give a link.

Anyway:


class IoContext {
    ...
    ubyte[] buf;
    ...
    this(uint bufSize) {
        buf = new ubyte[bufSize];
    }
}

class IoContext {
     ...
     ubyte[] buf;
     ...
     this(uint bufSize) {
         buf.length = bufSize; //this should do the same thing, I believe
     }
}
>

The buffer contains (ascii) string terminated with '\n'.
In order to print it not as an array of numbers (buf is 1024 bytes long),
but as usual string I do

char[] s = cast(char[])ioCtx.buf[0 .. strlen(cast(char*)ioCtx.buf.ptr) - 1];
// -1 is to eliminate terminating '\n'
writefln("got '%s' from '%s:%d'", s, client.addr, client.port);
char[] s = cast(char[])ioCtx.buf[0 .. $];// please remember that in `[0 .. $]` last index is automatically `length - 1` but just buf[$] will be an error since there the actual `length` will be used

I think the above code is correct, please verify

December 30, 2021

On Thursday, 30 December 2021 at 09:34:27 UTC, eugene wrote:

>
char[] s = cast(char[])ioCtx.buf[0 .. strlen(cast(char*)ioCtx.buf.ptr) - 1];
// -1 is to eliminate terminating '\n'
writefln("got '%s' from '%s:%d'", s, client.addr, client.port);

Is there some more concise/elegant way to do that?

char[] s = fromStringz(cast(char*)ioCtx.buf.ptr).strip;
writefln("got '%s' from '%s:%d'", s, client.addr, client.port);

Well, this will do:)

December 30, 2021

On Thursday, 30 December 2021 at 16:49:17 UTC, Tejas wrote:

>
char[] s = cast(char[])ioCtx.buf[0 .. $];// please remember that in `[0 .. $]` last index is automatically `length - 1` but just buf[$] will be an error since there the actual `length` will be used

I think the above code is correct, please verify

There is one pecularity:

char[] s = fromStringz(cast(char*)ioCtx.buf.ptr);
writefln("got '%s' from '%s:%d'", s.strip, client.addr, client.port);
// strip works :)
char[] s = cast(char[])ioCtx.buf[0 .. $];
writefln("got '%s' from '%s:%d'", s.strip, client.addr, client.port);
// strip does not work :(
December 30, 2021

On Thursday, 30 December 2021 at 17:07:20 UTC, eugene wrote:

>

On Thursday, 30 December 2021 at 16:49:17 UTC, Tejas wrote:

>
char[] s = cast(char[])ioCtx.buf[0 .. $];// please remember that in `[0 .. $]` last index is automatically `length - 1` but just buf[$] will be an error since there the actual `length` will be used

I think the above code is correct, please verify

There is one pecularity:

char[] s = fromStringz(cast(char*)ioCtx.buf.ptr);
writefln("got '%s' from '%s:%d'", s.strip, client.addr, client.port);
// strip works :)
char[] s = cast(char[])ioCtx.buf[0 .. $];
writefln("got '%s' from '%s:%d'", s.strip, client.addr, client.port);
// strip does not work :(

I'll need to know the error message, because the following works:

import std;
void main()
{
    ubyte[] c ;
    c.length = 100;
    char[] arr = cast(char[])c[0 .. $];
    foreach(ref elem; arr)
        elem = 'a';
    writeln(arr.strip);
}
December 30, 2021

On Thursday, 30 December 2021 at 16:49:17 UTC, Tejas wrote:

>

I think the above code is correct, please verify

Self-contained example:

import std.stdio;
import std.string;

void main() {
    ubyte[8] b = [0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00];
    /* "hello\n\0\0" */

    char[] s = fromStringz(cast(char*)b.ptr);
    writefln("'%s, world'", s.strip);

    s = cast(char[])b[0 .. $];
    writefln("'%s, world'", s.strip);
}

Output:

@mono:~/2-coding/d-lang/misc$ ./p
'hello, world'
'hello
, world'
December 30, 2021

On Thursday, 30 December 2021 at 17:16:10 UTC, Tejas wrote:

>

I'll need to know the error message

There is none, see example in my prev message

>

because the following works:

import std;
void main()
{
    ubyte[] c ;
    c.length = 100;
    char[] arr = cast(char[])c[0 .. $];
    foreach(ref elem; arr)
        elem = 'a';
    writeln(arr.strip);
}

There is nothing to strip here (spaces, new lines...).

December 30, 2021

On Thursday, 30 December 2021 at 17:31:27 UTC, eugene wrote:

>

On Thursday, 30 December 2021 at 16:49:17 UTC, Tejas wrote:

>

I think the above code is correct, please verify

Self-contained example:

import std.stdio;
import std.string;

void main() {
    ubyte[8] b = [0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00];
    /* "hello\n\0\0" */

    char[] s = fromStringz(cast(char*)b.ptr);
    writefln("'%s, world'", s.strip);

    s = cast(char[])b[0 .. $];
    writefln("'%s, world'", s.strip);
}

Output:

@mono:~/2-coding/d-lang/misc$ ./p
'hello, world'
'hello
, world'

I'm not at my computer anymore, could you please replace the 0x00 with 0x0A and tell me if strip still doesn't work for my solution?

I think the fromstringz is trimming the null bytes for you, making the \n the last character, allowing strip to work.

« First   ‹ Prev
1 2 3 4