BCUninstaller/Bulk-Crap-Uninstaller · error · ArgumentException
Pattern doesn't match the string. Sum of the pattern's parts
Error message
Pattern doesn't match the string. Sum of the pattern's parts has to have length equal to the length the string.
What it means
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.
Source
Thrown at source/KlocTools/Extensions/StringExtensions.cs:37
{
/// <summary>
/// Reverse the string using the specified pattern. The string is split into parts corresponding
/// to the pattern's values, then each of the parts is reversed and finally they are joined back.
/// Example: String("Tester") Pattern(1,3,2) -> T est er -> T tse re -> Result("Ttsere")
/// </summary>
/// <param name="value">String to reverse</param>
/// <param name="pattern">
/// Pattern used to reverse the string.
/// Warning: The pattern has to have identical total length to the length of the string.
/// </param>
public static string Reverse(this string value, int[] pattern)
{
if (value == null)
throw new NullReferenceException();
if (pattern == null)
throw new ArgumentNullException(nameof(pattern));
if (value.Length != pattern.Sum())
throw new ArgumentException(
"Pattern doesn't match the string. Sum of the pattern's parts has to have length equal to the length the string.");
var returnString = new StringBuilder();
var index = 0;
// Iterate over the reversal pattern
foreach (var length in pattern)
{
// Reverse the sub-string and append it
returnString.Append(value.Substring(index, length).Reverse().ToArray());
// Increment our posistion in the string
index += length;
}
return returnString.ToString();
}View on GitHub (pinned to 608321de98)
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].
Example fix
// before
var r = s.Reverse(new[] { 2, 5 }); // fails unless s.Length == 7
// after
int[] p = ComputePattern(s); // guarantees Sum() == s.Length
if (p.Sum() != s.Length) throw new ArgumentException("bad pattern");
var r = s.Reverse(p); Defensive patterns
Strategy: validation
Validate before calling
if (pattern.Sum() != value.Length)
throw new ArgumentException("pattern length mismatch"); Prevention
- Assert pattern.Sum() == value.Length before calling Reverse.
- Derive the pattern from the same string you reverse.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- String contains invalid characters. Data:
- Argument {argument} has already been defined
- {argument} has been specified more than once, expecting sing
- New value must be equal to or in between the minimum and max
- Minimum value must be lower than or equal to the maximum val
AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13).
Data as JSON: /api/errors/c754d83db11b4cd2.
Report an issue: GitHub.