Avoid useless cast on call luaS_newlstr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Avoid useless cast on call luaS_newlstr

Ranier Vilela-2
Maybe Lua never has to convert such large numbers.
But to satisfy luaS_newlstr, better pass the correct type?

LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);

diff --git a/lobject.c b/lobject.c
index f8ea917a..42144a1c 100644
--- a/lobject.c
+++ b/lobject.c
@@ -373,7 +373,7 @@ static int tostringbuff (TValue *obj, char *buff) {
 */
 void luaO_tostring (lua_State *L, TValue *obj) {
   char buff[MAXNUMBER2STR];
-  int len = tostringbuff(obj, buff);
+  size_t len = tostringbuff(obj, buff);
   setsvalue(L, obj, luaS_newlstr(L, buff, len));
 }
Reply | Threaded
Open this post in threaded view
|

Re: Avoid useless cast on call luaS_newlstr

Roberto Ierusalimschy
> Maybe Lua never has to convert such large numbers.

The current limit is 44, you are worried that 32000 [or 2M] can be too
little?


> But to satisfy luaS_newlstr, better pass the correct type?
>
> [...]
> -  int len = tostringbuff(obj, buff);
> +  size_t len = tostringbuff(obj, buff);

'tostringbuff' gets its result from lua_integer2str or lua_number2str,
which get this length from sprintf (or snprintf), which returns int.
This change would only move the cast to another place.

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: Avoid useless cast on call luaS_newlstr

Ranier Vilela-2
Em sex., 2 de out. de 2020 às 11:18, Roberto Ierusalimschy <[hidden email]> escreveu:
> Maybe Lua never has to convert such large numbers.

The current limit is 44, you are worried that 32000 [or 2M] can be too
little?
Not really.



> But to satisfy luaS_newlstr, better pass the correct type?
>
> [...]
> -  int len = tostringbuff(obj, buff);
> +  size_t len = tostringbuff(obj, buff);

'tostringbuff' gets its result from lua_integer2str or lua_number2str,
which get this length from sprintf (or snprintf), which returns int.
This change would only move the cast to another place.
One step.
To remove all casts from strings, as possible.

regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Avoid useless cast on call luaS_newlstr

Ranier Vilela-2
Em sex., 2 de out. de 2020 às 13:15, Ranier Vilela <[hidden email]> escreveu:
Em sex., 2 de out. de 2020 às 11:18, Roberto Ierusalimschy <[hidden email]> escreveu:
> Maybe Lua never has to convert such large numbers.

The current limit is 44, you are worried that 32000 [or 2M] can be too
little?
Not really.



> But to satisfy luaS_newlstr, better pass the correct type?
>
> [...]
> -  int len = tostringbuff(obj, buff);
> +  size_t len = tostringbuff(obj, buff);

'tostringbuff' gets its result from lua_integer2str or lua_number2str,
which get this length from sprintf (or snprintf), which returns int.
This change would only move the cast to another place.
One step.
To remove all casts from strings, as possible.
One another example.

Maybe, is not useful to capture a string more than 2GB, but,
Lua can!
 
diff --git a/lstrlib.c b/lstrlib.c
index 2ba8bde4..18d63bb7 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -529,7 +529,7 @@ static const char *min_expand (MatchState *ms, const char *s,
 
 
 static const char *start_capture (MatchState *ms, const char *s,
-                                    const char *p, int what) {
+                                    const char *p, size_t what) {
   const char *res;
   int level = ms->level;
   if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");

  ms->capture[level].len = what;
.len is ptrdiff_t.

regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Avoid useless cast on call luaS_newlstr

Ranier Vilela-2
Em sex., 2 de out. de 2020 às 14:20, Ranier Vilela <[hidden email]> escreveu:
Em sex., 2 de out. de 2020 às 13:15, Ranier Vilela <[hidden email]> escreveu:
Em sex., 2 de out. de 2020 às 11:18, Roberto Ierusalimschy <[hidden email]> escreveu:
> Maybe Lua never has to convert such large numbers.

The current limit is 44, you are worried that 32000 [or 2M] can be too
little?
Not really.



> But to satisfy luaS_newlstr, better pass the correct type?
>
> [...]
> -  int len = tostringbuff(obj, buff);
> +  size_t len = tostringbuff(obj, buff);

'tostringbuff' gets its result from lua_integer2str or lua_number2str,
which get this length from sprintf (or snprintf), which returns int.
This change would only move the cast to another place.
One step.
To remove all casts from strings, as possible.
One another example.

Maybe, is not useful to capture a string more than 2GB, but,
Lua can!
 
diff --git a/lstrlib.c b/lstrlib.c
index 2ba8bde4..18d63bb7 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -529,7 +529,7 @@ static const char *min_expand (MatchState *ms, const char *s,
 
 
 static const char *start_capture (MatchState *ms, const char *s,
-                                    const char *p, int what) {
+                                    const char *p, size_t what) {
   const char *res;
   int level = ms->level;
   if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");

  ms->capture[level].len = what;
.len is ptrdiff_t.
Think better "what" param should be ptrdiff_t?
size_t can too much.
 
regards,
Ranier Vilela