On Sun, Apr 12, 2009 at 9:28 AM, bearophile <[email protected]> wrote: > The following one isn't a problem of D, it's a small bug I've created while > translating C code to D. > Here I have reduced the code to a very small proggy, so you probably need > only a moment to spot the problem. > This program takes a string that contains more than one natural numbers, and > returns their sum. > Example: > 200+350 => 550 > > The original C code: > > #include <stdio.h> > > int main() { > char* numbers = "200 350"; > > int total = 0; > char* p = &numbers[0]; > int cur = 0; > > while (*p != 0) { > char c = (*p) - 48; > if (c >= 0) { > cur = (cur * 10) + c; > } else { > total += cur; > cur = 0; > } > p++; > } > total += cur; > > printf("total: %d\n", total); > return 0; > }
Your C code also contains a bug, if chars are unsigned. I will agree, however, that the D compiler (*all* compilers for all languages, really!) should detect and complain about "nontrivial trivial" comparisons - comparisons that *look* nontrivial (like "c >= 0"), but which always evaluate to true or always to false.
