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.
This commit is contained in:
Adrien Destugues
2017-01-11 20:50:41 +01:00
parent 92b9c8649b
commit 3eac8208df
8 changed files with 36 additions and 98 deletions
+5 -26
View File
@@ -183,6 +183,9 @@ public:
int Compare(const BString& string, int32 length) const; int Compare(const BString& string, int32 length) const;
int Compare(const char* 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, int CompareChars(const BString& string,
int32 charCount) const; int32 charCount) const;
int CompareChars(const char* string, int CompareChars(const char* string,
@@ -293,9 +296,7 @@ public:
// Unchecked char access // Unchecked char access
char operator[](int32 index) const; char operator[](int32 index) const;
#if __GNUC__ > 3 #if __GNUC__ == 2
BStringRef operator[](int32 index);
#else
char& operator[](int32 index); char& operator[](int32 index);
#endif #endif
@@ -308,6 +309,7 @@ public:
// Fast low-level manipulation // Fast low-level manipulation
char* LockBuffer(int32 maxLength); char* LockBuffer(int32 maxLength);
BString& UnlockBuffer(int32 length = -1); BString& UnlockBuffer(int32 length = -1);
BString& SetCharAt(int32 pos, char to);
// Upercase <-> Lowercase // Upercase <-> Lowercase
BString& ToLower(); BString& ToLower();
@@ -350,7 +352,6 @@ public:
private: private:
class PosVect; class PosVect;
friend class BStringRef;
enum PrivateDataTag { enum PrivateDataTag {
PRIVATE_DATA 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 #endif // _B_STRING_H
+4 -4
View File
@@ -288,13 +288,13 @@ ExpressionTextView::SetValue(BString value)
if (digit != 10) if (digit != 10)
break; break;
value[offset] = '0'; value.SetCharAt(offset, '0');
} }
if (digit == 10) { if (digit == 10) {
// carry over, shift the result // carry over, shift the result
if (value[firstDigit + 1] == '.') { if (value[firstDigit + 1] == '.') {
value[firstDigit + 1] = '0'; value.SetCharAt(firstDigit + 1, '0');
value[firstDigit] = '.'; value.SetCharAt(firstDigit, '.');
} }
value.Insert('1', 1, firstDigit); value.Insert('1', 1, firstDigit);
@@ -311,7 +311,7 @@ ExpressionTextView::SetValue(BString value)
value << 'E' << exponent; value << 'E' << exponent;
} else { } else {
// increase the current digit value with one // increase the current digit value with one
value[offset] = char(digit + 48); value.SetCharAt(offset, char(digit + 48));
// set offset to last digit // set offset to last digit
offset = value.FindFirst('E'); offset = value.FindFirst('E');
+1 -1
View File
@@ -2435,7 +2435,7 @@ BrowserWindow::_EncodeURIComponent(const BString& search)
for (int32 i = 0; i < result.Length(); i++) { for (int32 i = 0; i < result.Length(); i++) {
if (escCharList.FindFirst(result[i]) != B_ERROR) { if (escCharList.FindFirst(result[i]) != B_ERROR) {
sprintf(hexcode, "%02X", (unsigned int)result[i]); sprintf(hexcode, "%02X", (unsigned int)result[i]);
result[i] = '%'; result.SetCharAt(i, '%');
result.Insert(hexcode, i + 1); result.Insert(hexcode, i + 1);
i += 2; i += 2;
} }
+1 -1
View File
@@ -212,7 +212,7 @@ BFormattingConventions::BFormattingConventions(
for (int t = 0; t < B_TIME_FORMAT_STYLE_COUNT; ++t) { for (int t = 0; t < B_TIME_FORMAT_STYLE_COUNT; ++t) {
fCachedDateTimeFormats[s][t] = other.fCachedDateFormats[s][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) { for (int s = 0; s < B_TIME_FORMAT_STYLE_COUNT; ++s) {
+1 -1
View File
@@ -247,7 +247,7 @@ HaikuMailFormatFilter::_RemoveExtraWhitespace(BString& name)
if (i == remove + 1 || i == name.Length()) if (i == remove + 1 || i == name.Length())
remove++; remove++;
else else
name[i - spaces] = ' '; name.SetCharAt(i - spaces, ' ');
name.Remove(i - remove, remove); name.Remove(i - remove, remove);
i -= remove; i -= remove;
spaces = 0; spaces = 0;
+18 -58
View File
@@ -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 // #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 int
BString::CompareChars(const BString& string, int32 charCount) const 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 // #pragma mark - Unchecked char access
#if __GNUC__ > 3 #if __GNUC__ == 2
BStringRef
BString::operator[](int32 index)
{
return BStringRef(*this, index);
}
#else
char& char&
BString::operator[](int32 index) 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 // #pragma mark - Uppercase <-> Lowercase
+3 -4
View File
@@ -710,7 +710,7 @@ KeyboardLayout::_SubstituteVariables(BString& term, VariableMap& variables,
for (; iterator != variables.end(); iterator++) { for (; iterator != variables.end(); iterator++) {
const BString& name = iterator->first; const BString& name = iterator->first;
if (!name.Compare(&term[index], name.Length()) if (!term.CompareAt(index, name, name.Length())
&& name.Length() > bestLength) { && name.Length() > bestLength) {
best = iterator; best = iterator;
bestLength = name.Length(); bestLength = name.Length();
@@ -723,12 +723,11 @@ KeyboardLayout::_SubstituteVariables(BString& term, VariableMap& variables,
term.Insert(best->second.String(), index); term.Insert(best->second.String(), index);
} else { } else {
// variable has not been found // variable has not been found
unknown = &term[index];
int32 length = 1; int32 length = 1;
while (isalpha(unknown[length])) { while (isalpha(term[length + index])) {
length++; length++;
} }
unknown.Truncate(length); term.Truncate(length + index);
return false; return false;
} }
} }
+3 -3
View File
@@ -229,14 +229,14 @@ DNSTools::ConvertToDNSName(const BString& string)
// set a counts to the dot // set a counts to the dot
diff = dot - 1 - lastDot; diff = dot - 1 - lastDot;
outString[lastDot] = (char)diff; outString.SetCharAt(lastDot, (char)diff);
lastDot = dot; lastDot = dot;
} }
} else } else
lastDot = 0; lastDot = 0;
diff = outString.CountChars() - 1 - lastDot; diff = outString.CountChars() - 1 - lastDot;
outString[lastDot] = (char)diff; outString.SetCharAt(lastDot, (char)diff);
return outString; return outString;
} }
@@ -259,7 +259,7 @@ DNSTools::ConvertFromDNSName(const BString& string)
if (dot == 0) if (dot == 0)
break; break;
// set a "." // set a "."
outString[nextDot] = '.'; outString.SetCharAt(nextDot, '.');
nextDot+= dot + 1; nextDot+= dot + 1;
} }
return outString; return outString;