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
488c1139
Commit
488c1139
authored
Jul 24, 2014
by
Krzysztof Klinikowski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implementation of isalnum, isalpha, isdigit, islower, isspace, istitle and isupper
parent
e891c285
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
348 additions
and
0 deletions
+348
-0
src/runtime/str.cpp
src/runtime/str.cpp
+144
-0
test/tests/str_isalnum_test.py
test/tests/str_isalnum_test.py
+31
-0
test/tests/str_isalpha_test.py
test/tests/str_isalpha_test.py
+27
-0
test/tests/str_isdigit_test.py
test/tests/str_isdigit_test.py
+21
-0
test/tests/str_islower_test.py
test/tests/str_islower_test.py
+28
-0
test/tests/str_isspace_test.py
test/tests/str_isspace_test.py
+30
-0
test/tests/str_istitle_test.py
test/tests/str_istitle_test.py
+39
-0
test/tests/str_isupper_test.py
test/tests/str_isupper_test.py
+28
-0
No files found.
src/runtime/str.cpp
View file @
488c1139
...
...
@@ -377,6 +377,142 @@ 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
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
i
:
str
)
if
(
!
std
::
isalpha
(
str
[
i
]))
return
False
;
return
True
;
}
Box
*
strIsDigit
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
i
:
str
)
if
(
!
std
::
isdigit
(
str
[
i
]))
return
False
;
return
True
;
}
Box
*
strIsAlnum
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
i
:
str
)
if
(
!
std
::
isalnum
(
str
[
i
]))
return
False
;
return
True
;
}
Box
*
strIsLower
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
i
:
str
)
if
(
!
std
::
islower
(
i
))
return
False
;
return
True
;
}
Box
*
strIsUpper
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
i
:
str
)
if
(
!
std
::
isupper
(
str
[
i
]))
return
False
;
return
True
;
}
Box
*
strIsSpace
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
for
(
const
auto
&
i
:
str
)
if
(
!
std
::
isspace
(
str
[
i
]))
return
False
;
return
True
;
}
Box
*
strIsTitle
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
str
(
self
->
s
);
std
::
string
::
size_type
i
;
if
(
str
.
empty
())
return
False
;
if
(
str
.
size
()
==
1
)
return
boxBool
(
std
::
isupper
(
str
[
0
]));
bool
cased
=
false
,
previous_is_cased
=
false
;
for
(
const
auto
&
i
:
str
)
{
if
(
std
::
isupper
(
str
[
i
]))
{
if
(
previous_is_cased
)
{
return
False
;
}
previous_is_cased
=
true
;
cased
=
true
;
}
else
if
(
std
::
islower
(
str
[
i
]))
{
if
(
!
previous_is_cased
)
{
return
False
;
}
previous_is_cased
=
true
;
cased
=
true
;
}
else
{
previous_is_cased
=
false
;
}
}
return
boxBool
(
cased
);
}
Box
*
strLower
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
std
::
string
lowered
(
self
->
s
);
std
::
transform
(
lowered
.
begin
(),
lowered
.
end
(),
lowered
.
begin
(),
tolower
);
return
boxString
(
std
::
move
(
lowered
));
}
Box
*
strJoin
(
BoxedString
*
self
,
Box
*
rhs
)
{
assert
(
self
->
cls
==
str_cls
);
...
...
@@ -664,6 +800,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_isalnum_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
isalnum
()
==
False
var
=
'a'
assert
var
.
isalnum
()
==
True
var
=
'A'
assert
var
.
isalnum
()
==
True
var
=
'
\
n
'
assert
var
.
isalnum
()
==
False
var
=
'123abc456'
assert
var
.
isalnum
()
==
True
var
=
'a1b3c'
assert
var
.
isalnum
()
==
True
var
=
'aBc000 '
assert
var
.
isalnum
()
==
False
var
=
'abc
\
n
'
assert
var
.
isalnum
()
==
False
try
:
var
=
'abc'
var
.
isalnum
(
42
)
assert
False
except
TypeError
:
assert
True
test/tests/str_isalpha_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
isalpha
()
==
False
var
=
'a'
assert
var
.
isalpha
()
==
True
var
=
'A'
assert
var
.
isalpha
()
==
True
var
=
'
\
n
'
assert
var
.
isalpha
()
==
False
var
=
'abc'
assert
var
.
isalpha
()
==
True
var
=
'aBc123'
assert
var
.
isalpha
()
==
False
var
=
'abc
\
n
'
assert
var
.
isalpha
()
==
False
try
:
var
=
'abc'
var
.
isalpha
(
42
)
assert
False
except
TypeError
:
assert
True
test/tests/str_isdigit_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
isdigit
()
==
False
var
=
'a'
assert
var
.
isdigit
()
==
False
var
=
'0'
assert
var
.
isdigit
()
==
True
var
=
'0123456789'
assert
var
.
isdigit
()
==
True
var
=
'0123456789a'
assert
var
.
isdigit
()
==
False
try
:
var
=
'abc'
var
.
isdigit
(
42
)
assert
False
except
TypeError
:
assert
True
test/tests/str_islower_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
islower
()
==
False
var
=
'a'
assert
var
.
islower
()
==
True
var
=
'A'
assert
var
.
islower
()
==
False
var
=
'
\
n
'
assert
var
.
islower
()
==
False
var
=
'abc'
assert
var
.
islower
()
==
True
var
=
'aBc'
assert
var
.
islower
()
==
False
# TODO: not supported yet
#var = 'abc\n'
#assert var.islower() == True
try
:
var
=
'abc'
var
.
islower
(
42
)
assert
False
except
TypeError
:
assert
True
test/tests/str_isspace_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
isspace
()
==
False
var
=
'a'
assert
var
.
isspace
()
==
False
var
=
' '
assert
var
.
isspace
()
==
True
var
=
'
\
t
'
assert
var
.
isspace
()
==
True
var
=
'
\
r
'
assert
var
.
isspace
()
==
True
var
=
'
\
n
'
assert
var
.
isspace
()
==
True
var
=
'
\
t
\
r
\
n
'
assert
var
.
isspace
()
==
True
var
=
'
\
t
\
r
\
n
a'
assert
var
.
isspace
()
==
False
try
:
var
=
'abc'
var
.
isspace
(
42
)
assert
False
except
TypeError
:
assert
True
test/tests/str_istitle_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
istitle
()
==
False
var
=
'a'
assert
var
.
istitle
()
==
False
var
=
'A'
assert
var
.
istitle
()
==
True
var
=
'
\
n
'
assert
var
.
istitle
()
==
False
var
=
'A Titlecased Line'
assert
var
.
istitle
()
==
True
var
=
'A
\
n
Titlecased Line'
assert
var
.
istitle
()
==
True
var
=
'A Titlecased, Line'
assert
var
.
istitle
()
==
True
var
=
'Not a capitalized String'
assert
var
.
istitle
()
==
False
var
=
'Not
\
t
a Titlecase String'
assert
var
.
istitle
()
==
False
var
=
'Not--a Titlecase String'
assert
var
.
istitle
()
==
False
var
=
'NOT'
assert
var
.
istitle
()
==
False
try
:
var
=
'abc'
var
.
istitle
(
42
)
assert
False
except
TypeError
:
assert
True
test/tests/str_isupper_test.py
0 → 100644
View file @
488c1139
var
=
''
assert
var
.
isupper
()
==
False
var
=
'a'
assert
var
.
isupper
()
==
False
var
=
'A'
assert
var
.
isupper
()
==
True
var
=
'
\
n
'
assert
var
.
isupper
()
==
False
var
=
'ABC'
assert
var
.
isupper
()
==
True
var
=
'AbC'
assert
var
.
isupper
()
==
False
# TODO: not supported yet
#var = 'ABC\n'
#assert var.isupper() == True
try
:
var
=
'abc'
var
.
isupper
(
42
)
assert
False
except
TypeError
:
assert
True
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