re2js 1.3.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  * re2js
3
3
  * RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
4
4
  *
5
- * @version v1.3.3
5
+ * @version v2.0.0
6
6
  * @author Alexey Vasiliev
7
7
  * @homepage https://github.com/le0pard/re2js#readme
8
8
  * @repository github:le0pard/re2js
@@ -1130,16 +1130,31 @@
1130
1130
  * {@link #appendReplacement} as a literal replacement of {@code s}.
1131
1131
  *
1132
1132
  * @param {string} str the string to be quoted
1133
+ * @param {boolean} [javaMode=false] whether the replacement will be used in javaMode
1133
1134
  * @returns {string} the quoted string
1134
1135
  */
1135
- static quoteReplacement(str) {
1136
- if (str.indexOf('\\') < 0 && str.indexOf('$') < 0) {
1136
+ static quoteReplacement(str, javaMode = false) {
1137
+ if (javaMode) {
1138
+ // Java mode escape '\' and '$' with a backslash
1139
+ if (str.indexOf('\\') < 0 && str.indexOf('$') < 0) {
1140
+ return str;
1141
+ }
1142
+ return str.split('').map(s => {
1143
+ const c = s.codePointAt(0);
1144
+ if (c === Codepoint.CODES.get('\\') || c === Codepoint.CODES.get('$')) {
1145
+ return `\\${s}`;
1146
+ }
1147
+ return s;
1148
+ }).join('');
1149
+ }
1150
+
1151
+ // In JS mode, '\' is not a special character, but '$' must be escaped as '$$'
1152
+ if (str.indexOf('$') < 0) {
1137
1153
  return str;
1138
1154
  }
1139
1155
  return str.split('').map(s => {
1140
- const c = s.codePointAt(0);
1141
- if (c === Codepoint.CODES.get('\\') || c === Codepoint.CODES.get('$')) {
1142
- return `\\${s}`;
1156
+ if (s.codePointAt(0) === Codepoint.CODES.get('$')) {
1157
+ return '$$';
1143
1158
  }
1144
1159
  return s;
1145
1160
  }).join('');
@@ -1429,13 +1444,13 @@
1429
1444
  * earlier, escape the first digit that should not be used.
1430
1445
  *
1431
1446
  * @param {string} replacement the replacement string
1432
- * @param {boolean} [perlMode=false] activate perl/js mode (different behaviour for capture groups and special characters)
1447
+ * @param {boolean} [javaMode=false] activate java mode (different behaviour for capture groups and special characters)
1433
1448
  * @returns {string}
1434
1449
  * @throws IllegalStateException if there was no most recent match
1435
1450
  * @throws IndexOutOfBoundsException if replacement refers to an invalid group
1436
1451
  * @private
1437
1452
  */
1438
- appendReplacement(replacement, perlMode = false) {
1453
+ appendReplacement(replacement, javaMode = false) {
1439
1454
  let res = '';
1440
1455
  const s = this.start();
1441
1456
  const e = this.end();
@@ -1443,7 +1458,7 @@
1443
1458
  res += this.substring(this.appendPos, s);
1444
1459
  }
1445
1460
  this.appendPos = e;
1446
- res += perlMode ? this.appendReplacementInternalPerl(replacement) : this.appendReplacementInternal(replacement);
1461
+ res += javaMode ? this.appendReplacementInternalJava(replacement) : this.appendReplacementInternalJs(replacement);
1447
1462
  return res;
1448
1463
  }
1449
1464
 
@@ -1452,7 +1467,7 @@
1452
1467
  * @returns {string}
1453
1468
  * @private
1454
1469
  */
1455
- appendReplacementInternal(replacement) {
1470
+ appendReplacementInternalJava(replacement) {
1456
1471
  let res = '';
1457
1472
  let last = 0;
1458
1473
  const m = replacement.length;
@@ -1518,7 +1533,7 @@
1518
1533
  * @returns {string}
1519
1534
  * @private
1520
1535
  */
1521
- appendReplacementInternalPerl(replacement) {
1536
+ appendReplacementInternalJs(replacement) {
1522
1537
  let res = '';
1523
1538
  let last = 0;
1524
1539
  const m = replacement.length;
@@ -1615,12 +1630,12 @@
1615
1630
  * {@code appendReplacement}.
1616
1631
  *
1617
1632
  * @param {string} replacement - the replacement string
1618
- * @param {boolean} [perlMode=false] - activate perl/js mode (different behaviour for capture groups and special characters)
1633
+ * @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
1619
1634
  * @returns {string} the input string with the matches replaced
1620
- * @throws IndexOutOfBoundsException if replacement refers to an invalid group and perlMode is false
1635
+ * @throws IndexOutOfBoundsException if replacement refers to an invalid group and javaMode is true
1621
1636
  */
1622
- replaceAll(replacement, perlMode = false) {
1623
- return this.replace(replacement, true, perlMode);
1637
+ replaceAll(replacement, javaMode = false) {
1638
+ return this.replace(replacement, true, javaMode);
1624
1639
  }
1625
1640
 
1626
1641
  /**
@@ -1628,27 +1643,27 @@
1628
1643
  * {@code appendReplacement}.
1629
1644
  *
1630
1645
  * @param {string} replacement - the replacement string
1631
- * @param {boolean} [perlMode=false] - activate perl/js mode (different behaviour for capture groups and special characters)
1646
+ * @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
1632
1647
  * @returns {string} the input string with the first match replaced
1633
- * @throws IndexOutOfBoundsException if replacement refers to an invalid group and perlMode is false
1648
+ * @throws IndexOutOfBoundsException if replacement refers to an invalid group and javaMode is true
1634
1649
  */
1635
- replaceFirst(replacement, perlMode = false) {
1636
- return this.replace(replacement, false, perlMode);
1650
+ replaceFirst(replacement, javaMode = false) {
1651
+ return this.replace(replacement, false, javaMode);
1637
1652
  }
1638
1653
 
1639
1654
  /**
1640
1655
  * Helper: replaceAll/replaceFirst hybrid.
1641
1656
  * @param {string} replacement - the replacement string
1642
1657
  * @param {boolean} [all=true] - replace all matches
1643
- * @param {boolean} [perlMode=false] - activate perl/js mode (different behaviour for capture groups and special characters)
1658
+ * @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
1644
1659
  * @returns {string}
1645
1660
  * @private
1646
1661
  */
1647
- replace(replacement, all = true, perlMode = false) {
1662
+ replace(replacement, all = true, javaMode = false) {
1648
1663
  let res = '';
1649
1664
  this.reset();
1650
1665
  while (this.find()) {
1651
- res += this.appendReplacement(replacement, perlMode);
1666
+ res += this.appendReplacement(replacement, javaMode);
1652
1667
  if (!all) {
1653
1668
  break;
1654
1669
  }
@@ -6205,6 +6220,20 @@
6205
6220
  return Utils.quoteMeta(str);
6206
6221
  }
6207
6222
 
6223
+ /**
6224
+ * Quotes '\' and '$' in {@code str}, so that the returned string could be used in
6225
+ * replacement methods as a literal replacement of {@code str}.
6226
+ *
6227
+ * This is a convenience delegation to {@link Matcher.quoteReplacement}.
6228
+ *
6229
+ * @param {string} str the string to be quoted
6230
+ * @param {boolean} [javaMode=false] whether the replacement will be used in javaMode
6231
+ * @returns {string} the quoted string
6232
+ */
6233
+ static quoteReplacement(str, javaMode = false) {
6234
+ return Matcher.quoteReplacement(str, javaMode);
6235
+ }
6236
+
6208
6237
  /**
6209
6238
  * Translates a given regular expression string to ensure compatibility with RE2JS.
6210
6239
  *