string to char array?
Kyoji Klyden via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jun 7 10:41:10 PDT 2015
On Saturday, 6 June 2015 at 18:43:08 UTC, Marc Schütz wrote:
>
> _d_arraybounds() always throws an error because that's its
> purpose. It's implemented here:
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/exception.d#L640
>
> My point was that _d_arraybounds never returns, instead it
> throws that Error object.
>
> The compiler inserts the checks for the array length whenever
> you access an array element, _except_ if it can either prove
> that the array is always long enough (e.g. if its a fixed-size
> array), in which case it can leave the check out because it's
> unnecessary, or if it can prove that the array is never long
> enough, in which case it may already print an error during
> compilation.
>
Okay I think I roughly get it. Another thing that was on my to-do
list just moved up in priority I think.. that thing is learning
everything about the D runtime.
>
> No, the length of the string.
>
> It's roughly the equivalent of this pseudo-code:
>
> extern void _d_arraybounds(void* filename_ptr, size_t
> filename_len, size_t line);
>
> void f(void* a_ptr, size_t a_length) {
> if(a_length == 0)
> goto LBB0_4;
> *cast(int*) a_ptr = 0; // line 5
> if(a_length <= 1)
> goto LBB0_5;
> *cast(int*) (a_ptr+4) = 1; // line 6
> if(a_length <= 2)
> goto LBB0_6;
> *cast(int*) (a_ptr+8) = 1; // line 7
> return;
> LBB0_4:
> // (pretend this filename is 55 chars long)
> static string __FILE__ = "/path/to/your/source/file.d";
> _d_arraybounds(__FILE__.ptr, __FILE__.length, 5 /* line
> number */);
> LBB0_5:
> _d_arraybounds(__FILE__.ptr, __FILE__.length, 6 /* line
> number */);
> LBB0_6:
> _d_arraybounds(__FILE__.ptr, __FILE__.length, 7 /* line
> number */);
> }
>> ...
> Yes, but it is extremely fast. I'm pretty sure accessing memory
> at [RSI] and [RSI+4] both take exactly the same time (but can't
> find a reference now).
Oh I forgot that the path is part of the filename, so it now
makes a bit more sense on why the names might be so long.
I'm also getting the logic here in a theoretical sense, but in a
practical sense, not quite yet. That'll probably take doing
experiments if anything.
Do you perchance have any links to learning resources for the D
runtime(aside from just the github repository), and also maybe
x86 architecture stuff? (I know intel has some 1000+ page pdf on
their site, but I think that's more for hardware and/or OS
designers..)
Thanks! :)
More information about the Digitalmars-d-learn
mailing list