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
GitLab 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
e14d096c
Commit
e14d096c
authored
Apr 26, 2014
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
objstr: Optimize .rstrip() by scanning string from end.
parent
88107840
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
py/objstr.c
+12
-5
12 additions, 5 deletions
py/objstr.c
with
12 additions
and
5 deletions
py/objstr.c
+
12
−
5
View file @
e14d096c
...
@@ -518,19 +518,29 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
...
@@ -518,19 +518,29 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
machine_uint_t
first_good_char_pos
=
0
;
machine_uint_t
first_good_char_pos
=
0
;
bool
first_good_char_pos_set
=
false
;
bool
first_good_char_pos_set
=
false
;
machine_uint_t
last_good_char_pos
=
0
;
machine_uint_t
last_good_char_pos
=
0
;
// TODO: For RSPLIT, scan from end
machine_uint_t
i
=
0
;
for
(
machine_uint_t
i
=
0
;
i
<
orig_str_len
;
i
++
)
{
machine_int_t
delta
=
1
;
if
(
type
==
RSTRIP
)
{
i
=
orig_str_len
-
1
;
delta
=
-
1
;
}
for
(
machine_uint_t
len
=
orig_str_len
;
len
>
0
;
len
--
)
{
if
(
find_subbytes
(
chars_to_del
,
chars_to_del_len
,
&
orig_str
[
i
],
1
,
1
)
==
NULL
)
{
if
(
find_subbytes
(
chars_to_del
,
chars_to_del_len
,
&
orig_str
[
i
],
1
,
1
)
==
NULL
)
{
if
(
!
first_good_char_pos_set
)
{
if
(
!
first_good_char_pos_set
)
{
first_good_char_pos
=
i
;
first_good_char_pos
=
i
;
if
(
type
==
LSTRIP
)
{
if
(
type
==
LSTRIP
)
{
last_good_char_pos
=
orig_str_len
-
1
;
last_good_char_pos
=
orig_str_len
-
1
;
break
;
break
;
}
else
if
(
type
==
RSTRIP
)
{
first_good_char_pos
=
0
;
last_good_char_pos
=
i
;
break
;
}
}
first_good_char_pos_set
=
true
;
first_good_char_pos_set
=
true
;
}
}
last_good_char_pos
=
i
;
last_good_char_pos
=
i
;
}
}
i
+=
delta
;
}
}
if
(
first_good_char_pos
==
0
&&
last_good_char_pos
==
0
)
{
if
(
first_good_char_pos
==
0
&&
last_good_char_pos
==
0
)
{
...
@@ -539,9 +549,6 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
...
@@ -539,9 +549,6 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
}
}
assert
(
last_good_char_pos
>=
first_good_char_pos
);
assert
(
last_good_char_pos
>=
first_good_char_pos
);
if
(
type
==
RSTRIP
)
{
first_good_char_pos
=
0
;
}
//+1 to accomodate the last character
//+1 to accomodate the last character
machine_uint_t
stripped_len
=
last_good_char_pos
-
first_good_char_pos
+
1
;
machine_uint_t
stripped_len
=
last_good_char_pos
-
first_good_char_pos
+
1
;
return
mp_obj_new_str
(
orig_str
+
first_good_char_pos
,
stripped_len
,
false
);
return
mp_obj_new_str
(
orig_str
+
first_good_char_pos
,
stripped_len
,
false
);
...
...
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