James Miller:
I realised that when you want the number of characters, you
normally actually want to use walkLength, not length.
As with strlen() in C, unfortunately the result of
walkLength(somestring) is computed every time you call it...
because it's doesn't get cached.
A partial improvement for this situation is to assure
walkLength(somestring) to be strongly pure, and to assure the D
compiler is able to move this invariant pure computation out of
loops.
Is is reasonable for the compiler to pick this up during
semantic analysis and point out this situation?
This is not easy to do, because sometimes you want to know the
number of code points, and sometimes of code units.
I remember even a proposal to rename the "length" field to
another name for narrow strings, to avoid such bugs.
-----------------------
Adam D. Ruppe:
Maybe... but it is important that this works:
string s;
if(s.length)
do_something(s);
since that's always right and quite common.
Better:
if (!s.empty)
do_something(s);
(or even better, built-in non-ulls, usable for strings too).
Bye,
bearophile