jsstring.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. #include "jsi.h"
  2. #include "utf.h"
  3. #include "regexp.h"
  4. static int js_doregexec(js_State *J, Reprog *prog, const char *string, Resub *sub, int eflags)
  5. {
  6. int result;
  7. if (J->runlimit)
  8. eflags |= REG_RUNAWAY;
  9. result = js_regexec(prog, string, sub, eflags);
  10. if (result < 0)
  11. js_error(J, "regexec failed");
  12. return result;
  13. }
  14. static const char *checkstring(js_State *J, int idx)
  15. {
  16. if (!js_iscoercible(J, idx))
  17. js_typeerror(J, "string function called on null or undefined");
  18. return js_tostring(J, idx);
  19. }
  20. int js_runeat(js_State *J, const char *s, int i)
  21. {
  22. Rune rune = EOF;
  23. while (i >= 0) {
  24. rune = *(unsigned char*)s;
  25. if (rune < Runeself) {
  26. if (rune == 0)
  27. return EOF;
  28. ++s;
  29. --i;
  30. } else {
  31. s += chartorune(&rune, s);
  32. if (rune >= 0x10000)
  33. i -= 2;
  34. else
  35. --i;
  36. }
  37. }
  38. if (rune >= 0x10000) {
  39. /* high surrogate */
  40. if (i == -2)
  41. return 0xd800 + ((rune - 0x10000) >> 10);
  42. /* low surrogate */
  43. else
  44. return 0xdc00 + ((rune - 0x10000) & 0x3ff);
  45. }
  46. return rune;
  47. }
  48. int js_utflen(const char *s)
  49. {
  50. int c;
  51. int n;
  52. Rune rune;
  53. n = 0;
  54. for(;;) {
  55. c = *(unsigned char *)s;
  56. if (c < Runeself) {
  57. if (c == 0)
  58. return n;
  59. s++;
  60. n++;
  61. } else {
  62. s += chartorune(&rune, s);
  63. if (rune >= 0x10000)
  64. n += 2;
  65. else
  66. n++;
  67. }
  68. }
  69. }
  70. int js_utfptrtoidx(const char *s, const char *p)
  71. {
  72. Rune rune;
  73. int i = 0;
  74. while (s < p) {
  75. if (*(unsigned char *)s < Runeself)
  76. {
  77. ++s;
  78. ++i;
  79. }
  80. else
  81. {
  82. s += chartorune(&rune, s);
  83. if (rune >= 0x10000)
  84. i += 2;
  85. else
  86. i += 1;
  87. }
  88. }
  89. return i;
  90. }
  91. static void jsB_new_String(js_State *J)
  92. {
  93. js_newstring(J, js_gettop(J) > 1 ? js_tostring(J, 1) : "");
  94. }
  95. static void jsB_String(js_State *J)
  96. {
  97. js_pushstring(J, js_gettop(J) > 1 ? js_tostring(J, 1) : "");
  98. }
  99. static void Sp_toString(js_State *J)
  100. {
  101. js_Object *self = js_toobject(J, 0);
  102. if (self->type != JS_CSTRING) js_typeerror(J, "not a string");
  103. js_pushstring(J, self->u.s.string);
  104. }
  105. static void Sp_valueOf(js_State *J)
  106. {
  107. js_Object *self = js_toobject(J, 0);
  108. if (self->type != JS_CSTRING) js_typeerror(J, "not a string");
  109. js_pushstring(J, self->u.s.string);
  110. }
  111. static void Sp_charAt(js_State *J)
  112. {
  113. char buf[UTFmax + 1];
  114. const char *s = checkstring(J, 0);
  115. int pos = js_tointeger(J, 1);
  116. Rune rune = js_runeat(J, s, pos);
  117. if (rune >= 0) {
  118. buf[runetochar(buf, &rune)] = 0;
  119. js_pushstring(J, buf);
  120. } else {
  121. js_pushliteral(J, "");
  122. }
  123. }
  124. static void Sp_charCodeAt(js_State *J)
  125. {
  126. const char *s = checkstring(J, 0);
  127. int pos = js_tointeger(J, 1);
  128. Rune rune = js_runeat(J, s, pos);
  129. if (rune >= 0)
  130. js_pushnumber(J, rune);
  131. else
  132. js_pushnumber(J, NAN);
  133. }
  134. static void Sp_concat(js_State *J)
  135. {
  136. int i, top = js_gettop(J);
  137. int n;
  138. char * volatile out = NULL;
  139. const char *s;
  140. if (top == 1)
  141. return;
  142. s = checkstring(J, 0);
  143. n = 1 + strlen(s);
  144. if (js_try(J)) {
  145. js_free(J, out);
  146. js_throw(J);
  147. }
  148. if (n > JS_STRLIMIT)
  149. js_rangeerror(J, "invalid string length");
  150. out = js_malloc(J, n);
  151. strcpy(out, s);
  152. for (i = 1; i < top; ++i) {
  153. s = js_tostring(J, i);
  154. n += strlen(s);
  155. if (n > JS_STRLIMIT)
  156. js_rangeerror(J, "invalid string length");
  157. out = js_realloc(J, out, n);
  158. strcat(out, s);
  159. }
  160. js_pushstring(J, out);
  161. js_endtry(J);
  162. js_free(J, out);
  163. }
  164. static void Sp_indexOf(js_State *J)
  165. {
  166. const char *haystack = checkstring(J, 0);
  167. const char *needle = js_tostring(J, 1);
  168. int pos = js_tointeger(J, 2);
  169. int len = strlen(needle);
  170. int k = 0;
  171. Rune rune;
  172. while (*haystack) {
  173. if (k >= pos && !strncmp(haystack, needle, len)) {
  174. js_pushnumber(J, k);
  175. return;
  176. }
  177. haystack += chartorune(&rune, haystack);
  178. ++k;
  179. }
  180. js_pushnumber(J, -1);
  181. }
  182. static void Sp_lastIndexOf(js_State *J)
  183. {
  184. const char *haystack = checkstring(J, 0);
  185. const char *needle = js_tostring(J, 1);
  186. int pos = js_isdefined(J, 2) ? js_tointeger(J, 2) : (int)strlen(haystack);
  187. int len = strlen(needle);
  188. int k = 0, last = -1;
  189. Rune rune;
  190. while (*haystack && k <= pos) {
  191. if (!strncmp(haystack, needle, len))
  192. last = k;
  193. haystack += chartorune(&rune, haystack);
  194. ++k;
  195. }
  196. js_pushnumber(J, last);
  197. }
  198. static void Sp_localeCompare(js_State *J)
  199. {
  200. const char *a = checkstring(J, 0);
  201. const char *b = js_tostring(J, 1);
  202. js_pushnumber(J, strcmp(a, b));
  203. }
  204. static void Sp_substring_imp(js_State *J, const char *s, int a, int n)
  205. {
  206. Rune head_rune = 0, tail_rune = 0;
  207. const char *head, *tail;
  208. char *p;
  209. int i, k, head_len, tail_len;
  210. /* find start of substring */
  211. head = s;
  212. for (i = 0; i < a; ++i) {
  213. head += chartorune(&head_rune, head);
  214. if (head_rune >= 0x10000)
  215. ++i;
  216. }
  217. /* find end of substring */
  218. tail = head;
  219. for (k = i - a; k < n; ++k) {
  220. tail += chartorune(&tail_rune, tail);
  221. if (tail_rune >= 0x10000)
  222. ++k;
  223. }
  224. /* no surrogate pair splits! */
  225. if (i == a && k == n) {
  226. js_pushlstring(J, head, tail - head);
  227. return;
  228. }
  229. if (js_try(J)) {
  230. js_free(J, p);
  231. js_throw(J);
  232. }
  233. p = js_malloc(J, UTFmax + (tail - head));
  234. /* substring starts with low surrogate (head is just after character) */
  235. if (i > a) {
  236. head_rune = 0xdc00 + ((head_rune - 0x10000) & 0x3ff);
  237. head_len = runetochar(p, &head_rune);
  238. memcpy(p + head_len, head, tail - head);
  239. js_pushlstring(J, p, head_len + (tail - head));
  240. }
  241. /* substring ends with high surrogate (tail is just after character) */
  242. if (k > n) {
  243. tail -= runelen(tail_rune);
  244. memcpy(p, head, tail - head);
  245. tail_rune = 0xd800 + ((tail_rune - 0x10000) >> 10);
  246. tail_len = runetochar(p + (tail - head), &tail_rune);
  247. js_pushlstring(J, p, (tail - head) + tail_len);
  248. }
  249. js_endtry(J);
  250. js_free(J, p);
  251. }
  252. static void Sp_slice(js_State *J)
  253. {
  254. const char *str = checkstring(J, 0);
  255. int len = js_utflen(str);
  256. int s = js_tointeger(J, 1);
  257. int e = js_isdefined(J, 2) ? js_tointeger(J, 2) : len;
  258. s = s < 0 ? s + len : s;
  259. e = e < 0 ? e + len : e;
  260. s = s < 0 ? 0 : s > len ? len : s;
  261. e = e < 0 ? 0 : e > len ? len : e;
  262. if (s < e)
  263. Sp_substring_imp(J, str, s, e - s);
  264. else if (s > e)
  265. Sp_substring_imp(J, str, e, s - e);
  266. else
  267. js_pushliteral(J, "");
  268. }
  269. static void Sp_substring(js_State *J)
  270. {
  271. const char *str = checkstring(J, 0);
  272. int len = js_utflen(str);
  273. int s = js_tointeger(J, 1);
  274. int e = js_isdefined(J, 2) ? js_tointeger(J, 2) : len;
  275. s = s < 0 ? 0 : s > len ? len : s;
  276. e = e < 0 ? 0 : e > len ? len : e;
  277. if (s < e)
  278. Sp_substring_imp(J, str, s, e - s);
  279. else if (s > e)
  280. Sp_substring_imp(J, str, e, s - e);
  281. else
  282. js_pushliteral(J, "");
  283. }
  284. static void Sp_toLowerCase(js_State *J)
  285. {
  286. const char *s, *s0 = checkstring(J, 0);
  287. char * volatile dst = NULL;
  288. char *d;
  289. Rune rune;
  290. const Rune *full;
  291. int n;
  292. n = 1;
  293. for (s = s0; *s;) {
  294. s += chartorune(&rune, s);
  295. full = tolowerrune_full(rune);
  296. if (full) {
  297. while (*full) {
  298. n += runelen(*full);
  299. ++full;
  300. }
  301. } else {
  302. rune = tolowerrune(rune);
  303. n += runelen(rune);
  304. }
  305. }
  306. if (js_try(J)) {
  307. js_free(J, dst);
  308. js_throw(J);
  309. }
  310. d = dst = js_malloc(J, n);
  311. for (s = s0; *s;) {
  312. s += chartorune(&rune, s);
  313. full = tolowerrune_full(rune);
  314. if (full) {
  315. while (*full) {
  316. d += runetochar(d, full);
  317. ++full;
  318. }
  319. } else {
  320. rune = tolowerrune(rune);
  321. d += runetochar(d, &rune);
  322. }
  323. }
  324. *d = 0;
  325. js_pushstring(J, dst);
  326. js_endtry(J);
  327. js_free(J, dst);
  328. }
  329. static void Sp_toUpperCase(js_State *J)
  330. {
  331. const char *s, *s0 = checkstring(J, 0);
  332. char * volatile dst = NULL;
  333. char *d;
  334. const Rune *full;
  335. Rune rune;
  336. int n;
  337. n = 1;
  338. for (s = s0; *s;) {
  339. s += chartorune(&rune, s);
  340. full = toupperrune_full(rune);
  341. if (full) {
  342. while (*full) {
  343. n += runelen(*full);
  344. ++full;
  345. }
  346. } else {
  347. rune = toupperrune(rune);
  348. n += runelen(rune);
  349. }
  350. }
  351. if (js_try(J)) {
  352. js_free(J, dst);
  353. js_throw(J);
  354. }
  355. d = dst = js_malloc(J, n);
  356. for (s = s0; *s;) {
  357. s += chartorune(&rune, s);
  358. full = toupperrune_full(rune);
  359. if (full) {
  360. while (*full) {
  361. d += runetochar(d, full);
  362. ++full;
  363. }
  364. } else {
  365. rune = toupperrune(rune);
  366. d += runetochar(d, &rune);
  367. }
  368. }
  369. *d = 0;
  370. js_pushstring(J, dst);
  371. js_endtry(J);
  372. js_free(J, dst);
  373. }
  374. static int isbol(js_Regexp *re, const char *text, const char *a)
  375. {
  376. return a == text || ((re->flags & JS_REGEXP_M) && a[-1] == '\n');
  377. }
  378. static int istrim(int c)
  379. {
  380. return c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0 || c == 0xFEFF ||
  381. c == 0xA || c == 0xD || c == 0x2028 || c == 0x2029;
  382. }
  383. static void Sp_trim(js_State *J)
  384. {
  385. const char *s, *e;
  386. s = checkstring(J, 0);
  387. while (istrim(*s))
  388. ++s;
  389. e = s + strlen(s);
  390. while (e > s && istrim(e[-1]))
  391. --e;
  392. js_pushlstring(J, s, e - s);
  393. }
  394. static void S_fromCharCode(js_State *J)
  395. {
  396. int i, top = js_gettop(J);
  397. char * volatile s = NULL;
  398. char *p;
  399. Rune c;
  400. if (js_try(J)) {
  401. js_free(J, s);
  402. js_throw(J);
  403. }
  404. s = p = js_malloc(J, (top-1) * UTFmax + 1);
  405. for (i = 1; i < top; ++i) {
  406. c = js_touint32(J, i);
  407. p += runetochar(p, &c);
  408. }
  409. *p = 0;
  410. js_pushstring(J, s);
  411. js_endtry(J);
  412. js_free(J, s);
  413. }
  414. static void Sp_match(js_State *J)
  415. {
  416. js_Regexp *re;
  417. const char *text;
  418. int len;
  419. const char *a, *b, *c, *e;
  420. Resub m;
  421. Rune rune;
  422. text = checkstring(J, 0);
  423. if (js_isregexp(J, 1))
  424. js_copy(J, 1);
  425. else if (js_isundefined(J, 1))
  426. js_newregexp(J, "", 0);
  427. else
  428. js_newregexp(J, js_tostring(J, 1), 0);
  429. re = js_toregexp(J, -1);
  430. if (!(re->flags & JS_REGEXP_G)) {
  431. js_RegExp_prototype_exec(J, re, text);
  432. return;
  433. }
  434. re->last = 0;
  435. js_newarray(J);
  436. len = 0;
  437. a = text;
  438. e = text + strlen(text);
  439. while (a <= e) {
  440. if (js_doregexec(J, re->prog, a, &m, isbol(re, text, a) ? 0 : REG_NOTBOL))
  441. break;
  442. b = m.sub[0].sp;
  443. c = m.sub[0].ep;
  444. js_pushlstring(J, b, c - b);
  445. js_setindex(J, -2, len++);
  446. a = c;
  447. if (c - b == 0)
  448. a += chartorune(&rune, a);
  449. }
  450. if (len == 0) {
  451. js_pop(J, 1);
  452. js_pushnull(J);
  453. }
  454. }
  455. static void Sp_search(js_State *J)
  456. {
  457. js_Regexp *re;
  458. const char *text;
  459. Resub m;
  460. text = checkstring(J, 0);
  461. if (js_isregexp(J, 1))
  462. js_copy(J, 1);
  463. else if (js_isundefined(J, 1))
  464. js_newregexp(J, "", 0);
  465. else
  466. js_newregexp(J, js_tostring(J, 1), 0);
  467. re = js_toregexp(J, -1);
  468. if (!js_doregexec(J, re->prog, text, &m, 0))
  469. js_pushnumber(J, js_utfptrtoidx(text, m.sub[0].sp));
  470. else
  471. js_pushnumber(J, -1);
  472. }
  473. static void Sp_replace_regexp(js_State *J)
  474. {
  475. js_Regexp *re;
  476. const char *source, *source0, *s, *r;
  477. js_Buffer *sb = NULL;
  478. int n, x;
  479. Resub m;
  480. source = source0 = checkstring(J, 0);
  481. re = js_toregexp(J, 1);
  482. if (js_doregexec(J, re->prog, source, &m, 0)) {
  483. js_copy(J, 0);
  484. return;
  485. }
  486. re->last = 0;
  487. if (js_try(J)) {
  488. js_free(J, sb);
  489. js_throw(J);
  490. }
  491. loop:
  492. s = m.sub[0].sp;
  493. n = m.sub[0].ep - m.sub[0].sp;
  494. if (js_iscallable(J, 2)) {
  495. js_copy(J, 2);
  496. js_pushundefined(J);
  497. for (x = 0; m.sub[x].sp; ++x) /* arg 0..x: substring and subexps that matched */
  498. js_pushlstring(J, m.sub[x].sp, m.sub[x].ep - m.sub[x].sp);
  499. js_pushnumber(J, s - source); /* arg x+2: offset within search string */
  500. js_copy(J, 0); /* arg x+3: search string */
  501. js_call(J, 2 + x);
  502. r = js_tostring(J, -1);
  503. js_putm(J, &sb, source, s);
  504. js_puts(J, &sb, r);
  505. js_pop(J, 1);
  506. } else {
  507. r = js_tostring(J, 2);
  508. js_putm(J, &sb, source, s);
  509. while (*r) {
  510. if (*r == '$') {
  511. switch (*(++r)) {
  512. case 0: --r; /* end of string; back up */
  513. /* fallthrough */
  514. case '$': js_putc(J, &sb, '$'); break;
  515. case '`': js_putm(J, &sb, source0, s); break;
  516. case '\'': js_puts(J, &sb, s + n); break;
  517. case '&':
  518. js_putm(J, &sb, s, s + n);
  519. break;
  520. case '0': case '1': case '2': case '3': case '4':
  521. case '5': case '6': case '7': case '8': case '9':
  522. x = *r - '0';
  523. if (r[1] >= '0' && r[1] <= '9')
  524. x = x * 10 + *(++r) - '0';
  525. if (x > 0 && x < m.nsub) {
  526. js_putm(J, &sb, m.sub[x].sp, m.sub[x].ep);
  527. } else {
  528. js_putc(J, &sb, '$');
  529. if (x > 10) {
  530. js_putc(J, &sb, '0' + x / 10);
  531. js_putc(J, &sb, '0' + x % 10);
  532. } else {
  533. js_putc(J, &sb, '0' + x);
  534. }
  535. }
  536. break;
  537. default:
  538. js_putc(J, &sb, '$');
  539. js_putc(J, &sb, *r);
  540. break;
  541. }
  542. ++r;
  543. } else {
  544. js_putc(J, &sb, *r++);
  545. }
  546. }
  547. }
  548. if (re->flags & JS_REGEXP_G) {
  549. source = m.sub[0].ep;
  550. if (n == 0) {
  551. if (*source)
  552. js_putc(J, &sb, *source++);
  553. else
  554. goto end;
  555. }
  556. if (!js_doregexec(J, re->prog, source, &m, isbol(re, source0, source) ? 0 : REG_NOTBOL))
  557. goto loop;
  558. }
  559. end:
  560. js_puts(J, &sb, s + n);
  561. js_putc(J, &sb, 0);
  562. js_pushstring(J, sb ? sb->s : "");
  563. js_endtry(J);
  564. js_free(J, sb);
  565. }
  566. static void Sp_replace_string(js_State *J)
  567. {
  568. const char *source, *needle, *s, *r;
  569. js_Buffer *sb = NULL;
  570. int n;
  571. source = checkstring(J, 0);
  572. needle = js_tostring(J, 1);
  573. s = strstr(source, needle);
  574. if (!s) {
  575. js_copy(J, 0);
  576. return;
  577. }
  578. n = strlen(needle);
  579. if (js_try(J)) {
  580. js_free(J, sb);
  581. js_throw(J);
  582. }
  583. if (js_iscallable(J, 2)) {
  584. js_copy(J, 2);
  585. js_pushundefined(J);
  586. js_pushlstring(J, s, n); /* arg 1: substring that matched */
  587. js_pushnumber(J, s - source); /* arg 2: offset within search string */
  588. js_copy(J, 0); /* arg 3: search string */
  589. js_call(J, 3);
  590. r = js_tostring(J, -1);
  591. js_putm(J, &sb, source, s);
  592. js_puts(J, &sb, r);
  593. js_puts(J, &sb, s + n);
  594. js_putc(J, &sb, 0);
  595. js_pop(J, 1);
  596. } else {
  597. r = js_tostring(J, 2);
  598. js_putm(J, &sb, source, s);
  599. while (*r) {
  600. if (*r == '$') {
  601. switch (*(++r)) {
  602. case 0: --r; /* end of string; back up */
  603. /* fallthrough */
  604. case '$': js_putc(J, &sb, '$'); break;
  605. case '&': js_putm(J, &sb, s, s + n); break;
  606. case '`': js_putm(J, &sb, source, s); break;
  607. case '\'': js_puts(J, &sb, s + n); break;
  608. default: js_putc(J, &sb, '$'); js_putc(J, &sb, *r); break;
  609. }
  610. ++r;
  611. } else {
  612. js_putc(J, &sb, *r++);
  613. }
  614. }
  615. js_puts(J, &sb, s + n);
  616. js_putc(J, &sb, 0);
  617. }
  618. js_pushstring(J, sb ? sb->s : "");
  619. js_endtry(J);
  620. js_free(J, sb);
  621. }
  622. static void Sp_replace(js_State *J)
  623. {
  624. if (js_isregexp(J, 1))
  625. Sp_replace_regexp(J);
  626. else
  627. Sp_replace_string(J);
  628. }
  629. static void Sp_split_regexp(js_State *J)
  630. {
  631. js_Regexp *re;
  632. const char *text;
  633. int limit, len, k;
  634. const char *p, *a, *b, *c, *e;
  635. Resub m;
  636. Rune rune;
  637. text = checkstring(J, 0);
  638. re = js_toregexp(J, 1);
  639. limit = js_isdefined(J, 2) ? js_tointeger(J, 2) : 1 << 30;
  640. js_newarray(J);
  641. len = 0;
  642. if (limit == 0)
  643. return;
  644. e = text + strlen(text);
  645. /* splitting the empty string */
  646. if (e == text) {
  647. if (js_doregexec(J, re->prog, text, &m, 0)) {
  648. js_pushliteral(J, "");
  649. js_setindex(J, -2, 0);
  650. }
  651. return;
  652. }
  653. p = a = text;
  654. while (a < e) {
  655. if (js_doregexec(J, re->prog, a, &m, isbol(re, text, a) ? 0 : REG_NOTBOL))
  656. break; /* no match */
  657. b = m.sub[0].sp;
  658. c = m.sub[0].ep;
  659. /* empty string at end of last match */
  660. if (b == c && b == p) {
  661. a += chartorune(&rune, a);
  662. continue;
  663. }
  664. if (len == limit) return;
  665. js_pushlstring(J, p, b - p);
  666. js_setindex(J, -2, len++);
  667. for (k = 1; k < m.nsub; ++k) {
  668. if (len == limit) return;
  669. js_pushlstring(J, m.sub[k].sp, m.sub[k].ep - m.sub[k].sp);
  670. js_setindex(J, -2, len++);
  671. }
  672. a = p = c;
  673. }
  674. if (len == limit) return;
  675. js_pushstring(J, p);
  676. js_setindex(J, -2, len);
  677. }
  678. static void Sp_split_string(js_State *J)
  679. {
  680. const char *str = checkstring(J, 0);
  681. const char *sep = js_tostring(J, 1);
  682. int limit = js_isdefined(J, 2) ? js_tointeger(J, 2) : 1 << 30;
  683. int i, n;
  684. js_newarray(J);
  685. if (limit == 0)
  686. return;
  687. n = strlen(sep);
  688. /* empty string */
  689. if (n == 0) {
  690. Rune rune;
  691. for (i = 0; *str && i < limit; ++i) {
  692. n = chartorune(&rune, str);
  693. js_pushlstring(J, str, n);
  694. js_setindex(J, -2, i);
  695. str += n;
  696. }
  697. return;
  698. }
  699. for (i = 0; str && i < limit; ++i) {
  700. const char *s = strstr(str, sep);
  701. if (s) {
  702. js_pushlstring(J, str, s-str);
  703. js_setindex(J, -2, i);
  704. str = s + n;
  705. } else {
  706. js_pushstring(J, str);
  707. js_setindex(J, -2, i);
  708. str = NULL;
  709. }
  710. }
  711. }
  712. static void Sp_split(js_State *J)
  713. {
  714. if (js_isundefined(J, 1)) {
  715. js_newarray(J);
  716. js_pushstring(J, js_tostring(J, 0));
  717. js_setindex(J, -2, 0);
  718. } else if (js_isregexp(J, 1)) {
  719. Sp_split_regexp(J);
  720. } else {
  721. Sp_split_string(J);
  722. }
  723. }
  724. void jsB_initstring(js_State *J)
  725. {
  726. J->String_prototype->u.s.shrstr[0] = 0;
  727. J->String_prototype->u.s.string = J->String_prototype->u.s.shrstr;
  728. J->String_prototype->u.s.length = 0;
  729. js_pushobject(J, J->String_prototype);
  730. {
  731. jsB_propf(J, "String.prototype.toString", Sp_toString, 0);
  732. jsB_propf(J, "String.prototype.valueOf", Sp_valueOf, 0);
  733. jsB_propf(J, "String.prototype.charAt", Sp_charAt, 1);
  734. jsB_propf(J, "String.prototype.charCodeAt", Sp_charCodeAt, 1);
  735. jsB_propf(J, "String.prototype.concat", Sp_concat, 0); /* 1 */
  736. jsB_propf(J, "String.prototype.indexOf", Sp_indexOf, 1);
  737. jsB_propf(J, "String.prototype.lastIndexOf", Sp_lastIndexOf, 1);
  738. jsB_propf(J, "String.prototype.localeCompare", Sp_localeCompare, 1);
  739. jsB_propf(J, "String.prototype.match", Sp_match, 1);
  740. jsB_propf(J, "String.prototype.replace", Sp_replace, 2);
  741. jsB_propf(J, "String.prototype.search", Sp_search, 1);
  742. jsB_propf(J, "String.prototype.slice", Sp_slice, 2);
  743. jsB_propf(J, "String.prototype.split", Sp_split, 2);
  744. jsB_propf(J, "String.prototype.substring", Sp_substring, 2);
  745. jsB_propf(J, "String.prototype.toLowerCase", Sp_toLowerCase, 0);
  746. jsB_propf(J, "String.prototype.toLocaleLowerCase", Sp_toLowerCase, 0);
  747. jsB_propf(J, "String.prototype.toUpperCase", Sp_toUpperCase, 0);
  748. jsB_propf(J, "String.prototype.toLocaleUpperCase", Sp_toUpperCase, 0);
  749. /* ES5 */
  750. jsB_propf(J, "String.prototype.trim", Sp_trim, 0);
  751. }
  752. js_newcconstructor(J, jsB_String, jsB_new_String, "String", 0); /* 1 */
  753. {
  754. jsB_propf(J, "String.fromCharCode", S_fromCharCode, 0); /* 1 */
  755. }
  756. js_defglobal(J, "String", JS_DONTENUM);
  757. }