![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Isspace | vipas | UNIX for Dummies Questions & Answers | 7 | 06-10-2004 10:05 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
isspace?
Hello,
Does somebody know what is happening here? This piece of code should skip leading spaces (and others). If isspace encounters a non space character, it doesn't return false. If we analyze the " isspace(*cs_str);" in the debugger, it returns 0. If we check the value in b_space after assigning the value, the value is <> 0. We are using an UTF-8 encoded string. Could that have anything to do with it? It confuses the hell out of us. const char* glb_strnskip_leading(const char* cs_str, size_t ul_size) { size_t ul_idx = 0; int b_space = 0; while((ul_idx < ul_size)) { b_space = isspace(*cs_str); if ( ! b_space ) { break; } else { ++cs_str; ++ul_idx; } } ... I hope anyone has a clue what is happening here Miriam |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Do you have to store the value in the variable to use it later on?
Or could you just replace Code:
b_space = isspace(*cs_str); if ( ! b_space ) Code:
if (!isspace(*cs_str)) |
|
#3
|
|||
|
|||
|
We had it like that, but then we saw that the code was doing strange things. (It was not leaving the if loop the first time, so that we would lose our first character).
To see what the isspace returned, we put the value in a variable. |
|
#4
|
||||
|
||||
|
Well the code looks ok to me. I downloaded it and wrote a little wrapper. It works with both c and c++.
The only way that I can see this happening is a 64 bit/32 bit mismatch. Under that scenario, isspace would be returning a 32 bit integer. But b_space would want a 64 bit integer. So it grabs the correct 2 bytes (which are zero) and it grabs two more bytes which are not zero. |
|
#5
|
|||
|
|||
|
what if...
you used something like:
while ( (ul_idx < ul_size) && (isspace(*cs_str)) ){ cs_str++; ul_idx++; } you wouldn't loose the first character and you would not have to worry about casting and 32 vs 64 bit. Rogier |
|||
| Google The UNIX and Linux Forums |