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
4b882187
Commit
4b882187
authored
Jun 02, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #75 from undingen/strip
Implement str.lstrip and str.rstrip
parents
1ac8b1ac
6c41c33a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
23 deletions
+60
-23
src/runtime/str.cpp
src/runtime/str.cpp
+55
-22
test/tests/str_functions.py
test/tests/str_functions.py
+5
-1
No files found.
src/runtime/str.cpp
View file @
4b882187
...
@@ -380,34 +380,55 @@ Box* strSplit2(BoxedString* self, BoxedString* sep) {
...
@@ -380,34 +380,55 @@ Box* strSplit2(BoxedString* self, BoxedString* sep) {
}
}
}
}
Box
*
strStrip
(
BoxedString
*
self
)
{
Box
*
strStrip
1
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
assert
(
self
->
cls
==
str_cls
);
return
new
BoxedString
(
llvm
::
StringRef
(
self
->
s
).
trim
(
"
\t\n\r\f\v
"
));
}
const
std
::
string
&
s
=
self
->
s
;
Box
*
strStrip2
(
BoxedString
*
self
,
Box
*
chars
)
{
int
n
=
s
.
size
();
assert
(
self
->
cls
==
str_cls
);
int
strip_beginning
=
0
;
if
(
chars
->
cls
==
str_cls
)
{
while
(
strip_beginning
<
n
)
{
return
new
BoxedString
(
llvm
::
StringRef
(
self
->
s
).
trim
(
static_cast
<
BoxedString
*>
(
chars
)
->
s
));
char
c
=
s
[
strip_beginning
];
}
else
if
(
chars
->
cls
==
none_cls
)
{
if
(
c
==
' '
||
c
==
'\t'
||
c
==
'\n'
||
c
==
'\r'
||
c
==
'\f'
||
c
==
'\v'
)
return
strStrip1
(
self
);
strip_beginning
++
;
}
else
{
else
raiseExcHelper
(
TypeError
,
"strip arg must be None, str or unicode"
);
break
;
}
}
}
if
(
strip_beginning
==
n
)
Box
*
strLStrip1
(
BoxedString
*
self
)
{
return
boxStrConstant
(
""
);
assert
(
self
->
cls
==
str_cls
);
return
new
BoxedString
(
llvm
::
StringRef
(
self
->
s
).
ltrim
(
"
\t\n\r\f\v
"
));
}
int
strip_end
=
0
;
Box
*
strLStrip2
(
BoxedString
*
self
,
Box
*
chars
)
{
while
(
strip_end
<
n
)
{
assert
(
self
->
cls
==
str_cls
);
char
c
=
s
[
n
-
strip_end
-
1
];
if
(
c
==
' '
||
c
==
'\t'
||
c
==
'\n'
||
c
==
'\r'
||
c
==
'\f'
||
c
==
'\v'
)
if
(
chars
->
cls
==
str_cls
)
{
strip_end
++
;
return
new
BoxedString
(
llvm
::
StringRef
(
self
->
s
).
ltrim
(
static_cast
<
BoxedString
*>
(
chars
)
->
s
));
else
}
else
if
(
chars
->
cls
==
none_cls
)
{
break
;
return
strLStrip1
(
self
);
}
else
{
raiseExcHelper
(
TypeError
,
"lstrip arg must be None, str or unicode"
);
}
}
}
Box
*
strRStrip1
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
return
new
BoxedString
(
llvm
::
StringRef
(
self
->
s
).
rtrim
(
"
\t\n\r\f\v
"
));
}
Box
*
strRStrip2
(
BoxedString
*
self
,
Box
*
chars
)
{
assert
(
self
->
cls
==
str_cls
);
return
new
BoxedString
(
s
.
substr
(
strip_beginning
,
n
-
strip_beginning
-
strip_end
));
if
(
chars
->
cls
==
str_cls
)
{
return
new
BoxedString
(
llvm
::
StringRef
(
self
->
s
).
rtrim
(
static_cast
<
BoxedString
*>
(
chars
)
->
s
));
}
else
if
(
chars
->
cls
==
none_cls
)
{
return
strRStrip1
(
self
);
}
else
{
raiseExcHelper
(
TypeError
,
"rstrip arg must be None, str or unicode"
);
}
}
}
Box
*
strContains
(
BoxedString
*
self
,
Box
*
elt
)
{
Box
*
strContains
(
BoxedString
*
self
,
Box
*
elt
)
{
...
@@ -541,7 +562,19 @@ void setupStr() {
...
@@ -541,7 +562,19 @@ void setupStr() {
str_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strNonzero
,
NULL
,
1
,
false
)));
str_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strNonzero
,
NULL
,
1
,
false
)));
str_cls
->
giveAttr
(
"lower"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strLower
,
STR
,
1
,
false
)));
str_cls
->
giveAttr
(
"lower"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strLower
,
STR
,
1
,
false
)));
str_cls
->
giveAttr
(
"strip"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strStrip
,
STR
,
1
,
false
)));
CLFunction
*
strStrip
=
boxRTFunction
((
void
*
)
strStrip1
,
STR
,
1
,
false
);
addRTFunction
(
strStrip
,
(
void
*
)
strStrip2
,
STR
,
2
,
false
);
str_cls
->
giveAttr
(
"strip"
,
new
BoxedFunction
(
strStrip
));
CLFunction
*
strLStrip
=
boxRTFunction
((
void
*
)
strLStrip1
,
STR
,
1
,
false
);
addRTFunction
(
strLStrip
,
(
void
*
)
strLStrip2
,
STR
,
2
,
false
);
str_cls
->
giveAttr
(
"lstrip"
,
new
BoxedFunction
(
strLStrip
));
CLFunction
*
strRStrip
=
boxRTFunction
((
void
*
)
strRStrip1
,
STR
,
1
,
false
);
addRTFunction
(
strRStrip
,
(
void
*
)
strRStrip2
,
STR
,
2
,
false
);
str_cls
->
giveAttr
(
"rstrip"
,
new
BoxedFunction
(
strRStrip
));
str_cls
->
giveAttr
(
"__contains__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strContains
,
BOXED_BOOL
,
2
,
false
)));
str_cls
->
giveAttr
(
"__contains__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strContains
,
BOXED_BOOL
,
2
,
false
)));
str_cls
->
giveAttr
(
"__add__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strAdd
,
NULL
,
2
,
false
)));
str_cls
->
giveAttr
(
"__add__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strAdd
,
NULL
,
2
,
false
)));
...
...
test/tests/str_functions.py
View file @
4b882187
...
@@ -20,7 +20,11 @@ print map(bool, ["hello", "", "world"])
...
@@ -20,7 +20,11 @@ print map(bool, ["hello", "", "world"])
if
""
:
if
""
:
print
"bad"
print
"bad"
print
repr
(
"
\
t
\
n
\
v
\
f
test
\
t
\
n
\
v
\
f
"
.
strip
())
testStr
=
"
\
t
\
n
\
v
\
f
test
\
t
\
n
\
v
\
f
"
print
repr
(
testStr
.
strip
()),
repr
(
testStr
.
lstrip
()),
repr
(
testStr
.
rstrip
())
for
c
in
[
None
,
" "
,
"
\
t
\
f
"
,
"test"
]:
print
repr
(
testStr
.
strip
(
c
)),
repr
(
testStr
.
lstrip
(
c
)),
repr
(
testStr
.
rstrip
(
c
))
for
pattern
in
[
"hello"
,
"o w"
,
"nope"
]:
for
pattern
in
[
"hello"
,
"o w"
,
"nope"
]:
print
pattern
in
"hello world"
print
pattern
in
"hello world"
...
...
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