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
69135219
Commit
69135219
authored
May 10, 2014
by
Paul Sokolovsky
Browse files
objstr: Implement .lower() and .upper().
parent
1b82e9af
Changes
5
Hide whitespace changes
Inline
Side-by-side
py/misc.h
View file @
69135219
...
...
@@ -96,6 +96,8 @@ bool unichar_isalpha(unichar c);
bool
unichar_isprint
(
unichar
c
);
bool
unichar_isdigit
(
unichar
c
);
bool
unichar_isxdigit
(
unichar
c
);
unichar
unichar_tolower
(
unichar
c
);
unichar
unichar_toupper
(
unichar
c
);
/** variable string *********************************************/
...
...
py/objstr.c
View file @
69135219
...
...
@@ -1365,6 +1365,32 @@ STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
return
str_partitioner
(
self_in
,
arg
,
-
1
);
}
enum
{
CASE_UPPER
,
CASE_LOWER
};
// Supposedly not too critical operations, so optimize for code size
STATIC
mp_obj_t
str_caseconv
(
int
op
,
mp_obj_t
self_in
)
{
GET_STR_DATA_LEN
(
self_in
,
self_data
,
self_len
);
byte
*
data
;
mp_obj_t
s
=
mp_obj_str_builder_start
(
mp_obj_get_type
(
self_in
),
self_len
,
&
data
);
for
(
int
i
=
0
;
i
<
self_len
;
i
++
)
{
if
(
op
==
CASE_UPPER
)
{
*
data
++
=
unichar_toupper
(
*
self_data
++
);
}
else
{
*
data
++
=
unichar_tolower
(
*
self_data
++
);
}
}
*
data
=
0
;
return
mp_obj_str_builder_end
(
s
);
}
STATIC
mp_obj_t
str_lower
(
mp_obj_t
self_in
)
{
return
str_caseconv
(
CASE_LOWER
,
self_in
);
}
STATIC
mp_obj_t
str_upper
(
mp_obj_t
self_in
)
{
return
str_caseconv
(
CASE_UPPER
,
self_in
);
}
#if MICROPY_CPYTHON_COMPAT
// These methods are superfluous in the presense of str() and bytes()
// constructors.
...
...
@@ -1428,6 +1454,8 @@ 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
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_2
(
str_partition_obj
,
str_partition
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_2
(
str_rpartition_obj
,
str_rpartition
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
str_lower_obj
,
str_lower
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
str_upper_obj
,
str_upper
);
STATIC
const
mp_map_elem_t
str_locals_dict_table
[]
=
{
#if MICROPY_CPYTHON_COMPAT
...
...
@@ -1449,6 +1477,8 @@ STATIC const mp_map_elem_t str_locals_dict_table[] = {
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_count
),
(
mp_obj_t
)
&
str_count_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_partition
),
(
mp_obj_t
)
&
str_partition_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_rpartition
),
(
mp_obj_t
)
&
str_rpartition_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_lower
),
(
mp_obj_t
)
&
str_lower_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_upper
),
(
mp_obj_t
)
&
str_upper_obj
},
};
STATIC
MP_DEFINE_CONST_DICT
(
str_locals_dict
,
str_locals_dict_table
);
...
...
py/qstrdefs.h
View file @
69135219
...
...
@@ -239,6 +239,8 @@ Q(startswith)
Q
(
replace
)
Q
(
partition
)
Q
(
rpartition
)
Q
(
lower
)
Q
(
upper
)
Q
(
iterable
)
Q
(
start
)
...
...
py/unicode.c
View file @
69135219
...
...
@@ -97,6 +97,7 @@ bool unichar_isxdigit(unichar c) {
bool char_is_alpha_or_digit(unichar c) {
return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
}
*/
bool
char_is_upper
(
unichar
c
)
{
return
c
<
128
&&
(
attr
[
c
]
&
FL_UPPER
)
!=
0
;
...
...
@@ -105,4 +106,17 @@ bool char_is_upper(unichar c) {
bool
char_is_lower
(
unichar
c
)
{
return
c
<
128
&&
(
attr
[
c
]
&
FL_LOWER
)
!=
0
;
}
*/
unichar
unichar_tolower
(
unichar
c
)
{
if
(
char_is_upper
(
c
))
{
return
c
+
0x20
;
}
return
c
;
}
unichar
unichar_toupper
(
unichar
c
)
{
if
(
char_is_lower
(
c
))
{
return
c
-
0x20
;
}
return
c
;
}
tests/basics/string_upperlow.py
0 → 100644
View file @
69135219
print
(
""
.
lower
())
print
(
" t
\t
n
\n
r
\r
v
\v
f
\f
"
.
upper
())
print
(
" T E S T"
.
lower
())
print
(
"*@a1b2cabc_[]/
\\
"
.
upper
())
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