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-09 23:12:52 +01:00
parent 92b9c8649b
commit 3eac8208df
8 changed files with 36 additions and 98 deletions

View File

@ -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

View File

@ -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');

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;

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
@ -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

View File

@ -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;
}
}

View File

@ -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;