print ubyte[] as (ascii) string
Steven Schveighoffer
schveiguy at gmail.com
Fri Jan 7 21:17:33 UTC 2022
On 1/7/22 2:54 PM, eugene wrote:
> A couple of impressions...
>
> * Does .until() make a copy of original string? And GC then will take
> care of it?
No, `until` will iterate the string one character at a time until it
sees that character (excluding it). It doesn't make a copy of the data.
In effect, it's doing the "trim" and the iteration to print in one step.
In your chosen solution earlier, you were doing it 3 times -- once to
find the end of the null-terminated string (via `fromStringz`), once to
strip the newline (well, this isn't too bad, since it goes from both
ends), and once to actually print it.
> * So many ways to do simple C printf("%s", (char*)buf)... I am feeling
> like Buridan's ass
There are a million ways to get what you want. I like to go for ones that:
1. don't allocate needlessly (I hate allocating a string just to print
it, and then throw it away, regardless of whether it's GC or not)
2. read concisely and clearly.
3. Are as efficient as possible.
The one wart here is the cast, but that's impossible to avoid unless you
want to allocate. Adhering to 2 and 3 above sometimes are in conflict.
But rule 1 is essential for performance.
In C you have one choice (printf), but that choice may not fit your needs.
-Steve
More information about the Digitalmars-d-learn
mailing list