// This returns an AtomicString because it is always passed as argument to // setValue() and setValue() takes an AtomicString in argument. AtomicString DOMTokenList::removeTokens(const AtomicString& input, const Vector<String>& tokens) { // Algorithm defined at // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#remove-a-token-from-a-string // New spec is at https://dom.spec.whatwg.org/#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<UChar>(input[position])) { // 6 position++; continue; // 6.3 } // Step 7 StringBuilder tokenBuilder; while (position < inputLength && isNotHTMLSpace<UChar>(input[position])) tokenBuilder.append(input[position++]); // Step 8 String token = tokenBuilder.toString(); if (tokens.contains(token)) { // Step 8.1 while (position < inputLength && isHTMLSpace<UChar>(input[position])) ++position; // Step 8.2 size_t j = output.length(); while (j > 0 && isHTMLSpace<UChar>(output[j - 1])) --j; output.resize(j); } else { output.append(token); // Step 9 } if (position < inputLength && !output.isEmpty()) output.append(' '); } size_t j = output.length(); if (j > 0 && isHTMLSpace<UChar>(output[j - 1])) output.resize(j - 1); return output.toAtomicString(); }
TEST(StringBuilderTest, ToString) { StringBuilder builder; builder.append("0123456789"); String string = builder.toString(); ASSERT_EQ(String("0123456789"), string); ASSERT_EQ(string.impl(), builder.toString().impl()); // Changing the StringBuilder should not affect the original result of toString(). builder.append("abcdefghijklmnopqrstuvwxyz"); ASSERT_EQ(String("0123456789"), string); // Changing the StringBuilder should not affect the original result of toString() in case the capacity is not changed. builder.reserveCapacity(200); string = builder.toString(); ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string); builder.append("ABC"); ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string); // Changing the original result of toString() should not affect the content of the StringBuilder. String string1 = builder.toString(); ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1); string1.append("DEF"); ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), builder.toString()); ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABCDEF"), string1); // Resizing the StringBuilder should not affect the original result of toString(). string1 = builder.toString(); builder.resize(10); builder.append("###"); ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1); }
String StructureShape::leastUpperBound(Vector<RefPtr<StructureShape>>* shapes) { if (!shapes->size()) return ""; StringBuilder lub; RefPtr<StructureShape> origin = shapes->at(0); lub.append("{"); for (auto iter = origin->m_fields.begin(), end = origin->m_fields.end(); iter != end; ++iter) { bool shouldAdd = true; for (size_t i = 1, size = shapes->size(); i < size; i++) { // If all other Shapes have the same field as origin, add it to the least upper bound. if (!shapes->at(i)->m_fields.contains(iter->key)) { shouldAdd = false; break; } } if (shouldAdd) lub.append(String(iter->key.get()) + String(", ")); } if (lub.length() >= 3) lub.resize(lub.length() - 2); // Remove the trailing ', ' lub.append("}"); return lub.toString(); }
static void stripTrailingNewline(StringBuilder& result) { // Remove one trailing newline; there's always one that's collapsed out by rendering. size_t size = result.length(); if (size && result[size - 1] == newlineCharacter) result.resize(size - 1); }
String StructureShape::stringRepresentation() { StringBuilder representation; RefPtr<StructureShape> curShape = this; representation.append('{'); while (curShape) { for (auto it = curShape->m_fields.begin(), end = curShape->m_fields.end(); it != end; ++it) { String prop((*it).get()); representation.append(prop); representation.append(", "); } if (curShape->m_proto) { String prot = makeString("__proto__ [", curShape->m_proto->m_constructorName, ']'); representation.append(prot); representation.append(", "); } curShape = curShape->m_proto; } if (representation.length() >= 3) representation.resize(representation.length() - 2); representation.append('}'); return representation.toString(); }
static String finishText(StringBuilder& result) { // Remove one trailing newline; there's always one that's collapsed out by rendering. size_t size = result.length(); if (size && result[size - 1] == '\n') result.resize(--size); return result.toString(); }
TEST(StringBuilderTest, Resize) { StringBuilder builder; builder.append("0123456789"); builder.resize(10); EXPECT_EQ(10U, builder.length()); expectBuilderContent("0123456789", builder); builder.resize(8); EXPECT_EQ(8U, builder.length()); expectBuilderContent("01234567", builder); builder.toString(); builder.resize(7); EXPECT_EQ(7U, builder.length()); expectBuilderContent("0123456", builder); builder.resize(0); expectEmpty(builder); }
String WebMediaSessionManager::toString(ConfigurationTasks tasks) { StringBuilder string; if (tasks & InitialConfigurationTask) string.append("InitialConfigurationTask + "); if (tasks & TargetClientsConfigurationTask) string.append("TargetClientsConfigurationTask + "); if (tasks & TargetMonitoringConfigurationTask) string.append("TargetMonitoringConfigurationTask + "); if (string.isEmpty()) string.append("NoTask"); else string.resize(string.length() - 2); return string.toString(); }
String StructureShape::stringRepresentation() { StringBuilder representation; representation.append("{"); for (auto iter = m_fields.begin(), end = m_fields.end(); iter != end; ++iter) { String prop(iter->key.get()); representation.append(prop); representation.append(", "); } if (representation.length() >= 3) representation.resize(representation.length() - 2); representation.append("}"); return representation.toString(); }
String ScrollingCoordinator::synchronousScrollingReasonsAsText(SynchronousScrollingReasons reasons) { StringBuilder stringBuilder; if (reasons & ScrollingCoordinator::ForcedOnMainThread) stringBuilder.append("Forced on main thread, "); if (reasons & ScrollingCoordinator::HasSlowRepaintObjects) stringBuilder.append("Has slow repaint objects, "); if (reasons & ScrollingCoordinator::HasViewportConstrainedObjectsWithoutSupportingFixedLayers) stringBuilder.append("Has viewport constrained objects without supporting fixed layers, "); if (reasons & ScrollingCoordinator::HasNonLayerViewportConstrainedObjects) stringBuilder.append("Has non-layer viewport-constrained objects, "); if (reasons & ScrollingCoordinator::IsImageDocument) stringBuilder.append("Is image document, "); if (stringBuilder.length()) stringBuilder.resize(stringBuilder.length() - 2); return stringBuilder.toString(); }
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(); }
static String mediaProducerStateString(MediaProducer::MediaStateFlags flags) { StringBuilder string; if (flags & MediaProducer::IsPlayingAudio) string.append("IsPlayingAudio + "); if (flags & MediaProducer::IsPlayingVideo) string.append("IsPlayingVideo + "); if (flags & MediaProducer::IsPlayingToExternalDevice) string.append("IsPlayingToExternalDevice + "); if (flags & MediaProducer::RequiresPlaybackTargetMonitoring) string.append("RequiresPlaybackTargetMonitoring + "); if (flags & MediaProducer::ExternalDeviceAutoPlayCandidate) string.append("ExternalDeviceAutoPlayCandidate + "); if (string.isEmpty()) string.append("IsNotPlaying"); else string.resize(string.length() - 2); return string.toString(); }
static bool saveResultToString(xmlDocPtr resultDoc, xsltStylesheetPtr sheet, String& resultString) { xmlOutputBufferPtr outputBuf = xmlAllocOutputBuffer(0); if (!outputBuf) return false; StringBuilder resultBuilder; outputBuf->context = &resultBuilder; outputBuf->writecallback = writeToStringBuilder; int retval = xsltSaveResultTo(outputBuf, resultDoc, sheet); xmlOutputBufferClose(outputBuf); if (retval < 0) return false; // Workaround for <http://bugzilla.gnome.org/show_bug.cgi?id=495668>: libxslt appends an extra line feed to the result. if (resultBuilder.length() > 0 && resultBuilder[resultBuilder.length() - 1] == '\n') resultBuilder.resize(resultBuilder.length() - 1); resultString = resultBuilder.toString(); return true; }