[haiku-commits] haiku: hrev43772 - src/kits/shared

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 22 Feb 2012 23:45:22 +0100 (CET)

hrev43772 adds 1 changeset to branch 'master'
old head: 71171a7d8c65354c131944d9e37d5010054d228c
new head: f1553a0fc450d8a1442f6a52873906ca98e14a86

----------------------------------------------------------------------------

f1553a0: 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 :-)

                                   [ Axel Dörfler <axeld@xxxxxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev43772
Commit:      f1553a0fc450d8a1442f6a52873906ca98e14a86
URL:         http://cgit.haiku-os.org/haiku/commit/?id=f1553a0
Author:      Axel Dörfler <axeld@xxxxxxxxxxxxxxxx>
Date:        Wed Feb 22 22:43:22 2012 UTC

----------------------------------------------------------------------------

1 files changed, 17 insertions(+), 24 deletions(-)
src/kits/shared/NaturalCompare.cpp |   41 +++++++++++++------------------

----------------------------------------------------------------------------

diff --git a/src/kits/shared/NaturalCompare.cpp 
b/src/kits/shared/NaturalCompare.cpp
index 4085f54..1d322cc 100644
--- a/src/kits/shared/NaturalCompare.cpp
+++ b/src/kits/shared/NaturalCompare.cpp
@@ -49,38 +49,29 @@ FetchNaturalChunk(natural_chunk& chunk, const char* source)
                return pos;
        }
 
-       // skip leading zeros and whitespace characters
+       // Skip leading zeros and whitespace characters
        int32 skip = 0;
        while (source[0] == '0' || isspace(source[0])) {
                source++;
                skip++;
        }
 
-       // number chunk (stop at next white space)
+       // Number chunk (stop at next white space)
        int32 pos = 0;
-       while (isdigit(source[pos]) && source[pos] != '\0') {
+       while (isdigit(source[pos])) {
                pos++;
        }
-       strlcpy(&chunk.buffer[sizeof(chunk.buffer) - 1 - pos], source, pos + 1);
-       chunk.length = pos;
-
-       return pos + skip;
-}
 
+       strlcpy(chunk.buffer, source, pos + 1);
+       chunk.length = pos;
 
-//! 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;
+       // Skip trailing whitespace as well
+       while (isspace(source[pos])) {
+               source++;
+               skip++;
        }
+
+       return pos + skip;
 }
 
 
@@ -138,11 +129,13 @@ NaturalCompare(const char* stringA, const char* stringB)
                                return result;
                } else {
                        // Number chunks - they are compared as strings to 
allow an
-                       // arbitrary number of digits.
-                       NormalizeNumberChunks(a, b);
+                       // almost arbitrary number of digits.
+                       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,
-                               b.buffer - 1 + sizeof(b.buffer) - b.length);
+                       int result = strcmp(a.buffer, b.buffer);
                        if (result != 0)
                                return result;
                }


Other related posts:

  • » [haiku-commits] haiku: hrev43772 - src/kits/shared - axeld