Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
a157e4ca
Commit
a157e4ca
authored
Apr 09, 2014
by
Damien George
Browse files
py: str.join can now take arbitrary iterable as argument.
parent
13d6739c
Changes
2
Show whitespace changes
Inline
Side-by-side
py/objstr.c
View file @
a157e4ca
...
...
@@ -329,17 +329,19 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_t
*
seq_items
;
if
(
MP_OBJ_IS_TYPE
(
arg
,
&
mp_type_tuple
))
{
mp_obj_tuple_get
(
arg
,
&
seq_len
,
&
seq_items
);
}
else
if
(
MP_OBJ_IS_TYPE
(
arg
,
&
mp_type_list
))
{
mp_obj_list_get
(
arg
,
&
seq_len
,
&
seq_items
);
}
else
{
goto
bad_arg
;
if
(
!
MP_OBJ_IS_TYPE
(
arg
,
&
mp_type_list
))
{
// arg is not a list, try to convert it to one
arg
=
mp_type_list
.
make_new
((
mp_obj_t
)
&
mp_type_list
,
1
,
0
,
&
arg
);
}
mp_obj_list_get
(
arg
,
&
seq_len
,
&
seq_items
);
}
// count required length
int
required_len
=
0
;
for
(
int
i
=
0
;
i
<
seq_len
;
i
++
)
{
if
(
!
MP_OBJ_IS_STR
(
seq_items
[
i
]))
{
goto
bad_arg
;
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_TypeError
,
"join expected a list of str's"
))
;
}
if
(
i
>
0
)
{
required_len
+=
sep_len
;
...
...
@@ -363,9 +365,6 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
// return joined string
return
mp_obj_str_builder_end
(
joined_str
);
bad_arg:
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_TypeError
,
"?str.join expecting a list of str's"
));
}
#define is_ws(c) ((c) == ' ' || (c) == '\t')
...
...
tests/basics/string-join.py
0 → 100644
View file @
a157e4ca
print
(
','
.
join
(()))
print
(
','
.
join
((
'a'
,)))
print
(
','
.
join
((
'a'
,
'b'
)))
print
(
','
.
join
([]))
print
(
','
.
join
([
'a'
]))
print
(
','
.
join
([
'a'
,
'b'
]))
print
(
''
.
join
(
''
))
print
(
''
.
join
(
'abc'
))
print
(
','
.
join
(
'abc'
))
print
(
','
.
join
(
'abc'
for
i
in
range
(
5
)))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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