Skip to content

Commit 7a78d63

Browse files
committed
Reduce use of std::span::subspan() by leveraging ParsingUtilities
https://bugs.webkit.org/show_bug.cgi?id=285271 Reviewed by Darin Adler. A lot more code can be ported but this is a step in this direction. * Source/JavaScriptCore/API/JSScript.mm: (-[JSScript readCache]): * Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp: (JSC::CallArguments::CallArguments): * Source/JavaScriptCore/runtime/CachedTypes.cpp: (JSC::Encoder::release): * Source/JavaScriptCore/runtime/IntlLocale.cpp: (JSC::LocaleIDBuilder::overrideLanguageScriptRegion): * Source/JavaScriptCore/runtime/IntlObject.cpp: (JSC::canonicalizeUnicodeExtensionsAfterICULocaleCanonicalization): * Source/JavaScriptCore/runtime/JSGenericTypedArrayViewConstructor.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/runtime/JSGenericTypedArrayViewPrototype.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp: (JSC::jsBinaryIntegerLiteral): (JSC::jsOctalIntegerLiteral): (JSC::jsHexIntegerLiteral): (JSC::toDouble): (JSC::parseFloat): * Source/JavaScriptCore/runtime/JSONObject.cpp: (JSC::bufferMode>::append): * Source/JavaScriptCore/runtime/JSStringJoiner.cpp: (JSC::appendStringToDataWithOneCharacterSeparatorRepeatedly): * Source/JavaScriptCore/runtime/ParseInt.h: (JSC::isStrWhiteSpace): * Source/WTF/WTF.xcodeproj/project.pbxproj: * Source/WTF/wtf/CMakeLists.txt: * Source/WTF/wtf/DateMath.cpp: (WTF::parseES5DatePortion): (WTF::parseES5TimePortion): (WTF::parseDate): * Source/WTF/wtf/text/ParsingUtilities.h: Renamed from Source/WebCore/html/parser/ParsingUtilities.h. (WTF::isNotASCIISpace): (WTF::skipExactly): (WTF::characterPredicate): (WTF::skipUntil): (WTF::skipWhile): (WTF::skipExactlyIgnoringASCIICase): (WTF::skipLettersExactlyIgnoringASCIICase): (WTF::skipCharactersExactly): (WTF::consumeSingleElement): (WTF::consumeSpan): * Source/WebCore/WebCore.xcodeproj/project.pbxproj: * Source/WebCore/bindings/js/SerializedScriptValue.cpp: (WebCore::CloneDeserializer::readString): * Source/WebCore/css/parser/CSSCustomPropertySyntax.cpp: * Source/WebCore/css/parser/CSSPropertyParser.cpp: * Source/WebCore/css/parser/CSSPropertyParserConsumer+Font.cpp: * Source/WebCore/html/HTMLFontElement.cpp: (WebCore::parseFontSize): * Source/WebCore/html/parser/HTMLDocumentParserFastPath.cpp: * Source/WebCore/html/parser/HTMLParserIdioms.cpp: (WebCore::parseHTMLIntegerInternal): (WebCore::isHTMLSpaceOrDelimiter): (WebCore::parseHTMLListOfOfFloatingPointNumberValuesInternal): (WebCore::parseHTTPRefreshInternal): * Source/WebCore/html/parser/HTMLSrcsetParser.cpp: * Source/WebCore/html/track/VTTScanner.h: (WebCore::characterPredicate): (WebCore::VTTScanner::characterPredicate): Deleted. * Source/WebCore/loader/LinkHeader.cpp: * Source/WebCore/loader/ResourceCryptographicDigest.cpp: * Source/WebCore/loader/SubresourceIntegrity.cpp: * Source/WebCore/loader/appcache/ApplicationCacheManifestParser.cpp: * Source/WebCore/page/csp/ContentSecurityPolicy.cpp: * Source/WebCore/page/csp/ContentSecurityPolicyDirectiveList.cpp: * Source/WebCore/page/csp/ContentSecurityPolicyMediaListDirective.cpp: * Source/WebCore/page/csp/ContentSecurityPolicySourceList.cpp: * Source/WebCore/page/csp/ContentSecurityPolicyTrustedTypesDirective.cpp: * Source/WebCore/platform/DateComponents.cpp: * Source/WebCore/platform/network/RFC8941.cpp: * Source/WebCore/svg/SVGParserUtilities.h: Canonical link: https://commits.webkit.org/288353@main
1 parent 62a01cd commit 7a78d63

38 files changed

Lines changed: 192 additions & 183 deletions

‎Source/JavaScriptCore/API/JSScript.mm‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#import <wtf/SHA1.h>
4646
#import <wtf/SafeStrerror.h>
4747
#import <wtf/Scope.h>
48+
#import <wtf/StdLibExtras.h>
4849
#import <wtf/WeakObjCPtr.h>
4950
#import <wtf/spi/darwin/DataVaultSPI.h>
5051
#import <wtf/text/MakeString.h>
@@ -184,12 +185,12 @@ - (void)readCache
184185

185186
SHA1::Digest computedHash;
186187
SHA1 sha1;
187-
sha1.addBytes(fileData.subspan(0, fileDataSize));
188+
sha1.addBytes(fileData.first(fileDataSize));
188189
sha1.computeHash(computedHash);
189190

190191
SHA1::Digest fileHash;
191192
auto hashSpan = fileData.subspan(fileDataSize, sizeof(SHA1::Digest));
192-
memcpy(&fileHash, hashSpan.data(), hashSpan.size());
193+
memcpySpan(std::span { fileHash }, hashSpan);
193194

194195
if (computedHash != fileHash) {
195196
FileSystem::deleteFile(cacheFilename);

‎Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ CallArguments::CallArguments(BytecodeGenerator& generator, ArgumentsNode* argume
12391239
// We initialize 0 based on offset. And adjust m_argv based on that.
12401240
if ((-m_allocatedRegisters[1]->index() + CallFrame::headerSizeInRegisters) % stackAlignmentRegisters()) {
12411241
m_allocatedRegisters[0] = generator.newTemporary();
1242-
m_argv = m_allocatedRegisters.mutableSpan().subspan(0, argumentCountIncludingThis);
1242+
m_argv = m_allocatedRegisters.mutableSpan().first(argumentCountIncludingThis);
12431243
} else
12441244
m_argv = m_allocatedRegisters.mutableSpan().subspan(1, argumentCountIncludingThis);
12451245
}

‎Source/JavaScriptCore/runtime/CachedTypes.cpp‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <wtf/StdLibExtras.h>
5050
#include <wtf/UUID.h>
5151
#include <wtf/text/AtomStringImpl.h>
52+
#include <wtf/text/ParsingUtilities.h>
5253

5354
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
5455

@@ -166,12 +167,9 @@ class Encoder {
166167
size_t size = m_baseOffset + m_currentPage->size();
167168
auto buffer = MallocSpan<uint8_t, VMMalloc>::malloc(size);
168169
auto bufferSpan = buffer.mutableSpan();
169-
unsigned offset = 0;
170-
for (const auto& page : m_pages) {
171-
memcpySpan(bufferSpan.subspan(offset), page.span());
172-
offset += page.size();
173-
}
174-
RELEASE_ASSERT(offset == size);
170+
for (const auto& page : m_pages)
171+
memcpySpan(consumeSpan(bufferSpan, page.size()), page.span());
172+
RELEASE_ASSERT(bufferSpan.empty());
175173
return CachedBytecode::create(WTFMove(buffer), WTFMove(m_leafExecutables));
176174
}
177175

‎Source/JavaScriptCore/runtime/IntlLocale.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void LocaleIDBuilder::overrideLanguageScriptRegion(StringView language, StringVi
113113
{
114114
unsigned length = strlen(m_buffer.data());
115115

116-
StringView localeIDView { m_buffer.subspan(0, length) };
116+
StringView localeIDView { m_buffer.span().first(length) };
117117

118118
auto endOfLanguageScriptRegionVariant = localeIDView.find(ULOC_KEYWORD_SEPARATOR);
119119
if (endOfLanguageScriptRegionVariant == notFound)

‎Source/JavaScriptCore/runtime/IntlObject.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ Vector<char, 32> canonicalizeUnicodeExtensionsAfterICULocaleCanonicalization(Vec
351351
end++;
352352
}
353353

354-
Vector<char, 32> result(buffer.subspan(0, extensionIndex + 2)); // "-u" is included.
354+
Vector<char, 32> result(buffer.span().first(extensionIndex + 2)); // "-u" is included.
355355
StringView extension = locale.substring(extensionIndex, extensionLength);
356356
ASSERT(extension.is8Bit());
357357
auto subtags = unicodeExtensionComponents(extension);

‎Source/JavaScriptCore/runtime/JSGenericTypedArrayViewConstructor.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ JSC_DEFINE_HOST_FUNCTION(uint8ArrayConstructorFromBase64, (JSGlobalObject* globa
9999
JSUint8Array* uint8Array = JSUint8Array::createUninitialized(globalObject, globalObject->typedArrayStructure(TypeUint8, false), writeLength);
100100
RETURN_IF_EXCEPTION(scope, { });
101101

102-
uint8_t* data = uint8Array->typedVector();
103-
memcpySpan(std::span { data, data + writeLength }, output.span().subspan(0, writeLength));
102+
memcpySpan(uint8Array->typedSpan(), output.span().first(writeLength));
104103
return JSValue::encode(uint8Array);
105104
}
106105

‎Source/JavaScriptCore/runtime/JSGenericTypedArrayViewPrototype.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ JSC_DEFINE_HOST_FUNCTION(uint8ArrayPrototypeSetFromHex, (JSGlobalObject* globalO
140140

141141
bool success = false;
142142
if (view.is8Bit())
143-
success = decodeHex(view.span8().subspan(0, readCount), result) == WTF::notFound;
143+
success = decodeHex(view.span8().first(readCount), result) == WTF::notFound;
144144
else
145-
success = decodeHex(view.span16().subspan(0, readCount), result) == WTF::notFound;
145+
success = decodeHex(view.span16().first(readCount), result) == WTF::notFound;
146146

147147
if (UNLIKELY(!success))
148148
return JSValue::encode(throwSyntaxError(globalObject, scope, "Uint8Array.prototype.setFromHex requires a string containing only \"0123456789abcdefABCDEF\""_s));

‎Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <wtf/Assertions.h>
4747
#include <wtf/HexNumber.h>
4848
#include <wtf/dtoa.h>
49+
#include <wtf/text/ParsingUtilities.h>
4950
#include <wtf/text/StringBuilder.h>
5051

5152
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
@@ -265,8 +266,7 @@ static double jsBinaryIntegerLiteral(std::span<const CharType>& data)
265266
auto firstDigitPosition = data;
266267
double number = 0;
267268
while (true) {
268-
number = number * 2 + (data.front() - '0');
269-
data = data.subspan(1);
269+
number = number * 2 + (consumeSingleElement(data) - '0');
270270
if (data.empty())
271271
break;
272272
if (!isASCIIBinaryDigit(data.front()))
@@ -287,8 +287,7 @@ static double jsOctalIntegerLiteral(std::span<const CharType>& data)
287287
auto firstDigitPosition = data;
288288
double number = 0;
289289
while (true) {
290-
number = number * 8 + (data.front() - '0');
291-
data = data.subspan(1);
290+
number = number * 8 + (consumeSingleElement(data) - '0');
292291
if (data.empty())
293292
break;
294293
if (!isASCIIOctalDigit(data.front()))
@@ -309,8 +308,7 @@ static double jsHexIntegerLiteral(std::span<const CharType>& data)
309308
auto firstDigitPosition = data;
310309
double number = 0;
311310
while (true) {
312-
number = number * 16 + toASCIIHexValue(data.front());
313-
data = data.subspan(1);
311+
number = number * 16 + toASCIIHexValue(consumeSingleElement(data));
314312
if (data.empty())
315313
break;
316314
if (!isASCIIHexDigit(data.front()))
@@ -367,10 +365,7 @@ template <typename CharacterType>
367365
static double toDouble(std::span<const CharacterType> characters)
368366
{
369367
// Skip leading white space.
370-
for (; !characters.empty(); characters = characters.subspan(1)) {
371-
if (!isStrWhiteSpace(characters.front()))
372-
break;
373-
}
368+
skipWhile<isStrWhiteSpace>(characters);
374369

375370
// Empty string.
376371
if (characters.empty())
@@ -390,10 +385,8 @@ static double toDouble(std::span<const CharacterType> characters)
390385
number = jsStrDecimalLiteral(characters);
391386

392387
// Allow trailing white space.
393-
for (; !characters.empty(); characters = characters.subspan(1)) {
394-
if (!isStrWhiteSpace(characters.front()))
395-
break;
396-
}
388+
skipWhile<isStrWhiteSpace>(characters);
389+
397390
if (!characters.empty())
398391
return PNaN;
399392

@@ -445,10 +438,7 @@ static double parseFloat(StringView s)
445438
auto data = s.span8();
446439

447440
// Skip leading white space.
448-
for (; !data.empty(); data = data.subspan(1)) {
449-
if (!isStrWhiteSpace(data.front()))
450-
break;
451-
}
441+
skipWhile<isStrWhiteSpace>(data);
452442

453443
// Empty string.
454444
if (data.empty())
@@ -460,10 +450,7 @@ static double parseFloat(StringView s)
460450
auto data = s.span16();
461451

462452
// Skip leading white space.
463-
for (; !data.empty(); data = data.subspan(1)) {
464-
if (!isStrWhiteSpace(data.front()))
465-
break;
466-
}
453+
skipWhile<isStrWhiteSpace>(data);
467454

468455
// Empty string.
469456
if (data.empty())

‎Source/JavaScriptCore/runtime/JSONObject.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <wtf/dragonbox/dragonbox_to_chars.h>
4444
#include <wtf/text/EscapedFormsForJSON.h>
4545
#include <wtf/text/MakeString.h>
46+
#include <wtf/text/ParsingUtilities.h>
4647
#include <wtf/text/StringBuilder.h>
4748
#include <wtf/text/StringBuilderJSON.h>
4849
#include <wtf/text/StringCommon.h>
@@ -1306,8 +1307,7 @@ void FastStringifier<CharType, bufferMode>::append(JSValue value)
13061307
WTF::appendEscapedJSONStringContent(output, string.data.span16());
13071308
} else
13081309
WTF::appendEscapedJSONStringContent(output, string.data.span8());
1309-
output[0] = '"';
1310-
output = output.subspan(1);
1310+
consumeSingleElement(output) = '"';
13111311
m_length = output.data() - buffer();
13121312
return;
13131313
}

‎Source/JavaScriptCore/runtime/JSStringJoiner.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "JSStringJoiner.h"
2828

2929
#include "JSCJSValueInlines.h"
30+
#include <wtf/text/ParsingUtilities.h>
3031

3132
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
3233

@@ -95,8 +96,7 @@ static inline void appendStringToDataWithOneCharacterSeparatorRepeatedly(std::sp
9596
#endif
9697

9798
while (count--) {
98-
data[0] = separatorCharacter;
99-
data = data.subspan(1);
99+
consumeSingleElement(data) = separatorCharacter;
100100
appendStringToData(data, string);
101101
}
102102
}

0 commit comments

Comments
 (0)