Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
8827682b
Commit
8827682b
authored
May 30, 2014
by
Paul Sokolovsky
Browse files
objstr: *strip(): If nothing is stripped, don't create dup string.
parent
bcdffe53
Changes
2
Hide whitespace changes
Inline
Side-by-side
py/objstr.c
View file @
8827682b
...
...
@@ -691,6 +691,12 @@ 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
);
//+1 to accomodate the last character
machine_uint_t
stripped_len
=
last_good_char_pos
-
first_good_char_pos
+
1
;
if
(
stripped_len
==
orig_str_len
)
{
// If nothing was stripped, don't bother to dup original string
// TODO: watch out for this case when we'll get to bytearray.strip()
assert
(
first_good_char_pos
==
0
);
return
args
[
0
];
}
return
mp_obj_new_str_of_type
(
self_type
,
orig_str
+
first_good_char_pos
,
stripped_len
);
}
...
...
tests/basics/string_strip.py
View file @
8827682b
...
...
@@ -31,3 +31,7 @@ print(" a".rstrip())
print
(
"a "
.
strip
())
print
(
"a "
.
lstrip
())
print
(
"a "
.
rstrip
())
# Test that stripping unstrippable string returns original object
s
=
"abc"
print
(
id
(
s
.
strip
())
==
id
(
s
))
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment