[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: numeric captures (string.find)
- From: Adrián Pérez <copylight@...>
- Date: Wed, 15 Sep 2004 00:39:06 +0200
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
Attachment:
PGP.sig
Description: Mensaje firmado digitalmente