MCPcopy Index your code
hub / github.com/nodejs/node / escapeField

Method escapeField

deps/v8/tools/csvparser.mjs:38–82  ·  view source on GitHub ↗

* Converts \x00 and \u0000 escape sequences in the given string. * * @param {string} input field.

(string)

Source from the content-addressed store, hash-verified

36 * @param {string} input field.
37 **/
38 escapeField(string) {
39 let nextPos = string.indexOf("\\");
40 if (nextPos === -1) return string;
41 let result = [string.substring(0, nextPos)];
42 // Escape sequences of the form \x00 and \u0000;
43 let pos = 0;
44 while (nextPos !== -1) {
45 const escapeIdentifier = string[nextPos + 1];
46 pos = nextPos + 2;
47 if (escapeIdentifier === 'n') {
48 result.push('\n');
49 nextPos = pos;
50 } else if (escapeIdentifier === '\\') {
51 result.push('\\');
52 nextPos = pos;
53 } else {
54 if (escapeIdentifier === 'x') {
55 // \x00 ascii range escapes consume 2 chars.
56 nextPos = pos + 2;
57 } else {
58 // \u0000 unicode range escapes consume 4 chars.
59 nextPos = pos + 4;
60 }
61 // Convert the selected escape sequence to a single character.
62 const escapeChars = string.substring(pos, nextPos);
63 if (escapeChars === '2C') {
64 result.push(',');
65 } else {
66 result.push(String.fromCharCode(parseInt(escapeChars, 16)));
67 }
68 }
69
70 // Continue looking for the next escape sequence.
71 pos = nextPos;
72 nextPos = string.indexOf("\\", pos);
73 // If there are no more escape sequences consume the rest of the string.
74 if (nextPos === -1) {
75 result.push(string.substr(pos));
76 break;
77 } else if (pos !== nextPos) {
78 result.push(string.substring(pos, nextPos));
79 }
80 }
81 return result.join('');
82 }
83
84 /**
85 * Parses a line of CSV-encoded values. Returns an array of fields.

Callers 1

parseLineMethod · 0.95

Calls 3

indexOfMethod · 0.45
pushMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected