lstrlib.c 19 KB

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