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
3dc94a15
Commit
3dc94a15
authored
Jul 18, 2016
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
str: use cpythons [r]partition
it support non str separators fixes #1297
parent
ae075362
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
35 deletions
+60
-35
from_cpython/Objects/stringlib/stringdefs.h
from_cpython/Objects/stringlib/stringdefs.h
+5
-1
from_cpython/Objects/stringobject.c
from_cpython/Objects/stringobject.c
+50
-0
src/runtime/str.cpp
src/runtime/str.cpp
+4
-34
test/tests/str_functions.py
test/tests/str_functions.py
+1
-0
No files found.
from_cpython/Objects/stringlib/stringdefs.h
View file @
3dc94a15
...
@@ -10,7 +10,11 @@
...
@@ -10,7 +10,11 @@
#define STRINGLIB_CHAR char
#define STRINGLIB_CHAR char
#define STRINGLIB_TYPE_NAME "string"
#define STRINGLIB_TYPE_NAME "string"
#define STRINGLIB_PARSE_CODE "S"
#define STRINGLIB_PARSE_CODE "S"
#define STRINGLIB_EMPTY nullstring
// Pyston change:
// #define STRINGLIB_EMPTY nullstring
extern
PyObject
*
EmptyString
;
#define STRINGLIB_EMPTY EmptyString
#define STRINGLIB_ISSPACE Py_ISSPACE
#define STRINGLIB_ISSPACE Py_ISSPACE
#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
#define STRINGLIB_ISDECIMAL(x) ((x >= '0') && (x <= '9'))
#define STRINGLIB_ISDECIMAL(x) ((x >= '0') && (x <= '9'))
...
...
from_cpython/Objects/stringobject.c
View file @
3dc94a15
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
#include "stringlib/count.h"
#include "stringlib/count.h"
#include "stringlib/find.h"
#include "stringlib/find.h"
#include "stringlib/partition.h"
#include "stringlib/split.h"
#include "stringlib/split.h"
#define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping
#define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping
...
@@ -1014,3 +1015,52 @@ PyObject* string_repeat(register PyStringObject *a, register Py_ssize_t n)
...
@@ -1014,3 +1015,52 @@ PyObject* string_repeat(register PyStringObject *a, register Py_ssize_t n)
}
}
return
(
PyObject
*
)
op
;
return
(
PyObject
*
)
op
;
}
}
PyObject
*
string_partition
(
PyStringObject
*
self
,
PyObject
*
sep_obj
)
{
const
char
*
sep
;
Py_ssize_t
sep_len
;
if
(
PyString_Check
(
sep_obj
))
{
sep
=
PyString_AS_STRING
(
sep_obj
);
sep_len
=
PyString_GET_SIZE
(
sep_obj
);
}
#ifdef Py_USING_UNICODE
else
if
(
PyUnicode_Check
(
sep_obj
))
return
PyUnicode_Partition
((
PyObject
*
)
self
,
sep_obj
);
#endif
else
if
(
PyObject_AsCharBuffer
(
sep_obj
,
&
sep
,
&
sep_len
))
return
NULL
;
return
stringlib_partition
(
(
PyObject
*
)
self
,
PyString_AS_STRING
(
self
),
PyString_GET_SIZE
(
self
),
sep_obj
,
sep
,
sep_len
);
}
PyObject
*
string_rpartition
(
PyStringObject
*
self
,
PyObject
*
sep_obj
)
{
const
char
*
sep
;
Py_ssize_t
sep_len
;
if
(
PyString_Check
(
sep_obj
))
{
sep
=
PyString_AS_STRING
(
sep_obj
);
sep_len
=
PyString_GET_SIZE
(
sep_obj
);
}
#ifdef Py_USING_UNICODE
else
if
(
PyUnicode_Check
(
sep_obj
))
return
PyUnicode_RPartition
((
PyObject
*
)
self
,
sep_obj
);
#endif
else
if
(
PyObject_AsCharBuffer
(
sep_obj
,
&
sep
,
&
sep_len
))
return
NULL
;
return
stringlib_rpartition
(
(
PyObject
*
)
self
,
PyString_AS_STRING
(
self
),
PyString_GET_SIZE
(
self
),
sep_obj
,
sep
,
sep_len
);
}
src/runtime/str.cpp
View file @
3dc94a15
...
@@ -42,6 +42,8 @@ extern "C" PyObject* string_find(PyStringObject* self, PyObject* args) noexcept;
...
@@ -42,6 +42,8 @@ extern "C" PyObject* string_find(PyStringObject* self, PyObject* args) noexcept;
extern
"C"
PyObject
*
string_index
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_index
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_rindex
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_rindex
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_rfind
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_rfind
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_partition
(
PyStringObject
*
self
,
PyObject
*
sep_obj
)
noexcept
;
extern
"C"
PyObject
*
string_rpartition
(
PyStringObject
*
self
,
PyObject
*
sep_obj
)
noexcept
;
extern
"C"
PyObject
*
string_repeat
(
PyStringObject
*
a
,
Py_ssize_t
n
)
noexcept
;
extern
"C"
PyObject
*
string_repeat
(
PyStringObject
*
a
,
Py_ssize_t
n
)
noexcept
;
extern
"C"
PyObject
*
string_replace
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_replace
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_splitlines
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
extern
"C"
PyObject
*
string_splitlines
(
PyStringObject
*
self
,
PyObject
*
args
)
noexcept
;
...
@@ -1892,38 +1894,7 @@ extern "C" PyObject* _PyString_Join(PyObject* sep, PyObject* x) noexcept {
...
@@ -1892,38 +1894,7 @@ extern "C" PyObject* _PyString_Join(PyObject* sep, PyObject* x) noexcept {
return
string_join
((
PyStringObject
*
)
sep
,
x
);
return
string_join
((
PyStringObject
*
)
sep
,
x
);
}
}
Box
*
strPartition
(
BoxedString
*
self
,
BoxedString
*
sep
)
{
RELEASE_ASSERT
(
PyString_Check
(
self
),
""
);
RELEASE_ASSERT
(
PyString_Check
(
sep
),
""
);
size_t
found_idx
;
if
(
sep
->
size
()
==
1
)
found_idx
=
self
->
s
().
find
(
sep
->
s
()[
0
]);
else
found_idx
=
self
->
s
().
find
(
sep
->
s
());
if
(
found_idx
==
std
::
string
::
npos
)
return
BoxedTuple
::
create
({
self
,
EmptyString
,
EmptyString
});
return
BoxedTuple
::
create
({
autoDecref
(
boxString
(
llvm
::
StringRef
(
self
->
data
(),
found_idx
))),
autoDecref
(
boxString
(
llvm
::
StringRef
(
self
->
data
()
+
found_idx
,
sep
->
size
()))),
autoDecref
(
boxString
(
llvm
::
StringRef
(
self
->
data
()
+
found_idx
+
sep
->
size
(),
self
->
size
()
-
found_idx
-
sep
->
size
())))
});
}
Box
*
strRpartition
(
BoxedString
*
self
,
BoxedString
*
sep
)
{
RELEASE_ASSERT
(
PyString_Check
(
self
),
""
);
RELEASE_ASSERT
(
PyString_Check
(
sep
),
""
);
size_t
found_idx
=
self
->
s
().
rfind
(
sep
->
s
());
if
(
found_idx
==
std
::
string
::
npos
)
return
BoxedTuple
::
create
({
EmptyString
,
EmptyString
,
self
});
return
BoxedTuple
::
create
({
autoDecref
(
boxString
(
llvm
::
StringRef
(
self
->
data
(),
found_idx
))),
autoDecref
(
boxString
(
llvm
::
StringRef
(
self
->
data
()
+
found_idx
,
sep
->
size
()))),
autoDecref
(
boxString
(
llvm
::
StringRef
(
self
->
data
()
+
found_idx
+
sep
->
size
(),
self
->
size
()
-
found_idx
-
sep
->
size
())))
});
}
extern
"C"
PyObject
*
_do_string_format
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
);
extern
"C"
PyObject
*
_do_string_format
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
);
...
@@ -2767,6 +2738,8 @@ static PyMethodDef string_methods[] = {
...
@@ -2767,6 +2738,8 @@ static PyMethodDef string_methods[] = {
{
"rsplit"
,
(
PyCFunction
)
string_rsplit
,
METH_VARARGS
,
NULL
},
{
"rsplit"
,
(
PyCFunction
)
string_rsplit
,
METH_VARARGS
,
NULL
},
{
"find"
,
(
PyCFunction
)
string_find
,
METH_VARARGS
,
NULL
},
{
"find"
,
(
PyCFunction
)
string_find
,
METH_VARARGS
,
NULL
},
{
"index"
,
(
PyCFunction
)
string_index
,
METH_VARARGS
,
NULL
},
{
"index"
,
(
PyCFunction
)
string_index
,
METH_VARARGS
,
NULL
},
{
"partition"
,
(
PyCFunction
)
string_partition
,
METH_O
,
NULL
},
{
"rpartition"
,
(
PyCFunction
)
string_rpartition
,
METH_O
,
NULL
},
{
"rindex"
,
(
PyCFunction
)
string_rindex
,
METH_VARARGS
,
NULL
},
{
"rindex"
,
(
PyCFunction
)
string_rindex
,
METH_VARARGS
,
NULL
},
{
"rfind"
,
(
PyCFunction
)
string_rfind
,
METH_VARARGS
,
NULL
},
{
"rfind"
,
(
PyCFunction
)
string_rfind
,
METH_VARARGS
,
NULL
},
{
"expandtabs"
,
(
PyCFunction
)
string_expandtabs
,
METH_VARARGS
,
NULL
},
{
"expandtabs"
,
(
PyCFunction
)
string_expandtabs
,
METH_VARARGS
,
NULL
},
...
@@ -2841,9 +2814,6 @@ void setupStr() {
...
@@ -2841,9 +2814,6 @@ void setupStr() {
str_cls
->
giveAttr
(
"endswith"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strEndswith
,
BOXED_BOOL
,
4
,
0
,
0
),
str_cls
->
giveAttr
(
"endswith"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strEndswith
,
BOXED_BOOL
,
4
,
0
,
0
),
{
NULL
,
NULL
}));
{
NULL
,
NULL
}));
str_cls
->
giveAttr
(
"partition"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strPartition
,
UNKNOWN
,
2
)));
str_cls
->
giveAttr
(
"rpartition"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strRpartition
,
UNKNOWN
,
2
)));
str_cls
->
giveAttr
(
"format"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strFormat
,
UNKNOWN
,
1
,
true
,
true
)));
str_cls
->
giveAttr
(
"format"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strFormat
,
UNKNOWN
,
1
,
true
,
true
)));
str_cls
->
giveAttr
(
"__add__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strAdd
<
CXX
>
,
UNKNOWN
,
2
)));
str_cls
->
giveAttr
(
"__add__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
strAdd
<
CXX
>
,
UNKNOWN
,
2
)));
...
...
test/tests/str_functions.py
View file @
3dc94a15
...
@@ -94,6 +94,7 @@ for i in xrange(-10, 10):
...
@@ -94,6 +94,7 @@ for i in xrange(-10, 10):
print
"hello world"
.
partition
(
"hi"
)
print
"hello world"
.
partition
(
"hi"
)
print
"hello world"
.
partition
(
"hello"
)
print
"hello world"
.
partition
(
"hello"
)
print
"hello world"
.
partition
(
"o"
)
print
"hello world"
.
partition
(
"o"
)
print
"hello world"
.
partition
(
u"o"
)
print
"hello world"
[
False
:
True
:
True
]
print
"hello world"
[
False
:
True
:
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