lstrlib.c 18 KB

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