replacePattern.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { buildReplaceStringWithCasePreserved } from '../../../base/common/search.js';
  6. /**
  7. * Assigned when the replace pattern is entirely static.
  8. */
  9. class StaticValueReplacePattern {
  10. constructor(staticValue) {
  11. this.staticValue = staticValue;
  12. this.kind = 0 /* StaticValue */;
  13. }
  14. }
  15. /**
  16. * Assigned when the replace pattern has replacement patterns.
  17. */
  18. class DynamicPiecesReplacePattern {
  19. constructor(pieces) {
  20. this.pieces = pieces;
  21. this.kind = 1 /* DynamicPieces */;
  22. }
  23. }
  24. export class ReplacePattern {
  25. constructor(pieces) {
  26. if (!pieces || pieces.length === 0) {
  27. this._state = new StaticValueReplacePattern('');
  28. }
  29. else if (pieces.length === 1 && pieces[0].staticValue !== null) {
  30. this._state = new StaticValueReplacePattern(pieces[0].staticValue);
  31. }
  32. else {
  33. this._state = new DynamicPiecesReplacePattern(pieces);
  34. }
  35. }
  36. static fromStaticValue(value) {
  37. return new ReplacePattern([ReplacePiece.staticValue(value)]);
  38. }
  39. get hasReplacementPatterns() {
  40. return (this._state.kind === 1 /* DynamicPieces */);
  41. }
  42. buildReplaceString(matches, preserveCase) {
  43. if (this._state.kind === 0 /* StaticValue */) {
  44. if (preserveCase) {
  45. return buildReplaceStringWithCasePreserved(matches, this._state.staticValue);
  46. }
  47. else {
  48. return this._state.staticValue;
  49. }
  50. }
  51. let result = '';
  52. for (let i = 0, len = this._state.pieces.length; i < len; i++) {
  53. let piece = this._state.pieces[i];
  54. if (piece.staticValue !== null) {
  55. // static value ReplacePiece
  56. result += piece.staticValue;
  57. continue;
  58. }
  59. // match index ReplacePiece
  60. let match = ReplacePattern._substitute(piece.matchIndex, matches);
  61. if (piece.caseOps !== null && piece.caseOps.length > 0) {
  62. let repl = [];
  63. let lenOps = piece.caseOps.length;
  64. let opIdx = 0;
  65. for (let idx = 0, len = match.length; idx < len; idx++) {
  66. if (opIdx >= lenOps) {
  67. repl.push(match.slice(idx));
  68. break;
  69. }
  70. switch (piece.caseOps[opIdx]) {
  71. case 'U':
  72. repl.push(match[idx].toUpperCase());
  73. break;
  74. case 'u':
  75. repl.push(match[idx].toUpperCase());
  76. opIdx++;
  77. break;
  78. case 'L':
  79. repl.push(match[idx].toLowerCase());
  80. break;
  81. case 'l':
  82. repl.push(match[idx].toLowerCase());
  83. opIdx++;
  84. break;
  85. default:
  86. repl.push(match[idx]);
  87. }
  88. }
  89. match = repl.join('');
  90. }
  91. result += match;
  92. }
  93. return result;
  94. }
  95. static _substitute(matchIndex, matches) {
  96. if (matches === null) {
  97. return '';
  98. }
  99. if (matchIndex === 0) {
  100. return matches[0];
  101. }
  102. let remainder = '';
  103. while (matchIndex > 0) {
  104. if (matchIndex < matches.length) {
  105. // A match can be undefined
  106. let match = (matches[matchIndex] || '');
  107. return match + remainder;
  108. }
  109. remainder = String(matchIndex % 10) + remainder;
  110. matchIndex = Math.floor(matchIndex / 10);
  111. }
  112. return '$' + remainder;
  113. }
  114. }
  115. /**
  116. * A replace piece can either be a static string or an index to a specific match.
  117. */
  118. export class ReplacePiece {
  119. constructor(staticValue, matchIndex, caseOps) {
  120. this.staticValue = staticValue;
  121. this.matchIndex = matchIndex;
  122. if (!caseOps || caseOps.length === 0) {
  123. this.caseOps = null;
  124. }
  125. else {
  126. this.caseOps = caseOps.slice(0);
  127. }
  128. }
  129. static staticValue(value) {
  130. return new ReplacePiece(value, -1, null);
  131. }
  132. static caseOps(index, caseOps) {
  133. return new ReplacePiece(null, index, caseOps);
  134. }
  135. }
  136. class ReplacePieceBuilder {
  137. constructor(source) {
  138. this._source = source;
  139. this._lastCharIndex = 0;
  140. this._result = [];
  141. this._resultLen = 0;
  142. this._currentStaticPiece = '';
  143. }
  144. emitUnchanged(toCharIndex) {
  145. this._emitStatic(this._source.substring(this._lastCharIndex, toCharIndex));
  146. this._lastCharIndex = toCharIndex;
  147. }
  148. emitStatic(value, toCharIndex) {
  149. this._emitStatic(value);
  150. this._lastCharIndex = toCharIndex;
  151. }
  152. _emitStatic(value) {
  153. if (value.length === 0) {
  154. return;
  155. }
  156. this._currentStaticPiece += value;
  157. }
  158. emitMatchIndex(index, toCharIndex, caseOps) {
  159. if (this._currentStaticPiece.length !== 0) {
  160. this._result[this._resultLen++] = ReplacePiece.staticValue(this._currentStaticPiece);
  161. this._currentStaticPiece = '';
  162. }
  163. this._result[this._resultLen++] = ReplacePiece.caseOps(index, caseOps);
  164. this._lastCharIndex = toCharIndex;
  165. }
  166. finalize() {
  167. this.emitUnchanged(this._source.length);
  168. if (this._currentStaticPiece.length !== 0) {
  169. this._result[this._resultLen++] = ReplacePiece.staticValue(this._currentStaticPiece);
  170. this._currentStaticPiece = '';
  171. }
  172. return new ReplacePattern(this._result);
  173. }
  174. }
  175. /**
  176. * \n => inserts a LF
  177. * \t => inserts a TAB
  178. * \\ => inserts a "\".
  179. * \u => upper-cases one character in a match.
  180. * \U => upper-cases ALL remaining characters in a match.
  181. * \l => lower-cases one character in a match.
  182. * \L => lower-cases ALL remaining characters in a match.
  183. * $$ => inserts a "$".
  184. * $& and $0 => inserts the matched substring.
  185. * $n => Where n is a non-negative integer lesser than 100, inserts the nth parenthesized submatch string
  186. * everything else stays untouched
  187. *
  188. * Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
  189. */
  190. export function parseReplaceString(replaceString) {
  191. if (!replaceString || replaceString.length === 0) {
  192. return new ReplacePattern(null);
  193. }
  194. let caseOps = [];
  195. let result = new ReplacePieceBuilder(replaceString);
  196. for (let i = 0, len = replaceString.length; i < len; i++) {
  197. let chCode = replaceString.charCodeAt(i);
  198. if (chCode === 92 /* Backslash */) {
  199. // move to next char
  200. i++;
  201. if (i >= len) {
  202. // string ends with a \
  203. break;
  204. }
  205. let nextChCode = replaceString.charCodeAt(i);
  206. // let replaceWithCharacter: string | null = null;
  207. switch (nextChCode) {
  208. case 92 /* Backslash */:
  209. // \\ => inserts a "\"
  210. result.emitUnchanged(i - 1);
  211. result.emitStatic('\\', i + 1);
  212. break;
  213. case 110 /* n */:
  214. // \n => inserts a LF
  215. result.emitUnchanged(i - 1);
  216. result.emitStatic('\n', i + 1);
  217. break;
  218. case 116 /* t */:
  219. // \t => inserts a TAB
  220. result.emitUnchanged(i - 1);
  221. result.emitStatic('\t', i + 1);
  222. break;
  223. // Case modification of string replacements, patterned after Boost, but only applied
  224. // to the replacement text, not subsequent content.
  225. case 117 /* u */:
  226. // \u => upper-cases one character.
  227. case 85 /* U */:
  228. // \U => upper-cases ALL following characters.
  229. case 108 /* l */:
  230. // \l => lower-cases one character.
  231. case 76 /* L */:
  232. // \L => lower-cases ALL following characters.
  233. result.emitUnchanged(i - 1);
  234. result.emitStatic('', i + 1);
  235. caseOps.push(String.fromCharCode(nextChCode));
  236. break;
  237. }
  238. continue;
  239. }
  240. if (chCode === 36 /* DollarSign */) {
  241. // move to next char
  242. i++;
  243. if (i >= len) {
  244. // string ends with a $
  245. break;
  246. }
  247. let nextChCode = replaceString.charCodeAt(i);
  248. if (nextChCode === 36 /* DollarSign */) {
  249. // $$ => inserts a "$"
  250. result.emitUnchanged(i - 1);
  251. result.emitStatic('$', i + 1);
  252. continue;
  253. }
  254. if (nextChCode === 48 /* Digit0 */ || nextChCode === 38 /* Ampersand */) {
  255. // $& and $0 => inserts the matched substring.
  256. result.emitUnchanged(i - 1);
  257. result.emitMatchIndex(0, i + 1, caseOps);
  258. caseOps.length = 0;
  259. continue;
  260. }
  261. if (49 /* Digit1 */ <= nextChCode && nextChCode <= 57 /* Digit9 */) {
  262. // $n
  263. let matchIndex = nextChCode - 48 /* Digit0 */;
  264. // peek next char to probe for $nn
  265. if (i + 1 < len) {
  266. let nextNextChCode = replaceString.charCodeAt(i + 1);
  267. if (48 /* Digit0 */ <= nextNextChCode && nextNextChCode <= 57 /* Digit9 */) {
  268. // $nn
  269. // move to next char
  270. i++;
  271. matchIndex = matchIndex * 10 + (nextNextChCode - 48 /* Digit0 */);
  272. result.emitUnchanged(i - 2);
  273. result.emitMatchIndex(matchIndex, i + 1, caseOps);
  274. caseOps.length = 0;
  275. continue;
  276. }
  277. }
  278. result.emitUnchanged(i - 1);
  279. result.emitMatchIndex(matchIndex, i + 1, caseOps);
  280. caseOps.length = 0;
  281. continue;
  282. }
  283. }
  284. }
  285. return result.finalize();
  286. }