Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
8cfc9f07
Commit
8cfc9f07
authored
11 years ago
by
xyb
Browse files
Options
Downloads
Patches
Plain Diff
Implements str iterator
parent
45b43c21
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/objstr.c
+52
-1
52 additions, 1 deletion
py/objstr.c
tests/basics/tests/string1.py
+3
-0
3 additions, 0 deletions
tests/basics/tests/string1.py
with
55 additions
and
1 deletion
py/objstr.c
+
52
−
1
View file @
8cfc9f07
...
...
@@ -17,6 +17,11 @@ typedef struct _mp_obj_str_t {
qstr
qstr
;
}
mp_obj_str_t
;
static
mp_obj_t
mp_obj_new_str_iterator
(
mp_obj_str_t
*
str
,
int
cur
);
/******************************************************************************/
/* str */
void
str_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
)
{
mp_obj_str_t
*
self
=
self_in
;
// TODO need to escape chars etc
...
...
@@ -85,6 +90,10 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return
MP_OBJ_NULL
;
// op not supported
}
static
mp_obj_t
str_getiter
(
mp_obj_t
o_in
)
{
return
mp_obj_new_str_iterator
(
o_in
,
0
);
}
mp_obj_t
str_join
(
mp_obj_t
self_in
,
mp_obj_t
arg
)
{
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
str_type
));
mp_obj_str_t
*
self
=
self_in
;
...
...
@@ -183,7 +192,7 @@ const mp_obj_type_t str_type = {
NULL
,
// call_n
NULL
,
// unary_op
str_binary_op
,
// binary_op
NULL
,
// getiter
str_getiter
,
// getiter
NULL
,
// iternext
{
// method list
{
"join"
,
&
str_join_obj
},
...
...
@@ -204,3 +213,45 @@ qstr mp_obj_str_get(mp_obj_t self_in) {
mp_obj_str_t
*
self
=
self_in
;
return
self
->
qstr
;
}
/******************************************************************************/
/* str iterator */
typedef
struct
_mp_obj_str_it_t
{
mp_obj_base_t
base
;
mp_obj_str_t
*
str
;
machine_uint_t
cur
;
}
mp_obj_str_it_t
;
mp_obj_t
str_it_iternext
(
mp_obj_t
self_in
)
{
mp_obj_str_it_t
*
self
=
self_in
;
const
char
*
str
=
qstr_str
(
self
->
str
->
qstr
);
if
(
self
->
cur
<
strlen
(
str
))
{
mp_obj_t
o_out
=
mp_obj_new_str
(
qstr_from_strn_copy
(
str
+
self
->
cur
,
1
));
self
->
cur
+=
1
;
return
o_out
;
}
else
{
return
mp_const_stop_iteration
;
}
}
static
const
mp_obj_type_t
str_it_type
=
{
{
&
mp_const_type
},
"str_iterator"
,
NULL
,
// print
NULL
,
// make_new
NULL
,
// call_n
NULL
,
// unary_op
NULL
,
// binary_op
NULL
,
// getiter
str_it_iternext
,
// iternext
{
{
NULL
,
NULL
},
},
// method str
};
mp_obj_t
mp_obj_new_str_iterator
(
mp_obj_str_t
*
str
,
int
cur
)
{
mp_obj_str_it_t
*
o
=
m_new_obj
(
mp_obj_str_it_t
);
o
->
base
.
type
=
&
str_it_type
;
o
->
str
=
str
;
o
->
cur
=
cur
;
return
o
;
}
This diff is collapsed.
Click to expand it.
tests/basics/tests/string1.py
+
3
−
0
View file @
8cfc9f07
...
...
@@ -7,3 +7,6 @@ x += 'def'
print
(
x
)
print
(
'
123
'
+
"
456
"
)
# iter
print
(
list
(
'
str
'
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment