lstrlib.c 18 KB

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