Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
13cfabd1
Commit
13cfabd1
authored
Feb 02, 2014
by
Paul Sokolovsky
Browse files
Implement slicing for lists.
parent
7364af2d
Changes
3
Hide whitespace changes
Inline
Side-by-side
py/obj.h
View file @
13cfabd1
...
...
@@ -396,3 +396,4 @@ typedef struct _mp_obj_classmethod_t {
// sequence helpers
void
mp_seq_multiply
(
const
void
*
items
,
uint
item_sz
,
uint
len
,
uint
times
,
void
*
dest
);
bool
m_seq_get_fast_slice_indexes
(
machine_uint_t
len
,
mp_obj_t
slice
,
machine_uint_t
*
begin
,
machine_uint_t
*
end
);
#define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz))
py/objlist.c
View file @
13cfabd1
...
...
@@ -136,7 +136,15 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
switch
(
op
)
{
case
RT_BINARY_OP_SUBSCR
:
{
// list load
#if MICROPY_ENABLE_SLICE
if
(
MP_OBJ_IS_TYPE
(
rhs
,
&
slice_type
))
{
machine_uint_t
start
,
stop
;
assert
(
m_seq_get_fast_slice_indexes
(
o
->
len
,
rhs
,
&
start
,
&
stop
));
mp_obj_list_t
*
res
=
list_new
(
stop
-
start
);
m_seq_copy
(
res
->
items
,
o
->
items
+
start
,
res
->
len
,
mp_obj_t
);
return
res
;
}
#endif
uint
index
=
mp_get_index
(
o
->
base
.
type
,
o
->
len
,
rhs
);
return
o
->
items
[
index
];
}
...
...
tests/basics/list1.py
View file @
13cfabd1
...
...
@@ -16,3 +16,7 @@ print(x)
x
+=
[
2
,
1
]
print
(
x
)
print
(
x
[
1
:])
print
(
x
[:
-
1
])
print
(
x
[
2
:
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