sqstdstring.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 = (sq_getsize(v,nformatstringidx)+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;
  92. SQInteger ti;
  93. SQFloat tf;
  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. SQChar *prec = (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. SQInteger destcharsize = (size * 6); //assumes every char could be escaped
  247. resstr = dest = (SQChar *)sq_getscratchpad(v,destcharsize * sizeof(SQChar));
  248. SQChar c;
  249. SQChar escch;
  250. SQInteger escaped = 0;
  251. for(int n = 0; n < size; n++){
  252. c = *str++;
  253. escch = 0;
  254. if(scisprint(c) || c == 0) {
  255. switch(c) {
  256. case '\a': escch = 'a'; break;
  257. case '\b': escch = 'b'; break;
  258. case '\t': escch = 't'; break;
  259. case '\n': escch = 'n'; break;
  260. case '\v': escch = 'v'; break;
  261. case '\f': escch = 'f'; break;
  262. case '\r': escch = 'r'; break;
  263. case '\\': escch = '\\'; break;
  264. case '\"': escch = '\"'; break;
  265. case '\'': escch = '\''; break;
  266. case 0: escch = '0'; break;
  267. }
  268. if(escch) {
  269. *dest++ = '\\';
  270. *dest++ = escch;
  271. escaped++;
  272. }
  273. else {
  274. *dest++ = c;
  275. }
  276. }
  277. else {
  278. dest += scsprintf(dest,destcharsize,_SC("\\x%x"),c);
  279. escaped++;
  280. }
  281. }
  282. if(escaped) {
  283. sq_pushstring(v,resstr,dest - resstr);
  284. }
  285. else {
  286. sq_push(v,2); //nothing escaped
  287. }
  288. return 1;
  289. }
  290. #define SETUP_REX(v) \
  291. SQRex *self = NULL; \
  292. sq_getinstanceup(v,1,(SQUserPointer *)&self,0);
  293. static SQInteger _rexobj_releasehook(SQUserPointer p, SQInteger size)
  294. {
  295. SQRex *self = ((SQRex *)p);
  296. sqstd_rex_free(self);
  297. return 1;
  298. }
  299. static SQInteger _regexp_match(HSQUIRRELVM v)
  300. {
  301. SETUP_REX(v);
  302. const SQChar *str;
  303. sq_getstring(v,2,&str);
  304. if(sqstd_rex_match(self,str) == SQTrue)
  305. {
  306. sq_pushbool(v,SQTrue);
  307. return 1;
  308. }
  309. sq_pushbool(v,SQFalse);
  310. return 1;
  311. }
  312. static void _addrexmatch(HSQUIRRELVM v,const SQChar *str,const SQChar *begin,const SQChar *end)
  313. {
  314. sq_newtable(v);
  315. sq_pushstring(v,_SC("begin"),-1);
  316. sq_pushinteger(v,begin - str);
  317. sq_rawset(v,-3);
  318. sq_pushstring(v,_SC("end"),-1);
  319. sq_pushinteger(v,end - str);
  320. sq_rawset(v,-3);
  321. }
  322. static SQInteger _regexp_search(HSQUIRRELVM v)
  323. {
  324. SETUP_REX(v);
  325. const SQChar *str,*begin,*end;
  326. SQInteger start = 0;
  327. sq_getstring(v,2,&str);
  328. if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
  329. if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
  330. _addrexmatch(v,str,begin,end);
  331. return 1;
  332. }
  333. return 0;
  334. }
  335. static SQInteger _regexp_capture(HSQUIRRELVM v)
  336. {
  337. SETUP_REX(v);
  338. const SQChar *str,*begin,*end;
  339. SQInteger start = 0;
  340. sq_getstring(v,2,&str);
  341. if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
  342. if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
  343. SQInteger n = sqstd_rex_getsubexpcount(self);
  344. SQRexMatch match;
  345. sq_newarray(v,0);
  346. for(SQInteger i = 0;i < n; i++) {
  347. sqstd_rex_getsubexp(self,i,&match);
  348. if(match.len > 0)
  349. _addrexmatch(v,str,match.begin,match.begin+match.len);
  350. else
  351. _addrexmatch(v,str,str,str); //empty match
  352. sq_arrayappend(v,-2);
  353. }
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. static SQInteger _regexp_subexpcount(HSQUIRRELVM v)
  359. {
  360. SETUP_REX(v);
  361. sq_pushinteger(v,sqstd_rex_getsubexpcount(self));
  362. return 1;
  363. }
  364. static SQInteger _regexp_constructor(HSQUIRRELVM v)
  365. {
  366. const SQChar *error,*pattern;
  367. sq_getstring(v,2,&pattern);
  368. SQRex *rex = sqstd_rex_compile(pattern,&error);
  369. if(!rex) return sq_throwerror(v,error);
  370. sq_setinstanceup(v,1,rex);
  371. sq_setreleasehook(v,1,_rexobj_releasehook);
  372. return 0;
  373. }
  374. static SQInteger _regexp__typeof(HSQUIRRELVM v)
  375. {
  376. sq_pushstring(v,_SC("regexp"),-1);
  377. return 1;
  378. }
  379. #define _DECL_REX_FUNC(name,nparams,pmask) {_SC(#name),_regexp_##name,nparams,pmask}
  380. static SQRegFunction rexobj_funcs[]={
  381. _DECL_REX_FUNC(constructor,2,_SC(".s")),
  382. _DECL_REX_FUNC(search,-2,_SC("xsn")),
  383. _DECL_REX_FUNC(match,2,_SC("xs")),
  384. _DECL_REX_FUNC(capture,-2,_SC("xsn")),
  385. _DECL_REX_FUNC(subexpcount,1,_SC("x")),
  386. _DECL_REX_FUNC(_typeof,1,_SC("x")),
  387. {0,0}
  388. };
  389. #undef _DECL_REX_FUNC
  390. #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_string_##name,nparams,pmask}
  391. static SQRegFunction stringlib_funcs[]={
  392. _DECL_FUNC(format,-2,_SC(".s")),
  393. _DECL_FUNC(strip,2,_SC(".s")),
  394. _DECL_FUNC(lstrip,2,_SC(".s")),
  395. _DECL_FUNC(rstrip,2,_SC(".s")),
  396. _DECL_FUNC(split,3,_SC(".ss")),
  397. _DECL_FUNC(escape,2,_SC(".s")),
  398. {0,0}
  399. };
  400. #undef _DECL_FUNC
  401. SQInteger sqstd_register_stringlib(HSQUIRRELVM v)
  402. {
  403. sq_pushstring(v,_SC("regexp"),-1);
  404. sq_newclass(v,SQFalse);
  405. SQInteger i = 0;
  406. while(rexobj_funcs[i].name != 0) {
  407. SQRegFunction &f = rexobj_funcs[i];
  408. sq_pushstring(v,f.name,-1);
  409. sq_newclosure(v,f.f,0);
  410. sq_setparamscheck(v,f.nparamscheck,f.typemask);
  411. sq_setnativeclosurename(v,-1,f.name);
  412. sq_newslot(v,-3,SQFalse);
  413. i++;
  414. }
  415. sq_newslot(v,-3,SQFalse);
  416. i = 0;
  417. while(stringlib_funcs[i].name!=0)
  418. {
  419. sq_pushstring(v,stringlib_funcs[i].name,-1);
  420. sq_newclosure(v,stringlib_funcs[i].f,0);
  421. sq_setparamscheck(v,stringlib_funcs[i].nparamscheck,stringlib_funcs[i].typemask);
  422. sq_setnativeclosurename(v,-1,stringlib_funcs[i].name);
  423. sq_newslot(v,-3,SQFalse);
  424. i++;
  425. }
  426. return 1;
  427. }