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
0c49d8ec
Commit
0c49d8ec
authored
Mar 23, 2015
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add str.zfill()
parent
62f7f2f4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
0 deletions
+39
-0
src/runtime/str.cpp
src/runtime/str.cpp
+37
-0
test/tests/str_functions.py
test/tests/str_functions.py
+2
-0
No files found.
src/runtime/str.cpp
View file @
0c49d8ec
...
...
@@ -2350,6 +2350,42 @@ overflow1:
return
NULL
;
}
static
PyObject
*
string_zfill
(
PyObject
*
self
,
PyObject
*
args
)
{
Py_ssize_t
fill
;
PyObject
*
s
;
char
*
p
;
Py_ssize_t
width
;
if
(
!
PyArg_ParseTuple
(
args
,
"n:zfill"
,
&
width
))
return
NULL
;
if
(
PyString_GET_SIZE
(
self
)
>=
width
)
{
if
(
PyString_CheckExact
(
self
))
{
Py_INCREF
(
self
);
return
(
PyObject
*
)
self
;
}
else
return
PyString_FromStringAndSize
(
PyString_AS_STRING
(
self
),
PyString_GET_SIZE
(
self
));
}
fill
=
width
-
PyString_GET_SIZE
(
self
);
// Pyston change:
// s = pad(self, fill, 0, '0');
s
=
pad
((
BoxedString
*
)
self
,
boxInt
(
width
),
boxString
(
"0"
),
JUST_RIGHT
);
if
(
s
==
NULL
)
return
NULL
;
p
=
PyString_AS_STRING
(
s
);
if
(
p
[
fill
]
==
'+'
||
p
[
fill
]
==
'-'
)
{
/* move sign to beginning of string */
p
[
0
]
=
p
[
fill
];
p
[
fill
]
=
'0'
;
}
return
(
PyObject
*
)
s
;
}
static
Py_ssize_t
string_buffer_getreadbuf
(
PyObject
*
self
,
Py_ssize_t
index
,
const
void
**
ptr
)
noexcept
{
RELEASE_ASSERT
(
index
==
0
,
""
);
// I think maybe this can just be a non-release assert? shouldn't be able to call this with
...
...
@@ -2407,6 +2443,7 @@ static PyMethodDef string_methods[] = {
{
"rfind"
,
(
PyCFunction
)
string_rfind
,
METH_VARARGS
,
NULL
},
{
"expandtabs"
,
(
PyCFunction
)
string_expandtabs
,
METH_VARARGS
,
NULL
},
{
"splitlines"
,
(
PyCFunction
)
string_splitlines
,
METH_VARARGS
,
NULL
},
{
"zfill"
,
(
PyCFunction
)
string_zfill
,
METH_VARARGS
,
NULL
},
};
void
setupStr
()
{
...
...
test/tests/str_functions.py
View file @
0c49d8ec
...
...
@@ -155,3 +155,5 @@ print "a.b.c.d".rpartition('.')
print
'ab c
\
n
\
n
de fg
\
r
kl
\
r
\
n
'
.
splitlines
()
print
'ab c
\
n
\
n
de fg
\
r
kl
\
r
\
n
'
.
splitlines
(
True
)
print
"1"
.
zfill
(
3
),
"+1"
.
zfill
(
3
),
"-1"
.
zfill
(
3
),
"0"
.
zfill
(
3
)
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