Fix Mongolian ordinal IndexError on zero#661
Open
patchwright wants to merge 1 commit into
Open
Conversation
num2words(0, lang='mn', to='ordinal') and to='ordinal_num' raised IndexError because _get_ordinal_suffix indexed number_str[-2] on the single-character string '0'. The base contract accepts 0 (English returns 'zeroth'/'0th'; Dutch was fixed the same way in savoirfairelinux#365), so the Mongolian module should honour it too. Guard the tens-digit branch with a length check so 0 falls through to the default 'дугаар' suffix.
Author
|
Gentle nudge on this one. The fix adds a length guard for deep negative indexing ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
num2words(0, lang='mn', to='ordinal')raisesIndexError: string index out of rangeinstead of returning an ordinal string.Reproduction
Note
to='ordinal'for other languages does not crash on zero, e.g.num2words(0, lang='en', to='ordinal')returns'zeroth'— so this is anmn-specific gap.Cause
In
num2words/lang_MN.py, the ordinal suffix logic indexes the second-to-last character unconditionally:For input
0,number_stris'0'(length 1), so[-2]is out of range.Fix
Guard the index access:
For zero, the condition short-circuits and the suffix falls through to the default
'дугаар'branch. That is the grammatically consistent outcome:0is not in the special set (1,4,9) that take distinct ordinal suffixes, so it receives the same default suffix as other non-special values (e.g.10,100).Tests
Adds
test_to_ordinal_zeroandtest_to_ordinal_num_zero, assertingnum2words(0, lang='mn', to='ordinal') == 'тэг дугаар'and'0 дугаар'respectively. Both raiseIndexErroron the current code and pass with the fix.