Skip to content

Commit 80a3312

Browse files
committed
lib,src: replace toUSVString with toWellFormed()
1 parent 7ce2232 commit 80a3312

File tree

7 files changed

+30
-91
lines changed

7 files changed

+30
-91
lines changed

benchmark/url/usvstring.js

-27
This file was deleted.

doc/api/util.md

+4
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,10 @@ The encoding supported by the `TextEncoder` instance. Always set to `'utf-8'`.
19491949
added:
19501950
- v16.8.0
19511951
- v14.18.0
1952+
changes:
1953+
- version: REPLACEME
1954+
pr-url: https://github.com/nodejs/node/pull/47342
1955+
description: Implementation uses String.prototype.toWellFormed.
19521956
-->
19531957
19541958
* `string` {string}

lib/internal/file.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const {
1515
customInspectSymbol: kInspect,
1616
kEnumerableProperty,
1717
kEmptyObject,
18-
toUSVString,
1918
} = require('internal/util');
2019

2120
const {
@@ -55,7 +54,7 @@ class File extends Blob {
5554
lastModified = DateNow();
5655
}
5756

58-
this.#name = toUSVString(fileName);
57+
this.#name = `${fileName}`.toWellFormed();
5958
this.#lastModified = lastModified;
6059
}
6160

lib/internal/url.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const {
4343
const {
4444
getConstructorOf,
4545
removeColors,
46-
toUSVString,
4746
kEnumerableProperty,
4847
SideEffectFreeRegExpPrototypeSymbolReplace,
4948
} = require('internal/util');
@@ -181,7 +180,7 @@ class URLSearchParams {
181180
}
182181
const convertedPair = [];
183182
for (const element of pair)
184-
ArrayPrototypePush(convertedPair, toUSVString(element));
183+
ArrayPrototypePush(convertedPair, `${element}`.toWellFormed());
185184
ArrayPrototypePush(pairs, convertedPair);
186185
}
187186

