lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Yes, I certainly don't want to slow down any of Lua, and some of the comments did convince me it's a bad idea. That's why I asked..! :)

But ability to capture a number explicitly (some new "%" entry) still remains.. I'll think about it. :)

-ak


15.9.2004 kello 01:39, Adrián Pérez kirjoitti:


El 14/09/2004, a las 23:34, Asko Kauppi escribió:

Would it be hard to make 'string.find()' return true numbers [...]

I think it's not a true good idea, I agree with some of the opinions in the other replies. Even so, it's a matter of adding some lines in "lstrlib.c", at the "push_onecapture" function:

static void push_onecapture (MatchState *ms, int i) {
  int l = ms->capture[i].len;
  if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
  if (l == CAP_POSITION)
lua_pushnumber(ms->L, (lua_Number)(ms->capture[i].init - ms->src_init + 1));
  else {
    /* Code added here */
    double converted_number;
if (convert_strtodouble_ok(ms->capture[i].init, l, &converted_number)
       lua_pushnumber(ms->L, converted_number);
    else
       lua_pushlstring(ms->L, ms->capture[i].init, l);
  }
}


static int convert_strtodouble_ok(const char *string, size_t length, double *num)
{
   /*
* Fill with code here: try to convert with the strtod() C function, ad if it's OK * assign the converted number to *num, and return 1, else return 0.
     */
}


I have not tested this, but keep in mind that this is a sub-optimal approach: everytime a capture is pushed back to Lua it gets checked, and converted to number if possible. This is done for every capture (doing it only for "%d+" would need black magic!).


-ap