lstrlib.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. ** $Id: lstrlib.c,v 1.132.1.5 2010/05/14 15:34:19 roberto Exp $
  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. #define LUAC_CROSS_FILE
  9. #include "lua.h"
  10. #include C_HEADER_STDIO
  11. #include C_HEADER_STRING
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. #include "lrotable.h"
  15. /* macro to `unsign' a character */
  16. #define uchar(c) ((unsigned char)(c))
  17. static int str_len (lua_State *L) {
  18. size_t l;
  19. luaL_checklstring(L, 1, &l);
  20. lua_pushinteger(L, l);
  21. return 1;
  22. }
  23. static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
  24. /* relative string position: negative means back from end */
  25. if (pos < 0) pos += (ptrdiff_t)len + 1;
  26. return (pos >= 0) ? pos : 0;
  27. }
  28. static int str_sub (lua_State *L) {
  29. size_t l;
  30. const char *s = luaL_checklstring(L, 1, &l);
  31. ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
  32. ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
  33. if (start < 1) start = 1;
  34. if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
  35. if (start <= end)
  36. lua_pushlstring(L, s+start-1, end-start+1);
  37. else lua_pushliteral(L, "");
  38. return 1;
  39. }
  40. static int str_reverse (lua_State *L) {
  41. size_t l;
  42. luaL_Buffer b;
  43. const char *s = luaL_checklstring(L, 1, &l);
  44. luaL_buffinit(L, &b);
  45. while (l--) luaL_addchar(&b, s[l]);
  46. luaL_pushresult(&b);
  47. return 1;
  48. }
  49. static int str_lower (lua_State *L) {
  50. size_t l;
  51. size_t i;
  52. luaL_Buffer b;
  53. const char *s = luaL_checklstring(L, 1, &l);
  54. luaL_buffinit(L, &b);
  55. for (i=0; i<l; i++)
  56. luaL_addchar(&b, tolower(uchar(s[i])));
  57. luaL_pushresult(&b);
  58. return 1;
  59. }
  60. static int str_upper (lua_State *L) {
  61. size_t l;
  62. size_t i;
  63. luaL_Buffer b;
  64. const char *s = luaL_checklstring(L, 1, &l);
  65. luaL_buffinit(L, &b);
  66. for (i=0; i<l; i++)
  67. luaL_addchar(&b, toupper(uchar(s[i])));
  68. luaL_pushresult(&b);
  69. return 1;
  70. }
  71. static int str_rep (lua_State *L) {
  72. size_t l;
  73. luaL_Buffer b;
  74. const char *s = luaL_checklstring(L, 1, &l);
  75. int n = luaL_checkint(L, 2);
  76. luaL_buffinit(L, &b);
  77. while (n-- > 0)
  78. luaL_addlstring(&b, s, l);
  79. luaL_pushresult(&b);
  80. return 1;
  81. }
  82. static int str_byte (lua_State *L) {
  83. size_t l;
  84. const char *s = luaL_checklstring(L, 1, &l);
  85. ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
  86. ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
  87. int n, i;
  88. if (posi <= 0) posi = 1;
  89. if ((size_t)pose > l) pose = l;
  90. if (posi > pose) return 0; /* empty interval; return no values */
  91. n = (int)(pose - posi + 1);
  92. if (posi + n <= pose) /* overflow? */
  93. luaL_error(L, "string slice too long");
  94. luaL_checkstack(L, n, "string slice too long");
  95. for (i=0; i<n; i++)
  96. lua_pushinteger(L, uchar(s[posi+i-1]));
  97. return n;
  98. }
  99. static int str_char (lua_State *L) {
  100. int n = lua_gettop(L); /* number of arguments */
  101. int i;
  102. luaL_Buffer b;
  103. luaL_buffinit(L, &b);
  104. for (i=1; i<=n; i++) {
  105. int c = luaL_checkint(L, i);
  106. luaL_argcheck(L, uchar(c) == c, i, "invalid value");
  107. luaL_addchar(&b, uchar(c));
  108. }
  109. luaL_pushresult(&b);
  110. return 1;
  111. }
  112. static int writer (lua_State *L, const void* b, size_t size, void* B) {
  113. (void)L;
  114. luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
  115. return 0;
  116. }
  117. static int str_dump (lua_State *L) {
  118. luaL_Buffer b;
  119. luaL_checktype(L, 1, LUA_TFUNCTION);
  120. lua_settop(L, 1);
  121. luaL_buffinit(L,&b);
  122. if (lua_dump(L, writer, &b) != 0)
  123. luaL_error(L, "unable to dump given function");
  124. luaL_pushresult(&b);
  125. return 1;
  126. }
  127. /*
  128. ** {======================================================
  129. ** PATTERN MATCHING
  130. ** =======================================================
  131. */
  132. #define CAP_UNFINISHED (-1)
  133. #define CAP_POSITION (-2)
  134. typedef struct MatchState {
  135. const char *src_init; /* init of source string */
  136. const char *src_end; /* end (`\0') of source string */
  137. lua_State *L;
  138. int level; /* total number of captures (finished or unfinished) */
  139. struct {
  140. const char *init;
  141. ptrdiff_t len;
  142. } capture[LUA_MAXCAPTURES];
  143. } MatchState;
  144. #define L_ESC '%'
  145. #define SPECIALS "^$*+?.([%-"
  146. static int check_capture (MatchState *ms, int l) {
  147. l -= '1';
  148. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  149. return luaL_error(ms->L, "invalid capture index");
  150. return l;
  151. }
  152. static int capture_to_close (MatchState *ms) {
  153. int level = ms->level;
  154. for (level--; level>=0; level--)
  155. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  156. return luaL_error(ms->L, "invalid pattern capture");
  157. }
  158. static const char *classend (MatchState *ms, const char *p) {
  159. switch (*p++) {
  160. case L_ESC: {
  161. if (*p == '\0')
  162. luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
  163. return p+1;
  164. }
  165. case '[': {
  166. if (*p == '^') p++;
  167. do { /* look for a `]' */
  168. if (*p == '\0')
  169. luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
  170. if (*(p++) == L_ESC && *p != '\0')
  171. p++; /* skip escapes (e.g. `%]') */
  172. } while (*p != ']');
  173. return p+1;
  174. }
  175. default: {
  176. return p;
  177. }
  178. }
  179. }
  180. static int match_class (int c, int cl) {
  181. int res;
  182. switch (tolower(cl)) {
  183. case 'a' : res = isalpha(c); break;
  184. case 'c' : res = iscntrl(c); break;
  185. case 'd' : res = isdigit(c); break;
  186. case 'l' : res = islower(c); break;
  187. case 'p' : res = ispunct(c); break;
  188. case 's' : res = isspace(c); break;
  189. case 'u' : res = isupper(c); break;
  190. case 'w' : res = isalnum(c); break;
  191. case 'x' : res = isxdigit(c); break;
  192. case 'z' : res = (c == 0); break;
  193. default: return (cl == c);
  194. }
  195. return (islower(cl) ? res : !res);
  196. }
  197. static int matchbracketclass (int c, const char *p, const char *ec) {
  198. int sig = 1;
  199. if (*(p+1) == '^') {
  200. sig = 0;
  201. p++; /* skip the `^' */
  202. }
  203. while (++p < ec) {
  204. if (*p == L_ESC) {
  205. p++;
  206. if (match_class(c, uchar(*p)))
  207. return sig;
  208. }
  209. else if ((*(p+1) == '-') && (p+2 < ec)) {
  210. p+=2;
  211. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  212. return sig;
  213. }
  214. else if (uchar(*p) == c) return sig;
  215. }
  216. return !sig;
  217. }
  218. static int singlematch (int c, const char *p, const char *ep) {
  219. switch (*p) {
  220. case '.': return 1; /* matches any char */
  221. case L_ESC: return match_class(c, uchar(*(p+1)));
  222. case '[': return matchbracketclass(c, p, ep-1);
  223. default: return (uchar(*p) == c);
  224. }
  225. }
  226. static const char *match (MatchState *ms, const char *s, const char *p);
  227. static const char *matchbalance (MatchState *ms, const char *s,
  228. const char *p) {
  229. if (*p == 0 || *(p+1) == 0)
  230. luaL_error(ms->L, "unbalanced pattern");
  231. if (*s != *p) return NULL;
  232. else {
  233. int b = *p;
  234. int e = *(p+1);
  235. int cont = 1;
  236. while (++s < ms->src_end) {
  237. if (*s == e) {
  238. if (--cont == 0) return s+1;
  239. }
  240. else if (*s == b) cont++;
  241. }
  242. }
  243. return NULL; /* string ends out of balance */
  244. }
  245. static const char *max_expand (MatchState *ms, const char *s,
  246. const char *p, const char *ep) {
  247. ptrdiff_t i = 0; /* counts maximum expand for item */
  248. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
  249. i++;
  250. /* keeps trying to match with the maximum repetitions */
  251. while (i>=0) {
  252. const char *res = match(ms, (s+i), ep+1);
  253. if (res) return res;
  254. i--; /* else didn't match; reduce 1 repetition to try again */
  255. }
  256. return NULL;
  257. }
  258. static const char *min_expand (MatchState *ms, const char *s,
  259. const char *p, const char *ep) {
  260. for (;;) {
  261. const char *res = match(ms, s, ep+1);
  262. if (res != NULL)
  263. return res;
  264. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
  265. s++; /* try with one more repetition */
  266. else return NULL;
  267. }
  268. }
  269. static const char *start_capture (MatchState *ms, const char *s,
  270. const char *p, int what) {
  271. const char *res;
  272. int level = ms->level;
  273. if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
  274. ms->capture[level].init = s;
  275. ms->capture[level].len = what;
  276. ms->level = level+1;
  277. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  278. ms->level--; /* undo capture */
  279. return res;
  280. }
  281. static const char *end_capture (MatchState *ms, const char *s,
  282. const char *p) {
  283. int l = capture_to_close(ms);
  284. const char *res;
  285. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  286. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  287. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  288. return res;
  289. }
  290. static const char *match_capture (MatchState *ms, const char *s, int l) {
  291. size_t len;
  292. l = check_capture(ms, l);
  293. len = ms->capture[l].len;
  294. if ((size_t)(ms->src_end-s) >= len &&
  295. c_memcmp(ms->capture[l].init, s, len) == 0)
  296. return s+len;
  297. else return NULL;
  298. }
  299. static const char *match (MatchState *ms, const char *s, const char *p) {
  300. init: /* using goto's to optimize tail recursion */
  301. switch (*p) {
  302. case '(': { /* start capture */
  303. if (*(p+1) == ')') /* position capture? */
  304. return start_capture(ms, s, p+2, CAP_POSITION);
  305. else
  306. return start_capture(ms, s, p+1, CAP_UNFINISHED);
  307. }
  308. case ')': { /* end capture */
  309. return end_capture(ms, s, p+1);
  310. }
  311. case L_ESC: {
  312. switch (*(p+1)) {
  313. case 'b': { /* balanced string? */
  314. s = matchbalance(ms, s, p+2);
  315. if (s == NULL) return NULL;
  316. p+=4; goto init; /* else return match(ms, s, p+4); */
  317. }
  318. case 'f': { /* frontier? */
  319. const char *ep; char previous;
  320. p += 2;
  321. if (*p != '[')
  322. luaL_error(ms->L, "missing " LUA_QL("[") " after "
  323. LUA_QL("%%f") " in pattern");
  324. ep = classend(ms, p); /* points to what is next */
  325. previous = (s == ms->src_init) ? '\0' : *(s-1);
  326. if (matchbracketclass(uchar(previous), p, ep-1) ||
  327. !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
  328. p=ep; goto init; /* else return match(ms, s, ep); */
  329. }
  330. default: {
  331. if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
  332. s = match_capture(ms, s, uchar(*(p+1)));
  333. if (s == NULL) return NULL;
  334. p+=2; goto init; /* else return match(ms, s, p+2) */
  335. }
  336. goto dflt; /* case default */
  337. }
  338. }
  339. }
  340. case '\0': { /* end of pattern */
  341. return s; /* match succeeded */
  342. }
  343. case '$': {
  344. if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
  345. return (s == ms->src_end) ? s : NULL; /* check end of string */
  346. else goto dflt;
  347. }
  348. default: dflt: { /* it is a pattern item */
  349. const char *ep = classend(ms, p); /* points to what is next */
  350. int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
  351. switch (*ep) {
  352. case '?': { /* optional */
  353. const char *res;
  354. if (m && ((res=match(ms, s+1, ep+1)) != NULL))
  355. return res;
  356. p=ep+1; goto init; /* else return match(ms, s, ep+1); */
  357. }
  358. case '*': { /* 0 or more repetitions */
  359. return max_expand(ms, s, p, ep);
  360. }
  361. case '+': { /* 1 or more repetitions */
  362. return (m ? max_expand(ms, s+1, p, ep) : NULL);
  363. }
  364. case '-': { /* 0 or more repetitions (minimum) */
  365. return min_expand(ms, s, p, ep);
  366. }
  367. default: {
  368. if (!m) return NULL;
  369. s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
  370. }
  371. }
  372. }
  373. }
  374. }
  375. static const char *lmemfind (const char *s1, size_t l1,
  376. const char *s2, size_t l2) {
  377. if (l2 == 0) return s1; /* empty strings are everywhere */
  378. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  379. else {
  380. const char *init; /* to search for a `*s2' inside `s1' */
  381. l2--; /* 1st char will be checked by `memchr' */
  382. l1 = l1-l2; /* `s2' cannot be found after that */
  383. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  384. init++; /* 1st char is already checked */
  385. if (c_memcmp(init, s2+1, l2) == 0)
  386. return init-1;
  387. else { /* correct `l1' and `s1' to try again */
  388. l1 -= init-s1;
  389. s1 = init;
  390. }
  391. }
  392. return NULL; /* not found */
  393. }
  394. }
  395. static void push_onecapture (MatchState *ms, int i, const char *s,
  396. const char *e) {
  397. if (i >= ms->level) {
  398. if (i == 0) /* ms->level == 0, too */
  399. lua_pushlstring(ms->L, s, e - s); /* add whole match */
  400. else
  401. luaL_error(ms->L, "invalid capture index");
  402. }
  403. else {
  404. ptrdiff_t l = ms->capture[i].len;
  405. if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
  406. if (l == CAP_POSITION)
  407. lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
  408. else
  409. lua_pushlstring(ms->L, ms->capture[i].init, l);
  410. }
  411. }
  412. static int push_captures (MatchState *ms, const char *s, const char *e) {
  413. int i;
  414. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  415. luaL_checkstack(ms->L, nlevels, "too many captures");
  416. for (i = 0; i < nlevels; i++)
  417. push_onecapture(ms, i, s, e);
  418. return nlevels; /* number of strings pushed */
  419. }
  420. static int str_find_aux (lua_State *L, int find) {
  421. size_t l1, l2;
  422. const char *s = luaL_checklstring(L, 1, &l1);
  423. const char *p = luaL_checklstring(L, 2, &l2);
  424. ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
  425. if (init < 0) init = 0;
  426. else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
  427. if (find && (lua_toboolean(L, 4) || /* explicit request? */
  428. c_strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
  429. /* do a plain search */
  430. const char *s2 = lmemfind(s+init, l1-init, p, l2);
  431. if (s2) {
  432. lua_pushinteger(L, s2-s+1);
  433. lua_pushinteger(L, s2-s+l2);
  434. return 2;
  435. }
  436. }
  437. else {
  438. MatchState ms;
  439. int anchor = (*p == '^') ? (p++, 1) : 0;
  440. const char *s1=s+init;
  441. ms.L = L;
  442. ms.src_init = s;
  443. ms.src_end = s+l1;
  444. do {
  445. const char *res;
  446. ms.level = 0;
  447. if ((res=match(&ms, s1, p)) != NULL) {
  448. if (find) {
  449. lua_pushinteger(L, s1-s+1); /* start */
  450. lua_pushinteger(L, res-s); /* end */
  451. return push_captures(&ms, NULL, 0) + 2;
  452. }
  453. else
  454. return push_captures(&ms, s1, res);
  455. }
  456. } while (s1++ < ms.src_end && !anchor);
  457. }
  458. lua_pushnil(L); /* not found */
  459. return 1;
  460. }
  461. static int str_find (lua_State *L) {
  462. return str_find_aux(L, 1);
  463. }
  464. static int str_match (lua_State *L) {
  465. return str_find_aux(L, 0);
  466. }
  467. static int gmatch_aux (lua_State *L) {
  468. MatchState ms;
  469. size_t ls;
  470. const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
  471. const char *p = lua_tostring(L, lua_upvalueindex(2));
  472. const char *src;
  473. ms.L = L;
  474. ms.src_init = s;
  475. ms.src_end = s+ls;
  476. for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
  477. src <= ms.src_end;
  478. src++) {
  479. const char *e;
  480. ms.level = 0;
  481. if ((e = match(&ms, src, p)) != NULL) {
  482. lua_Integer newstart = e-s;
  483. if (e == src) newstart++; /* empty match? go at least one position */
  484. lua_pushinteger(L, newstart);
  485. lua_replace(L, lua_upvalueindex(3));
  486. return push_captures(&ms, src, e);
  487. }
  488. }
  489. return 0; /* not found */
  490. }
  491. static int gmatch (lua_State *L) {
  492. luaL_checkstring(L, 1);
  493. luaL_checkstring(L, 2);
  494. lua_settop(L, 2);
  495. lua_pushinteger(L, 0);
  496. lua_pushcclosure(L, gmatch_aux, 3);
  497. return 1;
  498. }
  499. #if LUA_OPTIMIZE_MEMORY == 0 || !defined(LUA_COMPAT_GFIND)
  500. static int gfind_nodef (lua_State *L) {
  501. return luaL_error(L, LUA_QL("string.gfind") " was renamed to "
  502. LUA_QL("string.gmatch"));
  503. }
  504. #endif
  505. static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
  506. const char *e) {
  507. size_t l, i;
  508. const char *news = lua_tolstring(ms->L, 3, &l);
  509. for (i = 0; i < l; i++) {
  510. if (news[i] != L_ESC)
  511. luaL_addchar(b, news[i]);
  512. else {
  513. i++; /* skip ESC */
  514. if (!isdigit(uchar(news[i])))
  515. luaL_addchar(b, news[i]);
  516. else if (news[i] == '0')
  517. luaL_addlstring(b, s, e - s);
  518. else {
  519. push_onecapture(ms, news[i] - '1', s, e);
  520. luaL_addvalue(b); /* add capture to accumulated result */
  521. }
  522. }
  523. }
  524. }
  525. static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
  526. const char *e) {
  527. lua_State *L = ms->L;
  528. switch (lua_type(L, 3)) {
  529. case LUA_TNUMBER:
  530. case LUA_TSTRING: {
  531. add_s(ms, b, s, e);
  532. return;
  533. }
  534. case LUA_TFUNCTION:
  535. case LUA_TLIGHTFUNCTION: {
  536. int n;
  537. lua_pushvalue(L, 3);
  538. n = push_captures(ms, s, e);
  539. lua_call(L, n, 1);
  540. break;
  541. }
  542. case LUA_TTABLE: {
  543. push_onecapture(ms, 0, s, e);
  544. lua_gettable(L, 3);
  545. break;
  546. }
  547. }
  548. if (!lua_toboolean(L, -1)) { /* nil or false? */
  549. lua_pop(L, 1);
  550. lua_pushlstring(L, s, e - s); /* keep original text */
  551. }
  552. else if (!lua_isstring(L, -1))
  553. luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
  554. luaL_addvalue(b); /* add result to accumulator */
  555. }
  556. static int str_gsub (lua_State *L) {
  557. size_t srcl;
  558. const char *src = luaL_checklstring(L, 1, &srcl);
  559. const char *p = luaL_checkstring(L, 2);
  560. int tr = lua_type(L, 3);
  561. int max_s = luaL_optint(L, 4, srcl+1);
  562. int anchor = (*p == '^') ? (p++, 1) : 0;
  563. int n = 0;
  564. MatchState ms;
  565. luaL_Buffer b;
  566. luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
  567. tr == LUA_TFUNCTION || tr == LUA_TTABLE ||
  568. tr == LUA_TLIGHTFUNCTION, 3,
  569. "string/function/table/lightfunction expected");
  570. luaL_buffinit(L, &b);
  571. ms.L = L;
  572. ms.src_init = src;
  573. ms.src_end = src+srcl;
  574. while (n < max_s) {
  575. const char *e;
  576. ms.level = 0;
  577. e = match(&ms, src, p);
  578. if (e) {
  579. n++;
  580. add_value(&ms, &b, src, e);
  581. }
  582. if (e && e>src) /* non empty match? */
  583. src = e; /* skip it */
  584. else if (src < ms.src_end)
  585. luaL_addchar(&b, *src++);
  586. else break;
  587. if (anchor) break;
  588. }
  589. luaL_addlstring(&b, src, ms.src_end-src);
  590. luaL_pushresult(&b);
  591. lua_pushinteger(L, n); /* number of substitutions */
  592. return 2;
  593. }
  594. /* }====================================================== */
  595. /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
  596. /* was 512, modified to 128 for eLua */
  597. #define MAX_ITEM 128
  598. /* valid flags in a format specification */
  599. #define FLAGS "-+ #0"
  600. /*
  601. ** maximum size of each format specification (such as '%-099.99d')
  602. ** (+10 accounts for %99.99x plus margin of error)
  603. */
  604. #define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
  605. static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  606. size_t l;
  607. const char *s = luaL_checklstring(L, arg, &l);
  608. luaL_addchar(b, '"');
  609. while (l--) {
  610. switch (*s) {
  611. case '"': case '\\': case '\n': {
  612. luaL_addchar(b, '\\');
  613. luaL_addchar(b, *s);
  614. break;
  615. }
  616. case '\r': {
  617. luaL_addlstring(b, "\\r", 2);
  618. break;
  619. }
  620. case '\0': {
  621. luaL_addlstring(b, "\\000", 4);
  622. break;
  623. }
  624. default: {
  625. luaL_addchar(b, *s);
  626. break;
  627. }
  628. }
  629. s++;
  630. }
  631. luaL_addchar(b, '"');
  632. }
  633. static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
  634. const char *p = strfrmt;
  635. while (*p != '\0' && c_strchr(FLAGS, *p) != NULL) p++; /* skip flags */
  636. if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
  637. luaL_error(L, "invalid format (repeated flags)");
  638. if (isdigit(uchar(*p))) p++; /* skip width */
  639. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  640. if (*p == '.') {
  641. p++;
  642. if (isdigit(uchar(*p))) p++; /* skip precision */
  643. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  644. }
  645. if (isdigit(uchar(*p)))
  646. luaL_error(L, "invalid format (width or precision too long)");
  647. *(form++) = '%';
  648. c_strncpy(form, strfrmt, p - strfrmt + 1);
  649. form += p - strfrmt + 1;
  650. *form = '\0';
  651. return p;
  652. }
  653. static void addintlen (char *form) {
  654. size_t l = c_strlen(form);
  655. char spec = form[l - 1];
  656. c_strcpy(form + l - 1, LUA_INTFRMLEN);
  657. form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
  658. form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
  659. }
  660. static int str_format (lua_State *L) {
  661. int top = lua_gettop(L);
  662. int arg = 1;
  663. size_t sfl;
  664. const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  665. const char *strfrmt_end = strfrmt+sfl;
  666. luaL_Buffer b;
  667. luaL_buffinit(L, &b);
  668. while (strfrmt < strfrmt_end) {
  669. if (*strfrmt != L_ESC)
  670. luaL_addchar(&b, *strfrmt++);
  671. else if (*++strfrmt == L_ESC)
  672. luaL_addchar(&b, *strfrmt++); /* %% */
  673. else { /* format item */
  674. char form[MAX_FORMAT]; /* to store the format (`%...') */
  675. char buff[MAX_ITEM]; /* to store the formatted item */
  676. if (++arg > top)
  677. luaL_argerror(L, arg, "no value");
  678. strfrmt = scanformat(L, strfrmt, form);
  679. switch (*strfrmt++) {
  680. case 'c': {
  681. c_sprintf(buff, form, (int)luaL_checknumber(L, arg));
  682. break;
  683. }
  684. case 'd': case 'i': {
  685. addintlen(form);
  686. c_sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
  687. break;
  688. }
  689. case 'o': case 'u': case 'x': case 'X': {
  690. addintlen(form);
  691. c_sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
  692. break;
  693. }
  694. #if !defined LUA_NUMBER_INTEGRAL
  695. case 'e': case 'E': case 'f':
  696. case 'g': case 'G': {
  697. c_sprintf(buff, form, (double)luaL_checknumber(L, arg));
  698. break;
  699. }
  700. #endif
  701. case 'q': {
  702. addquoted(L, &b, arg);
  703. continue; /* skip the 'addsize' at the end */
  704. }
  705. case 's': {
  706. size_t l;
  707. const char *s = luaL_checklstring(L, arg, &l);
  708. if (!c_strchr(form, '.') && l >= 100) {
  709. /* no precision and string is too long to be formatted;
  710. keep original string */
  711. lua_pushvalue(L, arg);
  712. luaL_addvalue(&b);
  713. continue; /* skip the `addsize' at the end */
  714. }
  715. else {
  716. c_sprintf(buff, form, s);
  717. break;
  718. }
  719. }
  720. default: { /* also treat cases `pnLlh' */
  721. return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
  722. LUA_QL("format"), *(strfrmt - 1));
  723. }
  724. }
  725. luaL_addlstring(&b, buff, c_strlen(buff));
  726. }
  727. }
  728. luaL_pushresult(&b);
  729. return 1;
  730. }
  731. #define MIN_OPT_LEVEL 1
  732. #include "lrodefs.h"
  733. const LUA_REG_TYPE strlib[] = {
  734. {LSTRKEY("byte"), LFUNCVAL(str_byte)},
  735. {LSTRKEY("char"), LFUNCVAL(str_char)},
  736. {LSTRKEY("dump"), LFUNCVAL(str_dump)},
  737. {LSTRKEY("find"), LFUNCVAL(str_find)},
  738. {LSTRKEY("format"), LFUNCVAL(str_format)},
  739. #if LUA_OPTIMIZE_MEMORY > 0 && defined(LUA_COMPAT_GFIND)
  740. {LSTRKEY("gfind"), LFUNCVAL(gmatch)},
  741. #else
  742. {LSTRKEY("gfind"), LFUNCVAL(gfind_nodef)},
  743. #endif
  744. {LSTRKEY("gmatch"), LFUNCVAL(gmatch)},
  745. {LSTRKEY("gsub"), LFUNCVAL(str_gsub)},
  746. {LSTRKEY("len"), LFUNCVAL(str_len)},
  747. {LSTRKEY("lower"), LFUNCVAL(str_lower)},
  748. {LSTRKEY("match"), LFUNCVAL(str_match)},
  749. {LSTRKEY("rep"), LFUNCVAL(str_rep)},
  750. {LSTRKEY("reverse"), LFUNCVAL(str_reverse)},
  751. {LSTRKEY("sub"), LFUNCVAL(str_sub)},
  752. {LSTRKEY("upper"), LFUNCVAL(str_upper)},
  753. #if LUA_OPTIMIZE_MEMORY > 0
  754. {LSTRKEY("__index"), LROVAL(strlib)},
  755. #endif
  756. {LNILKEY, LNILVAL}
  757. };
  758. #if LUA_OPTIMIZE_MEMORY != 2
  759. static void createmetatable (lua_State *L) {
  760. lua_createtable(L, 0, 1); /* create metatable for strings */
  761. lua_pushliteral(L, ""); /* dummy string */
  762. lua_pushvalue(L, -2);
  763. lua_setmetatable(L, -2); /* set string metatable */
  764. lua_pop(L, 1); /* pop dummy string */
  765. lua_pushvalue(L, -2); /* string library... */
  766. lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
  767. lua_pop(L, 1); /* pop metatable */
  768. }
  769. #endif
  770. /*
  771. ** Open string library
  772. */
  773. LUALIB_API int luaopen_string (lua_State *L) {
  774. #if LUA_OPTIMIZE_MEMORY == 0
  775. luaL_register(L, LUA_STRLIBNAME, strlib);
  776. #if defined(LUA_COMPAT_GFIND)
  777. lua_getfield(L, -1, "gmatch");
  778. lua_setfield(L, -2, "gfind");
  779. #endif
  780. createmetatable(L);
  781. return 1;
  782. #else
  783. lua_pushliteral(L,"");
  784. lua_pushrotable(L, (void*)strlib);
  785. lua_setmetatable(L, -2);
  786. lua_pop(L,1);
  787. return 0;
  788. #endif
  789. }