aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/basetypes
diff options
context:
space:
mode:
authorGravatar Hoa V. Dinh <dinh.viet.hoa@gmail.com>2015-09-03 15:35:58 -0700
committerGravatar Hoa V. Dinh <dinh.viet.hoa@gmail.com>2015-09-03 15:35:58 -0700
commit0fe810a8a62c94f85a82f9d9c1ab29a587ff8a3f (patch)
tree18553c0f488b536d4de0a2128da003db9f3f5985 /src/core/basetypes
parent025ffced74888e54fab87db8e7d7df727a0fbfd8 (diff)
Improved performance of String::stripWhiteSpace()
Diffstat (limited to 'src/core/basetypes')
-rw-r--r--src/core/basetypes/MCString.cpp76
1 files changed, 60 insertions, 16 deletions
diff --git a/src/core/basetypes/MCString.cpp b/src/core/basetypes/MCString.cpp
index 7c6539ac..251890af 100644
--- a/src/core/basetypes/MCString.cpp
+++ b/src/core/basetypes/MCString.cpp
@@ -2037,25 +2037,69 @@ String * String::flattenHTML()
String * String::stripWhitespace()
{
- String *str = (String *)copy();
-
- str->replaceOccurrencesOfString(MCSTR("\t"), MCSTR(" "));
- str->replaceOccurrencesOfString(MCSTR("\n"), MCSTR(" "));
- str->replaceOccurrencesOfString(MCSTR("\v"), MCSTR(" "));
- str->replaceOccurrencesOfString(MCSTR("\f"), MCSTR(" "));
- str->replaceOccurrencesOfString(MCSTR("\r"), MCSTR(" "));
- str->replaceOccurrencesOfString(s_unicode160, MCSTR(" "));
- str->replaceOccurrencesOfString(s_unicode133, MCSTR(" "));
- str->replaceOccurrencesOfString(s_unicode2028, MCSTR(" "));
+ String * str = (String *)copy();
+
+ // replace space-like characters with space.
+ const UChar * source = str->unicodeCharacters();
+ UChar * dest = str->mUnicodeChars;
+ while (* source != 0) {
+ if (* source == '\t') {
+ * dest = ' ';
+ }
+ else if (* source == '\n') {
+ * dest = ' ';
+ }
+ else if (* source == '\t') {
+ * dest = ' ';
+ }
+ else if (* source == '\f') {
+ * dest = ' ';
+ }
+ else if (* source == '\r') {
+ * dest = ' ';
+ }
+ else if (* source == 160) {
+ * dest = ' ';
+ }
+ else if (* source == 133) {
+ * dest = ' ';
+ }
+ else if (* source == 0x2028) {
+ * dest = ' ';
+ }
+ else {
+ * dest = * source;
+ }
+ dest ++;
+ source ++;
+ }
- while (str->replaceOccurrencesOfString(MCSTR(" "), MCSTR(" ")) > 0) {
- /* do nothing */
+ // skip spaces at the beginning.
+ source = str->unicodeCharacters();
+ dest = str->mUnicodeChars;
+ while (* source == ' ') {
+ source ++;
}
- while (str->hasPrefix(MCSTR(" "))) {
- str->deleteCharactersInRange(RangeMake(0, 1));
+
+ // copy content
+ while (* source != 0) {
+ if ((* source == ' ') && (* (source + 1) == ' ')) {
+ source ++;
+ }
+ * dest = * source;
+ source ++;
+ dest ++;
}
- while (str->hasSuffix(MCSTR(" "))) {
- str->deleteCharactersInRange(RangeMake(str->length() - 1, 1));
+ * dest = 0;
+ str->mLength = (unsigned int) (dest - str->mUnicodeChars);
+
+ // skip spaces at the end.
+ if (str->mLength > 0) {
+ while (* (dest - 1) == ' ') {
+ dest --;
+ }
+ * dest = 0;
+ str->mLength = (unsigned int) (dest - str->mUnicodeChars);
}
str->autorelease();