@@ -202,10 +201,10 @@ class URLSearchParams {
202201
const key = keys[i];
203202
const desc = ReflectGetOwnPropertyDescriptor(init, key);
204203
if (desc !== undefined && desc.enumerable) {
205-
const typedKey = toUSVString(key);
206-
const typedValue = toUSVString(init[key]);
204+
const typedKey = `${key}`.toWellFormed();
205+
const typedValue = `${init[key]}`.toWellFormed();
207206

208-
// Two different key may result same after `toUSVString()`, we only
207+
// Two different key may result same after `String.prototype.toWellFormed`, we only
209208
// leave the later one. Refers to WPT.
210209
if (visited[typedKey] !== undefined) {
211210
this[searchParams][visited[typedKey]] = typedValue;
@@ -219,7 +218,7 @@ class URLSearchParams {
219218
}
220219
} else {
221220
// USVString
222-
init = toUSVString(init);
221+
init = `${init}`.toWellFormed();
223222
this[searchParams] = init ? parseParams(init) : [];
224223
}
225224

@@ -277,8 +276,8 @@ class URLSearchParams {
277276
throw new ERR_MISSING_ARGS('name', 'value');
278277
}
279278

280-
name = toUSVString(name);
281-
value = toUSVString(value);
279+
name = `${name}`.toWellFormed();
280+
value = `${value}`.toWellFormed();
282281
ArrayPrototypePush(this[searchParams], name, value);
283282
if (this[context]) {
284283
this[context].search = this.toString();
@@ -294,7 +293,7 @@ class URLSearchParams {
294293
}
295294

296295
const list = this[searchParams];
297-
name = toUSVString(name);
296+
name = `${name}`.toWellFormed();
298297
for (let i = 0; i < list.length;) {
299298
const cur = list[i];
300299
if (cur === name) {
@@ -317,7 +316,7 @@ class URLSearchParams {
317316
}
318317

319318
const list = this[searchParams];
320-
name = toUSVString(name);
319+
name = `${name}`.toWellFormed();
321320
for (let i = 0; i < list.length; i += 2) {
322321
if (list[i] === name) {
323322
return list[i + 1];
@@ -336,7 +335,7 @@ class URLSearchParams {
336335

337336
const list = this[searchParams];
338337
const values = [];
339-
name = toUSVString(name);
338+
name = `${name}`.toWellFormed();
340339
for (let i = 0; i < list.length; i += 2) {
341340
if (list[i] === name) {
342341
values.push(list[i + 1]);
@@ -354,7 +353,7 @@ class URLSearchParams {
354353
}
355354

356355
const list = this[searchParams];
357-
name = toUSVString(name);
356+
name = `${name}`.toWellFormed();
358357
for (let i = 0; i < list.length; i += 2) {
359358
if (list[i] === name) {
360359
return true;
@@ -372,8 +371,8 @@ class URLSearchParams {
372371
}
373372

374373
const list = this[searchParams];
375-
name = toUSVString(name);
376-
value = toUSVString(value);
374+
name = `${name}`.toWellFormed();
375+
value = `${value}`.toWellFormed();
377376

378377
// If there are any name-value pairs whose name is `name`, in `list`, set
379378
// the value of the first such name-value pair to `value` and remove the
@@ -549,7 +548,7 @@ class URL {
549548
#searchParams;
550549

551550
constructor(input, base = undefined) {
552-
// toUSVString is not needed.
551+
// String.prototype.toWellFormed is not needed.
553552
input = `${input}`;
554553

555554
if (base !== undefined) {
@@ -690,7 +689,7 @@ class URL {
690689
}
691690

692691
set search(value) {
693-
updateUrl(this.#context.href, updateActions.kSearch, toUSVString(value), this.#onParseComplete);
692+
updateUrl(this.#context.href, updateActions.kSearch, `${value}`.toWellFormed(), this.#onParseComplete);
694693
}
695694

696695
// readonly
@@ -1106,15 +1105,15 @@ function domainToASCII(domain) {
11061105
if (arguments.length < 1)
11071106
throw new ERR_MISSING_ARGS('domain');
11081107

1109-
// toUSVString is not needed.
1108+
// String.prototype.toWellFormed is not needed.
11101109
return _domainToASCII(`${domain}`);
11111110
}
11121111

11131112
function domainToUnicode(domain) {
11141113
if (arguments.length < 1)
11151114
throw new ERR_MISSING_ARGS('domain');
11161115

1117-
// toUSVString is not needed.
1116+
// String.prototype.toWellFormed is not needed.
11181117
return _domainToUnicode(`${domain}`);
11191118
}
11201119

@@ -1287,7 +1286,6 @@ function toPathIfFileURL(fileURLOrPath) {
12871286
}
12881287

12891288
module.exports = {
1290-
toUSVString,
12911289
fileURLToPath,
12921290
pathToFileURL,
12931291
toPathIfFileURL,

lib/internal/util.js

-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const {
5959
decorated_private_symbol,
6060
},
6161
sleep: _sleep,
62-
toUSVString: _toUSVString,
6362
} = internalBinding('util');
6463
const { isNativeError } = internalBinding('types');
6564

@@ -69,10 +68,6 @@ const experimentalWarnings = new SafeSet();
6968

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

72-
function toUSVString(val) {
73-
return _toUSVString(`${val}`);
74-
}
75-
7671
let uvBinding;
7772

7873
function lazyUv() {
@@ -786,7 +781,6 @@ module.exports = {
786781
sleep,
787782
spliceOne,
788783
setupCoverageHooks,
789-
toUSVString,
790784
removeColors,
791785

792786
// Symbol used to customize promisify conversion

lib/util.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const {
7474
getSystemErrorMap,
7575
getSystemErrorName: internalErrorName,
7676
promisify,
77-
toUSVString,
7877
defineLazyProperties,
7978
} = require('internal/util');
8079

@@ -346,6 +345,14 @@ function getSystemErrorName(err) {
346345
return internalErrorName(err);
347346
}
348347

348+
/**
349+
* @param {string} input
350+
* @returns {string}
351+
*/
352+
function toUSVString(input) {
353+
return `${input}`.toWellFormed();
354+
}
355+
349356
// Keep the `exports =` so that various functions can still be monkeypatched
350357
module.exports = {
351358
_errnoException: errnoException,

src/node_util.cc

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

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

@@ -320,36 +317,6 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
320317
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
321318
}
322319

323-
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
324-
Environment* env = Environment::GetCurrent(args);
325-
CHECK_GE(args.Length(), 1);
326-
CHECK(args[0]->IsString());
327-
328-
TwoByteValue value(env->isolate(), args[0]);
329-
330-
for (size_t i = 0; i < value.length(); i++) {
331-
char16_t c = value[i];
332-
if (!IsUnicodeSurrogate(c)) {
333-
continue;
334-
} else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
335-
value[i] = kUnicodeReplacementCharacter;
336-
} else {
337-
char16_t d = value[i + 1];
338-
if (IsUnicodeTrail(d)) {
339-
i++;
340-
} else {
341-
value[i] = kUnicodeReplacementCharacter;
342-
}
343-
}
344-
}
345-
346-
args.GetReturnValue().Set(
347-
String::NewFromTwoByte(env->isolate(),
348-
*value,
349-
v8::NewStringType::kNormal,
350-
value.length()).ToLocalChecked());
351-
}
352-
353320
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
354321
registry->Register(GetPromiseDetails);
355322
registry->Register(GetProxyDetails);
@@ -366,7 +333,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
366333
registry->Register(WeakReference::IncRef);
367334
registry->Register(WeakReference::DecRef);
368335
registry->Register(GuessHandleType);
369-
registry->Register(ToUSVString);
370336
}
371337

372338
void Initialize(Local<Object> target,
@@ -472,8 +438,6 @@ void Initialize(Local<Object> target,
472438
SetConstructorFunction(context, target, "WeakReference", weak_ref);
473439

474440
SetMethod(context, target, "guessHandleType", GuessHandleType);
475-
476-
SetMethodNoSideEffect(context, target, "toUSVString", ToUSVString);
477441
}
478442

479443
} // namespace util

0 commit comments

Comments
 (0)