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
09df9da7
Commit
09df9da7
authored
May 18, 2015
by
Rudi Chen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle unicode string slicing.
parent
95fface9
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
59 additions
and
13 deletions
+59
-13
src/runtime/list.cpp
src/runtime/list.cpp
+2
-0
src/runtime/objmodel.cpp
src/runtime/objmodel.cpp
+2
-0
src/runtime/util.cpp
src/runtime/util.cpp
+24
-5
src/runtime/util.h
src/runtime/util.h
+3
-3
test/tests/slice.py
test/tests/slice.py
+28
-5
No files found.
src/runtime/list.cpp
View file @
09df9da7
...
...
@@ -470,6 +470,8 @@ extern "C" Box* listSetitemSlice(BoxedList* self, BoxedSlice* slice, Box* v) {
sliceIndex
(
slice
->
stop
,
&
stop
);
sliceIndex
(
slice
->
step
,
&
step
);
adjustNegativeIndicesOnObject
(
self
,
&
start
,
&
stop
);
if
(
step
!=
1
)
{
int
r
=
list_ass_ext_slice
(
self
,
slice
,
v
);
if
(
r
)
...
...
src/runtime/objmodel.cpp
View file @
09df9da7
...
...
@@ -4448,6 +4448,8 @@ Box* callItemOrSliceAttr(Box* target, BoxedString* item_str, BoxedString* slice_
sliceIndex
(
bslice
->
start
,
&
start
);
sliceIndex
(
bslice
->
stop
,
&
stop
);
adjustNegativeIndicesOnObject
(
target
,
&
start
,
&
stop
);
Box
*
boxedStart
=
boxInt
(
start
);
Box
*
boxedStop
=
boxInt
(
stop
);
...
...
src/runtime/util.cpp
View file @
09df9da7
...
...
@@ -30,13 +30,32 @@ bool isSliceIndex(Box* b) {
return
b
->
cls
==
none_cls
||
b
->
cls
==
int_cls
||
PyIndex_Check
(
b
);
}
void
boundSliceWithLength
(
i64
*
start_out
,
i64
*
stop_out
,
i64
start
,
i64
stop
,
i64
size
)
{
void
adjustNegativeIndicesOnObject
(
Box
*
obj
,
i64
*
start_out
,
i64
*
stop_out
)
{
i64
start
=
*
start_out
;
i64
stop
=
*
stop_out
;
PySequenceMethods
*
m
;
// Logic from PySequence_GetSlice:
if
(
start
<
0
)
start
+=
size
;
if
(
stop
<
0
)
stop
+=
size
;
m
=
obj
->
cls
->
tp_as_sequence
;
if
(
m
&&
m
->
sq_slice
)
{
if
(
start
<
0
||
stop
<
0
)
{
if
(
m
->
sq_length
)
{
Py_ssize_t
l
=
(
*
m
->
sq_length
)(
obj
);
if
(
l
>=
0
)
{
if
(
start
<
0
)
start
+=
l
;
if
(
stop
<
0
)
stop
+=
l
;
}
}
}
}
*
start_out
=
start
;
*
stop_out
=
stop
;
}
void
boundSliceWithLength
(
i64
*
start_out
,
i64
*
stop_out
,
i64
start
,
i64
stop
,
i64
size
)
{
if
(
start
<
0
)
start
=
0
;
else
if
(
start
>
size
)
...
...
src/runtime/util.h
View file @
09df9da7
...
...
@@ -38,10 +38,10 @@ inline void sliceIndex(Box* b, int64_t* out) {
bool
isSliceIndex
(
Box
*
b
);
void
adjustNegativeIndicesOnObject
(
Box
*
obj
,
i64
*
start
,
i64
*
stop
);
// Adjust the start and stop bounds of the sequence we are slicing to its size.
// Negative values greater or equal to (-length) become positive values.
// Ensure stop >= start
// Remain within bounds.
// Ensure stop >= start and remain within bounds.
void
boundSliceWithLength
(
i64
*
start_out
,
i64
*
stop_out
,
i64
start
,
i64
stop
,
i64
size
);
template
<
typename
T
>
void
copySlice
(
T
*
__restrict__
dst
,
const
T
*
__restrict__
src
,
i64
start
,
i64
step
,
i64
length
)
{
...
...
test/tests/slice.py
View file @
09df9da7
...
...
@@ -53,6 +53,8 @@ index_zero = IndexZero()
false_index
=
FalseIndex
()
both
=
Both
()
numbers
=
range
(
10
)
letters
=
"abcde"
unicodestr
=
unicode
(
"abcde"
)
# Can use index and slice notation for object with only getitem
indexable
[
0
]
...
...
@@ -109,7 +111,9 @@ both[::2] = xrange(2)
# Should all call getitem as a fallback
both
[
'a'
]
both
[
'a'
:
'b'
]
both
[
'a'
:
'b'
:
'c'
]
both
[
1
:
'b'
]
both
[
'a'
:
2
]
both
[
1
:
2
:
'c'
]
del
both
[
0
]
del
both
[:]
...
...
@@ -139,10 +143,29 @@ print numbers[:-2]
print
numbers
[
-
2
:]
# String support slicing
print
"abcd"
[
2
]
print
"abcd"
[:
2
]
print
"abcd"
[
2
:]
print
"abcd"
[
1
:
3
]
print
letters
[
2
]
print
letters
[:
2
]
print
letters
[
2
:]
print
letters
[
1
:
3
]
print
letters
[:
-
2
]
print
letters
[
-
2
:]
# Unicode string support slicing
# Note that unicode strings are not the same type of object as strings,
# (but both have base class basestring)
print
unicodestr
[
2
]
print
unicodestr
[:
2
]
print
unicodestr
[
2
:]
print
unicodestr
[
1
:
3
]
print
unicodestr
[:
-
2
]
print
unicodestr
[
-
2
:]
# Calling the slice operator directly does not have the same behavior
# as using the slice notation []. Namely, it will not modify negative
# indices.
print
numbers
.
__getslice__
(
0
,
-
1
);
print
letters
.
__getslice__
(
0
,
-
1
);
print
unicodestr
.
__getslice__
(
0
,
-
1
);
# Other
class
C
(
object
):
...
...
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