Skip to content

Commit 613a8e3

Browse files
committed
Implement str.partition and add tests for it.
1 parent c412998 commit 613a8e3

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,31 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
520520
return MP_OBJ_NEW_SMALL_INT(num_occurrences);
521521
}
522522

523+
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
524+
assert(MP_OBJ_IS_STR(self_in));
525+
if (!MP_OBJ_IS_STR(arg)) {
526+
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
527+
"Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
528+
}
529+
530+
GET_STR_DATA_LEN(self_in, str, str_len);
531+
GET_STR_DATA_LEN(arg, sep, sep_len);
532+
533+
if (sep_len == 0) {
534+
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
535+
}
536+
537+
for (machine_uint_t str_index = 0; str_index + sep_len <= str_len; str_index++) {
538+
if (memcmp(&str[str_index], sep, sep_len) == 0) {
539+
mp_obj_t items[] = {mp_obj_new_str(str, str_index, false), arg,
540+
mp_obj_new_str(str + str_index + sep_len, str_len - str_index - sep_len, false)};
541+
return mp_obj_new_tuple(3, items);
542+
}
543+
}
544+
mp_obj_t items[] = {mp_obj_new_str(str, str_len, false), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
545+
return mp_obj_new_tuple(3, items);
546+
}
547+
523548
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {
524549
if (flags == BUFFER_READ) {
525550
GET_STR_DATA_LEN(self_in, str_data, str_len);
@@ -542,6 +567,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
542567
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
543568
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
544569
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
570+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
545571

546572
STATIC const mp_method_t str_type_methods[] = {
547573
{ "find", &str_find_obj },
@@ -552,6 +578,7 @@ STATIC const mp_method_t str_type_methods[] = {
552578
{ "format", &str_format_obj },
553579
{ "replace", &str_replace_obj },
554580
{ "count", &str_count_obj },
581+
{ "partition", &str_partition_obj },
555582
{ NULL, NULL }, // end-of-list sentinel
556583
};
557584

tests/basics/string_partition.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
print("asdf".partition('g'))
2+
print("asdf".partition('a'))
3+
print("asdf".partition('s'))
4+
print("asdf".partition('f'))
5+
print("asdf".partition('d'))
6+
print("asdf".partition('asd'))
7+
print("asdf".partition('sdf'))
8+
print("asdf".partition('as'))
9+
print("asdf".partition('df'))
10+
print("asdf".partition('asdf'))
11+
print("asdf".partition('asdfa'))
12+
print("asdf".partition('fasdf'))
13+
print("asdf".partition('fasdfa'))
14+
print("abba".partition('a'))
15+
print("abba".partition('b'))
16+
17+
try:
18+
print("asdf".partition(1))
19+
except TypeError:
20+
print("Raised TypeError")
21+
else:
22+
print("Did not raise TypeError")
23+
24+
try:
25+
print("asdf".partition(''))
26+
except ValueError:
27+
print("Raised ValueError")
28+
else:
29+
print("Did not raise ValueError")

0 commit comments

Comments
 (0)