TheAlgorithms/C-Sharp · error · ArgumentException
The pattern is longer than 31 characters.
Error message
The pattern is longer than 31 characters.
What it means
BitapAlgorithm.FindExactPattern implements the Bitap shift-and algorithm using 32-bit integer bitmasks, so patterns longer than 31 characters cannot be represented. The library enforces this hard limit with an ArgumentException when the pattern length exceeds 31. It is a documented algorithmic constraint, not bad input formatting.
Solutions
- Check pattern length before calling: if (pattern.Length > 31) use a different algorithm (e.g. KMP/Naive) or split the search.
- Truncate or otherwise reduce the pattern to 31 characters or fewer when the use case allows.
- Use a string-search library without the bitmask limitation for long patterns.
- Catch ArgumentException and fall back to an alternative search implementation.
Example fix
// before
var index = BitapAlgorithm.FindExactPattern(text, longPattern); // may exceed 31 chars
// after
var index = longPattern.Length <= 31
? BitapAlgorithm.FindExactPattern(text, longPattern)
: text.IndexOf(longPattern, StringComparison.Ordinal); Defensive patterns
Strategy: validation
Validate before calling
if (pattern.Length > 31)
{
// use an alternative search (e.g. text.IndexOf) or split the pattern
} Try / catch
try
{
index = BitapAlgorithm.FindExactPattern(text, pattern);
}
catch (ArgumentException ex) when (ex.Message.Contains("longer than 31"))
{
index = text.IndexOf(pattern, StringComparison.Ordinal); // fallback
} Prevention
- Know the 31-character hard limit of bit-mask-based algorithms before choosing Bitap.
- Validate pattern length immediately after collecting it from users/config.
- Keep a length-unlimited fallback (KMP / IndexOf) in the search path.
- Add a regression test with a 32-character pattern.
When it happens
Trigger: Calling FindExactPattern with a pattern whose length is 32 or more, e.g. FindExactPattern(text, new string('a', 32)).
Common situations: Searching for full sentences or UUID/hash substrings as patterns; switching from a regex engine (no limit) to Bitap without checking length; patterns assembled dynamically from user input.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- The value for some a_i is smaller than 0.
- Pattern cannot start with *
- Invalid parameter settings for Ascon Hash
- Not enough space in input array for padding
- Invalid padding length
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/fa4b6f1420b141ab.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Strings/PatternMatching/Bitap.cs:94
var len = pattern.Length;
// An array of integers that will be used to mask the pattern.
// The pattern mask is a bitmask that we will use to search for the pattern characters
// in the text. We'll set the bit corresponding to the character in the pattern
// to 0, and then use bitwise operations to check for the pattern.
var patternMask = new int[128];
int index;
// Check if the pattern is empty.
if (string.IsNullOrEmpty(pattern))
{
return 0;
}
// Check if the pattern is longer than 31 characters.
if (len > 31)
{
throw new ArgumentException("The pattern is longer than 31 characters.");
}
// Initialize the register <c>R</c> to all 1s.
var r = ~1;
// Initialize the pattern mask to all 1s.
for (index = 0; index <= 127; ++index)
{
patternMask[index] = ~0;
}
// Set the bits corresponding to the characters in the pattern to 0 in the pattern mask.
for (index = 0; index < len; ++index)
{
patternMask[pattern[index]] &= ~(1 << index);
}
// Iterate through each character in the text.View on GitHub (pinned to 96e2905cab)