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
88107840
Commit
88107840
authored
10 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
objstr: Implement .lstrip() & .rstrip().
Share code with .strip(). TODO: optimize .rstrip().
parent
12a9cfed
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/objstr.c
+28
-2
28 additions, 2 deletions
py/objstr.c
py/qstrdefs.h
+2
-0
2 additions, 0 deletions
py/qstrdefs.h
tests/basics/string_strip.py
+6
-0
6 additions, 0 deletions
tests/basics/string_strip.py
with
36 additions
and
2 deletions
py/objstr.c
+
28
−
2
View file @
88107840
...
...
@@ -493,7 +493,9 @@ STATIC mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
return
MP_BOOL
(
memcmp
(
str
,
prefix
,
prefix_len
)
==
0
);
}
STATIC
mp_obj_t
str_strip
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
enum
{
LSTRIP
,
RSTRIP
,
STRIP
};
STATIC
mp_obj_t
str_uni_strip
(
int
type
,
uint
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
1
<=
n_args
&&
n_args
<=
2
);
assert
(
MP_OBJ_IS_STR
(
args
[
0
]));
...
...
@@ -516,13 +518,18 @@ STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
machine_uint_t
first_good_char_pos
=
0
;
bool
first_good_char_pos_set
=
false
;
machine_uint_t
last_good_char_pos
=
0
;
// TODO: For RSPLIT, scan from end
for
(
machine_uint_t
i
=
0
;
i
<
orig_str_len
;
i
++
)
{
if
(
find_subbytes
(
chars_to_del
,
chars_to_del_len
,
&
orig_str
[
i
],
1
,
1
)
==
NULL
)
{
last_good_char_pos
=
i
;
if
(
!
first_good_char_pos_set
)
{
first_good_char_pos
=
i
;
if
(
type
==
LSTRIP
)
{
last_good_char_pos
=
orig_str_len
-
1
;
break
;
}
first_good_char_pos_set
=
true
;
}
last_good_char_pos
=
i
;
}
}
...
...
@@ -532,11 +539,26 @@ STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
}
assert
(
last_good_char_pos
>=
first_good_char_pos
);
if
(
type
==
RSTRIP
)
{
first_good_char_pos
=
0
;
}
//+1 to accomodate the last character
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
);
}
STATIC
mp_obj_t
str_strip
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
return
str_uni_strip
(
STRIP
,
n_args
,
args
);
}
STATIC
mp_obj_t
str_lstrip
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
return
str_uni_strip
(
LSTRIP
,
n_args
,
args
);
}
STATIC
mp_obj_t
str_rstrip
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
return
str_uni_strip
(
RSTRIP
,
n_args
,
args
);
}
// Takes an int arg, but only parses unsigned numbers, and only changes
// *num if at least one digit was parsed.
static
int
str_to_int
(
const
char
*
str
,
int
*
num
)
{
...
...
@@ -1354,6 +1376,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_split_obj
,
1
,
3
,
str_split
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_2
(
str_startswith_obj
,
str_startswith
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_strip_obj
,
1
,
2
,
str_strip
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_lstrip_obj
,
1
,
2
,
str_lstrip
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_rstrip_obj
,
1
,
2
,
str_rstrip
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR
(
str_format_obj
,
1
,
mp_obj_str_format
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_replace_obj
,
3
,
4
,
str_replace
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
str_count_obj
,
2
,
4
,
str_count
);
...
...
@@ -1373,6 +1397,8 @@ STATIC const mp_map_elem_t str_locals_dict_table[] = {
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_split
),
(
mp_obj_t
)
&
str_split_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_startswith
),
(
mp_obj_t
)
&
str_startswith_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_strip
),
(
mp_obj_t
)
&
str_strip_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_lstrip
),
(
mp_obj_t
)
&
str_lstrip_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_rstrip
),
(
mp_obj_t
)
&
str_rstrip_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_format
),
(
mp_obj_t
)
&
str_format_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_replace
),
(
mp_obj_t
)
&
str_replace_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_count
),
(
mp_obj_t
)
&
str_count_obj
},
...
...
This diff is collapsed.
Click to expand it.
py/qstrdefs.h
+
2
−
0
View file @
88107840
...
...
@@ -180,6 +180,8 @@ Q(pop)
Q
(
sort
)
Q
(
join
)
Q
(
strip
)
Q
(
lstrip
)
Q
(
rstrip
)
Q
(
format
)
Q
(
key
)
Q
(
reverse
)
...
...
This diff is collapsed.
Click to expand it.
tests/basics/string_strip.py
+
6
−
0
View file @
88107840
...
...
@@ -4,3 +4,9 @@ print(" T E S T".strip())
print
(
"
abcabc
"
.
strip
(
"
ce
"
))
print
(
"
aaa
"
.
strip
(
"
b
"
))
print
(
"
abc efg
"
.
strip
(
"
g a
"
))
print
(
'
spacious
'
.
lstrip
())
print
(
'
www.example.com
'
.
lstrip
(
'
cmowz.
'
))
print
(
'
spacious
'
.
rstrip
())
print
(
'
mississippi
'
.
rstrip
(
'
ipz
'
))
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