-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement str.expandtabs() based on CPython #4858
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97111a8
Implement str.expandtabs() based on CPython
densmirn da43b20
Merge branch 'master' of https://github.com/numba/numba into feature/…
densmirn 61d44d9
Fix flake8 issue for str.expandtabs()
densmirn 8b061bb
Add constant variables to the outer scope
densmirn 73e0d1a
Merge branch 'master' of https://github.com/numba/numba into feature/…
densmirn 98f2441
Move "import sys" above
densmirn e775317
Merge branch 'master' into feature/str_expandtabs
seibert File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import operator | ||
import sys | ||
|
||
import numpy as np | ||
from llvmlite.ir import IntType, Constant | ||
|
@@ -675,6 +676,71 @@ def endswith_impl(a, b): | |
return endswith_impl | ||
|
||
|
||
# https://github.com/python/cpython/blob/1d4b6ba19466aba0eb91c4ba01ba509acf18c723/Objects/unicodeobject.c#L11519-L11595 # noqa: E501 | ||
@overload_method(types.UnicodeType, 'expandtabs') | ||
def unicode_expandtabs(data, tabsize=8): | ||
"""Implements str.expandtabs()""" | ||
thety = tabsize | ||
if isinstance(tabsize, types.Omitted): | ||
thety = tabsize.value | ||
# if the type is optional, the concrete type is the captured type | ||
elif isinstance(tabsize, types.Optional): | ||
thety = tabsize.type | ||
|
||
accepted = (types.Integer, int) | ||
if thety is not None and not isinstance(thety, accepted): | ||
raise TypingError( | ||
'"tabsize" must be {}, not {}'.format(accepted, tabsize)) | ||
|
||
def expandtabs_impl(data, tabsize=8): | ||
length = len(data) | ||
j = line_pos = 0 | ||
found = False | ||
for i in range(length): | ||
code_point = _get_code_point(data, i) | ||
if code_point == 9: # 0x9 '\t' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity perhaps use variables in the outer scope like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added such variables. |
||
found = True | ||
if tabsize > 0: | ||
# cannot overflow | ||
incr = tabsize - (line_pos % tabsize) | ||
if j > sys.maxsize - incr: | ||
raise OverflowError('new string is too long') | ||
line_pos += incr | ||
j += incr | ||
else: | ||
if j > sys.maxsize - 1: | ||
raise OverflowError('new string is too long') | ||
line_pos += 1 | ||
j += 1 | ||
if code_point in (10, 13): # (0xa '\n', 0xd '\r') | ||
line_pos = 0 | ||
|
||
if not found: | ||
return data | ||
|
||
res = _empty_string(data._kind, j, data._is_ascii) | ||
j = line_pos = 0 | ||
for i in range(length): | ||
code_point = _get_code_point(data, i) | ||
if code_point == 9: # 0x9 '\t' | ||
if tabsize > 0: | ||
incr = tabsize - (line_pos % tabsize) | ||
line_pos += incr | ||
for idx in range(j, j + incr): | ||
_set_code_point(res, idx, 32) # 0x20 ' ' | ||
j += incr | ||
else: | ||
line_pos += 1 | ||
_set_code_point(res, j, code_point) | ||
j += 1 | ||
if code_point in (10, 13): # (0xa '\n', 0xd '\r') | ||
line_pos = 0 | ||
|
||
return res | ||
|
||
return expandtabs_impl | ||
|
||
|
||
@overload_method(types.UnicodeType, 'split') | ||
def unicode_split(a, sep=None, maxsplit=-1): | ||
if not (maxsplit == -1 or | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!