Re: Converting CString to Integer
From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 10/15/04
- Next message: JSmith: "Re: icon and control list"
- Previous message: M: "Re: CSplitterWnd Creation"
- In reply to: Sergey Kochkarev: "Re: Converting CString to Integer"
- Next in thread: Sergey Kochkarev: "Re: Converting CString to Integer"
- Reply: Sergey Kochkarev: "Re: Converting CString to Integer"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 15 Oct 2004 11:49:34 -0500
Sergey Kochkarev wrote:
>OK, here you are:
>
>bool GetMyUnsigned(const char* c, unsigned& ret_val)
>{
> if(!c || !*c)
> return false;
>
> ret_val = 0;
> unsigned counter = 0;
> do
> {
> int digit = int(*c - '0');
> if(digit < 0 || digit > 9)
> return false;
>
> if(ret_val > 0xFFFFFFFF / 10 - 1)
> return false;
>
> ret_val = ret_val * 10 + digit;
> } while(*(++c));
>
> return true;
>}
>
>Easy, yeah?
>It is much faster than C++ Runtimes.
Did you measure it? And as mentioned in my last message, do you have an
application which calls it so frequently in such a short time that the
"improvement" actually matters?
Can you find the bug in your function? Hint: You might find it easier to
spot if you made it GetMyUnsignedChar and were to see if it can handle all
values of unsigned char.
And BTW, what's this magic number 0xFFFFFFFF? Do you know a portable way to
get the number you need? How about a portable way to find the largest value
of any unsigned type without #including a header file? To follow my hint
given earlier, you'll have to change this number.
What's "easy" is not writing code such as the above at all and using
standard functions like strtol when possible.
>It does not call other functions.
>It does what it should do and nothing else. It does not have atol bug
>(try converting "123213123123243423434234324234553454534" and see the
>incorrect result, should be 0).
There you go. You earlier expressed your disdain for consulting
documentation, and it shows. You think atol should return zero in that case,
when actually the result is undefined, because the result cannot be
represented in a 32 bit long (which is a typical size and the one VC uses).
That's one reason you use strtox when you don't know anything about the
input and you care about error checking.
>Want documentation? Here you are:
>GetMyUnsigned converts string into unsigned (32-bit) integer and
>returns false if the string does not contain unsigned (32-bit) only.
Unfortunately, your function contains a bug and doesn't fulfill that
contract.
>About STL. What implementation you mean?
Where did I say anything about STL?
>The one that is in VC 6.0 is
>not standard, it uses its own subset of the ANSI standard that is
>compatible with the compiler. Many good standard STL programs cannot
>be built with Microsoft's STL and on Microsoft's compiler (for
>example, try playing with Loki by Andrei Alexandrescu). I'm not sure
>this is a bug, but it is surelly not fully tested to be compatible
>with ANSI standards. Can you use untested code for critical
>applications? I'm not sure.
That's a different issue altogether. The functions we're talking about in
this thread were standardized 15 years ago.
>In conclusion. Certainly, you are right, it is not worthy to think
>yourself if somebody thinks for you. Windows 3.1 took 2 Megabytes on
>my hard drive. Windows XP takes 2 Gigabytes - you know why?
If it makes you feel better, by all means, go back to Windows 3.1. Then you
can revel in your 2 MB footprint on perhaps an 80 GB hard drive. :)
Seriously, while efficiency can't be ignored, it's ever more important to
have a sense of proportion.
-- Doug Harrison Microsoft MVP - Visual C++
- Next message: JSmith: "Re: icon and control list"
- Previous message: M: "Re: CSplitterWnd Creation"
- In reply to: Sergey Kochkarev: "Re: Converting CString to Integer"
- Next in thread: Sergey Kochkarev: "Re: Converting CString to Integer"
- Reply: Sergey Kochkarev: "Re: Converting CString to Integer"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|