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
56b9a70e
Commit
56b9a70e
authored
Apr 28, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #41 from undingen/strSplit2
Implement string.split(sep)
parents
a9783add
c44dfc56
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
2 deletions
+35
-2
src/runtime/str.cpp
src/runtime/str.cpp
+32
-1
test/tests/str_functions.py
test/tests/str_functions.py
+3
-1
No files found.
src/runtime/str.cpp
View file @
56b9a70e
...
...
@@ -28,6 +28,9 @@
#include "runtime/types.h"
#include "runtime/util.h"
#include <llvm/ADT/StringRef.h>
#include <llvm/ADT/SmallVector.h>
namespace
pyston
{
extern
"C"
BoxedString
*
strAdd
(
BoxedString
*
lhs
,
Box
*
_rhs
)
{
...
...
@@ -345,6 +348,31 @@ Box* strSplit1(BoxedString* self) {
return
rtn
;
}
Box
*
strSplit2
(
BoxedString
*
self
,
BoxedString
*
sep
)
{
assert
(
self
->
cls
==
str_cls
);
if
(
sep
->
cls
==
str_cls
)
{
if
(
!
sep
->
s
.
empty
())
{
llvm
::
SmallVector
<
llvm
::
StringRef
,
16
>
parts
;
llvm
::
StringRef
(
self
->
s
).
split
(
parts
,
sep
->
s
);
BoxedList
*
rtn
=
new
BoxedList
();
for
(
const
auto
&
s
:
parts
)
listAppendInternal
(
rtn
,
boxString
(
s
.
str
()));
return
rtn
;
}
else
{
fprintf
(
stderr
,
"ValueError: empty separator
\n
"
);
raiseExc
();
}
}
else
if
(
sep
->
cls
==
none_cls
)
{
return
strSplit1
(
self
);
}
else
{
fprintf
(
stderr
,
"TypeError: expected a character buffer object
\n
"
);
raiseExc
();
}
}
extern
"C"
Box
*
strGetitem
(
BoxedString
*
self
,
Box
*
slice
)
{
if
(
slice
->
cls
==
int_cls
)
{
BoxedInt
*
islice
=
static_cast
<
BoxedInt
*>
(
slice
);
...
...
@@ -390,7 +418,10 @@ void setupStr() {
str_cls
->
giveAttr
(
"__getitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strGetitem
,
NULL
,
2
,
false
)));
str_cls
->
giveAttr
(
"join"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strJoin
,
NULL
,
2
,
false
)));
str_cls
->
giveAttr
(
"split"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strSplit1
,
LIST
,
1
,
false
)));
CLFunction
*
strSplit
=
boxRTFunction
((
void
*
)
strSplit1
,
LIST
,
1
,
false
);
addRTFunction
(
strSplit
,
(
void
*
)
strSplit2
,
LIST
,
2
,
false
);
str_cls
->
giveAttr
(
"split"
,
new
BoxedFunction
(
strSplit
));
CLFunction
*
__new__
=
boxRTFunction
((
void
*
)
strNew1
,
NULL
,
1
,
false
);
addRTFunction
(
__new__
,
(
void
*
)
strNew2
,
NULL
,
2
,
false
);
...
...
test/tests/str_functions.py
View file @
56b9a70e
...
...
@@ -7,4 +7,6 @@ print repr("'\"")
print
"hello world
\
t
more
\
n
words
\
v
a
\
f
b
\
a
o"
.
split
()
print
" test "
.
split
()
# print " test ".split(' ')
print
" test "
.
split
(
' '
)
print
" test "
.
split
(
None
)
print
"1<>2<>3"
.
split
(
'<>'
)
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