{"record":{"id":"c754d83db11b4cd2","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"pattern-doesn-t-match-the-string-sum-of-the-patte","errorCode":null,"errorMessage":"Pattern doesn't match the string. Sum of the pattern's parts has to have length equal to the length the string.","messagePattern":"Pattern doesn't match the string\\. Sum of the pattern's parts has to have length equal to the length the string\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Extensions/StringExtensions.cs","lineNumber":37,"sourceCode":"    {\n        /// <summary>\n        ///     Reverse the string using the specified pattern. The string is split into parts corresponding\n        ///     to the pattern's values, then each of the parts is reversed and finally they are joined back.\n        ///     Example: String(\"Tester\") Pattern(1,3,2) -> T est er -> T tse re -> Result(\"Ttsere\")\n        /// </summary>\n        /// <param name=\"value\">String to reverse</param>\n        /// <param name=\"pattern\">\n        ///     Pattern used to reverse the string.\n        ///     Warning: The pattern has to have identical total length to the length of the string.\n        /// </param>\n        public static string Reverse(this string value, int[] pattern)\n        {\n            if (value == null)\n                throw new NullReferenceException();\n            if (pattern == null)\n                throw new ArgumentNullException(nameof(pattern));\n            if (value.Length != pattern.Sum())\n                throw new ArgumentException(\n                    \"Pattern doesn't match the string. Sum of the pattern's parts has to have length equal to the length the string.\");\n\n            var returnString = new StringBuilder();\n\n            var index = 0;\n\n            // Iterate over the reversal pattern\n            foreach (var length in pattern)\n            {\n                // Reverse the sub-string and append it\n                returnString.Append(value.Substring(index, length).Reverse().ToArray());\n\n                // Increment our posistion in the string\n                index += length;\n            }\n\n            return returnString.ToString();\n        }","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Extensions/StringExtensions.cs#L19-L55","documentation":"The Reverse(string, int[]) extension splits the string into segments sized by the pattern, reverses each segment, and rejoins them. It requires the sum of the pattern ints to exactly equal the string length; otherwise segmentation would over- or under-run the string, so mismatched patterns are rejected up front with ArgumentException.","triggerScenarios":"Calling Reverse with a pattern whose elements do not sum to value.Length; an off-by-one when computing the pattern; a pattern derived from a different string than the one passed.","commonSituations":"Pattern computed from byte counts applied to a UTF-16 string; mismatched encoding length vs character length; hardcoded pattern reused across strings of differing length.","solutions":["Compute the pattern so pattern.Sum() == value.Length before calling.","Validate the length match and handle the mismatch before reversing.","If a full reversal is all you need, pass a single-element pattern [value.Length]."],"exampleFix":"// before\nvar r = s.Reverse(new[] { 2, 5 }); // fails unless s.Length == 7\n// after\nint[] p = ComputePattern(s);          // guarantees Sum() == s.Length\nif (p.Sum() != s.Length) throw new ArgumentException(\"bad pattern\");\nvar r = s.Reverse(p);","handlingStrategy":"validation","validationCode":"if (pattern.Sum() != value.Length)\n    throw new ArgumentException(\"pattern length mismatch\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert pattern.Sum() == value.Length before calling Reverse.","Derive the pattern from the same string you reverse."],"tags":["string","validation","algorithm"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}