lstrlib.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /*
  2. ** $Id: lstrlib.c $
  3. ** Standard library for string operations and pattern-matching
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstrlib_c
  7. #define LUA_LIB
  8. #include "lprefix.h"
  9. #include <ctype.h>
  10. #include <float.h>
  11. #include <limits.h>
  12. #include <locale.h>
  13. #include <math.h>
  14. #include <stddef.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "lua.h"
  19. #include "lauxlib.h"
  20. #include "lualib.h"
  21. /*
  22. ** maximum number of captures that a pattern can do during
  23. ** pattern-matching. This limit is arbitrary, but must fit in
  24. ** an unsigned char.
  25. */
  26. #if !defined(LUA_MAXCAPTURES)
  27. #define LUA_MAXCAPTURES 32
  28. #endif
  29. /* macro to 'unsign' a character */
  30. #define uchar(c) ((unsigned char)(c))
  31. /*
  32. ** Some sizes are better limited to fit in 'int', but must also fit in
  33. ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
  34. */
  35. #define MAX_SIZET ((size_t)(~(size_t)0))
  36. #define MAXSIZE \
  37. (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
  38. static int str_len (lua_State *L) {
  39. size_t l;
  40. luaL_checklstring(L, 1, &l);
  41. lua_pushinteger(L, (lua_Integer)l);
  42. return 1;
  43. }
  44. /*
  45. ** translate a relative initial string position
  46. ** (negative means back from end): clip result to [1, inf).
  47. ** The length of any string in Lua must fit in a lua_Integer,
  48. ** so there are no overflows in the casts.
  49. ** The inverted comparison avoids a possible overflow
  50. ** computing '-pos'.
  51. */
  52. static size_t posrelatI (lua_Integer pos, size_t len) {
  53. if (pos > 0)
  54. return (size_t)pos;
  55. else if (pos == 0)
  56. return 1;
  57. else if (pos < -(lua_Integer)len) /* inverted comparison */
  58. return 1; /* clip to 1 */
  59. else return len + (size_t)pos + 1;
  60. }
  61. /*
  62. ** Gets an optional ending string position from argument 'arg',
  63. ** with default value 'def'.
  64. ** Negative means back from end: clip result to [0, len]
  65. */
  66. static size_t getendpos (lua_State *L, int arg, lua_Integer def,
  67. size_t len) {
  68. lua_Integer pos = luaL_optinteger(L, arg, def);
  69. if (pos > (lua_Integer)len)
  70. return len;
  71. else if (pos >= 0)
  72. return (size_t)pos;
  73. else if (pos < -(lua_Integer)len)
  74. return 0;
  75. else return len + (size_t)pos + 1;
  76. }
  77. static int str_sub (lua_State *L) {
  78. size_t l;
  79. const char *s = luaL_checklstring(L, 1, &l);
  80. size_t start = posrelatI(luaL_checkinteger(L, 2), l);
  81. size_t end = getendpos(L, 3, -1, l);
  82. if (start <= end)
  83. lua_pushlstring(L, s + start - 1, (end - start) + 1);
  84. else lua_pushliteral(L, "");
  85. return 1;
  86. }
  87. static int str_reverse (lua_State *L) {
  88. size_t l, i;
  89. luaL_Buffer b;
  90. const char *s = luaL_checklstring(L, 1, &l);
  91. char *p = luaL_buffinitsize(L, &b, l);
  92. for (i = 0; i < l; i++)
  93. p[i] = s[l - i - 1];
  94. luaL_pushresultsize(&b, l);
  95. return 1;
  96. }
  97. static int str_lower (lua_State *L) {
  98. size_t l;
  99. size_t i;
  100. luaL_Buffer b;
  101. const char *s = luaL_checklstring(L, 1, &l);
  102. char *p = luaL_buffinitsize(L, &b, l);
  103. for (i=0; i<l; i++)
  104. p[i] = tolower(uchar(s[i]));
  105. luaL_pushresultsize(&b, l);
  106. return 1;
  107. }
  108. static int str_upper (lua_State *L) {
  109. size_t l;
  110. size_t i;
  111. luaL_Buffer b;
  112. const char *s = luaL_checklstring(L, 1, &l);
  113. char *p = luaL_buffinitsize(L, &b, l);
  114. for (i=0; i<l; i++)
  115. p[i] = toupper(uchar(s[i]));
  116. luaL_pushresultsize(&b, l);
  117. return 1;
  118. }
  119. static int str_rep (lua_State *L) {
  120. size_t l, lsep;
  121. const char *s = luaL_checklstring(L, 1, &l);
  122. lua_Integer n = luaL_checkinteger(L, 2);
  123. const char *sep = luaL_optlstring(L, 3, "", &lsep);
  124. if (n <= 0) lua_pushliteral(L, "");
  125. else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
  126. return luaL_error(L, "resulting string too large");
  127. else {
  128. size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
  129. luaL_Buffer b;
  130. char *p = luaL_buffinitsize(L, &b, totallen);
  131. while (n-- > 1) { /* first n-1 copies (followed by separator) */
  132. memcpy(p, s, l * sizeof(char)); p += l;
  133. if (lsep > 0) { /* empty 'memcpy' is not that cheap */
  134. memcpy(p, sep, lsep * sizeof(char));
  135. p += lsep;
  136. }
  137. }
  138. memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
  139. luaL_pushresultsize(&b, totallen);
  140. }
  141. return 1;
  142. }
  143. static int str_byte (lua_State *L) {
  144. size_t l;
  145. const char *s = luaL_checklstring(L, 1, &l);
  146. lua_Integer pi = luaL_optinteger(L, 2, 1);
  147. size_t posi = posrelatI(pi, l);
  148. size_t pose = getendpos(L, 3, pi, l);
  149. int n, i;
  150. if (posi > pose) return 0; /* empty interval; return no values */
  151. if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */
  152. return luaL_error(L, "string slice too long");
  153. n = (int)(pose - posi) + 1;
  154. luaL_checkstack(L, n, "string slice too long");
  155. for (i=0; i<n; i++)
  156. lua_pushinteger(L, uchar(s[posi+i-1]));
  157. return n;
  158. }
  159. static int str_char (lua_State *L) {
  160. int n = lua_gettop(L); /* number of arguments */
  161. int i;
  162. luaL_Buffer b;
  163. char *p = luaL_buffinitsize(L, &b, n);
  164. for (i=1; i<=n; i++) {
  165. lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
  166. luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
  167. p[i - 1] = uchar(c);
  168. }
  169. luaL_pushresultsize(&b, n);
  170. return 1;
  171. }
  172. static int writer (lua_State *L, const void *b, size_t size, void *B) {
  173. (void)L;
  174. luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
  175. return 0;
  176. }
  177. static int str_dump (lua_State *L) {
  178. luaL_Buffer b;
  179. int strip = lua_toboolean(L, 2);
  180. luaL_checktype(L, 1, LUA_TFUNCTION);
  181. lua_settop(L, 1);
  182. luaL_buffinit(L,&b);
  183. if (lua_dump(L, writer, &b, strip) != 0)
  184. return luaL_error(L, "unable to dump given function");
  185. luaL_pushresult(&b);
  186. return 1;
  187. }
  188. /*
  189. ** {======================================================
  190. ** METAMETHODS
  191. ** =======================================================
  192. */
  193. #if defined(LUA_NOCVTS2N) /* { */
  194. /* no coercion from strings to numbers */
  195. static const luaL_Reg stringmetamethods[] = {
  196. {"__index", NULL}, /* placeholder */
  197. {NULL, NULL}
  198. };
  199. #else /* }{ */
  200. static int tonum (lua_State *L, int arg) {
  201. if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */
  202. lua_pushvalue(L, arg);
  203. return 1;
  204. }
  205. else { /* check whether it is a numerical string */
  206. size_t len;
  207. const char *s = lua_tolstring(L, arg, &len);
  208. return (s != NULL && lua_stringtonumber(L, s) == len + 1);
  209. }
  210. }
  211. static void trymt (lua_State *L, const char *mtname) {
  212. lua_settop(L, 2); /* back to the original arguments */
  213. if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname))
  214. luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2,
  215. luaL_typename(L, -2), luaL_typename(L, -1));
  216. lua_insert(L, -3); /* put metamethod before arguments */
  217. lua_call(L, 2, 1); /* call metamethod */
  218. }
  219. static int arith (lua_State *L, int op, const char *mtname) {
  220. if (tonum(L, 1) && tonum(L, 2))
  221. lua_arith(L, op); /* result will be on the top */
  222. else
  223. trymt(L, mtname);
  224. return 1;
  225. }
  226. static int arith_add (lua_State *L) {
  227. return arith(L, LUA_OPADD, "__add");
  228. }
  229. static int arith_sub (lua_State *L) {
  230. return arith(L, LUA_OPSUB, "__sub");
  231. }
  232. static int arith_mul (lua_State *L) {
  233. return arith(L, LUA_OPMUL, "__mul");
  234. }
  235. static int arith_mod (lua_State *L) {
  236. return arith(L, LUA_OPMOD, "__mod");
  237. }
  238. static int arith_pow (lua_State *L) {
  239. return arith(L, LUA_OPPOW, "__pow");
  240. }
  241. static int arith_div (lua_State *L) {
  242. return arith(L, LUA_OPDIV, "__div");
  243. }
  244. static int arith_idiv (lua_State *L) {
  245. return arith(L, LUA_OPIDIV, "__idiv");
  246. }
  247. static int arith_unm (lua_State *L) {
  248. return arith(L, LUA_OPUNM, "__unm");
  249. }
  250. static const luaL_Reg stringmetamethods[] = {
  251. {"__add", arith_add},
  252. {"__sub", arith_sub},
  253. {"__mul", arith_mul},
  254. {"__mod", arith_mod},
  255. {"__pow", arith_pow},
  256. {"__div", arith_div},
  257. {"__idiv", arith_idiv},
  258. {"__unm", arith_unm},
  259. {"__index", NULL}, /* placeholder */
  260. {NULL, NULL}
  261. };
  262. #endif /* } */
  263. /* }====================================================== */
  264. /*
  265. ** {======================================================
  266. ** PATTERN MATCHING
  267. ** =======================================================
  268. */
  269. #define CAP_UNFINISHED (-1)
  270. #define CAP_POSITION (-2)
  271. typedef struct MatchState {
  272. const char *src_init; /* init of source string */
  273. const char *src_end; /* end ('\0') of source string */
  274. const char *p_end; /* end ('\0') of pattern */
  275. lua_State *L;
  276. int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
  277. unsigned char level; /* total number of captures (finished or unfinished) */
  278. struct {
  279. const char *init;
  280. ptrdiff_t len;
  281. } capture[LUA_MAXCAPTURES];
  282. } MatchState;
  283. /* recursive function */
  284. static const char *match (MatchState *ms, const char *s, const char *p);
  285. /* maximum recursion depth for 'match' */
  286. #if !defined(MAXCCALLS)
  287. #define MAXCCALLS 200
  288. #endif
  289. #define L_ESC '%'
  290. #define SPECIALS "^$*+?.([%-"
  291. static int check_capture (MatchState *ms, int l) {
  292. l -= '1';
  293. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  294. return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
  295. return l;
  296. }
  297. static int capture_to_close (MatchState *ms) {
  298. int level = ms->level;
  299. for (level--; level>=0; level--)
  300. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  301. return luaL_error(ms->L, "invalid pattern capture");
  302. }
  303. static const char *classend (MatchState *ms, const char *p) {
  304. switch (*p++) {
  305. case L_ESC: {
  306. if (p == ms->p_end)
  307. luaL_error(ms->L, "malformed pattern (ends with '%%')");
  308. return p+1;
  309. }
  310. case '[': {
  311. if (*p == '^') p++;
  312. do { /* look for a ']' */
  313. if (p == ms->p_end)
  314. luaL_error(ms->L, "malformed pattern (missing ']')");
  315. if (*(p++) == L_ESC && p < ms->p_end)
  316. p++; /* skip escapes (e.g. '%]') */
  317. } while (*p != ']');
  318. return p+1;
  319. }
  320. default: {
  321. return p;
  322. }
  323. }
  324. }
  325. static int match_class (int c, int cl) {
  326. int res;
  327. switch (tolower(cl)) {
  328. case 'a' : res = isalpha(c); break;
  329. case 'c' : res = iscntrl(c); break;
  330. case 'd' : res = isdigit(c); break;
  331. case 'g' : res = isgraph(c); break;
  332. case 'l' : res = islower(c); break;
  333. case 'p' : res = ispunct(c); break;
  334. case 's' : res = isspace(c); break;
  335. case 'u' : res = isupper(c); break;
  336. case 'w' : res = isalnum(c); break;
  337. case 'x' : res = isxdigit(c); break;
  338. case 'z' : res = (c == 0); break; /* deprecated option */
  339. default: return (cl == c);
  340. }
  341. return (islower(cl) ? res : !res);
  342. }
  343. static int matchbracketclass (int c, const char *p, const char *ec) {
  344. int sig = 1;
  345. if (*(p+1) == '^') {
  346. sig = 0;
  347. p++; /* skip the '^' */
  348. }
  349. while (++p < ec) {
  350. if (*p == L_ESC) {
  351. p++;
  352. if (match_class(c, uchar(*p)))
  353. return sig;
  354. }
  355. else if ((*(p+1) == '-') && (p+2 < ec)) {
  356. p+=2;
  357. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  358. return sig;
  359. }
  360. else if (uchar(*p) == c) return sig;
  361. }
  362. return !sig;
  363. }
  364. static int singlematch (MatchState *ms, const char *s, const char *p,
  365. const char *ep) {
  366. if (s >= ms->src_end)
  367. return 0;
  368. else {
  369. int c = uchar(*s);
  370. switch (*p) {
  371. case '.': return 1; /* matches any char */
  372. case L_ESC: return match_class(c, uchar(*(p+1)));
  373. case '[': return matchbracketclass(c, p, ep-1);
  374. default: return (uchar(*p) == c);
  375. }
  376. }
  377. }
  378. static const char *matchbalance (MatchState *ms, const char *s,
  379. const char *p) {
  380. if (p >= ms->p_end - 1)
  381. luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
  382. if (*s != *p) return NULL;
  383. else {
  384. int b = *p;
  385. int e = *(p+1);
  386. int cont = 1;
  387. while (++s < ms->src_end) {
  388. if (*s == e) {
  389. if (--cont == 0) return s+1;
  390. }
  391. else if (*s == b) cont++;
  392. }
  393. }
  394. return NULL; /* string ends out of balance */
  395. }
  396. static const char *max_expand (MatchState *ms, const char *s,
  397. const char *p, const char *ep) {
  398. ptrdiff_t i = 0; /* counts maximum expand for item */
  399. while (singlematch(ms, s + i, p, ep))
  400. i++;
  401. /* keeps trying to match with the maximum repetitions */
  402. while (i>=0) {
  403. const char *res = match(ms, (s+i), ep+1);
  404. if (res) return res;
  405. i--; /* else didn't match; reduce 1 repetition to try again */
  406. }
  407. return NULL;
  408. }
  409. static const char *min_expand (MatchState *ms, const char *s,
  410. const char *p, const char *ep) {
  411. for (;;) {
  412. const char *res = match(ms, s, ep+1);
  413. if (res != NULL)
  414. return res;
  415. else if (singlematch(ms, s, p, ep))
  416. s++; /* try with one more repetition */
  417. else return NULL;
  418. }
  419. }
  420. static const char *start_capture (MatchState *ms, const char *s,
  421. const char *p, int what) {
  422. const char *res;
  423. int level = ms->level;
  424. if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
  425. ms->capture[level].init = s;
  426. ms->capture[level].len = what;
  427. ms->level = level+1;
  428. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  429. ms->level--; /* undo capture */
  430. return res;
  431. }
  432. static const char *end_capture (MatchState *ms, const char *s,
  433. const char *p) {
  434. int l = capture_to_close(ms);
  435. const char *res;
  436. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  437. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  438. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  439. return res;
  440. }
  441. static const char *match_capture (MatchState *ms, const char *s, int l) {
  442. size_t len;
  443. l = check_capture(ms, l);
  444. len = ms->capture[l].len;
  445. if ((size_t)(ms->src_end-s) >= len &&
  446. memcmp(ms->capture[l].init, s, len) == 0)
  447. return s+len;
  448. else return NULL;
  449. }
  450. static const char *match (MatchState *ms, const char *s, const char *p) {
  451. if (ms->matchdepth-- == 0)
  452. luaL_error(ms->L, "pattern too complex");
  453. init: /* using goto's to optimize tail recursion */
  454. if (p != ms->p_end) { /* end of pattern? */
  455. switch (*p) {
  456. case '(': { /* start capture */
  457. if (*(p + 1) == ')') /* position capture? */
  458. s = start_capture(ms, s, p + 2, CAP_POSITION);
  459. else
  460. s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
  461. break;
  462. }
  463. case ')': { /* end capture */
  464. s = end_capture(ms, s, p + 1);
  465. break;
  466. }
  467. case '$': {
  468. if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */
  469. goto dflt; /* no; go to default */
  470. s = (s == ms->src_end) ? s : NULL; /* check end of string */
  471. break;
  472. }
  473. case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
  474. switch (*(p + 1)) {
  475. case 'b': { /* balanced string? */
  476. s = matchbalance(ms, s, p + 2);
  477. if (s != NULL) {
  478. p += 4; goto init; /* return match(ms, s, p + 4); */
  479. } /* else fail (s == NULL) */
  480. break;
  481. }
  482. case 'f': { /* frontier? */
  483. const char *ep; char previous;
  484. p += 2;
  485. if (*p != '[')
  486. luaL_error(ms->L, "missing '[' after '%%f' in pattern");
  487. ep = classend(ms, p); /* points to what is next */
  488. previous = (s == ms->src_init) ? '\0' : *(s - 1);
  489. if (!matchbracketclass(uchar(previous), p, ep - 1) &&
  490. matchbracketclass(uchar(*s), p, ep - 1)) {
  491. p = ep; goto init; /* return match(ms, s, ep); */
  492. }
  493. s = NULL; /* match failed */
  494. break;
  495. }
  496. case '0': case '1': case '2': case '3':
  497. case '4': case '5': case '6': case '7':
  498. case '8': case '9': { /* capture results (%0-%9)? */
  499. s = match_capture(ms, s, uchar(*(p + 1)));
  500. if (s != NULL) {
  501. p += 2; goto init; /* return match(ms, s, p + 2) */
  502. }
  503. break;
  504. }
  505. default: goto dflt;
  506. }
  507. break;
  508. }
  509. default: dflt: { /* pattern class plus optional suffix */
  510. const char *ep = classend(ms, p); /* points to optional suffix */
  511. /* does not match at least once? */
  512. if (!singlematch(ms, s, p, ep)) {
  513. if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
  514. p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
  515. }
  516. else /* '+' or no suffix */
  517. s = NULL; /* fail */
  518. }
  519. else { /* matched once */
  520. switch (*ep) { /* handle optional suffix */
  521. case '?': { /* optional */
  522. const char *res;
  523. if ((res = match(ms, s + 1, ep + 1)) != NULL)
  524. s = res;
  525. else {
  526. p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
  527. }
  528. break;
  529. }
  530. case '+': /* 1 or more repetitions */
  531. s++; /* 1 match already done */
  532. /* FALLTHROUGH */
  533. case '*': /* 0 or more repetitions */
  534. s = max_expand(ms, s, p, ep);
  535. break;
  536. case '-': /* 0 or more repetitions (minimum) */
  537. s = min_expand(ms, s, p, ep);
  538. break;
  539. default: /* no suffix */
  540. s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
  541. }
  542. }
  543. break;
  544. }
  545. }
  546. }
  547. ms->matchdepth++;
  548. return s;
  549. }
  550. static const char *lmemfind (const char *s1, size_t l1,
  551. const char *s2, size_t l2) {
  552. if (l2 == 0) return s1; /* empty strings are everywhere */
  553. else if (l2 > l1) return NULL; /* avoids a negative 'l1' */
  554. else {
  555. const char *init; /* to search for a '*s2' inside 's1' */
  556. l2--; /* 1st char will be checked by 'memchr' */
  557. l1 = l1-l2; /* 's2' cannot be found after that */
  558. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  559. init++; /* 1st char is already checked */
  560. if (memcmp(init, s2+1, l2) == 0)
  561. return init-1;
  562. else { /* correct 'l1' and 's1' to try again */
  563. l1 -= init-s1;
  564. s1 = init;
  565. }
  566. }
  567. return NULL; /* not found */
  568. }
  569. }
  570. /*
  571. ** get information about the i-th capture. If there are no captures
  572. ** and 'i==0', return information about the whole match, which
  573. ** is the range 's'..'e'. If the capture is a string, return
  574. ** its length and put its address in '*cap'. If it is an integer
  575. ** (a position), push it on the stack and return CAP_POSITION.
  576. */
  577. static size_t get_onecapture (MatchState *ms, int i, const char *s,
  578. const char *e, const char **cap) {
  579. if (i >= ms->level) {
  580. if (i != 0)
  581. luaL_error(ms->L, "invalid capture index %%%d", i + 1);
  582. *cap = s;
  583. return e - s;
  584. }
  585. else {
  586. ptrdiff_t capl = ms->capture[i].len;
  587. *cap = ms->capture[i].init;
  588. if (capl == CAP_UNFINISHED)
  589. luaL_error(ms->L, "unfinished capture");
  590. else if (capl == CAP_POSITION)
  591. lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
  592. return capl;
  593. }
  594. }
  595. /*
  596. ** Push the i-th capture on the stack.
  597. */
  598. static void push_onecapture (MatchState *ms, int i, const char *s,
  599. const char *e) {
  600. const char *cap;
  601. ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
  602. if (l != CAP_POSITION)
  603. lua_pushlstring(ms->L, cap, l);
  604. /* else position was already pushed */
  605. }
  606. static int push_captures (MatchState *ms, const char *s, const char *e) {
  607. int i;
  608. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  609. luaL_checkstack(ms->L, nlevels, "too many captures");
  610. for (i = 0; i < nlevels; i++)
  611. push_onecapture(ms, i, s, e);
  612. return nlevels; /* number of strings pushed */
  613. }
  614. /* check whether pattern has no special characters */
  615. static int nospecials (const char *p, size_t l) {
  616. size_t upto = 0;
  617. do {
  618. if (strpbrk(p + upto, SPECIALS))
  619. return 0; /* pattern has a special character */
  620. upto += strlen(p + upto) + 1; /* may have more after \0 */
  621. } while (upto <= l);
  622. return 1; /* no special chars found */
  623. }
  624. static void prepstate (MatchState *ms, lua_State *L,
  625. const char *s, size_t ls, const char *p, size_t lp) {
  626. ms->L = L;
  627. ms->matchdepth = MAXCCALLS;
  628. ms->src_init = s;
  629. ms->src_end = s + ls;
  630. ms->p_end = p + lp;
  631. }
  632. static void reprepstate (MatchState *ms) {
  633. ms->level = 0;
  634. lua_assert(ms->matchdepth == MAXCCALLS);
  635. }
  636. static int str_find_aux (lua_State *L, int find) {
  637. size_t ls, lp;
  638. const char *s = luaL_checklstring(L, 1, &ls);
  639. const char *p = luaL_checklstring(L, 2, &lp);
  640. size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
  641. if (init > ls) { /* start after string's end? */
  642. luaL_pushfail(L); /* cannot find anything */
  643. return 1;
  644. }
  645. /* explicit request or no special characters? */
  646. if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
  647. /* do a plain search */
  648. const char *s2 = lmemfind(s + init, ls - init, p, lp);
  649. if (s2) {
  650. lua_pushinteger(L, (s2 - s) + 1);
  651. lua_pushinteger(L, (s2 - s) + lp);
  652. return 2;
  653. }
  654. }
  655. else {
  656. MatchState ms;
  657. const char *s1 = s + init;
  658. int anchor = (*p == '^');
  659. if (anchor) {
  660. p++; lp--; /* skip anchor character */
  661. }
  662. prepstate(&ms, L, s, ls, p, lp);
  663. do {
  664. const char *res;
  665. reprepstate(&ms);
  666. if ((res=match(&ms, s1, p)) != NULL) {
  667. if (find) {
  668. lua_pushinteger(L, (s1 - s) + 1); /* start */
  669. lua_pushinteger(L, res - s); /* end */
  670. return push_captures(&ms, NULL, 0) + 2;
  671. }
  672. else
  673. return push_captures(&ms, s1, res);
  674. }
  675. } while (s1++ < ms.src_end && !anchor);
  676. }
  677. luaL_pushfail(L); /* not found */
  678. return 1;
  679. }
  680. static int str_find (lua_State *L) {
  681. return str_find_aux(L, 1);
  682. }
  683. static int str_match (lua_State *L) {
  684. return str_find_aux(L, 0);
  685. }
  686. /* state for 'gmatch' */
  687. typedef struct GMatchState {
  688. const char *src; /* current position */
  689. const char *p; /* pattern */
  690. const char *lastmatch; /* end of last match */
  691. MatchState ms; /* match state */
  692. } GMatchState;
  693. static int gmatch_aux (lua_State *L) {
  694. GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
  695. const char *src;
  696. gm->ms.L = L;
  697. for (src = gm->src; src <= gm->ms.src_end; src++) {
  698. const char *e;
  699. reprepstate(&gm->ms);
  700. if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) {
  701. gm->src = gm->lastmatch = e;
  702. return push_captures(&gm->ms, src, e);
  703. }
  704. }
  705. return 0; /* not found */
  706. }
  707. static int gmatch (lua_State *L) {
  708. size_t ls, lp;
  709. const char *s = luaL_checklstring(L, 1, &ls);
  710. const char *p = luaL_checklstring(L, 2, &lp);
  711. size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
  712. GMatchState *gm;
  713. lua_settop(L, 2); /* keep strings on closure to avoid being collected */
  714. gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0);
  715. if (init > ls) /* start after string's end? */
  716. init = ls + 1; /* avoid overflows in 's + init' */
  717. prepstate(&gm->ms, L, s, ls, p, lp);
  718. gm->src = s + init; gm->p = p; gm->lastmatch = NULL;
  719. lua_pushcclosure(L, gmatch_aux, 3);
  720. return 1;
  721. }
  722. static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
  723. const char *e) {
  724. size_t l;
  725. lua_State *L = ms->L;
  726. const char *news = lua_tolstring(L, 3, &l);
  727. const char *p;
  728. while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
  729. luaL_addlstring(b, news, p - news);
  730. p++; /* skip ESC */
  731. if (*p == L_ESC) /* '%%' */
  732. luaL_addchar(b, *p);
  733. else if (*p == '0') /* '%0' */
  734. luaL_addlstring(b, s, e - s);
  735. else if (isdigit(uchar(*p))) { /* '%n' */
  736. const char *cap;
  737. ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
  738. if (resl == CAP_POSITION)
  739. luaL_addvalue(b); /* add position to accumulated result */
  740. else
  741. luaL_addlstring(b, cap, resl);
  742. }
  743. else
  744. luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
  745. l -= p + 1 - news;
  746. news = p + 1;
  747. }
  748. luaL_addlstring(b, news, l);
  749. }
  750. /*
  751. ** Add the replacement value to the string buffer 'b'.
  752. ** Return true if the original string was changed. (Function calls and
  753. ** table indexing resulting in nil or false do not change the subject.)
  754. */
  755. static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
  756. const char *e, int tr) {
  757. lua_State *L = ms->L;
  758. switch (tr) {
  759. case LUA_TFUNCTION: { /* call the function */
  760. int n;
  761. lua_pushvalue(L, 3); /* push the function */
  762. n = push_captures(ms, s, e); /* all captures as arguments */
  763. lua_call(L, n, 1); /* call it */
  764. break;
  765. }
  766. case LUA_TTABLE: { /* index the table */
  767. push_onecapture(ms, 0, s, e); /* first capture is the index */
  768. lua_gettable(L, 3);
  769. break;
  770. }
  771. default: { /* LUA_TNUMBER or LUA_TSTRING */
  772. add_s(ms, b, s, e); /* add value to the buffer */
  773. return 1; /* something changed */
  774. }
  775. }
  776. if (!lua_toboolean(L, -1)) { /* nil or false? */
  777. lua_pop(L, 1); /* remove value */
  778. luaL_addlstring(b, s, e - s); /* keep original text */
  779. return 0; /* no changes */
  780. }
  781. else if (!lua_isstring(L, -1))
  782. return luaL_error(L, "invalid replacement value (a %s)",
  783. luaL_typename(L, -1));
  784. else {
  785. luaL_addvalue(b); /* add result to accumulator */
  786. return 1; /* something changed */
  787. }
  788. }
  789. static int str_gsub (lua_State *L) {
  790. size_t srcl, lp;
  791. const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
  792. const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
  793. const char *lastmatch = NULL; /* end of last match */
  794. int tr = lua_type(L, 3); /* replacement type */
  795. lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
  796. int anchor = (*p == '^');
  797. lua_Integer n = 0; /* replacement count */
  798. int changed = 0; /* change flag */
  799. MatchState ms;
  800. luaL_Buffer b;
  801. luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
  802. tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
  803. "string/function/table");
  804. luaL_buffinit(L, &b);
  805. if (anchor) {
  806. p++; lp--; /* skip anchor character */
  807. }
  808. prepstate(&ms, L, src, srcl, p, lp);
  809. while (n < max_s) {
  810. const char *e;
  811. reprepstate(&ms); /* (re)prepare state for new match */
  812. if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
  813. n++;
  814. changed = add_value(&ms, &b, src, e, tr) | changed;
  815. src = lastmatch = e;
  816. }
  817. else if (src < ms.src_end) /* otherwise, skip one character */
  818. luaL_addchar(&b, *src++);
  819. else break; /* end of subject */
  820. if (anchor) break;
  821. }
  822. if (!changed) /* no changes? */
  823. lua_pushvalue(L, 1); /* return original string */
  824. else { /* something changed */
  825. luaL_addlstring(&b, src, ms.src_end-src);
  826. luaL_pushresult(&b); /* create and return new string */
  827. }
  828. lua_pushinteger(L, n); /* number of substitutions */
  829. return 2;
  830. }
  831. /* }====================================================== */
  832. /*
  833. ** {======================================================
  834. ** STRING FORMAT
  835. ** =======================================================
  836. */
  837. #if !defined(lua_number2strx) /* { */
  838. /*
  839. ** Hexadecimal floating-point formatter
  840. */
  841. #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
  842. /*
  843. ** Number of bits that goes into the first digit. It can be any value
  844. ** between 1 and 4; the following definition tries to align the number
  845. ** to nibble boundaries by making what is left after that first digit a
  846. ** multiple of 4.
  847. */
  848. #define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
  849. /*
  850. ** Add integer part of 'x' to buffer and return new 'x'
  851. */
  852. static lua_Number adddigit (char *buff, int n, lua_Number x) {
  853. lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
  854. int d = (int)dd;
  855. buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
  856. return x - dd; /* return what is left */
  857. }
  858. static int num2straux (char *buff, int sz, lua_Number x) {
  859. /* if 'inf' or 'NaN', format it like '%g' */
  860. if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
  861. return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
  862. else if (x == 0) { /* can be -0... */
  863. /* create "0" or "-0" followed by exponent */
  864. return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x);
  865. }
  866. else {
  867. int e;
  868. lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
  869. int n = 0; /* character count */
  870. if (m < 0) { /* is number negative? */
  871. buff[n++] = '-'; /* add sign */
  872. m = -m; /* make it positive */
  873. }
  874. buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */
  875. m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */
  876. e -= L_NBFD; /* this digit goes before the radix point */
  877. if (m > 0) { /* more digits? */
  878. buff[n++] = lua_getlocaledecpoint(); /* add radix point */
  879. do { /* add as many digits as needed */
  880. m = adddigit(buff, n++, m * 16);
  881. } while (m > 0);
  882. }
  883. n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
  884. lua_assert(n < sz);
  885. return n;
  886. }
  887. }
  888. static int lua_number2strx (lua_State *L, char *buff, int sz,
  889. const char *fmt, lua_Number x) {
  890. int n = num2straux(buff, sz, x);
  891. if (fmt[SIZELENMOD] == 'A') {
  892. int i;
  893. for (i = 0; i < n; i++)
  894. buff[i] = toupper(uchar(buff[i]));
  895. }
  896. else if (fmt[SIZELENMOD] != 'a')
  897. return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
  898. return n;
  899. }
  900. #endif /* } */
  901. /*
  902. ** Maximum size for items formatted with '%f'. This size is produced
  903. ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
  904. ** and '\0') + number of decimal digits to represent maxfloat (which
  905. ** is maximum exponent + 1). (99+3+1, adding some extra, 110)
  906. */
  907. #define MAX_ITEMF (110 + l_mathlim(MAX_10_EXP))
  908. /*
  909. ** All formats except '%f' do not need that large limit. The other
  910. ** float formats use exponents, so that they fit in the 99 limit for
  911. ** significant digits; 's' for large strings and 'q' add items directly
  912. ** to the buffer; all integer formats also fit in the 99 limit. The
  913. ** worst case are floats: they may need 99 significant digits, plus
  914. ** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120.
  915. */
  916. #define MAX_ITEM 120
  917. /* valid flags in a format specification */
  918. #define FLAGS "-+ #0"
  919. /*
  920. ** maximum size of each format specification (such as "%-099.99d")
  921. */
  922. #define MAX_FORMAT 32
  923. static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
  924. luaL_addchar(b, '"');
  925. while (len--) {
  926. if (*s == '"' || *s == '\\' || *s == '\n') {
  927. luaL_addchar(b, '\\');
  928. luaL_addchar(b, *s);
  929. }
  930. else if (iscntrl(uchar(*s))) {
  931. char buff[10];
  932. if (!isdigit(uchar(*(s+1))))
  933. l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
  934. else
  935. l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
  936. luaL_addstring(b, buff);
  937. }
  938. else
  939. luaL_addchar(b, *s);
  940. s++;
  941. }
  942. luaL_addchar(b, '"');
  943. }
  944. /*
  945. ** Serialize a floating-point number in such a way that it can be
  946. ** scanned back by Lua. Use hexadecimal format for "common" numbers
  947. ** (to preserve precision); inf, -inf, and NaN are handled separately.
  948. ** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.)
  949. */
  950. static int quotefloat (lua_State *L, char *buff, lua_Number n) {
  951. const char *s; /* for the fixed representations */
  952. if (n == (lua_Number)HUGE_VAL) /* inf? */
  953. s = "1e9999";
  954. else if (n == -(lua_Number)HUGE_VAL) /* -inf? */
  955. s = "-1e9999";
  956. else if (n != n) /* NaN? */
  957. s = "(0/0)";
  958. else { /* format number as hexadecimal */
  959. int nb = lua_number2strx(L, buff, MAX_ITEM,
  960. "%" LUA_NUMBER_FRMLEN "a", n);
  961. /* ensures that 'buff' string uses a dot as the radix character */
  962. if (memchr(buff, '.', nb) == NULL) { /* no dot? */
  963. char point = lua_getlocaledecpoint(); /* try locale point */
  964. char *ppoint = (char *)memchr(buff, point, nb);
  965. if (ppoint) *ppoint = '.'; /* change it to a dot */
  966. }
  967. return nb;
  968. }
  969. /* for the fixed representations */
  970. return l_sprintf(buff, MAX_ITEM, "%s", s);
  971. }
  972. static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
  973. switch (lua_type(L, arg)) {
  974. case LUA_TSTRING: {
  975. size_t len;
  976. const char *s = lua_tolstring(L, arg, &len);
  977. addquoted(b, s, len);
  978. break;
  979. }
  980. case LUA_TNUMBER: {
  981. char *buff = luaL_prepbuffsize(b, MAX_ITEM);
  982. int nb;
  983. if (!lua_isinteger(L, arg)) /* float? */
  984. nb = quotefloat(L, buff, lua_tonumber(L, arg));
  985. else { /* integers */
  986. lua_Integer n = lua_tointeger(L, arg);
  987. const char *format = (n == LUA_MININTEGER) /* corner case? */
  988. ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */
  989. : LUA_INTEGER_FMT; /* else use default format */
  990. nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
  991. }
  992. luaL_addsize(b, nb);
  993. break;
  994. }
  995. case LUA_TNIL: case LUA_TBOOLEAN: {
  996. luaL_tolstring(L, arg, NULL);
  997. luaL_addvalue(b);
  998. break;
  999. }
  1000. default: {
  1001. luaL_argerror(L, arg, "value has no literal form");
  1002. }
  1003. }
  1004. }
  1005. static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
  1006. const char *p = strfrmt;
  1007. while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
  1008. if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
  1009. luaL_error(L, "invalid format (repeated flags)");
  1010. if (isdigit(uchar(*p))) p++; /* skip width */
  1011. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  1012. if (*p == '.') {
  1013. p++;
  1014. if (isdigit(uchar(*p))) p++; /* skip precision */
  1015. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  1016. }
  1017. if (isdigit(uchar(*p)))
  1018. luaL_error(L, "invalid format (width or precision too long)");
  1019. *(form++) = '%';
  1020. memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char));
  1021. form += (p - strfrmt) + 1;
  1022. *form = '\0';
  1023. return p;
  1024. }
  1025. /*
  1026. ** add length modifier into formats
  1027. */
  1028. static void addlenmod (char *form, const char *lenmod) {
  1029. size_t l = strlen(form);
  1030. size_t lm = strlen(lenmod);
  1031. char spec = form[l - 1];
  1032. strcpy(form + l - 1, lenmod);
  1033. form[l + lm - 1] = spec;
  1034. form[l + lm] = '\0';
  1035. }
  1036. static int str_format (lua_State *L) {
  1037. int top = lua_gettop(L);
  1038. int arg = 1;
  1039. size_t sfl;
  1040. const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  1041. const char *strfrmt_end = strfrmt+sfl;
  1042. luaL_Buffer b;
  1043. luaL_buffinit(L, &b);
  1044. while (strfrmt < strfrmt_end) {
  1045. if (*strfrmt != L_ESC)
  1046. luaL_addchar(&b, *strfrmt++);
  1047. else if (*++strfrmt == L_ESC)
  1048. luaL_addchar(&b, *strfrmt++); /* %% */
  1049. else { /* format item */
  1050. char form[MAX_FORMAT]; /* to store the format ('%...') */
  1051. int maxitem = MAX_ITEM;
  1052. char *buff = luaL_prepbuffsize(&b, maxitem); /* to put formatted item */
  1053. int nb = 0; /* number of bytes in added item */
  1054. if (++arg > top)
  1055. return luaL_argerror(L, arg, "no value");
  1056. strfrmt = scanformat(L, strfrmt, form);
  1057. switch (*strfrmt++) {
  1058. case 'c': {
  1059. nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg));
  1060. break;
  1061. }
  1062. case 'd': case 'i':
  1063. case 'o': case 'u': case 'x': case 'X': {
  1064. lua_Integer n = luaL_checkinteger(L, arg);
  1065. addlenmod(form, LUA_INTEGER_FRMLEN);
  1066. nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n);
  1067. break;
  1068. }
  1069. case 'a': case 'A':
  1070. addlenmod(form, LUA_NUMBER_FRMLEN);
  1071. nb = lua_number2strx(L, buff, maxitem, form,
  1072. luaL_checknumber(L, arg));
  1073. break;
  1074. case 'f':
  1075. maxitem = MAX_ITEMF; /* extra space for '%f' */
  1076. buff = luaL_prepbuffsize(&b, maxitem);
  1077. /* FALLTHROUGH */
  1078. case 'e': case 'E': case 'g': case 'G': {
  1079. lua_Number n = luaL_checknumber(L, arg);
  1080. addlenmod(form, LUA_NUMBER_FRMLEN);
  1081. nb = snprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
  1082. break;
  1083. }
  1084. case 'p': {
  1085. const void *p = lua_topointer(L, arg);
  1086. nb = l_sprintf(buff, maxitem, form, p);
  1087. break;
  1088. }
  1089. case 'q': {
  1090. if (form[2] != '\0') /* modifiers? */
  1091. return luaL_error(L, "specifier '%%q' cannot have modifiers");
  1092. addliteral(L, &b, arg);
  1093. break;
  1094. }
  1095. case 's': {
  1096. size_t l;
  1097. const char *s = luaL_tolstring(L, arg, &l);
  1098. if (form[2] == '\0') /* no modifiers? */
  1099. luaL_addvalue(&b); /* keep entire string */
  1100. else {
  1101. luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
  1102. if (!strchr(form, '.') && l >= 100) {
  1103. /* no precision and string is too long to be formatted */
  1104. luaL_addvalue(&b); /* keep entire string */
  1105. }
  1106. else { /* format the string into 'buff' */
  1107. nb = l_sprintf(buff, maxitem, form, s);
  1108. lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
  1109. }
  1110. }
  1111. break;
  1112. }
  1113. default: { /* also treat cases 'pnLlh' */
  1114. return luaL_error(L, "invalid conversion '%s' to 'format'", form);
  1115. }
  1116. }
  1117. lua_assert(nb < maxitem);
  1118. luaL_addsize(&b, nb);
  1119. }
  1120. }
  1121. luaL_pushresult(&b);
  1122. return 1;
  1123. }
  1124. /* }====================================================== */
  1125. /*
  1126. ** {======================================================
  1127. ** PACK/UNPACK
  1128. ** =======================================================
  1129. */
  1130. /* value used for padding */
  1131. #if !defined(LUAL_PACKPADBYTE)
  1132. #define LUAL_PACKPADBYTE 0x00
  1133. #endif
  1134. /* maximum size for the binary representation of an integer */
  1135. #define MAXINTSIZE 16
  1136. /* number of bits in a character */
  1137. #define NB CHAR_BIT
  1138. /* mask for one character (NB 1's) */
  1139. #define MC ((1 << NB) - 1)
  1140. /* size of a lua_Integer */
  1141. #define SZINT ((int)sizeof(lua_Integer))
  1142. /* dummy union to get native endianness */
  1143. static const union {
  1144. int dummy;
  1145. char little; /* true iff machine is little endian */
  1146. } nativeendian = {1};
  1147. /* dummy structure to get native alignment requirements */
  1148. struct cD {
  1149. char c;
  1150. union { double d; void *p; lua_Integer i; lua_Number n; } u;
  1151. };
  1152. #define MAXALIGN (offsetof(struct cD, u))
  1153. /*
  1154. ** Union for serializing floats
  1155. */
  1156. typedef union Ftypes {
  1157. float f;
  1158. double d;
  1159. lua_Number n;
  1160. char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
  1161. } Ftypes;
  1162. /*
  1163. ** information to pack/unpack stuff
  1164. */
  1165. typedef struct Header {
  1166. lua_State *L;
  1167. int islittle;
  1168. int maxalign;
  1169. } Header;
  1170. /*
  1171. ** options for pack/unpack
  1172. */
  1173. typedef enum KOption {
  1174. Kint, /* signed integers */
  1175. Kuint, /* unsigned integers */
  1176. Kfloat, /* floating-point numbers */
  1177. Kchar, /* fixed-length strings */
  1178. Kstring, /* strings with prefixed length */
  1179. Kzstr, /* zero-terminated strings */
  1180. Kpadding, /* padding */
  1181. Kpaddalign, /* padding for alignment */
  1182. Knop /* no-op (configuration or spaces) */
  1183. } KOption;
  1184. /*
  1185. ** Read an integer numeral from string 'fmt' or return 'df' if
  1186. ** there is no numeral
  1187. */
  1188. static int digit (int c) { return '0' <= c && c <= '9'; }
  1189. static int getnum (const char **fmt, int df) {
  1190. if (!digit(**fmt)) /* no number? */
  1191. return df; /* return default value */
  1192. else {
  1193. int a = 0;
  1194. do {
  1195. a = a*10 + (*((*fmt)++) - '0');
  1196. } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
  1197. return a;
  1198. }
  1199. }
  1200. /*
  1201. ** Read an integer numeral and raises an error if it is larger
  1202. ** than the maximum size for integers.
  1203. */
  1204. static int getnumlimit (Header *h, const char **fmt, int df) {
  1205. int sz = getnum(fmt, df);
  1206. if (sz > MAXINTSIZE || sz <= 0)
  1207. return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
  1208. sz, MAXINTSIZE);
  1209. return sz;
  1210. }
  1211. /*
  1212. ** Initialize Header
  1213. */
  1214. static void initheader (lua_State *L, Header *h) {
  1215. h->L = L;
  1216. h->islittle = nativeendian.little;
  1217. h->maxalign = 1;
  1218. }
  1219. /*
  1220. ** Read and classify next option. 'size' is filled with option's size.
  1221. */
  1222. static KOption getoption (Header *h, const char **fmt, int *size) {
  1223. int opt = *((*fmt)++);
  1224. *size = 0; /* default */
  1225. switch (opt) {
  1226. case 'b': *size = sizeof(char); return Kint;
  1227. case 'B': *size = sizeof(char); return Kuint;
  1228. case 'h': *size = sizeof(short); return Kint;
  1229. case 'H': *size = sizeof(short); return Kuint;
  1230. case 'l': *size = sizeof(long); return Kint;
  1231. case 'L': *size = sizeof(long); return Kuint;
  1232. case 'j': *size = sizeof(lua_Integer); return Kint;
  1233. case 'J': *size = sizeof(lua_Integer); return Kuint;
  1234. case 'T': *size = sizeof(size_t); return Kuint;
  1235. case 'f': *size = sizeof(float); return Kfloat;
  1236. case 'd': *size = sizeof(double); return Kfloat;
  1237. case 'n': *size = sizeof(lua_Number); return Kfloat;
  1238. case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
  1239. case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
  1240. case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
  1241. case 'c':
  1242. *size = getnum(fmt, -1);
  1243. if (*size == -1)
  1244. luaL_error(h->L, "missing size for format option 'c'");
  1245. return Kchar;
  1246. case 'z': return Kzstr;
  1247. case 'x': *size = 1; return Kpadding;
  1248. case 'X': return Kpaddalign;
  1249. case ' ': break;
  1250. case '<': h->islittle = 1; break;
  1251. case '>': h->islittle = 0; break;
  1252. case '=': h->islittle = nativeendian.little; break;
  1253. case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break;
  1254. default: luaL_error(h->L, "invalid format option '%c'", opt);
  1255. }
  1256. return Knop;
  1257. }
  1258. /*
  1259. ** Read, classify, and fill other details about the next option.
  1260. ** 'psize' is filled with option's size, 'notoalign' with its
  1261. ** alignment requirements.
  1262. ** Local variable 'size' gets the size to be aligned. (Kpadal option
  1263. ** always gets its full alignment, other options are limited by
  1264. ** the maximum alignment ('maxalign'). Kchar option needs no alignment
  1265. ** despite its size.
  1266. */
  1267. static KOption getdetails (Header *h, size_t totalsize,
  1268. const char **fmt, int *psize, int *ntoalign) {
  1269. KOption opt = getoption(h, fmt, psize);
  1270. int align = *psize; /* usually, alignment follows size */
  1271. if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
  1272. if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
  1273. luaL_argerror(h->L, 1, "invalid next option for option 'X'");
  1274. }
  1275. if (align <= 1 || opt == Kchar) /* need no alignment? */
  1276. *ntoalign = 0;
  1277. else {
  1278. if (align > h->maxalign) /* enforce maximum alignment */
  1279. align = h->maxalign;
  1280. if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
  1281. luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
  1282. *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
  1283. }
  1284. return opt;
  1285. }
  1286. /*
  1287. ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
  1288. ** The final 'if' handles the case when 'size' is larger than
  1289. ** the size of a Lua integer, correcting the extra sign-extension
  1290. ** bytes if necessary (by default they would be zeros).
  1291. */
  1292. static void packint (luaL_Buffer *b, lua_Unsigned n,
  1293. int islittle, int size, int neg) {
  1294. char *buff = luaL_prepbuffsize(b, size);
  1295. int i;
  1296. buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
  1297. for (i = 1; i < size; i++) {
  1298. n >>= NB;
  1299. buff[islittle ? i : size - 1 - i] = (char)(n & MC);
  1300. }
  1301. if (neg && size > SZINT) { /* negative number need sign extension? */
  1302. for (i = SZINT; i < size; i++) /* correct extra bytes */
  1303. buff[islittle ? i : size - 1 - i] = (char)MC;
  1304. }
  1305. luaL_addsize(b, size); /* add result to buffer */
  1306. }
  1307. /*
  1308. ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
  1309. ** given 'islittle' is different from native endianness.
  1310. */
  1311. static void copywithendian (volatile char *dest, volatile const char *src,
  1312. int size, int islittle) {
  1313. if (islittle == nativeendian.little) {
  1314. while (size-- != 0)
  1315. *(dest++) = *(src++);
  1316. }
  1317. else {
  1318. dest += size - 1;
  1319. while (size-- != 0)
  1320. *(dest--) = *(src++);
  1321. }
  1322. }
  1323. static int str_pack (lua_State *L) {
  1324. luaL_Buffer b;
  1325. Header h;
  1326. const char *fmt = luaL_checkstring(L, 1); /* format string */
  1327. int arg = 1; /* current argument to pack */
  1328. size_t totalsize = 0; /* accumulate total size of result */
  1329. initheader(L, &h);
  1330. lua_pushnil(L); /* mark to separate arguments from string buffer */
  1331. luaL_buffinit(L, &b);
  1332. while (*fmt != '\0') {
  1333. int size, ntoalign;
  1334. KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
  1335. totalsize += ntoalign + size;
  1336. while (ntoalign-- > 0)
  1337. luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */
  1338. arg++;
  1339. switch (opt) {
  1340. case Kint: { /* signed integers */
  1341. lua_Integer n = luaL_checkinteger(L, arg);
  1342. if (size < SZINT) { /* need overflow check? */
  1343. lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
  1344. luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
  1345. }
  1346. packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
  1347. break;
  1348. }
  1349. case Kuint: { /* unsigned integers */
  1350. lua_Integer n = luaL_checkinteger(L, arg);
  1351. if (size < SZINT) /* need overflow check? */
  1352. luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
  1353. arg, "unsigned overflow");
  1354. packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
  1355. break;
  1356. }
  1357. case Kfloat: { /* floating-point options */
  1358. volatile Ftypes u;
  1359. char *buff = luaL_prepbuffsize(&b, size);
  1360. lua_Number n = luaL_checknumber(L, arg); /* get argument */
  1361. if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
  1362. else if (size == sizeof(u.d)) u.d = (double)n;
  1363. else u.n = n;
  1364. /* move 'u' to final result, correcting endianness if needed */
  1365. copywithendian(buff, u.buff, size, h.islittle);
  1366. luaL_addsize(&b, size);
  1367. break;
  1368. }
  1369. case Kchar: { /* fixed-size string */
  1370. size_t len;
  1371. const char *s = luaL_checklstring(L, arg, &len);
  1372. luaL_argcheck(L, len <= (size_t)size, arg,
  1373. "string longer than given size");
  1374. luaL_addlstring(&b, s, len); /* add string */
  1375. while (len++ < (size_t)size) /* pad extra space */
  1376. luaL_addchar(&b, LUAL_PACKPADBYTE);
  1377. break;
  1378. }
  1379. case Kstring: { /* strings with length count */
  1380. size_t len;
  1381. const char *s = luaL_checklstring(L, arg, &len);
  1382. luaL_argcheck(L, size >= (int)sizeof(size_t) ||
  1383. len < ((size_t)1 << (size * NB)),
  1384. arg, "string length does not fit in given size");
  1385. packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */
  1386. luaL_addlstring(&b, s, len);
  1387. totalsize += len;
  1388. break;
  1389. }
  1390. case Kzstr: { /* zero-terminated string */
  1391. size_t len;
  1392. const char *s = luaL_checklstring(L, arg, &len);
  1393. luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
  1394. luaL_addlstring(&b, s, len);
  1395. luaL_addchar(&b, '\0'); /* add zero at the end */
  1396. totalsize += len + 1;
  1397. break;
  1398. }
  1399. case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */
  1400. case Kpaddalign: case Knop:
  1401. arg--; /* undo increment */
  1402. break;
  1403. }
  1404. }
  1405. luaL_pushresult(&b);
  1406. return 1;
  1407. }
  1408. static int str_packsize (lua_State *L) {
  1409. Header h;
  1410. const char *fmt = luaL_checkstring(L, 1); /* format string */
  1411. size_t totalsize = 0; /* accumulate total size of result */
  1412. initheader(L, &h);
  1413. while (*fmt != '\0') {
  1414. int size, ntoalign;
  1415. KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
  1416. luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
  1417. "variable-length format");
  1418. size += ntoalign; /* total space used by option */
  1419. luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
  1420. "format result too large");
  1421. totalsize += size;
  1422. }
  1423. lua_pushinteger(L, (lua_Integer)totalsize);
  1424. return 1;
  1425. }
  1426. /*
  1427. ** Unpack an integer with 'size' bytes and 'islittle' endianness.
  1428. ** If size is smaller than the size of a Lua integer and integer
  1429. ** is signed, must do sign extension (propagating the sign to the
  1430. ** higher bits); if size is larger than the size of a Lua integer,
  1431. ** it must check the unread bytes to see whether they do not cause an
  1432. ** overflow.
  1433. */
  1434. static lua_Integer unpackint (lua_State *L, const char *str,
  1435. int islittle, int size, int issigned) {
  1436. lua_Unsigned res = 0;
  1437. int i;
  1438. int limit = (size <= SZINT) ? size : SZINT;
  1439. for (i = limit - 1; i >= 0; i--) {
  1440. res <<= NB;
  1441. res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
  1442. }
  1443. if (size < SZINT) { /* real size smaller than lua_Integer? */
  1444. if (issigned) { /* needs sign extension? */
  1445. lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
  1446. res = ((res ^ mask) - mask); /* do sign extension */
  1447. }
  1448. }
  1449. else if (size > SZINT) { /* must check unread bytes */
  1450. int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
  1451. for (i = limit; i < size; i++) {
  1452. if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
  1453. luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
  1454. }
  1455. }
  1456. return (lua_Integer)res;
  1457. }
  1458. static int str_unpack (lua_State *L) {
  1459. Header h;
  1460. const char *fmt = luaL_checkstring(L, 1);
  1461. size_t ld;
  1462. const char *data = luaL_checklstring(L, 2, &ld);
  1463. size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1;
  1464. int n = 0; /* number of results */
  1465. luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
  1466. initheader(L, &h);
  1467. while (*fmt != '\0') {
  1468. int size, ntoalign;
  1469. KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
  1470. luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
  1471. "data string too short");
  1472. pos += ntoalign; /* skip alignment */
  1473. /* stack space for item + next position */
  1474. luaL_checkstack(L, 2, "too many results");
  1475. n++;
  1476. switch (opt) {
  1477. case Kint:
  1478. case Kuint: {
  1479. lua_Integer res = unpackint(L, data + pos, h.islittle, size,
  1480. (opt == Kint));
  1481. lua_pushinteger(L, res);
  1482. break;
  1483. }
  1484. case Kfloat: {
  1485. volatile Ftypes u;
  1486. lua_Number num;
  1487. copywithendian(u.buff, data + pos, size, h.islittle);
  1488. if (size == sizeof(u.f)) num = (lua_Number)u.f;
  1489. else if (size == sizeof(u.d)) num = (lua_Number)u.d;
  1490. else num = u.n;
  1491. lua_pushnumber(L, num);
  1492. break;
  1493. }
  1494. case Kchar: {
  1495. lua_pushlstring(L, data + pos, size);
  1496. break;
  1497. }
  1498. case Kstring: {
  1499. size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
  1500. luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
  1501. lua_pushlstring(L, data + pos + size, len);
  1502. pos += len; /* skip string */
  1503. break;
  1504. }
  1505. case Kzstr: {
  1506. size_t len = (int)strlen(data + pos);
  1507. luaL_argcheck(L, pos + len < ld, 2,
  1508. "unfinished string for format 'z'");
  1509. lua_pushlstring(L, data + pos, len);
  1510. pos += len + 1; /* skip string plus final '\0' */
  1511. break;
  1512. }
  1513. case Kpaddalign: case Kpadding: case Knop:
  1514. n--; /* undo increment */
  1515. break;
  1516. }
  1517. pos += size;
  1518. }
  1519. lua_pushinteger(L, pos + 1); /* next position */
  1520. return n + 1;
  1521. }
  1522. /* }====================================================== */
  1523. static const luaL_Reg strlib[] = {
  1524. {"byte", str_byte},
  1525. {"char", str_char},
  1526. {"dump", str_dump},
  1527. {"find", str_find},
  1528. {"format", str_format},
  1529. {"gmatch", gmatch},
  1530. {"gsub", str_gsub},
  1531. {"len", str_len},
  1532. {"lower", str_lower},
  1533. {"match", str_match},
  1534. {"rep", str_rep},
  1535. {"reverse", str_reverse},
  1536. {"sub", str_sub},
  1537. {"upper", str_upper},
  1538. {"pack", str_pack},
  1539. {"packsize", str_packsize},
  1540. {"unpack", str_unpack},
  1541. {NULL, NULL}
  1542. };
  1543. static void createmetatable (lua_State *L) {
  1544. /* table to be metatable for strings */
  1545. luaL_newlibtable(L, stringmetamethods);
  1546. luaL_setfuncs(L, stringmetamethods, 0);
  1547. lua_pushliteral(L, ""); /* dummy string */
  1548. lua_pushvalue(L, -2); /* copy table */
  1549. lua_setmetatable(L, -2); /* set table as metatable for strings */
  1550. lua_pop(L, 1); /* pop dummy string */
  1551. lua_pushvalue(L, -2); /* get string library */
  1552. lua_setfield(L, -2, "__index"); /* metatable.__index = string */
  1553. lua_pop(L, 1); /* pop metatable */
  1554. }
  1555. /*
  1556. ** Open string library
  1557. */
  1558. LUAMOD_API int luaopen_string (lua_State *L) {
  1559. luaL_newlib(L, strlib);
  1560. createmetatable(L);
  1561. return 1;
  1562. }