P
Peter Davies
Hi all,
Plagued by the (to me) non-obvious functionality of DJB's str.h library, I
started looking at the source and what I saw took a while to make sense.
Each function runs an infinite loop, which iterates though a char* - in
order to search for needles or calculate a string length or whatever. What
got me was that each of the infinite loops held 4 identical statements:
for (; {
if (!*t) break; if (*t == ch) break; ++t;
if (!*t) break; if (*t == ch) break; ++t;
if (!*t) break; if (*t == ch) break; ++t;
if (!*t) break; if (*t == ch) break; ++t;
}
(in str_chr.c) and
for (; {
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
(in str_len.c)
Does anyone have any ideas as to why this is done? All I can think of is
that 4 chars=32bits and that nicely fills a register.
Plagued by the (to me) non-obvious functionality of DJB's str.h library, I
started looking at the source and what I saw took a while to make sense.
Each function runs an infinite loop, which iterates though a char* - in
order to search for needles or calculate a string length or whatever. What
got me was that each of the infinite loops held 4 identical statements:
for (; {
if (!*t) break; if (*t == ch) break; ++t;
if (!*t) break; if (*t == ch) break; ++t;
if (!*t) break; if (*t == ch) break; ++t;
if (!*t) break; if (*t == ch) break; ++t;
}
(in str_chr.c) and
for (; {
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
(in str_len.c)
Does anyone have any ideas as to why this is done? All I can think of is
that 4 chars=32bits and that nicely fills a register.