本文整理汇总了C++中StringBuilder::toStringPreserveCapacity方法的典型用法代码示例。如果您正苦于以下问题:C++ StringBuilder::toStringPreserveCapacity方法的具体用法?C++ StringBuilder::toStringPreserveCapacity怎么用?C++ StringBuilder::toStringPreserveCapacity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringBuilder
的用法示例。
在下文中一共展示了StringBuilder::toStringPreserveCapacity方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: removeToken
String DOMTokenList::removeToken(const AtomicString& input, const AtomicString& token)
{
// Algorithm defined at http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#remove-a-token-from-a-string
unsigned inputLength = input.length();
StringBuilder output; // 3
output.reserveCapacity(inputLength);
unsigned position = 0; // 4
// Step 5
while (position < inputLength) {
if (isHTMLSpace(input[position])) { // 6
output.append(input[position++]); // 6.1, 6.2
continue; // 6.3
}
// Step 7
StringBuilder s;
while (position < inputLength && isNotHTMLSpace(input[position]))
s.append(input[position++]);
// Step 8
if (s.toStringPreserveCapacity() == token) {
// Step 8.1
while (position < inputLength && isHTMLSpace(input[position]))
++position;
// Step 8.2
size_t j = output.length();
while (j > 0 && isHTMLSpace(output[j - 1]))
--j;
output.resize(j);
// Step 8.3
if (position < inputLength && !output.isEmpty())
output.append(' ');
} else
output.append(s.toStringPreserveCapacity()); // Step 9
}
return output.toString();
}
示例2:
TEST(StringBuilderTest, ToStringPreserveCapacity)
{
StringBuilder builder;
builder.append("0123456789");
String string = builder.toStringPreserveCapacity();
ASSERT_EQ(String("0123456789"), string);
ASSERT_EQ(string.impl(), builder.toStringPreserveCapacity().impl());
ASSERT_EQ(string.characters(), builder.characters());
// Changing the StringBuilder should not affect the original result of toStringPreserveCapacity().
builder.append("abcdefghijklmnopqrstuvwxyz");
ASSERT_EQ(String("0123456789"), string);
// Changing the StringBuilder should not affect the original result of toStringPreserveCapacity() in case the capacity is not changed.
builder.reserveCapacity(200);
string = builder.toStringPreserveCapacity();
ASSERT_EQ(string.characters(), builder.characters());
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
builder.append("ABC");
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
// Changing the original result of toStringPreserveCapacity() should not affect the content of the StringBuilder.
String string1 = builder.toStringPreserveCapacity();
ASSERT_EQ(string1.characters(), builder.characters());
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
string1.append("DEF");
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), builder.toStringPreserveCapacity());
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABCDEF"), string1);
// Resizing the StringBuilder should not affect the original result of toStringPreserveCapacity().
string1 = builder.toStringPreserveCapacity();
ASSERT_EQ(string.characters(), builder.characters());
builder.resize(10);
builder.append("###");
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
}