lstrlib.c 19 KB

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