[haiku-commits] r41084 - haiku/trunk/src/kits/support

  • From: clemens.zeidler@xxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 22 Mar 2011 21:38:13 +0100 (CET)

Author: czeidler
Date: 2011-03-22 21:38:13 +0100 (Tue, 22 Mar 2011)
New Revision: 41084
Changeset: https://dev.haiku-os.org/changeset/41084

Modified:
   haiku/trunk/src/kits/support/String.cpp
Log:
Switch to iswspace. Fix space detection at the right. Thanks 
J?\195?\169r?\195?\180me and Ingo. Please review, though.



Modified: haiku/trunk/src/kits/support/String.cpp
===================================================================
--- haiku/trunk/src/kits/support/String.cpp     2011-03-22 19:39:02 UTC (rev 
41083)
+++ haiku/trunk/src/kits/support/String.cpp     2011-03-22 20:38:13 UTC (rev 
41084)
@@ -14,15 +14,14 @@
 
 /*! String class supporting common string operations. */
 
-
-#include <String.h>
-
 #include <ctype.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <wctype.h>
 
 #include <Debug.h>
+#include <String.h>
 
 #include <utf8_functions.h>
 
@@ -1964,27 +1963,30 @@
 BString&
 BString::Trim()
 {
-       if (Length() <= 0)
+       size_t originalLength = Length();
+       if (originalLength <= 0)
                return *this;
 
        const char* string = String();
 
-       int32 startCount = 0;
-       while (isspace(string[startCount]))
+       // string is \0 terminated thus we don't need to check if we reached 
the end
+       uint32 startCount = 0;
+       while (iswspace(string[startCount]))
                startCount++;
 
-       int32 endCount = 0;
-       while (isspace(string[Length() - endCount - 1]))
+       uint32 endCount = 0;
+       while (endCount < originalLength - startCount
+               && iswspace(string[originalLength - endCount - 1])) {
                endCount++;
+       }
 
        if (startCount == 0 && endCount == 0)
                return *this;
 
        // We actually need to trim
 
-       ssize_t length = Length() - startCount - endCount;
-       if (length < 0)
-               length = 0;
+       ssize_t length = originalLength - startCount - endCount;
+       ASSERT(length >= 0);    
        if (startCount == 0 || length == 0) {
                _MakeWritable(length, true);
        } else if (_MakeWritable() == B_OK) {


Other related posts: