lstrlib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. ** $Id: lstrlib.c,v 1.34 1999/08/16 20:52:00 roberto Exp roberto $
  3. ** Standard library for strings and pattern-matching
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lauxlib.h"
  11. #include "lua.h"
  12. #include "lualib.h"
  13. static void addnchar (const char *s, int n) {
  14. char *b = luaL_openspace(n);
  15. memcpy(b, s, n);
  16. luaL_addsize(n);
  17. }
  18. static void str_len (void) {
  19. long l;
  20. luaL_check_lstr(1, &l);
  21. lua_pushnumber(l);
  22. }
  23. static void closeandpush (void) {
  24. lua_pushlstring(luaL_buffer(), luaL_getsize());
  25. }
  26. static long posrelat (long pos, long len) {
  27. /* relative string position: negative means back from end */
  28. return (pos>=0) ? pos : len+pos+1;
  29. }
  30. static void str_sub (void) {
  31. long l;
  32. const char *s = luaL_check_lstr(1, &l);
  33. long start = posrelat(luaL_check_long(2), l);
  34. long end = posrelat(luaL_opt_long(3, -1), l);
  35. if (start < 1) start = 1;
  36. if (end > l) end = l;
  37. if (start <= end)
  38. lua_pushlstring(s+start-1, end-start+1);
  39. else lua_pushstring("");
  40. }
  41. static void str_lower (void) {
  42. long l;
  43. int i;
  44. const char *s = luaL_check_lstr(1, &l);
  45. luaL_resetbuffer();
  46. for (i=0; i<l; i++)
  47. luaL_addchar(tolower((unsigned char)(s[i])));
  48. closeandpush();
  49. }
  50. static void str_upper (void) {
  51. long l;
  52. int i;
  53. const char *s = luaL_check_lstr(1, &l);
  54. luaL_resetbuffer();
  55. for (i=0; i<l; i++)
  56. luaL_addchar(toupper((unsigned char)(s[i])));
  57. closeandpush();
  58. }
  59. static void str_rep (void) {
  60. long l;
  61. const char *s = luaL_check_lstr(1, &l);
  62. int n = luaL_check_int(2);
  63. luaL_resetbuffer();
  64. while (n-- > 0)
  65. addnchar(s, l);
  66. closeandpush();
  67. }
  68. static void str_byte (void) {
  69. long l;
  70. const char *s = luaL_check_lstr(1, &l);
  71. long pos = posrelat(luaL_opt_long(2, 1), l);
  72. luaL_arg_check(0<pos && pos<=l, 2, "out of range");
  73. lua_pushnumber((unsigned char)s[pos-1]);
  74. }
  75. static void str_char (void) {
  76. int i = 0;
  77. luaL_resetbuffer();
  78. while (lua_getparam(++i) != LUA_NOOBJECT) {
  79. int c = luaL_check_int(i);
  80. luaL_arg_check((unsigned char)c == c, i, "invalid value");
  81. luaL_addchar((unsigned char)c);
  82. }
  83. closeandpush();
  84. }
  85. /*
  86. ** {======================================================
  87. ** PATTERN MATCHING
  88. ** =======================================================
  89. */
  90. #ifndef MAX_CAPT
  91. #define MAX_CAPT 32 /* arbitrary limit */
  92. #endif
  93. struct Capture {
  94. const char *src_end; /* end ('\0') of source string */
  95. int level; /* total number of captures (finished or unfinished) */
  96. struct {
  97. const char *init;
  98. int len; /* -1 signals unfinished capture */
  99. } capture[MAX_CAPT];
  100. };
  101. #define ESC '%'
  102. #define SPECIALS "^$*+?.([%-"
  103. static void push_captures (struct Capture *cap) {
  104. int i;
  105. for (i=0; i<cap->level; i++) {
  106. int l = cap->capture[i].len;
  107. if (l == -1) lua_error("unfinished capture");
  108. lua_pushlstring(cap->capture[i].init, l);
  109. }
  110. }
  111. static int check_cap (int l, struct Capture *cap) {
  112. l -= '1';
  113. if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
  114. lua_error("invalid capture index");
  115. return l;
  116. }
  117. static int capture_to_close (struct Capture *cap) {
  118. int level = cap->level;
  119. for (level--; level>=0; level--)
  120. if (cap->capture[level].len == -1) return level;
  121. lua_error("invalid pattern capture");
  122. return 0; /* to avoid warnings */
  123. }
  124. const char *luaI_classend (const char *p) {
  125. switch (*p++) {
  126. case ESC:
  127. if (*p == '\0') lua_error("incorrect pattern (ends with `%')");
  128. return p+1;
  129. case '[':
  130. if (*p == '^') p++;
  131. do { /* look for a ']' */
  132. if (*p == '\0') lua_error("incorrect pattern (missing `]')");
  133. if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */
  134. } while (*p != ']');
  135. return p+1;
  136. default:
  137. return p;
  138. }
  139. }
  140. static int matchclass (int c, int cl) {
  141. int res;
  142. switch (tolower(cl)) {
  143. case 'a' : res = isalpha(c); break;
  144. case 'c' : res = iscntrl(c); break;
  145. case 'd' : res = isdigit(c); break;
  146. case 'l' : res = islower(c); break;
  147. case 'p' : res = ispunct(c); break;
  148. case 's' : res = isspace(c); break;
  149. case 'u' : res = isupper(c); break;
  150. case 'w' : res = isalnum(c); break;
  151. case 'x' : res = isxdigit(c); break;
  152. case 'z' : res = (c == '\0'); break;
  153. default: return (cl == c);
  154. }
  155. return (islower(cl) ? res : !res);
  156. }
  157. static int matchbracketclass (int c, const char *p, const char *endclass) {
  158. int sig = 1;
  159. if (*(p+1) == '^') {
  160. sig = 0;
  161. p++; /* skip the '^' */
  162. }
  163. while (++p < endclass) {
  164. if (*p == ESC) {
  165. p++;
  166. if (matchclass(c, (unsigned char)*p))
  167. return sig;
  168. }
  169. else if ((*(p+1) == '-') && (p+2 < endclass)) {
  170. p+=2;
  171. if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p)
  172. return sig;
  173. }
  174. else if ((unsigned char)*p == c) return sig;
  175. }
  176. return !sig;
  177. }
  178. int luaI_singlematch (int c, const char *p, const char *ep) {
  179. switch (*p) {
  180. case '.': /* matches any char */
  181. return 1;
  182. case ESC:
  183. return matchclass(c, (unsigned char)*(p+1));
  184. case '[':
  185. return matchbracketclass(c, p, ep-1);
  186. default:
  187. return ((unsigned char)*p == c);
  188. }
  189. }
  190. static const char *match (const char *s, const char *p, struct Capture *cap);
  191. static const char *matchbalance (const char *s, const char *p,
  192. struct Capture *cap) {
  193. if (*p == 0 || *(p+1) == 0)
  194. lua_error("unbalanced pattern");
  195. if (*s != *p) return NULL;
  196. else {
  197. int b = *p;
  198. int e = *(p+1);
  199. int cont = 1;
  200. while (++s < cap->src_end) {
  201. if (*s == e) {
  202. if (--cont == 0) return s+1;
  203. }
  204. else if (*s == b) cont++;
  205. }
  206. }
  207. return NULL; /* string ends out of balance */
  208. }
  209. static const char *max_expand (const char *s, const char *p, const char *ep,
  210. struct Capture *cap) {
  211. int i = 0; /* counts maximum expand for item */
  212. while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
  213. i++;
  214. /* keeps trying to match mith the maximum repetitions */
  215. while (i>=0) {
  216. const char *res = match((s+i), ep+1, cap);
  217. if (res) return res;
  218. i--; /* else didn't match; reduce 1 repetition to try again */
  219. }
  220. return NULL;
  221. }
  222. static const char *min_expand (const char *s, const char *p, const char *ep,
  223. struct Capture *cap) {
  224. for (;;) {
  225. const char *res = match(s, ep+1, cap);
  226. if (res != NULL)
  227. return res;
  228. else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep))
  229. s++; /* try with one more repetition */
  230. else return NULL;
  231. }
  232. }
  233. static const char *start_capt (const char *s, const char *p,
  234. struct Capture *cap) {
  235. const char *res;
  236. int level = cap->level;
  237. if (level >= MAX_CAPT) lua_error("too many captures");
  238. cap->capture[level].init = s;
  239. cap->capture[level].len = -1;
  240. cap->level = level+1;
  241. if ((res=match(s, p+1, cap)) == NULL) /* match failed? */
  242. cap->level--; /* undo capture */
  243. return res;
  244. }
  245. static const char *end_capt (const char *s, const char *p,
  246. struct Capture *cap) {
  247. int l = capture_to_close(cap);
  248. const char *res;
  249. cap->capture[l].len = s - cap->capture[l].init; /* close capture */
  250. if ((res = match(s, p+1, cap)) == NULL) /* match failed? */
  251. cap->capture[l].len = -1; /* undo capture */
  252. return res;
  253. }
  254. static const char *match_capture (const char *s, int level,
  255. struct Capture *cap) {
  256. int l = check_cap(level, cap);
  257. int len = cap->capture[l].len;
  258. if (cap->src_end-s >= len &&
  259. memcmp(cap->capture[l].init, s, len) == 0)
  260. return s+len;
  261. else return NULL;
  262. }
  263. static const char *match (const char *s, const char *p, struct Capture *cap) {
  264. init: /* using goto's to optimize tail recursion */
  265. switch (*p) {
  266. case '(': /* start capture */
  267. return start_capt(s, p, cap);
  268. case ')': /* end capture */
  269. return end_capt(s, p, cap);
  270. case ESC: /* may be %[0-9] or %b */
  271. if (isdigit((unsigned char)(*(p+1)))) { /* capture? */
  272. s = match_capture(s, *(p+1), cap);
  273. if (s == NULL) return NULL;
  274. p+=2; goto init; /* else return match(s, p+2, cap) */
  275. }
  276. else if (*(p+1) == 'b') { /* balanced string? */
  277. s = matchbalance(s, p+2, cap);
  278. if (s == NULL) return NULL;
  279. p+=4; goto init; /* else return match(s, p+4, cap); */
  280. }
  281. else goto dflt; /* case default */
  282. case '\0': /* end of pattern */
  283. return s; /* match succeeded */
  284. case '$':
  285. if (*(p+1) == '\0') /* is the '$' the last char in pattern? */
  286. return (s == cap->src_end) ? s : NULL; /* check end of string */
  287. else goto dflt;
  288. default: dflt: { /* it is a pattern item */
  289. const char *ep = luaI_classend(p); /* points to what is next */
  290. int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep);
  291. switch (*ep) {
  292. case '?': { /* optional */
  293. const char *res;
  294. if (m && ((res=match(s+1, ep+1, cap)) != NULL))
  295. return res;
  296. p=ep+1; goto init; /* else return match(s, ep+1, cap); */
  297. }
  298. case '*': /* 0 or more repetitions */
  299. return max_expand(s, p, ep, cap);
  300. case '+': /* 1 or more repetitions */
  301. return (m ? max_expand(s+1, p, ep, cap) : NULL);
  302. case '-': /* 0 or more repetitions (minimum) */
  303. return min_expand(s, p, ep, cap);
  304. default:
  305. if (!m) return NULL;
  306. s++; p=ep; goto init; /* else return match(s+1, ep, cap); */
  307. }
  308. }
  309. }
  310. }
  311. static void str_find (void) {
  312. long l;
  313. const char *s = luaL_check_lstr(1, &l);
  314. const char *p = luaL_check_string(2);
  315. long init = posrelat(luaL_opt_long(3, 1), l) - 1;
  316. struct Capture cap;
  317. luaL_arg_check(0 <= init && init <= l, 3, "out of range");
  318. if (lua_getparam(4) != LUA_NOOBJECT ||
  319. strpbrk(p, SPECIALS) == NULL) { /* no special characters? */
  320. char *s2 = strstr(s+init, p);
  321. if (s2) {
  322. lua_pushnumber(s2-s+1);
  323. lua_pushnumber(s2-s+strlen(p));
  324. return;
  325. }
  326. }
  327. else {
  328. int anchor = (*p == '^') ? (p++, 1) : 0;
  329. const char *s1=s+init;
  330. cap.src_end = s+l;
  331. do {
  332. const char *res;
  333. cap.level = 0;
  334. if ((res=match(s1, p, &cap)) != NULL) {
  335. lua_pushnumber(s1-s+1); /* start */
  336. lua_pushnumber(res-s); /* end */
  337. push_captures(&cap);
  338. return;
  339. }
  340. } while (s1++<cap.src_end && !anchor);
  341. }
  342. lua_pushnil(); /* if arrives here, it didn't find */
  343. }
  344. static void add_s (lua_Object newp, struct Capture *cap) {
  345. if (lua_isstring(newp)) {
  346. const char *news = lua_getstring(newp);
  347. int l = lua_strlen(newp);
  348. int i;
  349. for (i=0; i<l; i++) {
  350. if (news[i] != ESC)
  351. luaL_addchar(news[i]);
  352. else {
  353. i++; /* skip ESC */
  354. if (!isdigit((unsigned char)news[i]))
  355. luaL_addchar(news[i]);
  356. else {
  357. int level = check_cap(news[i], cap);
  358. addnchar(cap->capture[level].init, cap->capture[level].len);
  359. }
  360. }
  361. }
  362. }
  363. else { /* is a function */
  364. lua_Object res;
  365. int status;
  366. int oldbuff;
  367. lua_beginblock();
  368. push_captures(cap);
  369. /* function may use buffer, so save it and create a new one */
  370. oldbuff = luaL_newbuffer(0);
  371. status = lua_callfunction(newp);
  372. /* restore old buffer */
  373. luaL_oldbuffer(oldbuff);
  374. if (status != 0) {
  375. lua_endblock();
  376. lua_error(NULL);
  377. }
  378. res = lua_getresult(1);
  379. if (lua_isstring(res))
  380. addnchar(lua_getstring(res), lua_strlen(res));
  381. lua_endblock();
  382. }
  383. }
  384. static void str_gsub (void) {
  385. long srcl;
  386. const char *src = luaL_check_lstr(1, &srcl);
  387. const char *p = luaL_check_string(2);
  388. lua_Object newp = lua_getparam(3);
  389. int max_s = luaL_opt_int(4, srcl+1);
  390. int anchor = (*p == '^') ? (p++, 1) : 0;
  391. int n = 0;
  392. struct Capture cap;
  393. luaL_arg_check(lua_isstring(newp) || lua_isfunction(newp), 3,
  394. "string or function expected");
  395. luaL_resetbuffer();
  396. cap.src_end = src+srcl;
  397. while (n < max_s) {
  398. const char *e;
  399. cap.level = 0;
  400. e = match(src, p, &cap);
  401. if (e) {
  402. n++;
  403. add_s(newp, &cap);
  404. }
  405. if (e && e>src) /* non empty match? */
  406. src = e; /* skip it */
  407. else if (src < cap.src_end)
  408. luaL_addchar(*src++);
  409. else break;
  410. if (anchor) break;
  411. }
  412. addnchar(src, cap.src_end-src);
  413. closeandpush();
  414. lua_pushnumber(n); /* number of substitutions */
  415. }
  416. /* }====================================================== */
  417. static void luaI_addquoted (int arg) {
  418. long l;
  419. const char *s = luaL_check_lstr(arg, &l);
  420. luaL_addchar('"');
  421. while (l--) {
  422. switch (*s) {
  423. case '"': case '\\': case '\n':
  424. luaL_addchar('\\');
  425. luaL_addchar(*s);
  426. break;
  427. case '\0': addnchar("\\000", 4); break;
  428. default: luaL_addchar(*s);
  429. }
  430. s++;
  431. }
  432. luaL_addchar('"');
  433. }
  434. /* maximum size of each format specification (such as '%-099.99d') */
  435. #define MAX_FORMAT 20 /* arbitrary limit */
  436. static void str_format (void) {
  437. int arg = 1;
  438. const char *strfrmt = luaL_check_string(arg);
  439. luaL_resetbuffer();
  440. while (*strfrmt) {
  441. if (*strfrmt != '%')
  442. luaL_addchar(*strfrmt++);
  443. else if (*++strfrmt == '%')
  444. luaL_addchar(*strfrmt++); /* %% */
  445. else { /* format item */
  446. struct Capture cap;
  447. char form[MAX_FORMAT]; /* to store the format ('%...') */
  448. char *buff; /* to store the formatted item */
  449. const char *initf = strfrmt;
  450. form[0] = '%';
  451. if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
  452. arg = *initf - '0';
  453. initf += 2; /* skip the 'n$' */
  454. }
  455. arg++;
  456. cap.src_end = strfrmt+strlen(strfrmt)+1;
  457. cap.level = 0;
  458. strfrmt = match(initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
  459. if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
  460. strfrmt-initf > MAX_FORMAT-2)
  461. lua_error("invalid format (width or precision too long)");
  462. strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
  463. form[strfrmt-initf+2] = 0;
  464. buff = luaL_openspace(512); /* 512 > soid luaI_addquot99.99f', -1e308) */
  465. switch (*strfrmt++) {
  466. case 'c': case 'd': case 'i':
  467. sprintf(buff, form, luaL_check_int(arg));
  468. break;
  469. case 'o': case 'u': case 'x': case 'X':
  470. sprintf(buff, form, (unsigned int)luaL_check_number(arg));
  471. break;
  472. case 'e': case 'E': case 'f': case 'g': case 'G':
  473. sprintf(buff, form, luaL_check_number(arg));
  474. break;
  475. case 'q':
  476. luaI_addquoted(arg);
  477. continue; /* skip the "addsize" at the end */
  478. case 's': {
  479. long l;
  480. const char *s = luaL_check_lstr(arg, &l);
  481. if (cap.capture[1].len == 0 && l >= 100) {
  482. /* no precision and string is too big to be formatted;
  483. keep original string */
  484. addnchar(s, l);
  485. continue; /* skip the "addsize" at the end */
  486. }
  487. else {
  488. sprintf(buff, form, s);
  489. break;
  490. }
  491. }
  492. default: /* also treat cases 'pnLlh' */
  493. lua_error("invalid option in `format'");
  494. }
  495. luaL_addsize(strlen(buff));
  496. }
  497. }
  498. closeandpush(); /* push the result */
  499. }
  500. static const struct luaL_reg strlib[] = {
  501. {"strlen", str_len},
  502. {"strsub", str_sub},
  503. {"strlower", str_lower},
  504. {"strupper", str_upper},
  505. {"strchar", str_char},
  506. {"strrep", str_rep},
  507. {"ascii", str_byte}, /* for compatibility with 3.0 and earlier */
  508. {"strbyte", str_byte},
  509. {"format", str_format},
  510. {"strfind", str_find},
  511. {"gsub", str_gsub}
  512. };
  513. /*
  514. ** Open string library
  515. */
  516. void strlib_open (void)
  517. {
  518. luaL_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));
  519. }