lstrlib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. ** $Id: lstrlib.c,v 1.88 2002/08/21 19:39:31 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: {
  283. switch (*(p+1)) {
  284. case 'b': { /* balanced string? */
  285. s = matchbalance(ms, s, p+2);
  286. if (s == NULL) return NULL;
  287. p+=4; goto init; /* else return match(ms, s, p+4); */
  288. }
  289. case 'f': { /* frontier? */
  290. const char *ep; char previous;
  291. p += 2;
  292. if (*p != '[')
  293. luaL_error(ms->L, "missing `[' after `%%f' in pattern");
  294. ep = luaI_classend(ms, p); /* points to what is next */
  295. previous = (s == ms->src_init) ? '\0' : *(s-1);
  296. if (matchbracketclass(uchar(previous), p, ep-1) ||
  297. !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
  298. p=ep; goto init; /* else return match(ms, s, ep); */
  299. }
  300. default: {
  301. if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
  302. s = match_capture(ms, s, *(p+1));
  303. if (s == NULL) return NULL;
  304. p+=2; goto init; /* else return match(ms, s, p+2) */
  305. }
  306. goto dflt; /* case default */
  307. }
  308. }
  309. }
  310. case '\0': { /* end of pattern */
  311. return s; /* match succeeded */
  312. }
  313. case '$': {
  314. if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
  315. return (s == ms->src_end) ? s : NULL; /* check end of string */
  316. else goto dflt;
  317. }
  318. default: dflt: { /* it is a pattern item */
  319. const char *ep = luaI_classend(ms, p); /* points to what is next */
  320. int m = s<ms->src_end && luaI_singlematch(uchar(*s), p, ep);
  321. switch (*ep) {
  322. case '?': { /* optional */
  323. const char *res;
  324. if (m && ((res=match(ms, s+1, ep+1)) != NULL))
  325. return res;
  326. p=ep+1; goto init; /* else return match(ms, s, ep+1); */
  327. }
  328. case '*': { /* 0 or more repetitions */
  329. return max_expand(ms, s, p, ep);
  330. }
  331. case '+': { /* 1 or more repetitions */
  332. return (m ? max_expand(ms, s+1, p, ep) : NULL);
  333. }
  334. case '-': { /* 0 or more repetitions (minimum) */
  335. return min_expand(ms, s, p, ep);
  336. }
  337. default: {
  338. if (!m) return NULL;
  339. s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
  340. }
  341. }
  342. }
  343. }
  344. }
  345. static const char *lmemfind (const char *s1, size_t l1,
  346. const char *s2, size_t l2) {
  347. if (l2 == 0) return s1; /* empty strings are everywhere */
  348. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  349. else {
  350. const char *init; /* to search for a `*s2' inside `s1' */
  351. l2--; /* 1st char will be checked by `memchr' */
  352. l1 = l1-l2; /* `s2' cannot be found after that */
  353. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  354. init++; /* 1st char is already checked */
  355. if (memcmp(init, s2+1, l2) == 0)
  356. return init-1;
  357. else { /* correct `l1' and `s1' to try again */
  358. l1 -= init-s1;
  359. s1 = init;
  360. }
  361. }
  362. return NULL; /* not found */
  363. }
  364. }
  365. static void push_onecapture (MatchState *ms, int i) {
  366. int l = ms->capture[i].len;
  367. if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
  368. if (l == CAP_POSITION)
  369. lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1);
  370. else
  371. lua_pushlstring(ms->L, ms->capture[i].init, l);
  372. }
  373. static int push_captures (MatchState *ms, const char *s, const char *e) {
  374. int i;
  375. luaL_check_stack(ms->L, ms->level, "too many captures");
  376. if (ms->level == 0 && s) { /* no explicit captures? */
  377. lua_pushlstring(ms->L, s, e-s); /* return whole match */
  378. return 1;
  379. }
  380. else { /* return all captures */
  381. for (i=0; i<ms->level; i++)
  382. push_onecapture(ms, i);
  383. return ms->level; /* number of strings pushed */
  384. }
  385. }
  386. static int str_find (lua_State *L) {
  387. size_t l1, l2;
  388. const char *s = luaL_check_lstr(L, 1, &l1);
  389. const char *p = luaL_check_lstr(L, 2, &l2);
  390. sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
  391. luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
  392. if (lua_toboolean(L, 4) || /* explicit request? */
  393. strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
  394. /* do a plain search */
  395. const char *s2 = lmemfind(s+init, l1-init, p, l2);
  396. if (s2) {
  397. lua_pushnumber(L, s2-s+1);
  398. lua_pushnumber(L, s2-s+l2);
  399. return 2;
  400. }
  401. }
  402. else {
  403. MatchState ms;
  404. int anchor = (*p == '^') ? (p++, 1) : 0;
  405. const char *s1=s+init;
  406. ms.L = L;
  407. ms.src_init = s;
  408. ms.src_end = s+l1;
  409. do {
  410. const char *res;
  411. ms.level = 0;
  412. if ((res=match(&ms, s1, p)) != NULL) {
  413. lua_pushnumber(L, s1-s+1); /* start */
  414. lua_pushnumber(L, res-s); /* end */
  415. return push_captures(&ms, NULL, 0) + 2;
  416. }
  417. } while (s1++<ms.src_end && !anchor);
  418. }
  419. lua_pushnil(L); /* not found */
  420. return 1;
  421. }
  422. static int gfind_aux (lua_State *L) {
  423. MatchState ms;
  424. const char *s = lua_tostring(L, lua_upvalueindex(1));
  425. size_t ls = lua_strlen(L, lua_upvalueindex(1));
  426. const char *p = lua_tostring(L, lua_upvalueindex(2));
  427. const char *src;
  428. ms.L = L;
  429. ms.src_init = s;
  430. ms.src_end = s+ls;
  431. for (src = s + (size_t)lua_tonumber(L, lua_upvalueindex(3));
  432. src <= ms.src_end;
  433. src++) {
  434. const char *e;
  435. ms.level = 0;
  436. if ((e = match(&ms, src, p)) != NULL) {
  437. int newstart = e-s;
  438. if (e == src) newstart++; /* empty match? go at least one position */
  439. lua_pushnumber(L, newstart);
  440. lua_replace(L, lua_upvalueindex(3));
  441. return push_captures(&ms, src, e);
  442. }
  443. }
  444. return 0; /* not found */
  445. }
  446. static int gfind (lua_State *L) {
  447. luaL_check_string(L, 1);
  448. luaL_check_string(L, 2);
  449. lua_settop(L, 2);
  450. lua_pushnumber(L, 0);
  451. lua_pushcclosure(L, gfind_aux, 3);
  452. return 1;
  453. }
  454. static void add_s (MatchState *ms, luaL_Buffer *b,
  455. const char *s, const char *e) {
  456. lua_State *L = ms->L;
  457. if (lua_isstring(L, 3)) {
  458. const char *news = lua_tostring(L, 3);
  459. size_t l = lua_strlen(L, 3);
  460. size_t i;
  461. for (i=0; i<l; i++) {
  462. if (news[i] != ESC)
  463. luaL_putchar(b, news[i]);
  464. else {
  465. i++; /* skip ESC */
  466. if (!isdigit(uchar(news[i])))
  467. luaL_putchar(b, news[i]);
  468. else {
  469. int level = check_capture(ms, news[i]);
  470. push_onecapture(ms, level);
  471. luaL_addvalue(b); /* add capture to accumulated result */
  472. }
  473. }
  474. }
  475. }
  476. else { /* is a function */
  477. int n;
  478. lua_pushvalue(L, 3);
  479. n = push_captures(ms, s, e);
  480. lua_call(L, n, 1);
  481. if (lua_isstring(L, -1))
  482. luaL_addvalue(b); /* add return to accumulated result */
  483. else
  484. lua_pop(L, 1); /* function result is not a string: pop it */
  485. }
  486. }
  487. static int str_gsub (lua_State *L) {
  488. size_t srcl;
  489. const char *src = luaL_check_lstr(L, 1, &srcl);
  490. const char *p = luaL_check_string(L, 2);
  491. int max_s = luaL_opt_int(L, 4, srcl+1);
  492. int anchor = (*p == '^') ? (p++, 1) : 0;
  493. int n = 0;
  494. MatchState ms;
  495. luaL_Buffer b;
  496. luaL_arg_check(L,
  497. lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
  498. 3, "string or function expected");
  499. luaL_buffinit(L, &b);
  500. ms.L = L;
  501. ms.src_init = src;
  502. ms.src_end = src+srcl;
  503. while (n < max_s) {
  504. const char *e;
  505. ms.level = 0;
  506. e = match(&ms, src, p);
  507. if (e) {
  508. n++;
  509. add_s(&ms, &b, src, e);
  510. }
  511. if (e && e>src) /* non empty match? */
  512. src = e; /* skip it */
  513. else if (src < ms.src_end)
  514. luaL_putchar(&b, *src++);
  515. else break;
  516. if (anchor) break;
  517. }
  518. luaL_addlstring(&b, src, ms.src_end-src);
  519. luaL_pushresult(&b);
  520. lua_pushnumber(L, n); /* number of substitutions */
  521. return 2;
  522. }
  523. /* }====================================================== */
  524. /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
  525. #define MAX_ITEM 512
  526. /* maximum size of each format specification (such as '%-099.99d') */
  527. #define MAX_FORMAT 20
  528. static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  529. size_t l;
  530. const char *s = luaL_check_lstr(L, arg, &l);
  531. luaL_putchar(b, '"');
  532. while (l--) {
  533. switch (*s) {
  534. case '"': case '\\': case '\n': {
  535. luaL_putchar(b, '\\');
  536. luaL_putchar(b, *s);
  537. break;
  538. }
  539. case '\0': {
  540. luaL_addlstring(b, "\\000", 4);
  541. break;
  542. }
  543. default: {
  544. luaL_putchar(b, *s);
  545. break;
  546. }
  547. }
  548. s++;
  549. }
  550. luaL_putchar(b, '"');
  551. }
  552. static const char *scanformat (lua_State *L, const char *strfrmt,
  553. char *form, int *hasprecision) {
  554. const char *p = strfrmt;
  555. while (strchr("-+ #0", *p)) p++; /* skip flags */
  556. if (isdigit(uchar(*p))) p++; /* skip width */
  557. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  558. if (*p == '.') {
  559. p++;
  560. *hasprecision = 1;
  561. if (isdigit(uchar(*p))) p++; /* skip precision */
  562. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  563. }
  564. if (isdigit(uchar(*p)))
  565. luaL_error(L, "invalid format (width or precision too long)");
  566. if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
  567. luaL_error(L, "invalid format (too long)");
  568. form[0] = '%';
  569. strncpy(form+1, strfrmt, p-strfrmt+1);
  570. form[p-strfrmt+2] = 0;
  571. return p;
  572. }
  573. static int str_format (lua_State *L) {
  574. int arg = 1;
  575. size_t sfl;
  576. const char *strfrmt = luaL_check_lstr(L, arg, &sfl);
  577. const char *strfrmt_end = strfrmt+sfl;
  578. luaL_Buffer b;
  579. luaL_buffinit(L, &b);
  580. while (strfrmt < strfrmt_end) {
  581. if (*strfrmt != '%')
  582. luaL_putchar(&b, *strfrmt++);
  583. else if (*++strfrmt == '%')
  584. luaL_putchar(&b, *strfrmt++); /* %% */
  585. else { /* format item */
  586. char form[MAX_FORMAT]; /* to store the format (`%...') */
  587. char buff[MAX_ITEM]; /* to store the formatted item */
  588. int hasprecision = 0;
  589. if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
  590. return luaL_error(L, "obsolete `format' option (d$)");
  591. arg++;
  592. strfrmt = scanformat(L, strfrmt, form, &hasprecision);
  593. switch (*strfrmt++) {
  594. case 'c': case 'd': case 'i': {
  595. sprintf(buff, form, luaL_check_int(L, arg));
  596. break;
  597. }
  598. case 'o': case 'u': case 'x': case 'X': {
  599. sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg)));
  600. break;
  601. }
  602. case 'e': case 'E': case 'f':
  603. case 'g': case 'G': {
  604. sprintf(buff, form, luaL_check_number(L, arg));
  605. break;
  606. }
  607. case 'q': {
  608. luaI_addquoted(L, &b, arg);
  609. continue; /* skip the `addsize' at the end */
  610. }
  611. case 's': {
  612. size_t l;
  613. const char *s = luaL_check_lstr(L, arg, &l);
  614. if (!hasprecision && l >= 100) {
  615. /* no precision and string is too long to be formatted;
  616. keep original string */
  617. lua_pushvalue(L, arg);
  618. luaL_addvalue(&b);
  619. continue; /* skip the `addsize' at the end */
  620. }
  621. else {
  622. sprintf(buff, form, s);
  623. break;
  624. }
  625. }
  626. default: { /* also treat cases `pnLlh' */
  627. return luaL_error(L, "invalid option in `format'");
  628. }
  629. }
  630. luaL_addlstring(&b, buff, strlen(buff));
  631. }
  632. }
  633. luaL_pushresult(&b);
  634. return 1;
  635. }
  636. static const luaL_reg strlib[] = {
  637. {"len", str_len},
  638. {"sub", str_sub},
  639. {"lower", str_lower},
  640. {"upper", str_upper},
  641. {"char", str_char},
  642. {"rep", str_rep},
  643. {"byte", str_byte},
  644. {"format", str_format},
  645. {"find", str_find},
  646. {"gfind", gfind},
  647. {"gsub", str_gsub},
  648. {NULL, NULL}
  649. };
  650. /*
  651. ** Open string library
  652. */
  653. LUALIB_API int lua_strlibopen (lua_State *L) {
  654. luaL_opennamedlib(L, LUA_STRLIBNAME, strlib, 0);
  655. return 0;
  656. }