A little of coordination for Rosettacode
bearophile
bearophileHUGS at lycos.com
Wed Feb 13 07:58:43 PST 2013
If some of you has a little of time to review code, I have
converted the very fast C memory mapped version of "Ordered
words" to D:
http://rosettacode.org/wiki/Ordered_words#Mmap
http://rosettacode.org/wiki/Ordered_words#Memory_Mapped_Version
The C version contains several pointers that get juggled around,
even with a negative index:
if (s[0] < s[-1]) r = 0;
So the C entry is rather bug-prone (and it doesn't compile on GCC
Windows). The D entry is portable, and uses mostly slices and
(non-negative) array indexes.
The D findWord function is fiddly still:
const(char)[] findWord(const char[] s) pure nothrow @safe {
size_t wordEnd = 0;
while (wordEnd < s.length && s[wordEnd] != '\n'
&& s[wordEnd] != '\r')
wordEnd++;
return s[0 .. wordEnd];
}
I think a takeWhile (similar to a Python itertools function) is
able to replace it:
return s.takeWhile!(c => !"\n\r".canFind(c));
Using the current Phobos (but a benchmark shows with DMD it's
slower):
const(char)[] findWord(const char[] s) pure /*nothrow*/ @safe {
immutable wordEnd = s.countUntil!q{a == '\n' || a == '\r'}();
return s[0 .. wordEnd >= 0 ? wordEnd : $];
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list