py: Make unichar_charlen() accept/return machine_uint_t.

This commit is contained in:
Paul Sokolovsky 2014-06-28 23:14:30 +03:00
parent a62da515af
commit 9e215fa4c2
5 changed files with 7 additions and 7 deletions

View File

@ -372,7 +372,7 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
uint len;
const char *str = mp_obj_str_get_data(o_in, &len);
#if MICROPY_PY_BUILTINS_STR_UNICODE
uint charlen = unichar_charlen(str, len);
machine_uint_t charlen = unichar_charlen(str, len);
if (charlen == 1) {
if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
machine_int_t ord = *str++ & 0x7F;

View File

@ -100,7 +100,7 @@ bool unichar_isupper(unichar c);
bool unichar_islower(unichar c);
unichar unichar_tolower(unichar c);
unichar unichar_toupper(unichar c);
uint unichar_charlen(const char *str, uint len); // TODO this should return machine_uint_t
machine_uint_t unichar_charlen(const char *str, machine_uint_t len);
#define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
#define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)

View File

@ -1448,7 +1448,7 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
// if needle_len is zero then we count each gap between characters as an occurrence
if (needle_len == 0) {
return MP_OBJ_NEW_SMALL_INT((machine_uint_t)unichar_charlen((const char*)start, end - start) + 1);
return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char*)start, end - start) + 1);
}
// count the occurrences

View File

@ -106,7 +106,7 @@ STATIC mp_obj_t uni_unary_op(int op, mp_obj_t self_in) {
case MP_UNARY_OP_BOOL:
return MP_BOOL(str_len != 0);
case MP_UNARY_OP_LEN:
return MP_OBJ_NEW_SMALL_INT((machine_int_t)unichar_charlen((const char *)str_data, str_len));
return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len));
default:
return MP_OBJ_NULL; // op not supported
}

View File

@ -107,11 +107,11 @@ machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) {
return i;
}
// TODO: Rename to str_charlen; return machine_uint_t
uint unichar_charlen(const char *str, uint len)
// TODO: Rename to str_charlen
machine_uint_t unichar_charlen(const char *str, machine_uint_t len)
{
#if MICROPY_PY_BUILTINS_STR_UNICODE
uint charlen = 0;
machine_uint_t charlen = 0;
for (const char *top = str + len; str < top; ++str) {
if (!UTF8_IS_CONT(*str)) {
++charlen;