lstrlib.c 19 KB

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