lstrlib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. ** $Id: lstrlib.c,v 1.82 2002/05/06 19:05:10 roberto Exp roberto $
  3. ** Standard library for string operations and pattern-matching
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stddef.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. /* macro to `unsign' a character */
  15. #ifndef uchar
  16. #define uchar(c) ((unsigned char)(c))
  17. #endif
  18. typedef long sint32; /* a signed version for size_t */
  19. static int str_len (lua_State *L) {
  20. size_t l;
  21. luaL_check_lstr(L, 1, &l);
  22. lua_pushnumber(L, l);
  23. return 1;
  24. }
  25. static sint32 posrelat (sint32 pos, size_t len) {
  26. /* relative string position: negative means back from end */
  27. return (pos>=0) ? pos : (sint32)len+pos+1;
  28. }
  29. static int str_sub (lua_State *L) {
  30. size_t l;
  31. const char *s = luaL_check_lstr(L, 1, &l);
  32. sint32 start = posrelat(luaL_check_long(L, 2), l);
  33. sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
  34. if (start < 1) start = 1;
  35. if (end > (sint32)l) end = l;
  36. if (start <= end)
  37. lua_pushlstring(L, s+start-1, end-start+1);
  38. else lua_pushliteral(L, "");
  39. return 1;
  40. }
  41. static int str_lower (lua_State *L) {
  42. size_t l;
  43. size_t i;
  44. luaL_Buffer b;
  45. const char *s = luaL_check_lstr(L, 1, &l);
  46. luaL_buffinit(L, &b);
  47. for (i=0; i<l; i++)
  48. luaL_putchar(&b, tolower(uchar(s[i])));
  49. luaL_pushresult(&b);
  50. return 1;
  51. }
  52. static int str_upper (lua_State *L) {
  53. size_t l;
  54. size_t i;
  55. luaL_Buffer b;
  56. const char *s = luaL_check_lstr(L, 1, &l);
  57. luaL_buffinit(L, &b);
  58. for (i=0; i<l; i++)
  59. luaL_putchar(&b, toupper(uchar(s[i])));
  60. luaL_pushresult(&b);
  61. return 1;
  62. }
  63. static int str_rep (lua_State *L) {
  64. size_t l;
  65. luaL_Buffer b;
  66. const char *s = luaL_check_lstr(L, 1, &l);
  67. int n = luaL_check_int(L, 2);
  68. luaL_buffinit(L, &b);
  69. while (n-- > 0)
  70. luaL_addlstring(&b, s, l);
  71. luaL_pushresult(&b);
  72. return 1;
  73. }
  74. static int str_concat (lua_State *L) {
  75. luaL_Buffer b;
  76. size_t lsep;
  77. const char *sep = luaL_opt_lstr(L, 2, "", &lsep);
  78. int n, i;
  79. luaL_check_type(L, 1, LUA_TTABLE);
  80. luaL_buffinit(L, &b);
  81. n = lua_getn(L, 1);
  82. for (i=1; i<=n; i++) {
  83. lua_rawgeti(L, 1, i);
  84. luaL_addvalue(&b);
  85. if (i != n)
  86. luaL_addlstring(&b, sep, lsep);
  87. }
  88. luaL_pushresult(&b);
  89. return 1;
  90. }
  91. static int str_byte (lua_State *L) {
  92. size_t l;
  93. const char *s = luaL_check_lstr(L, 1, &l);
  94. sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
  95. luaL_arg_check(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range");
  96. lua_pushnumber(L, uchar(s[pos-1]));
  97. return 1;
  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_check_int(L, i);
  106. luaL_arg_check(L, uchar(c) == c, i, "invalid value");
  107. luaL_putchar(&b, uchar(c));
  108. }
  109. luaL_pushresult(&b);
  110. return 1;
  111. }
  112. /*
  113. ** {======================================================
  114. ** PATTERN MATCHING
  115. ** =======================================================
  116. */
  117. #ifndef MAX_CAPTURES
  118. #define MAX_CAPTURES 32 /* arbitrary limit */
  119. #endif
  120. #define CAP_UNFINISHED (-1)
  121. #define CAP_POSITION (-2)
  122. typedef struct MatchState {
  123. const char *src_init; /* init of source string */
  124. const char *src_end; /* end (`\0') of source string */
  125. lua_State *L;
  126. int level; /* total number of captures (finished or unfinished) */
  127. struct {
  128. const char *init;
  129. sint32 len;
  130. } capture[MAX_CAPTURES];
  131. } MatchState;
  132. #define ESC '%'
  133. #define SPECIALS "^$*+?.([%-"
  134. static int check_capture (MatchState *ms, int l) {
  135. l -= '1';
  136. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  137. return luaL_verror(ms->L, "invalid capture index");
  138. return l;
  139. }
  140. static int capture_to_close (MatchState *ms) {
  141. int level = ms->level;
  142. for (level--; level>=0; level--)
  143. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  144. return luaL_verror(ms->L, "invalid pattern capture");
  145. }
  146. static const char *luaI_classend (MatchState *ms, const char *p) {
  147. switch (*p++) {
  148. case ESC:
  149. if (*p == '\0')
  150. luaL_verror(ms->L, "malformed pattern (ends with `%')");
  151. return p+1;
  152. case '[':
  153. if (*p == '^') p++;
  154. do { /* look for a `]' */
  155. if (*p == '\0')
  156. luaL_verror(ms->L, "malformed pattern (missing `]')");
  157. if (*(p++) == ESC && *p != '\0')
  158. p++; /* skip escapes (e.g. `%]') */
  159. } while (*p != ']');
  160. return p+1;
  161. default:
  162. return p;
  163. }
  164. }
  165. static int match_class (int c, int cl) {
  166. int res;
  167. switch (tolower(cl)) {
  168. case 'a' : res = isalpha(c); break;
  169. case 'c' : res = iscntrl(c); break;
  170. case 'd' : res = isdigit(c); break;
  171. case 'l' : res = islower(c); break;
  172. case 'p' : res = ispunct(c); break;
  173. case 's' : res = isspace(c); break;
  174. case 'u' : res = isupper(c); break;
  175. case 'w' : res = isalnum(c); break;
  176. case 'x' : res = isxdigit(c); break;
  177. case 'z' : res = (c == 0); break;
  178. default: return (cl == c);
  179. }
  180. return (islower(cl) ? res : !res);
  181. }
  182. static int matchbracketclass (int c, const char *p, const char *ec) {
  183. int sig = 1;
  184. if (*(p+1) == '^') {
  185. sig = 0;
  186. p++; /* skip the `^' */
  187. }
  188. while (++p < ec) {
  189. if (*p == ESC) {
  190. p++;
  191. if (match_class(c, *p))
  192. return sig;
  193. }
  194. else if ((*(p+1) == '-') && (p+2 < ec)) {
  195. p+=2;
  196. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  197. return sig;
  198. }
  199. else if (uchar(*p) == c) return sig;
  200. }
  201. return !sig;
  202. }
  203. static int luaI_singlematch (int c, const char *p, const char *ep) {
  204. switch (*p) {
  205. case '.': /* matches any char */
  206. return 1;
  207. case ESC:
  208. return match_class(c, *(p+1));
  209. case '[':
  210. return matchbracketclass(c, p, ep-1);
  211. default:
  212. return (uchar(*p) == c);
  213. }
  214. }
  215. static const char *match (MatchState *ms, const char *s, const char *p);
  216. static const char *matchbalance (MatchState *ms, const char *s,
  217. const char *p) {
  218. if (*p == 0 || *(p+1) == 0)
  219. luaL_verror(ms->L, "unbalanced pattern");
  220. if (*s != *p) return NULL;
  221. else {
  222. int b = *p;
  223. int e = *(p+1);
  224. int cont = 1;
  225. while (++s < ms->src_end) {
  226. if (*s == e) {
  227. if (--cont == 0) return s+1;
  228. }
  229. else if (*s == b) cont++;
  230. }
  231. }
  232. return NULL; /* string ends out of balance */
  233. }
  234. static const char *max_expand (MatchState *ms, const char *s,
  235. const char *p, const char *ep) {
  236. sint32 i = 0; /* counts maximum expand for item */
  237. while ((s+i)<ms->src_end && luaI_singlematch(uchar(*(s+i)), p, ep))
  238. i++;
  239. /* keeps trying to match with the maximum repetitions */
  240. while (i>=0) {
  241. const char *res = match(ms, (s+i), ep+1);
  242. if (res) return res;
  243. i--; /* else didn't match; reduce 1 repetition to try again */
  244. }
  245. return NULL;
  246. }
  247. static const char *min_expand (MatchState *ms, const char *s,
  248. const char *p, const char *ep) {
  249. for (;;) {
  250. const char *res = match(ms, s, ep+1);
  251. if (res != NULL)
  252. return res;
  253. else if (s<ms->src_end && luaI_singlematch(uchar(*s), p, ep))
  254. s++; /* try with one more repetition */
  255. else return NULL;
  256. }
  257. }
  258. static const char *start_capture (MatchState *ms, const char *s,
  259. const char *p, int what) {
  260. const char *res;
  261. int level = ms->level;
  262. if (level >= MAX_CAPTURES) luaL_verror(ms->L, "too many captures");
  263. ms->capture[level].init = s;
  264. ms->capture[level].len = what;
  265. ms->level = level+1;
  266. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  267. ms->level--; /* undo capture */
  268. return res;
  269. }
  270. static const char *end_capture (MatchState *ms, const char *s,
  271. const char *p) {
  272. int l = capture_to_close(ms);
  273. const char *res;
  274. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  275. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  276. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  277. return res;
  278. }
  279. static const char *match_capture (MatchState *ms, const char *s, int l) {
  280. size_t len;
  281. l = check_capture(ms, l);
  282. len = ms->capture[l].len;
  283. if ((size_t)(ms->src_end-s) >= len &&
  284. memcmp(ms->capture[l].init, s, len) == 0)
  285. return s+len;
  286. else return NULL;
  287. }
  288. static const char *match (MatchState *ms, const char *s, const char *p) {
  289. init: /* using goto's to optimize tail recursion */
  290. switch (*p) {
  291. case '(': /* start capture */
  292. if (*(p+1) == ')') /* position capture? */
  293. return start_capture(ms, s, p+2, CAP_POSITION);
  294. else
  295. return start_capture(ms, s, p+1, CAP_UNFINISHED);
  296. case ')': /* end capture */
  297. return end_capture(ms, s, p+1);
  298. case ESC: /* may be %[0-9] or %b */
  299. if (isdigit(uchar(*(p+1)))) { /* capture? */
  300. s = match_capture(ms, s, *(p+1));
  301. if (s == NULL) return NULL;
  302. p+=2; goto init; /* else return match(ms, s, p+2) */
  303. }
  304. else if (*(p+1) == 'b') { /* balanced string? */
  305. s = matchbalance(ms, s, p+2);
  306. if (s == NULL) return NULL;
  307. p+=4; goto init; /* else return match(ms, s, p+4); */
  308. }
  309. else goto dflt; /* case default */
  310. case '\0': /* end of pattern */
  311. return s; /* match succeeded */
  312. case '$':
  313. if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
  314. return (s == ms->src_end) ? s : NULL; /* check end of string */
  315. else goto dflt;
  316. default: dflt: { /* it is a pattern item */
  317. const char *ep = luaI_classend(ms, p); /* points to what is next */
  318. int m = s<ms->src_end && luaI_singlematch(uchar(*s), p, ep);
  319. switch (*ep) {
  320. case '?': { /* optional */
  321. const char *res;
  322. if (m && ((res=match(ms, s+1, ep+1)) != NULL))
  323. return res;
  324. p=ep+1; goto init; /* else return match(ms, s, ep+1); */
  325. }
  326. case '*': /* 0 or more repetitions */
  327. return max_expand(ms, s, p, ep);
  328. case '+': /* 1 or more repetitions */
  329. return (m ? max_expand(ms, s+1, p, ep) : NULL);
  330. case '-': /* 0 or more repetitions (minimum) */
  331. return min_expand(ms, s, p, ep);
  332. default:
  333. if (!m) return NULL;
  334. s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
  335. }
  336. }
  337. }
  338. }
  339. static const char *lmemfind (const char *s1, size_t l1,
  340. const char *s2, size_t l2) {
  341. if (l2 == 0) return s1; /* empty strings are everywhere */
  342. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  343. else {
  344. const char *init; /* to search for a `*s2' inside `s1' */
  345. l2--; /* 1st char will be checked by `memchr' */
  346. l1 = l1-l2; /* `s2' cannot be found after that */
  347. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  348. init++; /* 1st char is already checked */
  349. if (memcmp(init, s2+1, l2) == 0)
  350. return init-1;
  351. else { /* correct `l1' and `s1' to try again */
  352. l1 -= init-s1;
  353. s1 = init;
  354. }
  355. }
  356. return NULL; /* not found */
  357. }
  358. }
  359. static void push_onecapture (MatchState *ms, int i) {
  360. int l = ms->capture[i].len;
  361. if (l == CAP_UNFINISHED) luaL_verror(ms->L, "unfinished capture");
  362. if (l == CAP_POSITION)
  363. lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1);
  364. else
  365. lua_pushlstring(ms->L, ms->capture[i].init, l);
  366. }
  367. static int push_captures (MatchState *ms, const char *s, const char *e) {
  368. int i;
  369. luaL_check_stack(ms->L, ms->level, "too many captures");
  370. if (ms->level == 0 && s) { /* no explicit captures? */
  371. lua_pushlstring(ms->L, s, e-s); /* return whole match */
  372. return 1;
  373. }
  374. else { /* return all captures */
  375. for (i=0; i<ms->level; i++)
  376. push_onecapture(ms, i);
  377. return ms->level; /* number of strings pushed */
  378. }
  379. }
  380. static int str_find (lua_State *L) {
  381. size_t l1, l2;
  382. const char *s = luaL_check_lstr(L, 1, &l1);
  383. const char *p = luaL_check_lstr(L, 2, &l2);
  384. sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
  385. luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
  386. if (lua_toboolean(L, 4) || /* explicit request? */
  387. strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
  388. /* do a plain search */
  389. const char *s2 = lmemfind(s+init, l1-init, p, l2);
  390. if (s2) {
  391. lua_pushnumber(L, s2-s+1);
  392. lua_pushnumber(L, s2-s+l2);
  393. return 2;
  394. }
  395. }
  396. else {
  397. MatchState ms;
  398. int anchor = (*p == '^') ? (p++, 1) : 0;
  399. const char *s1=s+init;
  400. ms.L = L;
  401. ms.src_init = s;
  402. ms.src_end = s+l1;
  403. do {
  404. const char *res;
  405. ms.level = 0;
  406. if ((res=match(&ms, s1, p)) != NULL) {
  407. lua_pushnumber(L, s1-s+1); /* start */
  408. lua_pushnumber(L, res-s); /* end */
  409. return push_captures(&ms, NULL, 0) + 2;
  410. }
  411. } while (s1++<ms.src_end && !anchor);
  412. }
  413. lua_pushnil(L); /* not found */
  414. return 1;
  415. }
  416. static int gfind_aux (lua_State *L) {
  417. MatchState ms;
  418. const char *s = lua_tostring(L, lua_upvalueindex(1));
  419. size_t ls = lua_strlen(L, lua_upvalueindex(1));
  420. const char *p = lua_tostring(L, lua_upvalueindex(2));
  421. const char *src;
  422. ms.L = L;
  423. ms.src_init = s;
  424. ms.src_end = s+ls;
  425. for (src = s + (size_t)lua_tonumber(L, lua_upvalueindex(3));
  426. src <= ms.src_end;
  427. src++) {
  428. const char *e;
  429. ms.level = 0;
  430. if ((e = match(&ms, src, p)) != NULL) {
  431. int newstart = e-s;
  432. if (e == src) newstart++; /* empty match? go at least one position */
  433. lua_pushnumber(L, newstart);
  434. lua_replace(L, lua_upvalueindex(3));
  435. return push_captures(&ms, src, e);
  436. }
  437. }
  438. return 0; /* not found */
  439. }
  440. static int gfind (lua_State *L) {
  441. luaL_check_string(L, 1);
  442. luaL_check_string(L, 2);
  443. lua_settop(L, 2);
  444. lua_pushnumber(L, 0);
  445. lua_pushcclosure(L, gfind_aux, 3);
  446. return 1;
  447. }
  448. static void add_s (MatchState *ms, luaL_Buffer *b,
  449. const char *s, const char *e) {
  450. lua_State *L = ms->L;
  451. if (lua_isstring(L, 3)) {
  452. const char *news = lua_tostring(L, 3);
  453. size_t l = lua_strlen(L, 3);
  454. size_t i;
  455. for (i=0; i<l; i++) {
  456. if (news[i] != ESC)
  457. luaL_putchar(b, news[i]);
  458. else {
  459. i++; /* skip ESC */
  460. if (!isdigit(uchar(news[i])))
  461. luaL_putchar(b, news[i]);
  462. else {
  463. int level = check_capture(ms, news[i]);
  464. push_onecapture(ms, level);
  465. luaL_addvalue(b); /* add capture to accumulated result */
  466. }
  467. }
  468. }
  469. }
  470. else { /* is a function */
  471. int n;
  472. lua_pushvalue(L, 3);
  473. n = push_captures(ms, s, e);
  474. lua_rawcall(L, n, 1);
  475. if (lua_isstring(L, -1))
  476. luaL_addvalue(b); /* add return to accumulated result */
  477. else
  478. lua_pop(L, 1); /* function result is not a string: pop it */
  479. }
  480. }
  481. static int str_gsub (lua_State *L) {
  482. size_t srcl;
  483. const char *src = luaL_check_lstr(L, 1, &srcl);
  484. const char *p = luaL_check_string(L, 2);
  485. int max_s = luaL_opt_int(L, 4, srcl+1);
  486. int anchor = (*p == '^') ? (p++, 1) : 0;
  487. int n = 0;
  488. MatchState ms;
  489. luaL_Buffer b;
  490. luaL_arg_check(L,
  491. lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
  492. 3, "string or function expected");
  493. luaL_buffinit(L, &b);
  494. ms.L = L;
  495. ms.src_init = src;
  496. ms.src_end = src+srcl;
  497. while (n < max_s) {
  498. const char *e;
  499. ms.level = 0;
  500. e = match(&ms, src, p);
  501. if (e) {
  502. n++;
  503. add_s(&ms, &b, src, e);
  504. }
  505. if (e && e>src) /* non empty match? */
  506. src = e; /* skip it */
  507. else if (src < ms.src_end)
  508. luaL_putchar(&b, *src++);
  509. else break;
  510. if (anchor) break;
  511. }
  512. luaL_addlstring(&b, src, ms.src_end-src);
  513. luaL_pushresult(&b);
  514. lua_pushnumber(L, n); /* number of substitutions */
  515. return 2;
  516. }
  517. /* }====================================================== */
  518. /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
  519. #define MAX_ITEM 512
  520. /* maximum size of each format specification (such as '%-099.99d') */
  521. #define MAX_FORMAT 20
  522. static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  523. size_t l;
  524. const char *s = luaL_check_lstr(L, arg, &l);
  525. luaL_putchar(b, '"');
  526. while (l--) {
  527. switch (*s) {
  528. case '"': case '\\': case '\n':
  529. luaL_putchar(b, '\\');
  530. luaL_putchar(b, *s);
  531. break;
  532. case '\0': luaL_addlstring(b, "\\000", 4); break;
  533. default: luaL_putchar(b, *s);
  534. }
  535. s++;
  536. }
  537. luaL_putchar(b, '"');
  538. }
  539. static const char *scanformat (lua_State *L, const char *strfrmt,
  540. char *form, int *hasprecision) {
  541. const char *p = strfrmt;
  542. while (strchr("-+ #0", *p)) p++; /* skip flags */
  543. if (isdigit(uchar(*p))) p++; /* skip width */
  544. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  545. if (*p == '.') {
  546. p++;
  547. *hasprecision = 1;
  548. if (isdigit(uchar(*p))) p++; /* skip precision */
  549. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  550. }
  551. if (isdigit(uchar(*p)))
  552. luaL_verror(L, "invalid format (width or precision too long)");
  553. if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
  554. luaL_verror(L, "invalid format (too long)");
  555. form[0] = '%';
  556. strncpy(form+1, strfrmt, p-strfrmt+1);
  557. form[p-strfrmt+2] = 0;
  558. return p;
  559. }
  560. static int str_format (lua_State *L) {
  561. int arg = 1;
  562. size_t sfl;
  563. const char *strfrmt = luaL_check_lstr(L, arg, &sfl);
  564. const char *strfrmt_end = strfrmt+sfl;
  565. luaL_Buffer b;
  566. luaL_buffinit(L, &b);
  567. while (strfrmt < strfrmt_end) {
  568. if (*strfrmt != '%')
  569. luaL_putchar(&b, *strfrmt++);
  570. else if (*++strfrmt == '%')
  571. luaL_putchar(&b, *strfrmt++); /* %% */
  572. else { /* format item */
  573. char form[MAX_FORMAT]; /* to store the format (`%...') */
  574. char buff[MAX_ITEM]; /* to store the formatted item */
  575. int hasprecision = 0;
  576. if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
  577. return luaL_verror(L, "obsolete `format' option (d$)");
  578. arg++;
  579. strfrmt = scanformat(L, strfrmt, form, &hasprecision);
  580. switch (*strfrmt++) {
  581. case 'c': case 'd': case 'i':
  582. sprintf(buff, form, luaL_check_int(L, arg));
  583. break;
  584. case 'o': case 'u': case 'x': case 'X':
  585. sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg)));
  586. break;
  587. case 'e': case 'E': case 'f':
  588. case 'g': case 'G':
  589. sprintf(buff, form, luaL_check_number(L, arg));
  590. break;
  591. case 'q':
  592. luaI_addquoted(L, &b, arg);
  593. continue; /* skip the `addsize' at the end */
  594. case 's': {
  595. size_t l;
  596. const char *s = luaL_check_lstr(L, arg, &l);
  597. if (!hasprecision && l >= 100) {
  598. /* no precision and string is too long to be formatted;
  599. keep original string */
  600. lua_pushvalue(L, arg);
  601. luaL_addvalue(&b);
  602. continue; /* skip the `addsize' at the end */
  603. }
  604. else {
  605. sprintf(buff, form, s);
  606. break;
  607. }
  608. }
  609. default: /* also treat cases `pnLlh' */
  610. return luaL_verror(L, "invalid option in `format'");
  611. }
  612. luaL_addlstring(&b, buff, strlen(buff));
  613. }
  614. }
  615. luaL_pushresult(&b);
  616. return 1;
  617. }
  618. static const luaL_reg strlib[] = {
  619. {"len", str_len},
  620. {"sub", str_sub},
  621. {"lower", str_lower},
  622. {"upper", str_upper},
  623. {"char", str_char},
  624. {"rep", str_rep},
  625. {"byte", str_byte},
  626. {"concat", str_concat},
  627. {"format", str_format},
  628. {"find", str_find},
  629. {"gfind", gfind},
  630. {"gsub", str_gsub},
  631. {NULL, NULL}
  632. };
  633. /*
  634. ** Open string library
  635. */
  636. LUALIB_API int lua_strlibopen (lua_State *L) {
  637. luaL_opennamedlib(L, LUA_STRLIBNAME, strlib, 0);
  638. return 0;
  639. }