Przeglądaj źródła

Tidied up and added global matching to Subsection operation

n1474335 6 lat temu
rodzic
commit
c2068b343b
2 zmienionych plików z 26 dodań i 9 usunięć
  1. 5 0
      CHANGELOG.md
  2. 21 9
      src/core/operations/Subsection.mjs

+ 5 - 0
CHANGELOG.md

@@ -2,6 +2,9 @@
 All major and minor version changes will be documented in this file. Details of patch-level version changes can be found in [commit messages](https://github.com/gchq/CyberChef/commits/master).
 All major and minor version changes will be documented in this file. Details of patch-level version changes can be found in [commit messages](https://github.com/gchq/CyberChef/commits/master).
 
 
 
 
+### [8.22.0] - 2019-01-10
+- 'Subsection' operation added [@j433866] | [#467]
+
 ### [8.21.0] - 2019-01-10
 ### [8.21.0] - 2019-01-10
 - 'To Case Insensitive Regex' and 'From Case Insensitive Regex' operations added [@masq] | [#461]
 - 'To Case Insensitive Regex' and 'From Case Insensitive Regex' operations added [@masq] | [#461]
 
 
@@ -94,6 +97,7 @@ All major and minor version changes will be documented in this file. Details of
 
 
 
 
 
 
+[8.22.0]: https://github.com/gchq/CyberChef/releases/tag/v8.22.0
 [8.21.0]: https://github.com/gchq/CyberChef/releases/tag/v8.21.0
 [8.21.0]: https://github.com/gchq/CyberChef/releases/tag/v8.21.0
 [8.20.0]: https://github.com/gchq/CyberChef/releases/tag/v8.20.0
 [8.20.0]: https://github.com/gchq/CyberChef/releases/tag/v8.20.0
 [8.19.0]: https://github.com/gchq/CyberChef/releases/tag/v8.19.0
 [8.19.0]: https://github.com/gchq/CyberChef/releases/tag/v8.19.0
@@ -171,3 +175,4 @@ All major and minor version changes will be documented in this file. Details of
 [#455]: https://github.com/gchq/CyberChef/pull/455
 [#455]: https://github.com/gchq/CyberChef/pull/455
 [#458]: https://github.com/gchq/CyberChef/pull/458
 [#458]: https://github.com/gchq/CyberChef/pull/458
 [#461]: https://github.com/gchq/CyberChef/pull/461
 [#461]: https://github.com/gchq/CyberChef/pull/461
+[#467]: https://github.com/gchq/CyberChef/pull/467

+ 21 - 9
src/core/operations/Subsection.mjs

@@ -38,6 +38,11 @@ class Subsection extends Operation {
                 "type": "boolean",
                 "type": "boolean",
                 "value": true
                 "value": true
             },
             },
+            {
+                "name": "Global matching",
+                "type": "boolean",
+                "value": true
+            },
             {
             {
                 "name": "Ignore errors",
                 "name": "Ignore errors",
                 "type": "boolean",
                 "type": "boolean",
@@ -49,7 +54,7 @@ class Subsection extends Operation {
     /**
     /**
      * @param {Object} state - The current state of the recipe.
      * @param {Object} state - The current state of the recipe.
      * @param {number} state.progress - The current position in the recipe.
      * @param {number} state.progress - The current position in the recipe.
-     * @param {Dish} state.Dish - The Dish being operated on
+     * @param {Dish} state.dish - The Dish being operated on
      * @param {Operation[]} state.opList - The list of operations in the recipe
      * @param {Operation[]} state.opList - The list of operations in the recipe
      * @returns {Object} - The updated state of the recipe
      * @returns {Object} - The updated state of the recipe
      */
      */
@@ -59,12 +64,12 @@ class Subsection extends Operation {
             outputType  = opList[state.progress].outputType,
             outputType  = opList[state.progress].outputType,
             input       = await state.dish.get(inputType),
             input       = await state.dish.get(inputType),
             ings        = opList[state.progress].ingValues,
             ings        = opList[state.progress].ingValues,
-            [section, caseSensitive, ignoreErrors]   = ings,
+            [section, caseSensitive, global, ignoreErrors] = ings,
             subOpList   = [];
             subOpList   = [];
 
 
         if (input && section !== "") {
         if (input && section !== "") {
             // Create subOpList for each tranche to operate on
             // Create subOpList for each tranche to operate on
-            // (all remaining operations unless we encounter a Merge)
+            // all remaining operations unless we encounter a Merge
             for (let i = state.progress + 1; i < opList.length; i++) {
             for (let i = state.progress + 1; i < opList.length; i++) {
                 if (opList[i].name === "Merge" && !opList[i].disabled) {
                 if (opList[i].name === "Merge" && !opList[i].disabled) {
                     break;
                     break;
@@ -73,13 +78,15 @@ class Subsection extends Operation {
                 }
                 }
             }
             }
 
 
-            let flags = "g",
+            let flags = "",
                 inOffset = 0,
                 inOffset = 0,
                 output = "",
                 output = "",
                 m,
                 m,
                 progress = 0;
                 progress = 0;
-            if (!caseSensitive)
-                flags += "i";
+
+            if (!caseSensitive) flags += "i";
+            if (global) flags += "g";
+
             const regex = new XRegExp(section, flags),
             const regex = new XRegExp(section, flags),
                 recipe = new Recipe();
                 recipe = new Recipe();
 
 
@@ -95,16 +102,19 @@ class Subsection extends Operation {
                 matched = true;
                 matched = true;
                 // Add up to match
                 // Add up to match
                 let matchStr = m[0];
                 let matchStr = m[0];
-                if (m.length === 1) {
+
+                if (m.length === 1) { // No capture groups
                     output += input.slice(inOffset, m.index);
                     output += input.slice(inOffset, m.index);
-                    inOffset = regex.lastIndex;
+                    inOffset = m.index + m[0].length;
                 } else if (m.length >= 2) {
                 } else if (m.length >= 2) {
                     matchStr = m[1];
                     matchStr = m[1];
+
                     // Need to add some of the matched string that isn't in the capture group
                     // Need to add some of the matched string that isn't in the capture group
                     output += input.slice(inOffset, m.index + m[0].indexOf(m[1]));
                     output += input.slice(inOffset, m.index + m[0].indexOf(m[1]));
                     // Set i to be after the end of the first capture group
                     // Set i to be after the end of the first capture group
-                    inOffset = regex.lastIndex - (m[0].length - (m[0].indexOf(m[1]) + m[1].length));
+                    inOffset = m.index + m[0].indexOf(m[1]) + m[1].length;
                 }
                 }
+
                 // Baseline ing values for each tranche so that registers are reset
                 // Baseline ing values for each tranche so that registers are reset
                 subOpList.forEach((op, i) => {
                 subOpList.forEach((op, i) => {
                     op.ingValues = JSON.parse(JSON.stringify(ingValues[i]));
                     op.ingValues = JSON.parse(JSON.stringify(ingValues[i]));
@@ -122,7 +132,9 @@ class Subsection extends Operation {
                     progress = err.progress + 1;
                     progress = err.progress + 1;
                 }
                 }
                 output += await dish.get(outputType);
                 output += await dish.get(outputType);
+                if (!regex.global) break;
             }
             }
+
             // If no matches were found, advance progress to after a Merge op
             // If no matches were found, advance progress to after a Merge op
             // Otherwise, the operations below Subsection will be run on all the input data
             // Otherwise, the operations below Subsection will be run on all the input data
             if (!matched) {
             if (!matched) {