The Squirrel interpreter. See http://www.squirrel-lang.org/
sqstdlib/sqstdstring.cpp@0:97a4f8cc534c, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 10:20:34 2014 +0000
- Revision:
- 0:97a4f8cc534c
Initial import of Squirrel.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /* see copyright notice in squirrel.h */ |
jhnwkmn | 0:97a4f8cc534c | 2 | #include <squirrel.h> |
jhnwkmn | 0:97a4f8cc534c | 3 | #include <sqstdstring.h> |
jhnwkmn | 0:97a4f8cc534c | 4 | #include <string.h> |
jhnwkmn | 0:97a4f8cc534c | 5 | #include <stdlib.h> |
jhnwkmn | 0:97a4f8cc534c | 6 | #include <stdio.h> |
jhnwkmn | 0:97a4f8cc534c | 7 | #include <ctype.h> |
jhnwkmn | 0:97a4f8cc534c | 8 | #include <assert.h> |
jhnwkmn | 0:97a4f8cc534c | 9 | |
jhnwkmn | 0:97a4f8cc534c | 10 | #ifdef SQUNICODE |
jhnwkmn | 0:97a4f8cc534c | 11 | #define scstrchr wcschr |
jhnwkmn | 0:97a4f8cc534c | 12 | #define scsnprintf wsnprintf |
jhnwkmn | 0:97a4f8cc534c | 13 | #define scatoi _wtoi |
jhnwkmn | 0:97a4f8cc534c | 14 | #define scstrtok wcstok |
jhnwkmn | 0:97a4f8cc534c | 15 | #else |
jhnwkmn | 0:97a4f8cc534c | 16 | #define scstrchr strchr |
jhnwkmn | 0:97a4f8cc534c | 17 | #define scsnprintf snprintf |
jhnwkmn | 0:97a4f8cc534c | 18 | #define scatoi atoi |
jhnwkmn | 0:97a4f8cc534c | 19 | #define scstrtok strtok |
jhnwkmn | 0:97a4f8cc534c | 20 | #endif |
jhnwkmn | 0:97a4f8cc534c | 21 | #define MAX_FORMAT_LEN 20 |
jhnwkmn | 0:97a4f8cc534c | 22 | #define MAX_WFORMAT_LEN 3 |
jhnwkmn | 0:97a4f8cc534c | 23 | #define ADDITIONAL_FORMAT_SPACE (100*sizeof(SQChar)) |
jhnwkmn | 0:97a4f8cc534c | 24 | |
jhnwkmn | 0:97a4f8cc534c | 25 | static SQInteger validate_format(HSQUIRRELVM v, SQChar *fmt, const SQChar *src, SQInteger n,SQInteger &width) |
jhnwkmn | 0:97a4f8cc534c | 26 | { |
jhnwkmn | 0:97a4f8cc534c | 27 | SQChar swidth[MAX_WFORMAT_LEN]; |
jhnwkmn | 0:97a4f8cc534c | 28 | SQInteger wc = 0; |
jhnwkmn | 0:97a4f8cc534c | 29 | SQInteger start = n; |
jhnwkmn | 0:97a4f8cc534c | 30 | fmt[0] = '%'; |
jhnwkmn | 0:97a4f8cc534c | 31 | while (scstrchr(_SC("-+ #0"), src[n])) n++; |
jhnwkmn | 0:97a4f8cc534c | 32 | while (scisdigit(src[n])) { |
jhnwkmn | 0:97a4f8cc534c | 33 | swidth[wc] = src[n]; |
jhnwkmn | 0:97a4f8cc534c | 34 | n++; |
jhnwkmn | 0:97a4f8cc534c | 35 | wc++; |
jhnwkmn | 0:97a4f8cc534c | 36 | if(wc>=MAX_WFORMAT_LEN) |
jhnwkmn | 0:97a4f8cc534c | 37 | return sq_throwerror(v,_SC("width format too long")); |
jhnwkmn | 0:97a4f8cc534c | 38 | } |
jhnwkmn | 0:97a4f8cc534c | 39 | swidth[wc] = '\0'; |
jhnwkmn | 0:97a4f8cc534c | 40 | if(wc > 0) { |
jhnwkmn | 0:97a4f8cc534c | 41 | width = scatoi(swidth); |
jhnwkmn | 0:97a4f8cc534c | 42 | } |
jhnwkmn | 0:97a4f8cc534c | 43 | else |
jhnwkmn | 0:97a4f8cc534c | 44 | width = 0; |
jhnwkmn | 0:97a4f8cc534c | 45 | if (src[n] == '.') { |
jhnwkmn | 0:97a4f8cc534c | 46 | n++; |
jhnwkmn | 0:97a4f8cc534c | 47 | |
jhnwkmn | 0:97a4f8cc534c | 48 | wc = 0; |
jhnwkmn | 0:97a4f8cc534c | 49 | while (scisdigit(src[n])) { |
jhnwkmn | 0:97a4f8cc534c | 50 | swidth[wc] = src[n]; |
jhnwkmn | 0:97a4f8cc534c | 51 | n++; |
jhnwkmn | 0:97a4f8cc534c | 52 | wc++; |
jhnwkmn | 0:97a4f8cc534c | 53 | if(wc>=MAX_WFORMAT_LEN) |
jhnwkmn | 0:97a4f8cc534c | 54 | return sq_throwerror(v,_SC("precision format too long")); |
jhnwkmn | 0:97a4f8cc534c | 55 | } |
jhnwkmn | 0:97a4f8cc534c | 56 | swidth[wc] = '\0'; |
jhnwkmn | 0:97a4f8cc534c | 57 | if(wc > 0) { |
jhnwkmn | 0:97a4f8cc534c | 58 | width += scatoi(swidth); |
jhnwkmn | 0:97a4f8cc534c | 59 | } |
jhnwkmn | 0:97a4f8cc534c | 60 | } |
jhnwkmn | 0:97a4f8cc534c | 61 | if (n-start > MAX_FORMAT_LEN ) |
jhnwkmn | 0:97a4f8cc534c | 62 | return sq_throwerror(v,_SC("format too long")); |
jhnwkmn | 0:97a4f8cc534c | 63 | memcpy(&fmt[1],&src[start],((n-start)+1)*sizeof(SQChar)); |
jhnwkmn | 0:97a4f8cc534c | 64 | fmt[(n-start)+2] = '\0'; |
jhnwkmn | 0:97a4f8cc534c | 65 | return n; |
jhnwkmn | 0:97a4f8cc534c | 66 | } |
jhnwkmn | 0:97a4f8cc534c | 67 | |
jhnwkmn | 0:97a4f8cc534c | 68 | SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output) |
jhnwkmn | 0:97a4f8cc534c | 69 | { |
jhnwkmn | 0:97a4f8cc534c | 70 | const SQChar *format; |
jhnwkmn | 0:97a4f8cc534c | 71 | SQChar *dest; |
jhnwkmn | 0:97a4f8cc534c | 72 | SQChar fmt[MAX_FORMAT_LEN]; |
jhnwkmn | 0:97a4f8cc534c | 73 | sq_getstring(v,nformatstringidx,&format); |
jhnwkmn | 0:97a4f8cc534c | 74 | SQInteger allocated = (sq_getsize(v,nformatstringidx)+2)*sizeof(SQChar); |
jhnwkmn | 0:97a4f8cc534c | 75 | dest = sq_getscratchpad(v,allocated); |
jhnwkmn | 0:97a4f8cc534c | 76 | SQInteger n = 0,i = 0, nparam = nformatstringidx+1, w = 0; |
jhnwkmn | 0:97a4f8cc534c | 77 | while(format[n] != '\0') { |
jhnwkmn | 0:97a4f8cc534c | 78 | if(format[n] != '%') { |
jhnwkmn | 0:97a4f8cc534c | 79 | assert(i < allocated); |
jhnwkmn | 0:97a4f8cc534c | 80 | dest[i++] = format[n]; |
jhnwkmn | 0:97a4f8cc534c | 81 | n++; |
jhnwkmn | 0:97a4f8cc534c | 82 | } |
jhnwkmn | 0:97a4f8cc534c | 83 | else if(format[n+1] == '%') { //handles %% |
jhnwkmn | 0:97a4f8cc534c | 84 | dest[i++] = '%'; |
jhnwkmn | 0:97a4f8cc534c | 85 | n += 2; |
jhnwkmn | 0:97a4f8cc534c | 86 | } |
jhnwkmn | 0:97a4f8cc534c | 87 | else { |
jhnwkmn | 0:97a4f8cc534c | 88 | n++; |
jhnwkmn | 0:97a4f8cc534c | 89 | if( nparam > sq_gettop(v) ) |
jhnwkmn | 0:97a4f8cc534c | 90 | return sq_throwerror(v,_SC("not enough paramters for the given format string")); |
jhnwkmn | 0:97a4f8cc534c | 91 | n = validate_format(v,fmt,format,n,w); |
jhnwkmn | 0:97a4f8cc534c | 92 | if(n < 0) return -1; |
jhnwkmn | 0:97a4f8cc534c | 93 | SQInteger addlen = 0; |
jhnwkmn | 0:97a4f8cc534c | 94 | SQInteger valtype = 0; |
jhnwkmn | 0:97a4f8cc534c | 95 | const SQChar *ts; |
jhnwkmn | 0:97a4f8cc534c | 96 | SQInteger ti; |
jhnwkmn | 0:97a4f8cc534c | 97 | SQFloat tf; |
jhnwkmn | 0:97a4f8cc534c | 98 | switch(format[n]) { |
jhnwkmn | 0:97a4f8cc534c | 99 | case 's': |
jhnwkmn | 0:97a4f8cc534c | 100 | if(SQ_FAILED(sq_getstring(v,nparam,&ts))) |
jhnwkmn | 0:97a4f8cc534c | 101 | return sq_throwerror(v,_SC("string expected for the specified format")); |
jhnwkmn | 0:97a4f8cc534c | 102 | addlen = (sq_getsize(v,nparam)*sizeof(SQChar))+((w+1)*sizeof(SQChar)); |
jhnwkmn | 0:97a4f8cc534c | 103 | valtype = 's'; |
jhnwkmn | 0:97a4f8cc534c | 104 | break; |
jhnwkmn | 0:97a4f8cc534c | 105 | case 'i': case 'd': case 'o': case 'u': case 'x': case 'X': |
jhnwkmn | 0:97a4f8cc534c | 106 | #ifdef _SQ64 |
jhnwkmn | 0:97a4f8cc534c | 107 | { |
jhnwkmn | 0:97a4f8cc534c | 108 | size_t flen = scstrlen(fmt); |
jhnwkmn | 0:97a4f8cc534c | 109 | SQInteger fpos = flen - 1; |
jhnwkmn | 0:97a4f8cc534c | 110 | SQChar f = fmt[fpos]; |
jhnwkmn | 0:97a4f8cc534c | 111 | SQChar *prec = (SQChar *)_PRINT_INT_PREC; |
jhnwkmn | 0:97a4f8cc534c | 112 | while(*prec != _SC('\0')) { |
jhnwkmn | 0:97a4f8cc534c | 113 | fmt[fpos++] = *prec++; |
jhnwkmn | 0:97a4f8cc534c | 114 | } |
jhnwkmn | 0:97a4f8cc534c | 115 | fmt[fpos++] = f; |
jhnwkmn | 0:97a4f8cc534c | 116 | fmt[fpos++] = _SC('\0'); |
jhnwkmn | 0:97a4f8cc534c | 117 | } |
jhnwkmn | 0:97a4f8cc534c | 118 | #endif |
jhnwkmn | 0:97a4f8cc534c | 119 | case 'c': |
jhnwkmn | 0:97a4f8cc534c | 120 | if(SQ_FAILED(sq_getinteger(v,nparam,&ti))) |
jhnwkmn | 0:97a4f8cc534c | 121 | return sq_throwerror(v,_SC("integer expected for the specified format")); |
jhnwkmn | 0:97a4f8cc534c | 122 | addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar)); |
jhnwkmn | 0:97a4f8cc534c | 123 | valtype = 'i'; |
jhnwkmn | 0:97a4f8cc534c | 124 | break; |
jhnwkmn | 0:97a4f8cc534c | 125 | case 'f': case 'g': case 'G': case 'e': case 'E': |
jhnwkmn | 0:97a4f8cc534c | 126 | if(SQ_FAILED(sq_getfloat(v,nparam,&tf))) |
jhnwkmn | 0:97a4f8cc534c | 127 | return sq_throwerror(v,_SC("float expected for the specified format")); |
jhnwkmn | 0:97a4f8cc534c | 128 | addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar)); |
jhnwkmn | 0:97a4f8cc534c | 129 | valtype = 'f'; |
jhnwkmn | 0:97a4f8cc534c | 130 | break; |
jhnwkmn | 0:97a4f8cc534c | 131 | default: |
jhnwkmn | 0:97a4f8cc534c | 132 | return sq_throwerror(v,_SC("invalid format")); |
jhnwkmn | 0:97a4f8cc534c | 133 | } |
jhnwkmn | 0:97a4f8cc534c | 134 | n++; |
jhnwkmn | 0:97a4f8cc534c | 135 | allocated += addlen + sizeof(SQChar); |
jhnwkmn | 0:97a4f8cc534c | 136 | dest = sq_getscratchpad(v,allocated); |
jhnwkmn | 0:97a4f8cc534c | 137 | switch(valtype) { |
jhnwkmn | 0:97a4f8cc534c | 138 | case 's': i += scsprintf(&dest[i],fmt,ts); break; |
jhnwkmn | 0:97a4f8cc534c | 139 | case 'i': i += scsprintf(&dest[i],fmt,ti); break; |
jhnwkmn | 0:97a4f8cc534c | 140 | case 'f': i += scsprintf(&dest[i],fmt,tf); break; |
jhnwkmn | 0:97a4f8cc534c | 141 | }; |
jhnwkmn | 0:97a4f8cc534c | 142 | nparam ++; |
jhnwkmn | 0:97a4f8cc534c | 143 | } |
jhnwkmn | 0:97a4f8cc534c | 144 | } |
jhnwkmn | 0:97a4f8cc534c | 145 | *outlen = i; |
jhnwkmn | 0:97a4f8cc534c | 146 | dest[i] = '\0'; |
jhnwkmn | 0:97a4f8cc534c | 147 | *output = dest; |
jhnwkmn | 0:97a4f8cc534c | 148 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 149 | } |
jhnwkmn | 0:97a4f8cc534c | 150 | |
jhnwkmn | 0:97a4f8cc534c | 151 | static SQInteger _string_format(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 152 | { |
jhnwkmn | 0:97a4f8cc534c | 153 | SQChar *dest = NULL; |
jhnwkmn | 0:97a4f8cc534c | 154 | SQInteger length = 0; |
jhnwkmn | 0:97a4f8cc534c | 155 | if(SQ_FAILED(sqstd_format(v,2,&length,&dest))) |
jhnwkmn | 0:97a4f8cc534c | 156 | return -1; |
jhnwkmn | 0:97a4f8cc534c | 157 | sq_pushstring(v,dest,length); |
jhnwkmn | 0:97a4f8cc534c | 158 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 159 | } |
jhnwkmn | 0:97a4f8cc534c | 160 | |
jhnwkmn | 0:97a4f8cc534c | 161 | static void __strip_l(const SQChar *str,const SQChar **start) |
jhnwkmn | 0:97a4f8cc534c | 162 | { |
jhnwkmn | 0:97a4f8cc534c | 163 | const SQChar *t = str; |
jhnwkmn | 0:97a4f8cc534c | 164 | while(((*t) != '\0') && scisspace(*t)){ t++; } |
jhnwkmn | 0:97a4f8cc534c | 165 | *start = t; |
jhnwkmn | 0:97a4f8cc534c | 166 | } |
jhnwkmn | 0:97a4f8cc534c | 167 | |
jhnwkmn | 0:97a4f8cc534c | 168 | static void __strip_r(const SQChar *str,SQInteger len,const SQChar **end) |
jhnwkmn | 0:97a4f8cc534c | 169 | { |
jhnwkmn | 0:97a4f8cc534c | 170 | if(len == 0) { |
jhnwkmn | 0:97a4f8cc534c | 171 | *end = str; |
jhnwkmn | 0:97a4f8cc534c | 172 | return; |
jhnwkmn | 0:97a4f8cc534c | 173 | } |
jhnwkmn | 0:97a4f8cc534c | 174 | const SQChar *t = &str[len-1]; |
jhnwkmn | 0:97a4f8cc534c | 175 | while(t >= str && scisspace(*t)) { t--; } |
jhnwkmn | 0:97a4f8cc534c | 176 | *end = t + 1; |
jhnwkmn | 0:97a4f8cc534c | 177 | } |
jhnwkmn | 0:97a4f8cc534c | 178 | |
jhnwkmn | 0:97a4f8cc534c | 179 | static SQInteger _string_strip(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 180 | { |
jhnwkmn | 0:97a4f8cc534c | 181 | const SQChar *str,*start,*end; |
jhnwkmn | 0:97a4f8cc534c | 182 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 183 | SQInteger len = sq_getsize(v,2); |
jhnwkmn | 0:97a4f8cc534c | 184 | __strip_l(str,&start); |
jhnwkmn | 0:97a4f8cc534c | 185 | __strip_r(str,len,&end); |
jhnwkmn | 0:97a4f8cc534c | 186 | sq_pushstring(v,start,end - start); |
jhnwkmn | 0:97a4f8cc534c | 187 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 188 | } |
jhnwkmn | 0:97a4f8cc534c | 189 | |
jhnwkmn | 0:97a4f8cc534c | 190 | static SQInteger _string_lstrip(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 191 | { |
jhnwkmn | 0:97a4f8cc534c | 192 | const SQChar *str,*start; |
jhnwkmn | 0:97a4f8cc534c | 193 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 194 | __strip_l(str,&start); |
jhnwkmn | 0:97a4f8cc534c | 195 | sq_pushstring(v,start,-1); |
jhnwkmn | 0:97a4f8cc534c | 196 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 197 | } |
jhnwkmn | 0:97a4f8cc534c | 198 | |
jhnwkmn | 0:97a4f8cc534c | 199 | static SQInteger _string_rstrip(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 200 | { |
jhnwkmn | 0:97a4f8cc534c | 201 | const SQChar *str,*end; |
jhnwkmn | 0:97a4f8cc534c | 202 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 203 | SQInteger len = sq_getsize(v,2); |
jhnwkmn | 0:97a4f8cc534c | 204 | __strip_r(str,len,&end); |
jhnwkmn | 0:97a4f8cc534c | 205 | sq_pushstring(v,str,end - str); |
jhnwkmn | 0:97a4f8cc534c | 206 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 207 | } |
jhnwkmn | 0:97a4f8cc534c | 208 | |
jhnwkmn | 0:97a4f8cc534c | 209 | static SQInteger _string_split(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 210 | { |
jhnwkmn | 0:97a4f8cc534c | 211 | const SQChar *str,*seps; |
jhnwkmn | 0:97a4f8cc534c | 212 | SQChar *stemp,*tok; |
jhnwkmn | 0:97a4f8cc534c | 213 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 214 | sq_getstring(v,3,&seps); |
jhnwkmn | 0:97a4f8cc534c | 215 | if(sq_getsize(v,3) == 0) return sq_throwerror(v,_SC("empty separators string")); |
jhnwkmn | 0:97a4f8cc534c | 216 | SQInteger memsize = (sq_getsize(v,2)+1)*sizeof(SQChar); |
jhnwkmn | 0:97a4f8cc534c | 217 | stemp = sq_getscratchpad(v,memsize); |
jhnwkmn | 0:97a4f8cc534c | 218 | memcpy(stemp,str,memsize); |
jhnwkmn | 0:97a4f8cc534c | 219 | tok = scstrtok(stemp,seps); |
jhnwkmn | 0:97a4f8cc534c | 220 | sq_newarray(v,0); |
jhnwkmn | 0:97a4f8cc534c | 221 | while( tok != NULL ) { |
jhnwkmn | 0:97a4f8cc534c | 222 | sq_pushstring(v,tok,-1); |
jhnwkmn | 0:97a4f8cc534c | 223 | sq_arrayappend(v,-2); |
jhnwkmn | 0:97a4f8cc534c | 224 | tok = scstrtok( NULL, seps ); |
jhnwkmn | 0:97a4f8cc534c | 225 | } |
jhnwkmn | 0:97a4f8cc534c | 226 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 227 | } |
jhnwkmn | 0:97a4f8cc534c | 228 | |
jhnwkmn | 0:97a4f8cc534c | 229 | #define SETUP_REX(v) \ |
jhnwkmn | 0:97a4f8cc534c | 230 | SQRex *self = NULL; \ |
jhnwkmn | 0:97a4f8cc534c | 231 | sq_getinstanceup(v,1,(SQUserPointer *)&self,0); |
jhnwkmn | 0:97a4f8cc534c | 232 | |
jhnwkmn | 0:97a4f8cc534c | 233 | static SQInteger _rexobj_releasehook(SQUserPointer p, SQInteger size) |
jhnwkmn | 0:97a4f8cc534c | 234 | { |
jhnwkmn | 0:97a4f8cc534c | 235 | SQRex *self = ((SQRex *)p); |
jhnwkmn | 0:97a4f8cc534c | 236 | sqstd_rex_free(self); |
jhnwkmn | 0:97a4f8cc534c | 237 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 238 | } |
jhnwkmn | 0:97a4f8cc534c | 239 | |
jhnwkmn | 0:97a4f8cc534c | 240 | static SQInteger _regexp_match(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 241 | { |
jhnwkmn | 0:97a4f8cc534c | 242 | SETUP_REX(v); |
jhnwkmn | 0:97a4f8cc534c | 243 | const SQChar *str; |
jhnwkmn | 0:97a4f8cc534c | 244 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 245 | if(sqstd_rex_match(self,str) == SQTrue) |
jhnwkmn | 0:97a4f8cc534c | 246 | { |
jhnwkmn | 0:97a4f8cc534c | 247 | sq_pushbool(v,SQTrue); |
jhnwkmn | 0:97a4f8cc534c | 248 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 249 | } |
jhnwkmn | 0:97a4f8cc534c | 250 | sq_pushbool(v,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 251 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 252 | } |
jhnwkmn | 0:97a4f8cc534c | 253 | |
jhnwkmn | 0:97a4f8cc534c | 254 | static void _addrexmatch(HSQUIRRELVM v,const SQChar *str,const SQChar *begin,const SQChar *end) |
jhnwkmn | 0:97a4f8cc534c | 255 | { |
jhnwkmn | 0:97a4f8cc534c | 256 | sq_newtable(v); |
jhnwkmn | 0:97a4f8cc534c | 257 | sq_pushstring(v,_SC("begin"),-1); |
jhnwkmn | 0:97a4f8cc534c | 258 | sq_pushinteger(v,begin - str); |
jhnwkmn | 0:97a4f8cc534c | 259 | sq_rawset(v,-3); |
jhnwkmn | 0:97a4f8cc534c | 260 | sq_pushstring(v,_SC("end"),-1); |
jhnwkmn | 0:97a4f8cc534c | 261 | sq_pushinteger(v,end - str); |
jhnwkmn | 0:97a4f8cc534c | 262 | sq_rawset(v,-3); |
jhnwkmn | 0:97a4f8cc534c | 263 | } |
jhnwkmn | 0:97a4f8cc534c | 264 | |
jhnwkmn | 0:97a4f8cc534c | 265 | static SQInteger _regexp_search(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 266 | { |
jhnwkmn | 0:97a4f8cc534c | 267 | SETUP_REX(v); |
jhnwkmn | 0:97a4f8cc534c | 268 | const SQChar *str,*begin,*end; |
jhnwkmn | 0:97a4f8cc534c | 269 | SQInteger start = 0; |
jhnwkmn | 0:97a4f8cc534c | 270 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 271 | if(sq_gettop(v) > 2) sq_getinteger(v,3,&start); |
jhnwkmn | 0:97a4f8cc534c | 272 | if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) { |
jhnwkmn | 0:97a4f8cc534c | 273 | _addrexmatch(v,str,begin,end); |
jhnwkmn | 0:97a4f8cc534c | 274 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 275 | } |
jhnwkmn | 0:97a4f8cc534c | 276 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 277 | } |
jhnwkmn | 0:97a4f8cc534c | 278 | |
jhnwkmn | 0:97a4f8cc534c | 279 | static SQInteger _regexp_capture(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 280 | { |
jhnwkmn | 0:97a4f8cc534c | 281 | SETUP_REX(v); |
jhnwkmn | 0:97a4f8cc534c | 282 | const SQChar *str,*begin,*end; |
jhnwkmn | 0:97a4f8cc534c | 283 | SQInteger start = 0; |
jhnwkmn | 0:97a4f8cc534c | 284 | sq_getstring(v,2,&str); |
jhnwkmn | 0:97a4f8cc534c | 285 | if(sq_gettop(v) > 2) sq_getinteger(v,3,&start); |
jhnwkmn | 0:97a4f8cc534c | 286 | if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) { |
jhnwkmn | 0:97a4f8cc534c | 287 | SQInteger n = sqstd_rex_getsubexpcount(self); |
jhnwkmn | 0:97a4f8cc534c | 288 | SQRexMatch match; |
jhnwkmn | 0:97a4f8cc534c | 289 | sq_newarray(v,0); |
jhnwkmn | 0:97a4f8cc534c | 290 | for(SQInteger i = 0;i < n; i++) { |
jhnwkmn | 0:97a4f8cc534c | 291 | sqstd_rex_getsubexp(self,i,&match); |
jhnwkmn | 0:97a4f8cc534c | 292 | if(match.len > 0) |
jhnwkmn | 0:97a4f8cc534c | 293 | _addrexmatch(v,str,match.begin,match.begin+match.len); |
jhnwkmn | 0:97a4f8cc534c | 294 | else |
jhnwkmn | 0:97a4f8cc534c | 295 | _addrexmatch(v,str,str,str); //empty match |
jhnwkmn | 0:97a4f8cc534c | 296 | sq_arrayappend(v,-2); |
jhnwkmn | 0:97a4f8cc534c | 297 | } |
jhnwkmn | 0:97a4f8cc534c | 298 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 299 | } |
jhnwkmn | 0:97a4f8cc534c | 300 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 301 | } |
jhnwkmn | 0:97a4f8cc534c | 302 | |
jhnwkmn | 0:97a4f8cc534c | 303 | static SQInteger _regexp_subexpcount(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 304 | { |
jhnwkmn | 0:97a4f8cc534c | 305 | SETUP_REX(v); |
jhnwkmn | 0:97a4f8cc534c | 306 | sq_pushinteger(v,sqstd_rex_getsubexpcount(self)); |
jhnwkmn | 0:97a4f8cc534c | 307 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 308 | } |
jhnwkmn | 0:97a4f8cc534c | 309 | |
jhnwkmn | 0:97a4f8cc534c | 310 | static SQInteger _regexp_constructor(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 311 | { |
jhnwkmn | 0:97a4f8cc534c | 312 | const SQChar *error,*pattern; |
jhnwkmn | 0:97a4f8cc534c | 313 | sq_getstring(v,2,&pattern); |
jhnwkmn | 0:97a4f8cc534c | 314 | SQRex *rex = sqstd_rex_compile(pattern,&error); |
jhnwkmn | 0:97a4f8cc534c | 315 | if(!rex) return sq_throwerror(v,error); |
jhnwkmn | 0:97a4f8cc534c | 316 | sq_setinstanceup(v,1,rex); |
jhnwkmn | 0:97a4f8cc534c | 317 | sq_setreleasehook(v,1,_rexobj_releasehook); |
jhnwkmn | 0:97a4f8cc534c | 318 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 319 | } |
jhnwkmn | 0:97a4f8cc534c | 320 | |
jhnwkmn | 0:97a4f8cc534c | 321 | static SQInteger _regexp__typeof(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 322 | { |
jhnwkmn | 0:97a4f8cc534c | 323 | sq_pushstring(v,_SC("regexp"),-1); |
jhnwkmn | 0:97a4f8cc534c | 324 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 325 | } |
jhnwkmn | 0:97a4f8cc534c | 326 | |
jhnwkmn | 0:97a4f8cc534c | 327 | #define _DECL_REX_FUNC(name,nparams,pmask) {_SC(#name),_regexp_##name,nparams,pmask} |
jhnwkmn | 0:97a4f8cc534c | 328 | static SQRegFunction rexobj_funcs[]={ |
jhnwkmn | 0:97a4f8cc534c | 329 | _DECL_REX_FUNC(constructor,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 330 | _DECL_REX_FUNC(search,-2,_SC("xsn")), |
jhnwkmn | 0:97a4f8cc534c | 331 | _DECL_REX_FUNC(match,2,_SC("xs")), |
jhnwkmn | 0:97a4f8cc534c | 332 | _DECL_REX_FUNC(capture,-2,_SC("xsn")), |
jhnwkmn | 0:97a4f8cc534c | 333 | _DECL_REX_FUNC(subexpcount,1,_SC("x")), |
jhnwkmn | 0:97a4f8cc534c | 334 | _DECL_REX_FUNC(_typeof,1,_SC("x")), |
jhnwkmn | 0:97a4f8cc534c | 335 | {0,0} |
jhnwkmn | 0:97a4f8cc534c | 336 | }; |
jhnwkmn | 0:97a4f8cc534c | 337 | #undef _DECL_REX_FUNC |
jhnwkmn | 0:97a4f8cc534c | 338 | |
jhnwkmn | 0:97a4f8cc534c | 339 | #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_string_##name,nparams,pmask} |
jhnwkmn | 0:97a4f8cc534c | 340 | static SQRegFunction stringlib_funcs[]={ |
jhnwkmn | 0:97a4f8cc534c | 341 | _DECL_FUNC(format,-2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 342 | _DECL_FUNC(strip,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 343 | _DECL_FUNC(lstrip,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 344 | _DECL_FUNC(rstrip,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 345 | _DECL_FUNC(split,3,_SC(".ss")), |
jhnwkmn | 0:97a4f8cc534c | 346 | {0,0} |
jhnwkmn | 0:97a4f8cc534c | 347 | }; |
jhnwkmn | 0:97a4f8cc534c | 348 | #undef _DECL_FUNC |
jhnwkmn | 0:97a4f8cc534c | 349 | |
jhnwkmn | 0:97a4f8cc534c | 350 | |
jhnwkmn | 0:97a4f8cc534c | 351 | SQInteger sqstd_register_stringlib(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 352 | { |
jhnwkmn | 0:97a4f8cc534c | 353 | sq_pushstring(v,_SC("regexp"),-1); |
jhnwkmn | 0:97a4f8cc534c | 354 | sq_newclass(v,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 355 | SQInteger i = 0; |
jhnwkmn | 0:97a4f8cc534c | 356 | while(rexobj_funcs[i].name != 0) { |
jhnwkmn | 0:97a4f8cc534c | 357 | SQRegFunction &f = rexobj_funcs[i]; |
jhnwkmn | 0:97a4f8cc534c | 358 | sq_pushstring(v,f.name,-1); |
jhnwkmn | 0:97a4f8cc534c | 359 | sq_newclosure(v,f.f,0); |
jhnwkmn | 0:97a4f8cc534c | 360 | sq_setparamscheck(v,f.nparamscheck,f.typemask); |
jhnwkmn | 0:97a4f8cc534c | 361 | sq_setnativeclosurename(v,-1,f.name); |
jhnwkmn | 0:97a4f8cc534c | 362 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 363 | i++; |
jhnwkmn | 0:97a4f8cc534c | 364 | } |
jhnwkmn | 0:97a4f8cc534c | 365 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 366 | |
jhnwkmn | 0:97a4f8cc534c | 367 | i = 0; |
jhnwkmn | 0:97a4f8cc534c | 368 | while(stringlib_funcs[i].name!=0) |
jhnwkmn | 0:97a4f8cc534c | 369 | { |
jhnwkmn | 0:97a4f8cc534c | 370 | sq_pushstring(v,stringlib_funcs[i].name,-1); |
jhnwkmn | 0:97a4f8cc534c | 371 | sq_newclosure(v,stringlib_funcs[i].f,0); |
jhnwkmn | 0:97a4f8cc534c | 372 | sq_setparamscheck(v,stringlib_funcs[i].nparamscheck,stringlib_funcs[i].typemask); |
jhnwkmn | 0:97a4f8cc534c | 373 | sq_setnativeclosurename(v,-1,stringlib_funcs[i].name); |
jhnwkmn | 0:97a4f8cc534c | 374 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 375 | i++; |
jhnwkmn | 0:97a4f8cc534c | 376 | } |
jhnwkmn | 0:97a4f8cc534c | 377 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 378 | } |