{"record":{"id":"fa4b6f1420b141ab","repo":"TheAlgorithms/C-Sharp","slug":"the-pattern-is-longer-than-31-characters","errorCode":null,"errorMessage":"The pattern is longer than 31 characters.","messagePattern":"The pattern is longer than 31 characters\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Strings/PatternMatching/Bitap.cs","lineNumber":94,"sourceCode":"        var len = pattern.Length;\n\n        // An array of integers that will be used to mask the pattern.\n        // The pattern mask is a bitmask that we will use to search for the pattern characters\n        // in the text. We'll set the bit corresponding to the character in the pattern\n        // to 0, and then use bitwise operations to check for the pattern.\n        var patternMask = new int[128];\n        int index;\n\n        // Check if the pattern is empty.\n        if (string.IsNullOrEmpty(pattern))\n        {\n            return 0;\n        }\n\n        // Check if the pattern is longer than 31 characters.\n        if (len > 31)\n        {\n            throw new ArgumentException(\"The pattern is longer than 31 characters.\");\n        }\n\n        // Initialize the register <c>R</c> to all 1s.\n        var r = ~1;\n\n        // Initialize the pattern mask to all 1s.\n        for (index = 0; index <= 127; ++index)\n        {\n            patternMask[index] = ~0;\n        }\n\n        // Set the bits corresponding to the characters in the pattern to 0 in the pattern mask.\n        for (index = 0; index < len; ++index)\n        {\n            patternMask[pattern[index]] &= ~(1 << index);\n        }\n\n        // Iterate through each character in the text.","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Strings/PatternMatching/Bitap.cs#L76-L112","documentation":"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.","triggerScenarios":"Calling FindExactPattern with a pattern whose length is 32 or more, e.g. FindExactPattern(text, new string('a', 32)).","commonSituations":"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.","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."],"exampleFix":"// before\nvar index = BitapAlgorithm.FindExactPattern(text, longPattern); // may exceed 31 chars\n// after\nvar index = longPattern.Length <= 31\n    ? BitapAlgorithm.FindExactPattern(text, longPattern)\n    : text.IndexOf(longPattern, StringComparison.Ordinal);","handlingStrategy":"validation","validationCode":"if (pattern.Length > 31)\n{\n    // use an alternative search (e.g. text.IndexOf) or split the pattern\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    index = BitapAlgorithm.FindExactPattern(text, pattern);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"longer than 31\"))\n{\n    index = text.IndexOf(pattern, StringComparison.Ordinal); // fallback\n}","preventionTips":["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."],"tags":["value-out-of-range","bitap","pattern-matching","csharp","limitation"],"backgroundTag":"value-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}