Made it pass the tests, simplified number comparison.
* Space after a number was not correctly handled (must be ignored). * Better conceal the fact that I'm not always the brightest - the number comparison was pretty questionable :-)
This commit is contained in:
@@ -49,38 +49,29 @@ FetchNaturalChunk(natural_chunk& chunk, const char* source)
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip leading zeros and whitespace characters
|
// Skip leading zeros and whitespace characters
|
||||||
int32 skip = 0;
|
int32 skip = 0;
|
||||||
while (source[0] == '0' || isspace(source[0])) {
|
while (source[0] == '0' || isspace(source[0])) {
|
||||||
source++;
|
source++;
|
||||||
skip++;
|
skip++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// number chunk (stop at next white space)
|
// Number chunk (stop at next white space)
|
||||||
int32 pos = 0;
|
int32 pos = 0;
|
||||||
while (isdigit(source[pos]) && source[pos] != '\0') {
|
while (isdigit(source[pos])) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
strlcpy(&chunk.buffer[sizeof(chunk.buffer) - 1 - pos], source, pos + 1);
|
|
||||||
|
strlcpy(chunk.buffer, source, pos + 1);
|
||||||
chunk.length = pos;
|
chunk.length = pos;
|
||||||
|
|
||||||
return pos + skip;
|
// Skip trailing whitespace as well
|
||||||
}
|
while (isspace(source[pos])) {
|
||||||
|
source++;
|
||||||
|
skip++;
|
||||||
//! Makes sure both number strings have the same size
|
|
||||||
inline void
|
|
||||||
NormalizeNumberChunks(natural_chunk& a, natural_chunk& b)
|
|
||||||
{
|
|
||||||
if (a.length > b.length) {
|
|
||||||
memset(&b.buffer[sizeof(b.buffer) - 1 - a.length], ' ',
|
|
||||||
a.length - b.length);
|
|
||||||
b.length = a.length;
|
|
||||||
} else if (b.length > a.length) {
|
|
||||||
memset(&a.buffer[sizeof(a.buffer) - 1 - b.length], ' ',
|
|
||||||
b.length - a.length);
|
|
||||||
a.length = b.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return pos + skip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -138,11 +129,13 @@ NaturalCompare(const char* stringA, const char* stringB)
|
|||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
// Number chunks - they are compared as strings to allow an
|
// Number chunks - they are compared as strings to allow an
|
||||||
// arbitrary number of digits.
|
// almost arbitrary number of digits.
|
||||||
NormalizeNumberChunks(a, b);
|
if (a.length > b.length)
|
||||||
|
return 1;
|
||||||
|
if (a.length < b.length)
|
||||||
|
return -1;
|
||||||
|
|
||||||
int result = strcmp(a.buffer - 1 + sizeof(a.buffer) - a.length,
|
int result = strcmp(a.buffer, b.buffer);
|
||||||
b.buffer - 1 + sizeof(b.buffer) - b.length);
|
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user