The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Isspace vipas UNIX for Dummies Questions & Answers 7 06-10-2004 10:05 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-03-2003
Registered User
 

Join Date: Jun 2001
Posts: 16
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
Reply With Quote
Forum Sponsor
  #2  
Old 11-03-2003
oombera's Avatar
Registered User
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
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 )
with
Code:
if (!isspace(*cs_str))
I'm assuming that works; I haven't done much C programming.
Reply With Quote
  #3  
Old 11-03-2003
Registered User
 

Join Date: Jun 2001
Posts: 16
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.
Reply With Quote
  #4  
Old 11-03-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
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.
Reply With Quote
  #5  
Old 11-04-2003
Registered User
 

Join Date: Sep 2003
Posts: 20
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0