Skip to content

Commit 2504d4a

Browse files
authored
Remove used of String.substr from library code. NFC (#23513)
String.substr is deprecated.
1 parent df91026 commit 2504d4a

File tree

109 files changed

+134
-134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+134
-134
lines changed

src/cpuprofiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ var emscriptenCpuProfiler = {
225225
if (i != 2) cs += ' <- ';
226226
var fn = funcs[i];
227227
var at = fn.indexOf('@');
228-
if (at != -1) fn = fn.substr(0, at);
228+
if (at != -1) fn = fn.slice(0, at);
229229
fn = fn.trim();
230230
cs += '"' + fn + '"';
231231
}

src/emrun_prejs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// Route URL GET parameters to argc+argv
1111
if (typeof window == 'object') {
12-
Module['arguments'] = window.location.search.substr(1).trim().split('&');
12+
Module['arguments'] = window.location.search.slice(1).trim().split('&');
1313
for (let i = 0; i < Module['arguments'].length; ++i) {
1414
Module['arguments'][i] = decodeURI(Module['arguments'][i]);
1515
}

src/lib/libbootstrap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ addToLibrary({
4343
// extra effort to support printf, even without a filesystem. very partial, very hackish
4444
var result = formatString(format, varargs);
4545
var string = intArrayToString(result);
46-
if (string[string.length-1] === '\n') string = string.substr(0, string.length-1); // remove a final \n, as Module.print will do that
46+
if (string.endsWith('\n')) string = string.slice(0, -1); // remove a final \n, as Module.print will do that
4747
out(string);
4848
return result.length;
4949
},
@@ -52,8 +52,8 @@ addToLibrary({
5252
puts: (s) => {
5353
// extra effort to support puts, even without a filesystem. very partial, very hackish
5454
var result = UTF8ToString(s);
55-
var string = result.substr(0);
56-
if (string[string.length-1] === '\n') string = string.substr(0, string.length-1); // remove a final \n, as Module.print will do that
55+
var string = result;
56+
if (string.endsWith('\n')) string = string.slice(0, -1); // remove a final \n, as Module.print will do that
5757
out(string);
5858
return result.length;
5959
},

src/lib/libbrowser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var LibraryBrowser = {
8888

8989
var audioPlugin = {};
9090
audioPlugin['canHandle'] = function audioPlugin_canHandle(name) {
91-
return !Module['noAudioDecoding'] && name.substr(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 };
91+
return !Module['noAudioDecoding'] && name.slice(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 };
9292
};
9393
audioPlugin['handle'] = function audioPlugin_handle(byteArray, name, onload, onerror) {
9494
var done = false;
@@ -138,7 +138,7 @@ var LibraryBrowser = {
138138
}
139139
return ret;
140140
}
141-
audio.src = 'data:audio/x-' + name.substr(-3) + ';base64,' + encode64(byteArray);
141+
audio.src = 'data:audio/x-' + name.slice(-3) + ';base64,' + encode64(byteArray);
142142
finish(audio); // we don't wait for confirmation this worked - but it's worth trying
143143
};
144144
audio.src = url;
@@ -348,7 +348,7 @@ var LibraryBrowser = {
348348
'ogg': 'audio/ogg',
349349
'wav': 'audio/wav',
350350
'mp3': 'audio/mpeg'
351-
}[name.substr(name.lastIndexOf('.')+1)];
351+
}[name.slice(name.lastIndexOf('.')+1)];
352352
},
353353

354354
getUserMedia(func) {
@@ -732,7 +732,7 @@ var LibraryBrowser = {
732732
var styleSheet = document.styleSheets[0];
733733
var rules = styleSheet.cssRules;
734734
for (var i = 0; i < rules.length; i++) {
735-
if (rules[i].cssText.substr(0, 6) == 'canvas') {
735+
if (rules[i].cssText.startsWith('canvas')) {
736736
styleSheet.deleteRule(i);
737737
i--;
738738
}

src/lib/libc_preprocessor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ addToLibrary({
9696
}
9797
} else if (kind == 1/*operator symbol*/) {
9898
// Lookahead for two-character operators.
99-
var op2 = exprString.substr(i, 2);
99+
var op2 = exprString.slice(i, i + 2);
100100
if (['<=', '>=', '==', '!=', '&&', '||'].includes(op2)) {
101101
out.push(op2);
102102
++i;

src/lib/libembind_shared.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ var LibraryEmbindShared = {
128128
const argsIndex = signature.indexOf("(");
129129
if (argsIndex !== -1) {
130130
#if ASSERTIONS
131-
assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
131+
assert(signature.endsWith(")"), "Parentheses for argument names should match.");
132132
#endif
133-
return signature.substr(0, argsIndex);
133+
return signature.slice(0, argsIndex);
134134
} else {
135135
return signature;
136136
}
@@ -141,9 +141,9 @@ var LibraryEmbindShared = {
141141
const argsIndex = signature.indexOf("(") + 1;
142142
if (argsIndex !== 0) {
143143
#if ASSERTIONS
144-
assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
144+
assert(signature.endsWith(")"), "Parentheses for argument names should match.");
145145
#endif
146-
return signature.substr(argsIndex, signature.length - argsIndex - 1).replaceAll(" ", "").split(",").filter(n => n.length);
146+
return signature.slice(argsIndex, -1).replaceAll(" ", "").split(",").filter(n => n.length);
147147
} else {
148148
return [];
149149
}

src/lib/libexports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ addToLibrary({
1010
name = UTF8ToString(name);
1111
// Wasm backend does not use C name mangling on exports,
1212
// so adjust for that manually.
13-
if (name[0] == '_') name = name.substr(1);
13+
if (name[0] == '_') name = name.slice(1);
1414
var exportedFunc = wasmExports[name];
1515
if (exportedFunc) {
1616
// Record the created function pointer to each function object,

src/lib/libformatString.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ addToLibrary({
290290
// Move sign to prefix so we zero-pad after the sign
291291
if (argText.charAt(0) == '-') {
292292
prefix = '-' + prefix;
293-
argText = argText.substr(1);
293+
argText = argText.slice(1);
294294
}
295295

296296
// Add padding.
@@ -417,7 +417,7 @@ addToLibrary({
417417
ret.push({{{ makeGetValue('arg++', 0, 'u8') }}});
418418
}
419419
} else {
420-
ret = ret.concat(intArrayFromString('(null)'.substr(0, argLength), true));
420+
ret = ret.concat(intArrayFromString('(null)'.slice(0, argLength), true));
421421
}
422422
if (flagLeftAlign) {
423423
while (argLength < width--) {

src/lib/libfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ FS.ignorePermissions = false;
4343
FS.createPreloadedFile = FS_createPreloadedFile;
4444
FS.staticInit();
4545
// Set module methods based on EXPORTED_RUNTIME_METHODS
46-
{{{ EXPORTED_RUNTIME_METHODS.filter((func) => func.startsWith('FS_')).map((func) => 'Module["' + func + '"] = FS.' + func.substr(3) + ";\n").reduce((str, func) => str + func, '') }}}`;
46+
{{{ EXPORTED_RUNTIME_METHODS.filter((func) => func.startsWith('FS_')).map((func) => 'Module["' + func + '"] = FS.' + func.slice(3) + ";\n").reduce((str, func) => str + func, '') }}}`;
4747
},
4848
$FS: {
4949
root: null,

src/lib/libhtml5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ var LibraryHTML5 = {
360360
// represents the set of OffscreenCanvases owned by the current calling thread.
361361

362362
// First check out the list of OffscreenCanvases by CSS selector ID ('#myCanvasID')
363-
return GL.offscreenCanvases[target.substr(1)] // Remove '#' prefix
363+
return GL.offscreenCanvases[target.slice(1)] // Remove '#' prefix
364364
// If not found, if one is querying by using DOM tag name selector 'canvas', grab the first
365365
// OffscreenCanvas that we can find.
366366
|| (target == 'canvas' && Object.keys(GL.offscreenCanvases)[0])

0 commit comments

Comments
 (0)