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
4cfcfafb
Commit
4cfcfafb
authored
Aug 11, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'str_iseverything' of
https://github.com/kkszysiu/pyston
Merges PR #109
parents
32ccd8a8
c0516913
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
238 additions
and
6 deletions
+238
-6
src/runtime/str.cpp
src/runtime/str.cpp
+147
-0
test/tests/str_manipulation.py
test/tests/str_manipulation.py
+91
-6
No files found.
src/runtime/str.cpp
View file @
4cfcfafb
...
...
@@ -376,6 +376,145 @@ Box* _strSlice(BoxedString* self, i64 start, i64 stop, i64 step) {
return
boxString
(
std
::
string
(
chars
.
begin
(),
chars
.
end
()));
}
Box
*
strIsAlpha
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
c
:
str
)
{
if
(
!
std
::
isalpha
(
c
))
return
False
;
}
return
True
;
}
Box
*
strIsDigit
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
c
:
str
)
{
if
(
!
std
::
isdigit
(
c
))
return
False
;
}
return
True
;
}
Box
*
strIsAlnum
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
c
:
str
)
{
if
(
!
std
::
isalnum
(
c
))
return
False
;
}
return
True
;
}
Box
*
strIsLower
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
bool
lowered
=
false
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
c
:
str
)
{
if
(
std
::
isspace
(
c
)
||
std
::
isdigit
(
c
))
{
continue
;
}
else
if
(
!
std
::
islower
(
c
))
{
return
False
;
}
else
{
lowered
=
true
;
}
}
return
boxBool
(
lowered
);
}
Box
*
strIsUpper
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
bool
uppered
=
false
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
c
:
str
)
{
if
(
std
::
isspace
(
c
)
||
std
::
isdigit
(
c
))
{
continue
;
}
else
if
(
!
std
::
isupper
(
c
))
{
return
False
;
}
else
{
uppered
=
true
;
}
}
return
boxBool
(
uppered
);
}
Box
*
strIsSpace
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
c
:
str
)
{
if
(
!
std
::
isspace
(
c
))
return
False
;
}
return
True
;
}
Box
*
strIsTitle
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
const
std
::
string
&
str
(
self
->
s
);
if
(
str
.
empty
())
return
False
;
if
(
str
.
size
()
==
1
)
return
boxBool
(
std
::
isupper
(
str
[
0
]));
bool
cased
=
false
,
start_of_word
=
true
;
for
(
const
auto
&
c
:
str
)
{
if
(
std
::
isupper
(
c
))
{
if
(
!
start_of_word
)
{
return
False
;
}
start_of_word
=
false
;
cased
=
true
;
}
else
if
(
std
::
islower
(
c
))
{
if
(
start_of_word
)
{
return
False
;
}
start_of_word
=
false
;
cased
=
true
;
}
else
{
start_of_word
=
true
;
}
}
return
boxBool
(
cased
);
}
Box
*
strJoin
(
BoxedString
*
self
,
Box
*
rhs
)
{
assert
(
self
->
cls
==
str_cls
);
...
...
@@ -660,6 +799,14 @@ void setupStr() {
str_cls
->
giveAttr
(
"__hash__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strHash
,
BOXED_INT
,
1
)));
str_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strNonzero
,
BOXED_BOOL
,
1
)));
str_cls
->
giveAttr
(
"isalnum"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsAlnum
,
STR
,
1
)));
str_cls
->
giveAttr
(
"isalpha"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsAlpha
,
STR
,
1
)));
str_cls
->
giveAttr
(
"isdigit"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsDigit
,
STR
,
1
)));
str_cls
->
giveAttr
(
"islower"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsLower
,
STR
,
1
)));
str_cls
->
giveAttr
(
"isspace"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsSpace
,
STR
,
1
)));
str_cls
->
giveAttr
(
"istitle"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsTitle
,
STR
,
1
)));
str_cls
->
giveAttr
(
"isupper"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsUpper
,
STR
,
1
)));
str_cls
->
giveAttr
(
"lower"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strLower
,
STR
,
1
)));
str_cls
->
giveAttr
(
"swapcase"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strSwapcase
,
STR
,
1
)));
str_cls
->
giveAttr
(
"upper"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strUpper
,
STR
,
1
)));
...
...
test/tests/str_manipulation.py
View file @
4cfcfafb
...
...
@@ -32,12 +32,6 @@ try:
except
TypeError
:
print
'TypeError raised'
for
i
in
xrange
(
256
):
c
=
chr
(
i
)
s
=
"a%sb"
%
c
if
s
.
title
()[
2
]
==
'b'
:
print
repr
(
c
)
try
:
var
=
'hello'
var
.
lower
(
42
)
...
...
@@ -58,3 +52,94 @@ try:
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
# Testing isalnum, isalpha, isdigit, islower, isspace, istitle and isupper methods
def
test_is
(
s
):
print
'string:'
,
repr
(
s
),
'isalnum:'
,
s
.
isalnum
(),
'isalpha:'
,
s
.
isalpha
(),
'isdigit:'
,
s
.
isdigit
(),
'islower:'
,
s
.
islower
(),
'isspace:'
,
s
.
isspace
(),
'istitle:'
,
s
.
istitle
(),
'isupper:'
,
s
.
isupper
()
test_is
(
''
)
test_is
(
'a'
)
test_is
(
'A'
)
test_is
(
'123abc456'
)
test_is
(
'a1b3c'
)
test_is
(
'aBc000 '
)
test_is
(
'abc
\
n
'
)
test_is
(
'aBc123'
)
test_is
(
'abc'
)
test_is
(
'0'
)
test_is
(
'0123456789'
)
test_is
(
'0123456789a'
)
test_is
(
' '
)
test_is
(
'
\
t
'
)
test_is
(
'
\
r
'
)
test_is
(
'
\
n
'
)
test_is
(
'
\
t
\
r
\
n
'
)
test_is
(
'
\
t
\
r
\
n
a'
)
test_is
(
'A Titlecased Line'
)
test_is
(
'A
\
n
Titlecased Line'
)
test_is
(
'A Titlecased, Line'
)
test_is
(
'Not a capitalized String'
)
test_is
(
'Not
\
t
a Titlecase String'
)
test_is
(
'Not--a Titlecase String'
)
test_is
(
'NOT'
)
for
i
in
xrange
(
256
):
c
=
chr
(
i
)
s
=
"a%sb"
%
c
if
s
.
title
()[
2
]
==
'b'
:
print
repr
(
c
)
test
(
c
)
test_is
(
c
)
try
:
var
=
'abc'
var
.
isalnum
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
try
:
var
=
'abc'
var
.
isalpha
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
try
:
var
=
'abc'
var
.
isdigit
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
try
:
var
=
'abc'
var
.
islower
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
try
:
var
=
'abc'
var
.
isspace
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
try
:
var
=
'abc'
var
.
istitle
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
try
:
var
=
'abc'
var
.
isupper
(
42
)
print
'TypeError not raised'
except
TypeError
:
print
'TypeError raised'
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