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
e820491f
Commit
e820491f
authored
11 years ago
by
John R. Lenton
Browse files
Options
Downloads
Patches
Plain Diff
Implement a basic str.find; fixes #67
parent
fd17921b
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
+39
-0
39 additions, 0 deletions
py/objstr.c
tests/basics/tests/string_find.py
+11
-0
11 additions, 0 deletions
tests/basics/tests/string_find.py
with
50 additions
and
0 deletions
py/objstr.c
+
39
−
0
View file @
e820491f
...
...
@@ -156,6 +156,43 @@ static bool chr_in_str(const char* const str, const size_t str_len, const char c
return
false
;
}
static
mp_obj_t
str_find
(
int
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
2
<=
n_args
&&
n_args
<=
4
);
assert
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
str_type
));
if
(
!
MP_OBJ_IS_TYPE
(
args
[
1
],
&
str_type
))
{
nlr_jump
(
mp_obj_new_exception_msg_1_arg
(
MP_QSTR_TypeError
,
"Can't convert '%s' object to str implicitly"
,
mp_obj_get_type_str
(
args
[
1
])));
}
const
char
*
haystack
=
qstr_str
(((
mp_obj_str_t
*
)
args
[
0
])
->
qstr
);
const
char
*
needle
=
qstr_str
(((
mp_obj_str_t
*
)
args
[
1
])
->
qstr
);
ssize_t
haystack_len
=
strlen
(
haystack
);
ssize_t
needle_len
=
strlen
(
needle
);
size_t
start
=
0
;
size_t
end
=
haystack_len
;
/* TODO use a non-exception-throwing mp_get_index */
if
(
n_args
>=
3
&&
args
[
2
]
!=
mp_const_none
)
{
start
=
mp_get_index
(
&
str_type
,
haystack_len
,
args
[
2
]);
}
if
(
n_args
>=
4
&&
args
[
3
]
!=
mp_const_none
)
{
end
=
mp_get_index
(
&
str_type
,
haystack_len
,
args
[
3
]);
}
char
*
p
=
strstr
(
haystack
+
start
,
needle
);
ssize_t
pos
=
-
1
;
if
(
p
)
{
pos
=
p
-
haystack
;
if
(
pos
+
needle_len
>
end
)
{
pos
=
-
1
;
}
}
return
MP_OBJ_NEW_SMALL_INT
(
pos
);
}
mp_obj_t
str_strip
(
int
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
1
<=
n_args
&&
n_args
<=
2
);
assert
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
str_type
));
...
...
@@ -239,11 +276,13 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
return
mp_obj_new_str
(
qstr_from_str_take
(
vstr
->
buf
,
vstr
->
alloc
));
}
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_find_obj
,
2
,
4
,
str_find
);
static
MP_DEFINE_CONST_FUN_OBJ_2
(
str_join_obj
,
str_join
);
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_strip_obj
,
1
,
2
,
str_strip
);
static
MP_DEFINE_CONST_FUN_OBJ_VAR
(
str_format_obj
,
1
,
str_format
);
static
const
mp_method_t
str_type_methods
[]
=
{
{
"find"
,
&
str_find_obj
},
{
"join"
,
&
str_join_obj
},
{
"strip"
,
&
str_strip_obj
},
{
"format"
,
&
str_format_obj
},
...
...
This diff is collapsed.
Click to expand it.
tests/basics/tests/string_find.py
0 → 100644
+
11
−
0
View file @
e820491f
print
(
"
hello world
"
.
find
(
"
ll
"
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
None
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
None
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
None
,
None
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
-
1
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
1
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
2
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
3
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
4
))
print
(
"
hello world
"
.
find
(
"
ll
"
,
1
,
5
))
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