Skip to content

Commit 246cf73

Browse files
anonrigtargos
authored andcommitted
lib,src: replace toUSVString with toWellFormed()
PR-URL: #47342 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 4b1bed0 commit 246cf73

File tree

9 files changed

+31
-128
lines changed

9 files changed

+31
-128
lines changed

benchmark/url/usvstring.js

-27
This file was deleted.

benchmark/util/to-usv-string.js

-21
This file was deleted.

lib/internal/file.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
DateNow,
55
NumberIsNaN,
66
ObjectDefineProperties,
7+
StringPrototypeToWellFormed,
78
SymbolToStringTag,
89
} = primordials;
910

@@ -15,7 +16,6 @@ const {
1516
customInspectSymbol: kInspect,
1617
kEnumerableProperty,
1718
kEmptyObject,
18-
toUSVString,
1919
} = require('internal/util');
2020

2121
const {
@@ -55,7 +55,7 @@ class File extends Blob {
5555
lastModified = DateNow();
5656
}
5757

58-
this.#name = toUSVString(fileName);
58+
this.#name = StringPrototypeToWellFormed(`${fileName}`);
5959
this.#lastModified = lastModified;
6060
}
6161

lib/internal/url.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
StringPrototypeIndexOf,
2727
StringPrototypeSlice,
2828
StringPrototypeStartsWith,
29+
StringPrototypeToWellFormed,
2930
Symbol,
3031
SymbolIterator,
3132
SymbolToStringTag,
@@ -42,7 +43,6 @@ const {
4243
const {
4344
getConstructorOf,
4445
removeColors,
45-
toUSVString,
4646
kEnumerableProperty,
4747
SideEffectFreeRegExpPrototypeSymbolReplace,
4848
} = require('internal/util');
@@ -366,7 +366,11 @@ class URLSearchParams {
366366
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]');
367367
}
368368
// Append (innerSequence[0], innerSequence[1]) to querys list.
369-
ArrayPrototypePush(this.#searchParams, toUSVString(pair[0]), toUSVString(pair[1]));
369+
ArrayPrototypePush(
370+
this.#searchParams,
371+
StringPrototypeToWellFormed(`${pair[0]}`),
372+
StringPrototypeToWellFormed(`${pair[1]}`),
373+
);
370374
} else {
371375
if (((typeof pair !== 'object' && typeof pair !== 'function') ||
372376
typeof pair[SymbolIterator] !== 'function')) {
@@ -377,7 +381,7 @@ class URLSearchParams {
377381

378382
for (const element of pair) {
379383
length++;
380-
ArrayPrototypePush(this.#searchParams, toUSVString(element));
384+
ArrayPrototypePush(this.#searchParams, StringPrototypeToWellFormed(`${element}`));
381385
}
382386

383387
// If innerSequence's size is not 2, then throw a TypeError.
@@ -395,8 +399,8 @@ class URLSearchParams {
395399
const key = keys[i];
396400
const desc = ReflectGetOwnPropertyDescriptor(init, key);
397401
if (desc !== undefined && desc.enumerable) {
398-
const typedKey = toUSVString(key);
399-
const typedValue = toUSVString(init[key]);
402+
const typedKey = StringPrototypeToWellFormed(key);
403+
const typedValue = StringPrototypeToWellFormed(`${init[key]}`);
400404

401405
// Two different keys may become the same USVString after normalization.
402406
// In that case, we retain the later one. Refer to WPT.
@@ -413,7 +417,7 @@ class URLSearchParams {
413417
}
414418
} else {
415419
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
416-
init = toUSVString(init);
420+
init = StringPrototypeToWellFormed(`${init}`);
417421
this.#searchParams = init ? parseParams(init) : [];
418422
}
419423
}
@@ -468,8 +472,8 @@ class URLSearchParams {
468472
throw new ERR_MISSING_ARGS('name', 'value');
469473
}
470474

471-
name = toUSVString(name);
472-
value = toUSVString(value);
475+
name = StringPrototypeToWellFormed(`${name}`);
476+
value = StringPrototypeToWellFormed(`${value}`);
473477
ArrayPrototypePush(this.#searchParams, name, value);
474478
if (this.#context) {
475479
this.#context.search = this.toString();
@@ -485,10 +489,10 @@ class URLSearchParams {
485489
}
486490

487491
const list = this.#searchParams;
488-
name = toUSVString(name);
492+
name = StringPrototypeToWellFormed(`${name}`);
489493

490494
if (value !== undefined) {
491-
value = toUSVString(value);
495+
value = StringPrototypeToWellFormed(`${value}`);
492496
for (let i = 0; i < list.length;) {
493497
if (list[i] === name && list[i + 1] === value) {
494498
list.splice(i, 2);
@@ -519,7 +523,7 @@ class URLSearchParams {
519523
}
520524

521525
const list = this.#searchParams;
522-
name = toUSVString(name);
526+
name = StringPrototypeToWellFormed(`${name}`);
523527
for (let i = 0; i < list.length; i += 2) {
524528
if (list[i] === name) {
525529
return list[i + 1];
@@ -538,7 +542,7 @@ class URLSearchParams {
538542

539543
const list = this.#searchParams;
540544
const values = [];
541-
name = toUSVString(name);
545+
name = StringPrototypeToWellFormed(`${name}`);
542546
for (let i = 0; i < list.length; i += 2) {
543547
if (list[i] === name) {
544548
values.push(list[i + 1]);
@@ -556,10 +560,10 @@ class URLSearchParams {
556560
}
557561

558562
const list = this.#searchParams;
559-
name = toUSVString(name);
563+
name = StringPrototypeToWellFormed(`${name}`);
560564

561565
if (value !== undefined) {
562-
value = toUSVString(value);
566+
value = StringPrototypeToWellFormed(`${value}`);
563567
}
564568

565569
for (let i = 0; i < list.length; i += 2) {
@@ -582,8 +586,8 @@ class URLSearchParams {
582586
}
583587

584588
const list = this.#searchParams;
585-
name = toUSVString(name);
586-
value = toUSVString(value);
589+
name = StringPrototypeToWellFormed(`${name}`);
590+
value = StringPrototypeToWellFormed(`${value}`);
587591

588592
// If there are any name-value pairs whose name is `name`, in `list`, set
589593
// the value of the first such name-value pair to `value` and remove the
@@ -773,7 +777,7 @@ class URL {
773777
throw new ERR_MISSING_ARGS('url');
774778
}
775779

776-
// toUSVString is not needed.
780+
// StringPrototypeToWellFormed is not needed.
777781
input = `${input}`;
778782

779783
if (base !== undefined) {
@@ -1006,7 +1010,7 @@ class URL {
10061010
}
10071011

10081012
set search(value) {
1009-
const href = bindingUrl.update(this.#context.href, updateActions.kSearch, toUSVString(value));
1013+
const href = bindingUrl.update(this.#context.href, updateActions.kSearch, StringPrototypeToWellFormed(`${value}`));
10101014
if (href) {
10111015
this.#updateContext(href);
10121016
}
@@ -1297,15 +1301,15 @@ function domainToASCII(domain) {
12971301
if (arguments.length < 1)
12981302
throw new ERR_MISSING_ARGS('domain');
12991303

1300-
// toUSVString is not needed.
1304+
// StringPrototypeToWellFormed is not needed.
13011305
return bindingUrl.domainToASCII(`${domain}`);
13021306
}
13031307

13041308
function domainToUnicode(domain) {
13051309
if (arguments.length < 1)
13061310
throw new ERR_MISSING_ARGS('domain');
13071311

1308-
// toUSVString is not needed.
1312+
// StringPrototypeToWellFormed is not needed.
13091313
return bindingUrl.domainToUnicode(`${domain}`);
13101314
}
13111315

@@ -1501,7 +1505,6 @@ function getURLOrigin(url) {
15011505
}
15021506

15031507
module.exports = {
1504-
toUSVString,
15051508
fileURLToPath,
15061509
pathToFileURL,
15071510
toPathIfFileURL,

lib/internal/util.js

-14
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const {
6262
decorated_private_symbol,
6363
},
6464
sleep: _sleep,
65-
toUSVString: _toUSVString,
6665
} = internalBinding('util');
6766
const { isNativeError, isPromise } = internalBinding('types');
6867
const { getOptionValue } = require('internal/options');
@@ -73,18 +72,6 @@ const experimentalWarnings = new SafeSet();
7372

7473
const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
7574

76-
const unpairedSurrogateRe =
77-
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
78-
function toUSVString(val) {
79-
const str = `${val}`;
80-
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
81-
// slower than `unpairedSurrogateRe.exec()`.
82-
const match = RegExpPrototypeExec(unpairedSurrogateRe, str);
83-
if (!match)
84-
return str;
85-
return _toUSVString(str, match.index);
86-
}
87-
8875
let uvBinding;
8976

9077
function lazyUv() {
@@ -913,7 +900,6 @@ module.exports = {
913900
sleep,
914901
spliceOne,
915902
setupCoverageHooks,
916-
toUSVString,
917903
removeColors,
918904

919905
// Symbol used to customize promisify conversion

lib/util.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
ObjectValues,
4545
ReflectApply,
4646
StringPrototypePadStart,
47+
StringPrototypeToWellFormed,
4748
} = primordials;
4849

4950
const {
@@ -75,7 +76,6 @@ const {
7576
getSystemErrorMap,
7677
getSystemErrorName: internalErrorName,
7778
promisify,
78-
toUSVString,
7979
defineLazyProperties,
8080
} = require('internal/util');
8181

@@ -411,7 +411,9 @@ module.exports = {
411411
log,
412412
promisify,
413413
stripVTControlCharacters,
414-
toUSVString,
414+
toUSVString(input) {
415+
return StringPrototypeToWellFormed(`${input}`);
416+
},
415417
get transferableAbortSignal() {
416418
return lazyAbortController().transferableAbortSignal;
417419
},

src/node_util.cc

-40
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ using v8::String;
3737
using v8::Uint32;
3838
using v8::Value;
3939

40-
// Used in ToUSVString().
41-
constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD;
42-
4340
// If a UTF-16 character is a low/trailing surrogate.
4441
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)
4542

@@ -240,40 +237,6 @@ static uint32_t FastGuessHandleType(Local<Value> receiver, const uint32_t fd) {
240237

241238
CFunction fast_guess_handle_type_(CFunction::Make(FastGuessHandleType));
242239

243-
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
244-
Environment* env = Environment::GetCurrent(args);
245-
CHECK_GE(args.Length(), 2);
246-
CHECK(args[0]->IsString());
247-
CHECK(args[1]->IsNumber());
248-
249-
TwoByteValue value(env->isolate(), args[0]);
250-
251-
int64_t start = args[1]->IntegerValue(env->context()).FromJust();
252-
CHECK_GE(start, 0);
253-
254-
for (size_t i = start; i < value.length(); i++) {
255-
char16_t c = value[i];
256-
if (!IsUnicodeSurrogate(c)) {
257-
continue;
258-
} else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
259-
value[i] = kUnicodeReplacementCharacter;
260-
} else {
261-
char16_t d = value[i + 1];
262-
if (IsUnicodeTrail(d)) {
263-
i++;
264-
} else {
265-
value[i] = kUnicodeReplacementCharacter;
266-
}
267-
}
268-
}
269-
270-
args.GetReturnValue().Set(
271-
String::NewFromTwoByte(env->isolate(),
272-
*value,
273-
v8::NewStringType::kNormal,
274-
value.length()).ToLocalChecked());
275-
}
276-
277240
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
278241
registry->Register(GetPromiseDetails);
279242
registry->Register(GetProxyDetails);
@@ -288,7 +251,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
288251
registry->Register(GuessHandleType);
289252
registry->Register(FastGuessHandleType);
290253
registry->Register(fast_guess_handle_type_.GetTypeInfo());
291-
registry->Register(ToUSVString);
292254
}
293255

294256
void Initialize(Local<Object> target,
@@ -403,8 +365,6 @@ void Initialize(Local<Object> target,
403365
"guessHandleType",
404366
GuessHandleType,
405367
&fast_guess_handle_type_);
406-
407-
SetMethodNoSideEffect(context, target, "toUSVString", ToUSVString);
408368
}
409369

410370
} // namespace util

typings/internalBinding/util.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,4 @@ export interface UtilBinding {
4343
shouldAbortOnUncaughtToggle: [shouldAbort: 0 | 1];
4444
WeakReference: typeof InternalUtilBinding.WeakReference;
4545
guessHandleType(fd: number): 'TCP' | 'TTY' | 'UDP' | 'FILE' | 'PIPE' | 'UNKNOWN';
46-
toUSVString(str: string, start: number): string;
4746
}

typings/primordials.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ declare namespace primordials {
420420
export const StringPrototypeToLocaleUpperCase: UncurryThis<typeof String.prototype.toLocaleUpperCase>
421421
export const StringPrototypeToLowerCase: UncurryThis<typeof String.prototype.toLowerCase>
422422
export const StringPrototypeToUpperCase: UncurryThis<typeof String.prototype.toUpperCase>
423+
export const StringPrototypeToWellFormed: UncurryThis<typeof String.prototype.toWellFormed>
423424
export const StringPrototypeValueOf: UncurryThis<typeof String.prototype.valueOf>
424425
export const StringPrototypeReplaceAll: UncurryThis<typeof String.prototype.replaceAll>
425426
export import Symbol = globalThis.Symbol;

0 commit comments

Comments
 (0)