R
rayw
Following on from my post yesterday about nums -> binary strings ...
I've written two routines that can trim the leading zeros from the results,
so I call either of these like this:
char * binary3(I_TYPE n)
{
...
return trimZeros1(buffer);
OR
return trimZeros2(buffer);
}
trimZeros1 works fine it seems, but I found a bug in trimZeros2.
First the fixed trimZeros2 code, and then the question.
// Method 2 - use a standard library call to help us.
//
char * trimZeros2(char s[])
{
int n;
// strspn() - Get length of substring composed of given characters.
//
// Scans s character by character, returning the number of characters
// read before the first character not included in "0" is found.
// If strspn() returns a non 0 value, it found a character at 'n'
// that wasn't a '0' ADDED BUT IT COULD BE A '\0'
//
if((n = strspn(s, "0")) != 0 && s[n] != '\0')
{
return &s[n];
}
// s was all 00000000000s.
//
else
{
return s;
}
}
Q.
Originally the crucial lines read
if((n = strspn(s, "0")) != 0)
{
return &s[n];
}
That worked fine until I passed it an all "000000" string. Then the routine
found the nul terminator.
So, I had to add a sequence point ... && s[n] != '\0' to the test.
That seemed a little clumsy, so I 'intuitively' tried to embed the nul into
the "0" string instead
if((n = strspn(s, "0\0")) != 0)
Looks a bit dumb I know - as the original string ("0") already HAS a nul
terminator at its end, and so I'm assuming, even if this is the correct
syntax, that strspn treats my embedded nul as the end of its 'string of
important chars.?
However, it made me think/wonder whether there IS someway to embed a '\0'
into a quoted string?
*For completeness, here's trimZeros1*
// Method 1 - DIY.
//
char * trimZeros1(char s[])
{
char * p = NULL;
int n;
// If s starts with a '1', there's nothing to do, as there are no
// leading 0s to trim.
//
if(s[0] != '1')
{
// For each character in s ...
//
for(n = 0; n < strlen(s); ++n)
{
// If we find a '1', AND, as we know that s didn't begin with
// one, we're basically done.
//
if(s[n] == '1')
{
p = &s[n];
break;
}
}
// Check to see that s wasn't all 1111111111s.
//
if(p)
{
return p;
}
}
return s;
}
I've written two routines that can trim the leading zeros from the results,
so I call either of these like this:
char * binary3(I_TYPE n)
{
...
return trimZeros1(buffer);
OR
return trimZeros2(buffer);
}
trimZeros1 works fine it seems, but I found a bug in trimZeros2.
First the fixed trimZeros2 code, and then the question.
// Method 2 - use a standard library call to help us.
//
char * trimZeros2(char s[])
{
int n;
// strspn() - Get length of substring composed of given characters.
//
// Scans s character by character, returning the number of characters
// read before the first character not included in "0" is found.
// If strspn() returns a non 0 value, it found a character at 'n'
// that wasn't a '0' ADDED BUT IT COULD BE A '\0'
//
if((n = strspn(s, "0")) != 0 && s[n] != '\0')
{
return &s[n];
}
// s was all 00000000000s.
//
else
{
return s;
}
}
Q.
Originally the crucial lines read
if((n = strspn(s, "0")) != 0)
{
return &s[n];
}
That worked fine until I passed it an all "000000" string. Then the routine
found the nul terminator.
So, I had to add a sequence point ... && s[n] != '\0' to the test.
That seemed a little clumsy, so I 'intuitively' tried to embed the nul into
the "0" string instead
if((n = strspn(s, "0\0")) != 0)
Looks a bit dumb I know - as the original string ("0") already HAS a nul
terminator at its end, and so I'm assuming, even if this is the correct
syntax, that strspn treats my embedded nul as the end of its 'string of
important chars.?
However, it made me think/wonder whether there IS someway to embed a '\0'
into a quoted string?
*For completeness, here's trimZeros1*
// Method 1 - DIY.
//
char * trimZeros1(char s[])
{
char * p = NULL;
int n;
// If s starts with a '1', there's nothing to do, as there are no
// leading 0s to trim.
//
if(s[0] != '1')
{
// For each character in s ...
//
for(n = 0; n < strlen(s); ++n)
{
// If we find a '1', AND, as we know that s didn't begin with
// one, we're basically done.
//
if(s[n] == '1')
{
p = &s[n];
break;
}
}
// Check to see that s wasn't all 1111111111s.
//
if(p)
{
return p;
}
}
return s;
}