{"record":{"id":"dab482921ddf9c10","repo":"TheAlgorithms/Python","slug":"we-need-some-text-to-work-with","errorCode":null,"errorMessage":"We need some text to work with.","messagePattern":"We need some text to work with\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_compression/lz77.py","lineNumber":164,"sourceCode":"\n        Tests:\n            >>> lz77_compressor = LZ77Compressor()\n            >>> lz77_compressor._find_encoding_token(\"abrarrarrad\", \"abracad\").offset\n            7\n            >>> lz77_compressor._find_encoding_token(\"adabrarrarrad\", \"cabrac\").length\n            1\n            >>> lz77_compressor._find_encoding_token(\"abc\", \"xyz\").offset\n            0\n            >>> lz77_compressor._find_encoding_token(\"\", \"xyz\").offset\n            Traceback (most recent call last):\n                ...\n            ValueError: We need some text to work with.\n            >>> lz77_compressor._find_encoding_token(\"abc\", \"\").offset\n            0\n        \"\"\"\n\n        if not text:\n            raise ValueError(\"We need some text to work with.\")\n\n        # Initialise result parameters to default values\n        length, offset = 0, 0\n\n        if not search_buffer:\n            return Token(offset, length, text[length])\n\n        for i, character in enumerate(search_buffer):\n            found_offset = len(search_buffer) - i\n            if character == text[0]:\n                found_length = self._match_length_from_index(text, search_buffer, 0, i)\n                # if the found length is bigger than the current or if it's equal,\n                # which means it's offset is smaller: update offset and length\n                if found_length >= length:\n                    offset, length = found_offset, found_length\n\n        return Token(offset, length, text[length])\n","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_compression/lz77.py#L146-L182","documentation":"Raised by Lz77._find_encoding_token() in data_compression/lz77.py when the text argument is empty (falsy). The token encoder must return at least one literal character (Token(offset, length, text[length])), which is impossible with no text, so empty text is rejected. Note the empty search_buffer is fine — only empty text raises.","triggerScenarios":"Calling _find_encoding_token('', 'xyz') as in the doctest, or feeding an empty remainder during compression when the sliding window logic advances past the end of input and calls the method with an empty slice.","commonSituations":"Custom integrations that reuse this private helper on their own windowing scheme and mis-handle the final iteration; empty file or empty chunk reaching the compressor's inner loop.","solutions":["Do not call _find_encoding_token with an empty text slice — check `if not text: break` in your loop before the call.","If compressing user data, short-circuit empty inputs at the top of your compress function.","Prefer the public compress/decompress API of the Lz77 class instead of the private helper."],"exampleFix":"# before\nfor i in range(len(data)):\n    token = self._find_encoding_token(data[i:], window)  # last call may get ''\n\n# after\nremaining = data[i:]\nif not remaining:\n    break\ntoken = self._find_encoding_token(remaining, window)","handlingStrategy":"validation","validationCode":"remaining = text[pos:]\nif not remaining:\n    break  # done, no more tokens\ntoken = lz77_compressor._find_encoding_token(remaining, search_buffer)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call the private token finder with an empty text slice","Use the public compress() API, which handles windowing correctly"],"tags":["value-validation","lz77","compression","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}