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
69f3eb2c
Commit
69f3eb2c
authored
10 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
objstr: Make .[r]partition() work with bytes.
parent
285683d2
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
+17
-5
17 additions, 5 deletions
py/objstr.c
tests/basics/string_partition.py
+11
-0
11 additions, 0 deletions
tests/basics/string_partition.py
with
28 additions
and
5 deletions
py/objstr.c
+
17
−
5
View file @
69f3eb2c
...
...
@@ -54,6 +54,11 @@ STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
STATIC
mp_obj_t
mp_obj_new_bytes_iterator
(
mp_obj_t
str
);
STATIC
mp_obj_t
str_new
(
const
mp_obj_type_t
*
type
,
const
byte
*
data
,
uint
len
);
STATIC
NORETURN
void
bad_implicit_conversion
(
mp_obj_t
self_in
);
STATIC
NORETURN
void
arg_type_mixup
();
STATIC
bool
is_str_or_bytes
(
mp_obj_t
o
)
{
return
MP_OBJ_IS_STR
(
o
)
||
MP_OBJ_IS_TYPE
(
o
,
&
mp_type_bytes
);
}
/******************************************************************************/
/* str */
...
...
@@ -1326,9 +1331,12 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
}
STATIC
mp_obj_t
str_partitioner
(
mp_obj_t
self_in
,
mp_obj_t
arg
,
machine_int_t
direction
)
{
assert
(
MP_OBJ_IS_STR
(
self_in
));
if
(
!
MP_OBJ_IS_STR
(
arg
))
{
bad_implicit_conversion
(
arg
);
if
(
!
is_str_or_bytes
(
self_in
))
{
assert
(
0
);
}
mp_obj_type_t
*
self_type
=
mp_obj_get_type
(
self_in
);
if
(
self_type
!=
mp_obj_get_type
(
arg
))
{
arg_type_mixup
();
}
GET_STR_DATA_LEN
(
self_in
,
str
,
str_len
);
...
...
@@ -1349,9 +1357,9 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t di
const
byte
*
position_ptr
=
find_subbytes
(
str
,
str_len
,
sep
,
sep_len
,
direction
);
if
(
position_ptr
!=
NULL
)
{
machine_uint_t
position
=
position_ptr
-
str
;
result
[
0
]
=
mp_obj_new_str
(
str
,
position
,
false
);
result
[
0
]
=
str_new
(
self_type
,
str
,
position
);
result
[
1
]
=
arg
;
result
[
2
]
=
mp_obj_new_str
(
str
+
position
+
sep_len
,
str_len
-
position
-
sep_len
,
false
);
result
[
2
]
=
str_new
(
self_type
,
str
+
position
+
sep_len
,
str_len
-
position
-
sep_len
);
}
return
mp_obj_new_tuple
(
3
,
result
);
...
...
@@ -1586,6 +1594,10 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) {
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"Can't convert '%s' object to str implicitly"
,
mp_obj_get_type_str
(
self_in
)));
}
STATIC
void
arg_type_mixup
()
{
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_TypeError
,
"Can't mix str and bytes arguments"
));
}
uint
mp_obj_str_get_hash
(
mp_obj_t
self_in
)
{
// TODO: This has too big overhead for hash accessor
if
(
MP_OBJ_IS_STR
(
self_in
)
||
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_bytes
))
{
...
...
This diff is collapsed.
Click to expand it.
tests/basics/string_partition.py
+
11
−
0
View file @
69f3eb2c
...
...
@@ -27,3 +27,14 @@ except ValueError:
print
(
"
Raised ValueError
"
)
else
:
print
(
"
Did not raise ValueError
"
)
# Bytes
print
(
b
"
abba
"
.
partition
(
b
'
b
'
))
try
:
print
(
b
"
abba
"
.
partition
(
'
b
'
))
except
TypeError
:
print
(
"
Raised TypeError
"
)
try
:
print
(
"
abba
"
.
partition
(
b
'
b
'
))
except
TypeError
:
print
(
"
Raised TypeError
"
)
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