Skip to content

Commit dddb98d

Browse files
committed
py/parsenum: Use size_t to count bytes, and int for type of base arg.
size_t is the proper type to count number of bytes in a string. The base argument does not need to be a full mp_uint_t, int is enough.
1 parent 99fc0d1 commit dddb98d

4 files changed

Lines changed: 10 additions & 11 deletions

File tree

py/parsenum.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdlib.h>
2929

3030
#include "py/nlr.h"
31+
#include "py/parsenumbase.h"
3132
#include "py/parsenum.h"
3233
#include "py/smallint.h"
3334

@@ -45,7 +46,7 @@ STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
4546
nlr_raise(exc);
4647
}
4748

48-
mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base, mp_lexer_t *lex) {
49+
mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex) {
4950
const byte *restrict str = (const byte *)str_;
5051
const byte *restrict top = str + len;
5152
bool neg = false;
@@ -169,7 +170,7 @@ typedef enum {
169170
PARSE_DEC_IN_EXP,
170171
} parse_dec_in_t;
171172

172-
mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) {
173+
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) {
173174
#if MICROPY_PY_BUILTINS_FLOAT
174175
const char *top = str + len;
175176
mp_float_t dec_val = 0;

py/parsenum.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
#include "py/lexer.h"
3131
#include "py/obj.h"
3232

33-
mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base);
34-
3533
// these functions raise a SyntaxError if lex!=NULL, else a ValueError
36-
mp_obj_t mp_parse_num_integer(const char *restrict str, mp_uint_t len, mp_uint_t base, mp_lexer_t *lex);
37-
mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex);
34+
mp_obj_t mp_parse_num_integer(const char *restrict str, size_t len, int base, mp_lexer_t *lex);
35+
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex);
3836

3937
#endif // __MICROPY_INCLUDED_PY_PARSENUM_H__

py/parsenumbase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
// find real radix base, and strip preceding '0x', '0o' and '0b'
3030
// puts base in *base, and returns number of bytes to skip the prefix
31-
mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base) {
31+
size_t mp_parse_num_base(const char *str, size_t len, int *base) {
3232
const byte *p = (const byte*)str;
3333
if (len <= 1) {
3434
goto no_prefix;

py/parsenumbase.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26-
#ifndef __MICROPY_INCLUDED_PY_PARSENUM_H__
27-
#define __MICROPY_INCLUDED_PY_PARSENUM_H__
26+
#ifndef __MICROPY_INCLUDED_PY_PARSENUMBASE_H__
27+
#define __MICROPY_INCLUDED_PY_PARSENUMBASE_H__
2828

2929
#include "py/mpconfig.h"
3030

31-
mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base);
31+
size_t mp_parse_num_base(const char *str, size_t len, int *base);
3232

33-
#endif // __MICROPY_INCLUDED_PY_PARSENUM_H__
33+
#endif // __MICROPY_INCLUDED_PY_PARSENUMBASE_H__

0 commit comments

Comments
 (0)