Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
4a487488
Commit
4a487488
authored
Feb 25, 2015
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement str.translate() with delete chars
parent
9cd8624a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
11 deletions
+23
-11
src/runtime/str.cpp
src/runtime/str.cpp
+21
-11
test/tests/str_functions.py
test/tests/str_functions.py
+2
-0
No files found.
src/runtime/str.cpp
View file @
4a487488
...
...
@@ -1690,21 +1690,31 @@ Box* strTitle(BoxedString* self) {
}
Box
*
strTranslate
(
BoxedString
*
self
,
BoxedString
*
table
,
BoxedString
*
delete_chars
)
{
RELEASE_ASSERT
(
self
->
cls
==
str_cls
,
""
);
RELEASE_ASSERT
(
table
->
cls
==
str_cls
,
""
);
RELEASE_ASSERT
(
delete_chars
==
NULL
||
delete_chars
->
cls
==
str_cls
,
""
);
RELEASE_ASSERT
(
delete_chars
==
NULL
||
delete_chars
->
s
.
size
()
==
0
,
"delete_chars not supported yet"
);
if
(
self
->
cls
!=
str_cls
)
raiseExcHelper
(
TypeError
,
"descriptor 'translate' requires a 'str' object but received a '%s'"
,
getTypeName
(
self
));
std
::
ostringstream
oss
;
std
::
unordered_set
<
char
>
delete_set
;
if
(
delete_chars
)
{
if
(
delete_chars
->
cls
!=
str_cls
)
raiseExcHelper
(
TypeError
,
"expected a character buffer object"
);
delete_set
.
insert
(
delete_chars
->
s
.
begin
(),
delete_chars
->
s
.
end
());
}
if
(
table
->
s
.
size
()
!=
256
)
raiseExcHelper
(
ValueError
,
"translation table must be 256 characters long"
);
bool
have_table
=
table
!=
None
;
if
(
have_table
)
{
if
(
table
->
cls
!=
str_cls
)
raiseExcHelper
(
TypeError
,
"expected a character buffer object"
);
if
(
table
->
s
.
size
()
!=
256
)
raiseExcHelper
(
ValueError
,
"translation table must be 256 characters long"
);
}
for
(
unsigned
char
c
:
self
->
s
)
{
oss
<<
table
->
s
[
c
];
std
::
string
str
;
for
(
const
char
c
:
self
->
s
)
{
if
(
!
delete_set
.
count
(
c
))
str
.
append
(
1
,
have_table
?
table
->
s
[(
unsigned
char
)
c
]
:
c
);
}
return
boxString
(
oss
.
str
(
));
return
boxString
(
std
::
move
(
str
));
}
Box
*
strLower
(
BoxedString
*
self
)
{
...
...
test/tests/str_functions.py
View file @
4a487488
...
...
@@ -70,6 +70,8 @@ for c in "aeiou":
translation_map
=
''
.
join
(
translation_map
)
print
"hello world"
.
translate
(
translation_map
)
print
"hello world"
.
translate
(
translation_map
,
""
)
print
"hello world"
.
translate
(
translation_map
,
"llo"
)
print
"hello world"
.
translate
(
None
,
"llo"
)
for
i
in
xrange
(
-
10
,
10
):
print
i
,
"aaaaa"
.
find
(
"a"
,
i
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment