S
spinoza1111
Richard said:(e-mail address removed) said:>
This would be a straightforward test for numerics:
int to_digit(int ch)
{
return isdigit((unsigned char)ch) ? ch - '0' : -1;
}
If you think that's too obfuscated, here's another, which I don't like very
much as it trades structural elegance for simplicity, but it /is/ simple:
int to_digit(int ch)
{
if(isdigit((unsigned char)ch))
{
return ch - '0';
}
return -1;
}
I'm not a C "expert" by your lights, but I am an expert programmer, and
the above is one of the silliest things I've ever seen posted on the
net. You may be a "recognized C expert", but you can't program and
should not be permitted to write software or write about software. Here
is why:
(1) You call the code a "straightforward test for numerics" but it
isn't any such thing. It assumes that the library contains an isdigit()
function and doesn't "test" at all. Instead, using isdigit() it checks
for a digit (which makes your statement wrong and a lie because isdigit
does the work you claim "your" code does) and then converts the digit
to a number.
(2) Earlier this evening I mistook your code to fail if the character
value exceeded '9' because your code was so terribly wrong (in saying
it was something it was not) that I actually overlooked (1); you are
not making the test you say you are making. I was in a hurry because
I'm wasting my time replying to you.
To call the above code "straightforward" is obscene. You need to code
what you mean, and the minimal assumption you can make about digits is
that their value, when converted to an integer (whether byte, unsigned
or other) is inclusively between that of the integer value of '0' and
that of '9'.
The value of letters in EBCDIC did not conform to this, but this was a
mistake caused by the fact that Herman Hollerith, the designer of the
IBM punched card, needed "zone" bits. It was fixed in ASCII. In all
extant coding systems except perhaps obscure ones, digit values are
adjacent.
You may believe that a "standard" C programmer would always use isdigit
to ensure his code worked on non-ASCII, non-EBCDIC systems. The isdigit
function would then I suppose completely conceal the machine and code
dependent test and here, the code would be transportable between ASCII
and EBCDIC machines.
This would be a good thing. The problem is that actual working C
programmers can't be sure of having a fully conforming implementation
for legacy code, and don't waste time if they're smart making legacy
programs conform to your favorite standard. And, you intentionally
mislead them, as a self-appointed C expert, by saying that your code is
a "straighforward test for numerics" when it is not.
In your code, assuming that the C environment the programmer is using
(which doesn't have to be standard) has an isdigit, you don't break if
the digit value is greater than the value of 9. However, your code,
starting with your claim, is a lie, and you are posting (perhaps as a
paid agent) disinformation in a campaign of character assassination
similar to that you conducted against Herb Schildt. **** you.
You're a code monkey and on the job, if this is your style, your
interest is solely in spreading confusion, and, probably, office
politics and character assassination.
As much as you are, and probably more.
Plenty. What he's saying is that your code is unnecessarily obscure and
difficult to read. That's the mark of a bad programmer, unless your intent
is to win the IOCCC.
Obscure? Difficult to read? Give me a break. You say above that "this
would be a straightforward TEST FOR NUMERICS" [emphasis mine] and then
you use isdigit() to do the heavy lifting.