From 20b305007d3a5944e3ec6a9ef4b059217041cac7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Wed, 15 Dec 2021 16:59:41 +0100 Subject: [PATCH] :art: Put atoi and atof in string class --- src/lib/string.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib/string.c b/src/lib/string.c index 60559940..7d4c4138 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -397,6 +397,26 @@ static SFUN(string_load) { *(M_Object*)RETURN = new_string(shred->info->vm->gwion, c); } +static MFUN(string_atoi) { + const M_Object obj = *(M_Object*)MEM(0); + const m_str str = STRING(obj); + *(m_int*)RETURN = atoi(str); +} + +static MFUN(string_atof) { + const M_Object obj = *(M_Object*)MEM(0); + const m_str str = STRING(obj); + *(m_float*)RETURN = (m_float)atof(str); +} +/* +static MFUN(string_atoi2) { + const M_Object obj = *(M_Object*)MEM(0); + const m_str str = STRING(obj); + char *endptr; + *(m_int*)RETURN = strtol(str, &endptr, 10); + **(m_uint**)MEM(SZ_INT) = endptr - str; +} +*/ GWION_IMPORT(string) { const Type t_string = gwi_class_ini(gwi, "string", NULL); gwi->gwion->type[et_string] = t_string; // use func @@ -496,6 +516,16 @@ GWION_IMPORT(string) { gwi_func_arg(gwi, "string", "path"); GWI_BB(gwi_func_end(gwi, string_load, ae_flag_static)) + gwi_func_ini(gwi, "int", "atoi"); + GWI_BB(gwi_func_end(gwi, string_atoi, ae_flag_none)) + + gwi_func_ini(gwi, "float", "atof"); + GWI_BB(gwi_func_end(gwi, string_atof, ae_flag_none)) +/* + gwi_func_ini(gwi, "int", "atoi"); + gwi_func_arg(gwi, "Ref:[int]", "idx"); + GWI_BB(gwi_func_end(gwi, string_atoi2, ae_flag_none)) +*/ GWI_BB(gwi_class_end(gwi)) GWI_BB(gwi_oper_ini(gwi, "string", "string", "bool")) -- 2.43.0