From 3eac8208dfcd6bd73a534303414784d3754de855 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 9 Jan 2017 23:12:52 +0100 Subject: [PATCH] Remove BStringRef and users. As discussed in 2008 (http://www.freelists.org/post/haiku-development/BString-on-GCC4,1), this class was not efficient because of lack of inlining. Implement the suggested solution of a SetCharAt method instead. Also add a CompareAt which covers a specific use case in KeyboardLayout.cpp. Adjust all places which were using this feature to safer APIs. Also fixes a copypaste error in FormattingConventions.cpp. --- headers/os/support/String.h | 31 ++------- src/apps/deskcalc/ExpressionTextView.cpp | 8 +-- src/apps/webpositive/BrowserWindow.cpp | 2 +- src/kits/locale/FormattingConventions.cpp | 2 +- src/kits/mail/HaikuMailFormatFilter.cpp | 2 +- src/kits/support/String.cpp | 76 ++++++----------------- src/preferences/keymap/KeyboardLayout.cpp | 7 +-- src/preferences/mail/DNSQuery.cpp | 6 +- 8 files changed, 36 insertions(+), 98 deletions(-) diff --git a/headers/os/support/String.h b/headers/os/support/String.h index 9e33c3cd66..7471d52bae 100644 --- a/headers/os/support/String.h +++ b/headers/os/support/String.h @@ -183,6 +183,9 @@ public: int Compare(const BString& string, int32 length) const; int Compare(const char* string, int32 length) const; + int CompareAt(size_t offset, const BString& string, + int32 length) const; + int CompareChars(const BString& string, int32 charCount) const; int CompareChars(const char* string, @@ -293,9 +296,7 @@ public: // Unchecked char access char operator[](int32 index) const; -#if __GNUC__ > 3 - BStringRef operator[](int32 index); -#else +#if __GNUC__ == 2 char& operator[](int32 index); #endif @@ -308,6 +309,7 @@ public: // Fast low-level manipulation char* LockBuffer(int32 maxLength); BString& UnlockBuffer(int32 length = -1); + BString& SetCharAt(int32 pos, char to); // Upercase <-> Lowercase BString& ToLower(); @@ -350,7 +352,6 @@ public: private: class PosVect; - friend class BStringRef; enum PrivateDataTag { PRIVATE_DATA @@ -604,26 +605,4 @@ operator!=(const char* str, const BString& string) } -// #pragma mark - BStringRef - - -class BStringRef { -public: - BStringRef(BString& string, int32 position); - ~BStringRef() {} - - operator char() const; - - char* operator&(); - const char* operator&() const; - - BStringRef& operator=(char c); - BStringRef& operator=(const BStringRef& rc); - -private: - BString& fString; - int32 fPosition; -}; - - #endif // _B_STRING_H diff --git a/src/apps/deskcalc/ExpressionTextView.cpp b/src/apps/deskcalc/ExpressionTextView.cpp index e303731a45..6e527aaa5b 100644 --- a/src/apps/deskcalc/ExpressionTextView.cpp +++ b/src/apps/deskcalc/ExpressionTextView.cpp @@ -288,13 +288,13 @@ ExpressionTextView::SetValue(BString value) if (digit != 10) break; - value[offset] = '0'; + value.SetCharAt(offset, '0'); } if (digit == 10) { // carry over, shift the result if (value[firstDigit + 1] == '.') { - value[firstDigit + 1] = '0'; - value[firstDigit] = '.'; + value.SetCharAt(firstDigit + 1, '0'); + value.SetCharAt(firstDigit, '.'); } value.Insert('1', 1, firstDigit); @@ -311,7 +311,7 @@ ExpressionTextView::SetValue(BString value) value << 'E' << exponent; } else { // increase the current digit value with one - value[offset] = char(digit + 48); + value.SetCharAt(offset, char(digit + 48)); // set offset to last digit offset = value.FindFirst('E'); diff --git a/src/apps/webpositive/BrowserWindow.cpp b/src/apps/webpositive/BrowserWindow.cpp index 6eec697173..18e0519a15 100644 --- a/src/apps/webpositive/BrowserWindow.cpp +++ b/src/apps/webpositive/BrowserWindow.cpp @@ -2435,7 +2435,7 @@ BrowserWindow::_EncodeURIComponent(const BString& search) for (int32 i = 0; i < result.Length(); i++) { if (escCharList.FindFirst(result[i]) != B_ERROR) { sprintf(hexcode, "%02X", (unsigned int)result[i]); - result[i] = '%'; + result.SetCharAt(i, '%'); result.Insert(hexcode, i + 1); i += 2; } diff --git a/src/kits/locale/FormattingConventions.cpp b/src/kits/locale/FormattingConventions.cpp index 2c6a20da6e..4e7160bf7f 100644 --- a/src/kits/locale/FormattingConventions.cpp +++ b/src/kits/locale/FormattingConventions.cpp @@ -212,7 +212,7 @@ BFormattingConventions::BFormattingConventions( for (int t = 0; t < B_TIME_FORMAT_STYLE_COUNT; ++t) { fCachedDateTimeFormats[s][t] = other.fCachedDateFormats[s][t]; - fExplicitDateFormats[s][t] = other.fExplicitDateFormats[s][t]; + fExplicitDateTimeFormats[s][t] = other.fExplicitDateFormats[s][t]; } } for (int s = 0; s < B_TIME_FORMAT_STYLE_COUNT; ++s) { diff --git a/src/kits/mail/HaikuMailFormatFilter.cpp b/src/kits/mail/HaikuMailFormatFilter.cpp index 8c4b4d8c68..1cdaeab6e7 100644 --- a/src/kits/mail/HaikuMailFormatFilter.cpp +++ b/src/kits/mail/HaikuMailFormatFilter.cpp @@ -247,7 +247,7 @@ HaikuMailFormatFilter::_RemoveExtraWhitespace(BString& name) if (i == remove + 1 || i == name.Length()) remove++; else - name[i - spaces] = ' '; + name.SetCharAt(i - spaces, ' '); name.Remove(i - remove, remove); i -= remove; spaces = 0; diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 5533f03ebe..c59bb3f1c4 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -132,57 +132,6 @@ private: }; -// #pragma mark - BStringRef - - -BStringRef::BStringRef(BString& string, int32 position) - : - fString(string), fPosition(position) -{ -} - - -BStringRef::operator char() const -{ - return fPosition < fString.Length() ? fString.fPrivateData[fPosition] : 0; -} - - -BStringRef& -BStringRef::operator=(char c) -{ - fString._MakeWritable(); - fString.fPrivateData[fPosition] = c; - return *this; -} - - -BStringRef& -BStringRef::operator=(const BStringRef &rc) -{ - return operator=(rc.fString.fPrivateData[rc.fPosition]); -} - - -const char* -BStringRef::operator&() const -{ - return &fString.fPrivateData[fPosition]; -} - - -char* -BStringRef::operator&() -{ - if (fString._MakeWritable() != B_OK) - return NULL; - - fString._ReferenceCount() = -1; - // mark as unsharable - return &fString.fPrivateData[fPosition]; -} - - // #pragma mark - BString @@ -1109,6 +1058,13 @@ BString::Compare(const char* string, int32 length) const } +int +BString::CompareAt(size_t offset, const BString& string, int32 length) const +{ + return strncmp(String() + offset, string.String(), length); +} + + int BString::CompareChars(const BString& string, int32 charCount) const { @@ -1866,13 +1822,7 @@ BString::ReplaceCharsSet(const char* setOfChars, const char* with) // #pragma mark - Unchecked char access -#if __GNUC__ > 3 -BStringRef -BString::operator[](int32 index) -{ - return BStringRef(*this, index); -} -#else +#if __GNUC__ == 2 char& BString::operator[](int32 index) { @@ -1953,6 +1903,16 @@ BString::UnlockBuffer(int32 length) } +BString& +BString::SetCharAt(int32 pos, char to) +{ + if (pos < Length() && _MakeWritable() == B_OK) + fPrivateData[pos] = to; + + return *this; +} + + // #pragma mark - Uppercase <-> Lowercase diff --git a/src/preferences/keymap/KeyboardLayout.cpp b/src/preferences/keymap/KeyboardLayout.cpp index 2a7fd0b710..01ad5cf720 100644 --- a/src/preferences/keymap/KeyboardLayout.cpp +++ b/src/preferences/keymap/KeyboardLayout.cpp @@ -710,7 +710,7 @@ KeyboardLayout::_SubstituteVariables(BString& term, VariableMap& variables, for (; iterator != variables.end(); iterator++) { const BString& name = iterator->first; - if (!name.Compare(&term[index], name.Length()) + if (!term.CompareAt(index, name, name.Length()) && name.Length() > bestLength) { best = iterator; bestLength = name.Length(); @@ -723,12 +723,11 @@ KeyboardLayout::_SubstituteVariables(BString& term, VariableMap& variables, term.Insert(best->second.String(), index); } else { // variable has not been found - unknown = &term[index]; int32 length = 1; - while (isalpha(unknown[length])) { + while (isalpha(term[length + index])) { length++; } - unknown.Truncate(length); + term.Truncate(length + index); return false; } } diff --git a/src/preferences/mail/DNSQuery.cpp b/src/preferences/mail/DNSQuery.cpp index 0869701172..8826cdecd0 100644 --- a/src/preferences/mail/DNSQuery.cpp +++ b/src/preferences/mail/DNSQuery.cpp @@ -229,14 +229,14 @@ DNSTools::ConvertToDNSName(const BString& string) // set a counts to the dot diff = dot - 1 - lastDot; - outString[lastDot] = (char)diff; + outString.SetCharAt(lastDot, (char)diff); lastDot = dot; } } else lastDot = 0; diff = outString.CountChars() - 1 - lastDot; - outString[lastDot] = (char)diff; + outString.SetCharAt(lastDot, (char)diff); return outString; } @@ -259,7 +259,7 @@ DNSTools::ConvertFromDNSName(const BString& string) if (dot == 0) break; // set a "." - outString[nextDot] = '.'; + outString.SetCharAt(nextDot, '.'); nextDot+= dot + 1; } return outString;