PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/IronHellsOgre/src/lua_and_tolua++/lib/lstrlib.c

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