sqstdstring.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <sqstdstring.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <assert.h>
  9. #define MAX_FORMAT_LEN 20
  10. #define MAX_WFORMAT_LEN 3
  11. #define ADDITIONAL_FORMAT_SPACE (100*sizeof(SQChar))
  12. static SQBool isfmtchr(SQChar ch)
  13. {
  14. switch(ch) {
  15. case '-': case '+': case ' ': case '#': case '0': return SQTrue;
  16. }
  17. return SQFalse;
  18. }
  19. static SQInteger validate_format(HSQUIRRELVM v, SQChar *fmt, const SQChar *src, SQInteger n,SQInteger &width)
  20. {
  21. SQChar *dummy;
  22. SQChar swidth[MAX_WFORMAT_LEN];
  23. SQInteger wc = 0;
  24. SQInteger start = n;
  25. fmt[0] = '%';
  26. while (isfmtchr(src[n])) n++;
  27. while (scisdigit(src[n])) {
  28. swidth[wc] = src[n];
  29. n++;
  30. wc++;
  31. if(wc>=MAX_WFORMAT_LEN)
  32. return sq_throwerror(v,_SC("width format too long"));
  33. }
  34. swidth[wc] = '\0';
  35. if(wc > 0) {
  36. width = scstrtol(swidth,&dummy,10);
  37. }
  38. else
  39. width = 0;
  40. if (src[n] == '.') {
  41. n++;
  42. wc = 0;
  43. while (scisdigit(src[n])) {
  44. swidth[wc] = src[n];
  45. n++;
  46. wc++;
  47. if(wc>=MAX_WFORMAT_LEN)
  48. return sq_throwerror(v,_SC("precision format too long"));
  49. }
  50. swidth[wc] = '\0';
  51. if(wc > 0) {
  52. width += scstrtol(swidth,&dummy,10);
  53. }
  54. }
  55. if (n-start > MAX_FORMAT_LEN )
  56. return sq_throwerror(v,_SC("format too long"));
  57. memcpy(&fmt[1],&src[start],((n-start)+1)*sizeof(SQChar));
  58. fmt[(n-start)+2] = '\0';
  59. return n;
  60. }
  61. SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output)
  62. {
  63. const SQChar *format;
  64. SQChar *dest;
  65. SQChar fmt[MAX_FORMAT_LEN];
  66. sq_getstring(v,nformatstringidx,&format);
  67. SQInteger format_size = sq_getsize(v,nformatstringidx);
  68. SQInteger allocated = (format_size+2)*sizeof(SQChar);
  69. dest = sq_getscratchpad(v,allocated);
  70. SQInteger n = 0,i = 0, nparam = nformatstringidx+1, w = 0;
  71. //while(format[n] != '\0')
  72. while(n < format_size)
  73. {
  74. if(format[n] != '%') {
  75. assert(i < allocated);
  76. dest[i++] = format[n];
  77. n++;
  78. }
  79. else if(format[n+1] == '%') { //handles %%
  80. dest[i++] = '%';
  81. n += 2;
  82. }
  83. else {
  84. n++;
  85. if( nparam > sq_gettop(v) )
  86. return sq_throwerror(v,_SC("not enough paramters for the given format string"));
  87. n = validate_format(v,fmt,format,n,w);
  88. if(n < 0) return -1;
  89. SQInteger addlen = 0;
  90. SQInteger valtype = 0;
  91. const SQChar *ts = NULL;
  92. SQInteger ti = 0;
  93. SQFloat tf = 0;
  94. switch(format[n]) {
  95. case 's':
  96. if(SQ_FAILED(sq_getstring(v,nparam,&ts)))
  97. return sq_throwerror(v,_SC("string expected for the specified format"));
  98. addlen = (sq_getsize(v,nparam)*sizeof(SQChar))+((w+1)*sizeof(SQChar));
  99. valtype = 's';
  100. break;
  101. case 'i': case 'd': case 'o': case 'u': case 'x': case 'X':
  102. #ifdef _SQ64
  103. {
  104. size_t flen = scstrlen(fmt);
  105. SQInteger fpos = flen - 1;
  106. SQChar f = fmt[fpos];
  107. const SQChar *prec = (const SQChar *)_PRINT_INT_PREC;
  108. while(*prec != _SC('\0')) {
  109. fmt[fpos++] = *prec++;
  110. }
  111. fmt[fpos++] = f;
  112. fmt[fpos++] = _SC('\0');
  113. }
  114. #endif
  115. case 'c':
  116. if(SQ_FAILED(sq_getinteger(v,nparam,&ti)))
  117. return sq_throwerror(v,_SC("integer expected for the specified format"));
  118. addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar));
  119. valtype = 'i';
  120. break;
  121. case 'f': case 'g': case 'G': case 'e': case 'E':
  122. if(SQ_FAILED(sq_getfloat(v,nparam,&tf)))
  123. return sq_throwerror(v,_SC("float expected for the specified format"));
  124. addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar));
  125. valtype = 'f';
  126. break;
  127. default:
  128. return sq_throwerror(v,_SC("invalid format"));
  129. }
  130. n++;
  131. allocated += addlen + sizeof(SQChar);
  132. dest = sq_getscratchpad(v,allocated);
  133. switch(valtype) {
  134. case 's': i += scsprintf(&dest[i],allocated,fmt,ts); break;
  135. case 'i': i += scsprintf(&dest[i],allocated,fmt,ti); break;
  136. case 'f': i += scsprintf(&dest[i],allocated,fmt,tf); break;
  137. };
  138. nparam ++;
  139. }
  140. }
  141. *outlen = i;
  142. dest[i] = '\0';
  143. *output = dest;
  144. return SQ_OK;
  145. }
  146. static SQInteger _string_format(HSQUIRRELVM v)
  147. {
  148. SQChar *dest = NULL;
  149. SQInteger length = 0;
  150. if(SQ_FAILED(sqstd_format(v,2,&length,&dest)))
  151. return -1;
  152. sq_pushstring(v,dest,length);
  153. return 1;
  154. }
  155. static void __strip_l(const SQChar *str,const SQChar **start)
  156. {
  157. const SQChar *t = str;
  158. while(((*t) != '\0') && scisspace(*t)){ t++; }
  159. *start = t;
  160. }
  161. static void __strip_r(const SQChar *str,SQInteger len,const SQChar **end)
  162. {
  163. if(len == 0) {
  164. *end = str;
  165. return;
  166. }
  167. const SQChar *t = &str[len-1];
  168. while(t >= str && scisspace(*t)) { t--; }
  169. *end = t + 1;
  170. }
  171. static SQInteger _string_strip(HSQUIRRELVM v)
  172. {
  173. const SQChar *str,*start,*end;
  174. sq_getstring(v,2,&str);
  175. SQInteger len = sq_getsize(v,2);
  176. __strip_l(str,&start);
  177. __strip_r(str,len,&end);
  178. sq_pushstring(v,start,end - start);
  179. return 1;
  180. }
  181. static SQInteger _string_lstrip(HSQUIRRELVM v)
  182. {
  183. const SQChar *str,*start;
  184. sq_getstring(v,2,&str);
  185. __strip_l(str,&start);
  186. sq_pushstring(v,start,-1);
  187. return 1;
  188. }
  189. static SQInteger _string_rstrip(HSQUIRRELVM v)
  190. {
  191. const SQChar *str,*end;
  192. sq_getstring(v,2,&str);
  193. SQInteger len = sq_getsize(v,2);
  194. __strip_r(str,len,&end);
  195. sq_pushstring(v,str,end - str);
  196. return 1;
  197. }
  198. static SQInteger _string_split(HSQUIRRELVM v)
  199. {
  200. const SQChar *str,*seps;
  201. SQChar *stemp;
  202. sq_getstring(v,2,&str);
  203. sq_getstring(v,3,&seps);
  204. SQInteger sepsize = sq_getsize(v,3);
  205. if(sepsize == 0) return sq_throwerror(v,_SC("empty separators string"));
  206. SQInteger memsize = (sq_getsize(v,2)+1)*sizeof(SQChar);
  207. stemp = sq_getscratchpad(v,memsize);
  208. memcpy(stemp,str,memsize);
  209. SQChar *start = stemp;
  210. SQChar *end = stemp;
  211. sq_newarray(v,0);
  212. while(*end != '\0')
  213. {
  214. SQChar cur = *end;
  215. for(SQInteger i = 0; i < sepsize; i++)
  216. {
  217. if(cur == seps[i])
  218. {
  219. *end = 0;
  220. sq_pushstring(v,start,-1);
  221. sq_arrayappend(v,-2);
  222. start = end + 1;
  223. break;
  224. }
  225. }
  226. end++;
  227. }
  228. if(end != start)
  229. {
  230. sq_pushstring(v,start,-1);
  231. sq_arrayappend(v,-2);
  232. }
  233. return 1;
  234. }
  235. static SQInteger _string_escape(HSQUIRRELVM v)
  236. {
  237. const SQChar *str;
  238. SQChar *dest,*resstr;
  239. SQInteger size;
  240. sq_getstring(v,2,&str);
  241. size = sq_getsize(v,2);
  242. if(size == 0) {
  243. sq_push(v,2);
  244. return 1;
  245. }
  246. #ifdef SQUNICODE
  247. #if WCHAR_SIZE == 2
  248. const SQChar *escpat = _SC("\\x%04x");
  249. const SQInteger maxescsize = 6;
  250. #else //WCHAR_SIZE == 4
  251. const SQChar *escpat = _SC("\\x%08x");
  252. const SQInteger maxescsize = 10;
  253. #endif
  254. #else
  255. const SQChar *escpat = _SC("\\x%02x");
  256. const SQInteger maxescsize = 4;
  257. #endif
  258. SQInteger destcharsize = (size * maxescsize); //assumes every char could be escaped
  259. resstr = dest = (SQChar *)sq_getscratchpad(v,destcharsize * sizeof(SQChar));
  260. SQChar c;
  261. SQChar escch;
  262. SQInteger escaped = 0;
  263. for(int n = 0; n < size; n++){
  264. c = *str++;
  265. escch = 0;
  266. if(scisprint(c) || c == 0) {
  267. switch(c) {
  268. case '\a': escch = 'a'; break;
  269. case '\b': escch = 'b'; break;
  270. case '\t': escch = 't'; break;
  271. case '\n': escch = 'n'; break;
  272. case '\v': escch = 'v'; break;
  273. case '\f': escch = 'f'; break;
  274. case '\r': escch = 'r'; break;
  275. case '\\': escch = '\\'; break;
  276. case '\"': escch = '\"'; break;
  277. case '\'': escch = '\''; break;
  278. case 0: escch = '0'; break;
  279. }
  280. if(escch) {
  281. *dest++ = '\\';
  282. *dest++ = escch;
  283. escaped++;
  284. }
  285. else {
  286. *dest++ = c;
  287. }
  288. }
  289. else {
  290. dest += scsprintf(dest, destcharsize, escpat, c);
  291. escaped++;
  292. }
  293. }
  294. if(escaped) {
  295. sq_pushstring(v,resstr,dest - resstr);
  296. }
  297. else {
  298. sq_push(v,2); //nothing escaped
  299. }
  300. return 1;
  301. }
  302. static SQInteger _string_startswith(HSQUIRRELVM v)
  303. {
  304. const SQChar *str,*cmp;
  305. sq_getstring(v,2,&str);
  306. sq_getstring(v,3,&cmp);
  307. SQInteger len = sq_getsize(v,2);
  308. SQInteger cmplen = sq_getsize(v,3);
  309. SQBool ret = SQFalse;
  310. if(cmplen <= len) {
  311. ret = memcmp(str,cmp,sq_rsl(cmplen)) == 0 ? SQTrue : SQFalse;
  312. }
  313. sq_pushbool(v,ret);
  314. return 1;
  315. }
  316. static SQInteger _string_endswith(HSQUIRRELVM v)
  317. {
  318. const SQChar *str,*cmp;
  319. sq_getstring(v,2,&str);
  320. sq_getstring(v,3,&cmp);
  321. SQInteger len = sq_getsize(v,2);
  322. SQInteger cmplen = sq_getsize(v,3);
  323. SQBool ret = SQFalse;
  324. if(cmplen <= len) {
  325. ret = memcmp(&str[len - cmplen],cmp,sq_rsl(cmplen)) == 0 ? SQTrue : SQFalse;
  326. }
  327. sq_pushbool(v,ret);
  328. return 1;
  329. }
  330. #define SETUP_REX(v) \
  331. SQRex *self = NULL; \
  332. sq_getinstanceup(v,1,(SQUserPointer *)&self,0);
  333. static SQInteger _rexobj_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
  334. {
  335. SQRex *self = ((SQRex *)p);
  336. sqstd_rex_free(self);
  337. return 1;
  338. }
  339. static SQInteger _regexp_match(HSQUIRRELVM v)
  340. {
  341. SETUP_REX(v);
  342. const SQChar *str;
  343. sq_getstring(v,2,&str);
  344. if(sqstd_rex_match(self,str) == SQTrue)
  345. {
  346. sq_pushbool(v,SQTrue);
  347. return 1;
  348. }
  349. sq_pushbool(v,SQFalse);
  350. return 1;
  351. }
  352. static void _addrexmatch(HSQUIRRELVM v,const SQChar *str,const SQChar *begin,const SQChar *end)
  353. {
  354. sq_newtable(v);
  355. sq_pushstring(v,_SC("begin"),-1);
  356. sq_pushinteger(v,begin - str);
  357. sq_rawset(v,-3);
  358. sq_pushstring(v,_SC("end"),-1);
  359. sq_pushinteger(v,end - str);
  360. sq_rawset(v,-3);
  361. }
  362. static SQInteger _regexp_search(HSQUIRRELVM v)
  363. {
  364. SETUP_REX(v);
  365. const SQChar *str,*begin,*end;
  366. SQInteger start = 0;
  367. sq_getstring(v,2,&str);
  368. if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
  369. if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
  370. _addrexmatch(v,str,begin,end);
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. static SQInteger _regexp_capture(HSQUIRRELVM v)
  376. {
  377. SETUP_REX(v);
  378. const SQChar *str,*begin,*end;
  379. SQInteger start = 0;
  380. sq_getstring(v,2,&str);
  381. if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
  382. if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
  383. SQInteger n = sqstd_rex_getsubexpcount(self);
  384. SQRexMatch match;
  385. sq_newarray(v,0);
  386. for(SQInteger i = 0;i < n; i++) {
  387. sqstd_rex_getsubexp(self,i,&match);
  388. if(match.len > 0)
  389. _addrexmatch(v,str,match.begin,match.begin+match.len);
  390. else
  391. _addrexmatch(v,str,str,str); //empty match
  392. sq_arrayappend(v,-2);
  393. }
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. static SQInteger _regexp_subexpcount(HSQUIRRELVM v)
  399. {
  400. SETUP_REX(v);
  401. sq_pushinteger(v,sqstd_rex_getsubexpcount(self));
  402. return 1;
  403. }
  404. static SQInteger _regexp_constructor(HSQUIRRELVM v)
  405. {
  406. const SQChar *error,*pattern;
  407. sq_getstring(v,2,&pattern);
  408. SQRex *rex = sqstd_rex_compile(pattern,&error);
  409. if(!rex) return sq_throwerror(v,error);
  410. sq_setinstanceup(v,1,rex);
  411. sq_setreleasehook(v,1,_rexobj_releasehook);
  412. return 0;
  413. }
  414. static SQInteger _regexp__typeof(HSQUIRRELVM v)
  415. {
  416. sq_pushstring(v,_SC("regexp"),-1);
  417. return 1;
  418. }
  419. #define _DECL_REX_FUNC(name,nparams,pmask) {_SC(#name),_regexp_##name,nparams,pmask}
  420. static const SQRegFunction rexobj_funcs[]={
  421. _DECL_REX_FUNC(constructor,2,_SC(".s")),
  422. _DECL_REX_FUNC(search,-2,_SC("xsn")),
  423. _DECL_REX_FUNC(match,2,_SC("xs")),
  424. _DECL_REX_FUNC(capture,-2,_SC("xsn")),
  425. _DECL_REX_FUNC(subexpcount,1,_SC("x")),
  426. _DECL_REX_FUNC(_typeof,1,_SC("x")),
  427. {NULL,(SQFUNCTION)0,0,NULL}
  428. };
  429. #undef _DECL_REX_FUNC
  430. #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_string_##name,nparams,pmask}
  431. static const SQRegFunction stringlib_funcs[]={
  432. _DECL_FUNC(format,-2,_SC(".s")),
  433. _DECL_FUNC(strip,2,_SC(".s")),
  434. _DECL_FUNC(lstrip,2,_SC(".s")),
  435. _DECL_FUNC(rstrip,2,_SC(".s")),
  436. _DECL_FUNC(split,3,_SC(".ss")),
  437. _DECL_FUNC(escape,2,_SC(".s")),
  438. _DECL_FUNC(startswith,3,_SC(".ss")),
  439. _DECL_FUNC(endswith,3,_SC(".ss")),
  440. {NULL,(SQFUNCTION)0,0,NULL}
  441. };
  442. #undef _DECL_FUNC
  443. SQInteger sqstd_register_stringlib(HSQUIRRELVM v)
  444. {
  445. sq_pushstring(v,_SC("regexp"),-1);
  446. sq_newclass(v,SQFalse);
  447. SQInteger i = 0;
  448. while(rexobj_funcs[i].name != 0) {
  449. const SQRegFunction &f = rexobj_funcs[i];
  450. sq_pushstring(v,f.name,-1);
  451. sq_newclosure(v,f.f,0);
  452. sq_setparamscheck(v,f.nparamscheck,f.typemask);
  453. sq_setnativeclosurename(v,-1,f.name);
  454. sq_newslot(v,-3,SQFalse);
  455. i++;
  456. }
  457. sq_newslot(v,-3,SQFalse);
  458. i = 0;
  459. while(stringlib_funcs[i].name!=0)
  460. {
  461. sq_pushstring(v,stringlib_funcs[i].name,-1);
  462. sq_newclosure(v,stringlib_funcs[i].f,0);
  463. sq_setparamscheck(v,stringlib_funcs[i].nparamscheck,stringlib_funcs[i].typemask);
  464. sq_setnativeclosurename(v,-1,stringlib_funcs[i].name);
  465. sq_newslot(v,-3,SQFalse);
  466. i++;
  467. }
  468. return 1;
  469. }