| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <string.h> |
|---|
| 14 | |
|---|
| 15 | #include "config.h" |
|---|
| 16 | |
|---|
| 17 | #include "NewtCore.h" |
|---|
| 18 | #include "NewtStr.h" |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | static newtRef NewtParamStr(char * baseStr, size_t baseStrLen, newtRefArg paramStrArray, bool ifthen); |
|---|
| 23 | static bool NewtBeginsWith(const char * str, const char * sub); |
|---|
| 24 | static bool NewtEndsWith(const char * str, const char * sub); |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #if 0 |
|---|
| 28 | #pragma mark - |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | newtRef NewtParamStr(char * baseStr, size_t baseStrLen, newtRefArg paramStrArray, bool ifthen) |
|---|
| 42 | { |
|---|
| 43 | newtRefVar dstStr; |
|---|
| 44 | newtRefVar r; |
|---|
| 45 | size_t fpos = 0; |
|---|
| 46 | size_t fst = 0; |
|---|
| 47 | size_t len; |
|---|
| 48 | size_t n; |
|---|
| 49 | size_t truePos; |
|---|
| 50 | size_t trueLen; |
|---|
| 51 | size_t falsePos; |
|---|
| 52 | size_t falseLen; |
|---|
| 53 | char * found; |
|---|
| 54 | char c; |
|---|
| 55 | |
|---|
| 56 | dstStr = NewtMakeString("", false); |
|---|
| 57 | |
|---|
| 58 | do |
|---|
| 59 | { |
|---|
| 60 | found = memchr(&baseStr[fst], '^', baseStrLen - fst); |
|---|
| 61 | |
|---|
| 62 | if (found == NULL) |
|---|
| 63 | break; |
|---|
| 64 | |
|---|
| 65 | c = found[1]; |
|---|
| 66 | |
|---|
| 67 | len = found - (baseStr + fpos); |
|---|
| 68 | NewtStrCat2(dstStr, &baseStr[fpos], len); |
|---|
| 69 | fpos += len; |
|---|
| 70 | fst = fpos + 1; |
|---|
| 71 | |
|---|
| 72 | if ('0' <= c && c <= '9') |
|---|
| 73 | { |
|---|
| 74 | fpos += 2; |
|---|
| 75 | fst = fpos; |
|---|
| 76 | |
|---|
| 77 | r = NewtGetArraySlot(paramStrArray, c - '0'); |
|---|
| 78 | |
|---|
| 79 | if (NewtRefIsNotNIL(r)) |
|---|
| 80 | { |
|---|
| 81 | NcStrCat(dstStr, r); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | else if (ifthen && c == '?') |
|---|
| 85 | { |
|---|
| 86 | c = found[2]; |
|---|
| 87 | |
|---|
| 88 | if ('0' <= c && c <= '9') |
|---|
| 89 | { |
|---|
| 90 | n = c - '0'; |
|---|
| 91 | |
|---|
| 92 | truePos = fpos + 3; |
|---|
| 93 | found = memchr(&baseStr[truePos], '|', baseStrLen - truePos); |
|---|
| 94 | |
|---|
| 95 | if (found != NULL) |
|---|
| 96 | { |
|---|
| 97 | falsePos = found + 1 - baseStr; |
|---|
| 98 | trueLen = falsePos - truePos - 1; |
|---|
| 99 | |
|---|
| 100 | found = memchr(&baseStr[falsePos], '|', baseStrLen - falsePos); |
|---|
| 101 | |
|---|
| 102 | if (found != NULL) |
|---|
| 103 | { |
|---|
| 104 | fpos = found + 1 - baseStr; |
|---|
| 105 | } |
|---|
| 106 | else |
|---|
| 107 | { |
|---|
| 108 | fpos = baseStrLen; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | falseLen = fpos - falsePos - 1; |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | { |
|---|
| 115 | trueLen = baseStrLen - truePos; |
|---|
| 116 | |
|---|
| 117 | falsePos = baseStrLen; |
|---|
| 118 | falseLen = 0; |
|---|
| 119 | |
|---|
| 120 | fpos = baseStrLen; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | fst = fpos; |
|---|
| 124 | |
|---|
| 125 | r = NewtGetArraySlot(paramStrArray, n); |
|---|
| 126 | |
|---|
| 127 | if (NewtRefIsNotNIL(r)) |
|---|
| 128 | { |
|---|
| 129 | r = NewtParamStr(&baseStr[truePos], trueLen, paramStrArray, false); |
|---|
| 130 | NcStrCat(dstStr, r); |
|---|
| 131 | } |
|---|
| 132 | else |
|---|
| 133 | { |
|---|
| 134 | if (falsePos == baseStrLen) |
|---|
| 135 | break; |
|---|
| 136 | |
|---|
| 137 | r = NewtParamStr(&baseStr[falsePos], falseLen, paramStrArray, false); |
|---|
| 138 | NcStrCat(dstStr, r); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | } while(true); |
|---|
| 143 | |
|---|
| 144 | len = baseStrLen - fpos; |
|---|
| 145 | |
|---|
| 146 | if (0 < len) |
|---|
| 147 | { |
|---|
| 148 | NewtStrCat2(dstStr, &baseStr[fpos], len); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return dstStr; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | bool NewtBeginsWith(const char * str, const char * sub) |
|---|
| 166 | { |
|---|
| 167 | int32_t len; |
|---|
| 168 | int32_t sublen; |
|---|
| 169 | |
|---|
| 170 | len = strlen(str); |
|---|
| 171 | sublen = strlen(sub); |
|---|
| 172 | |
|---|
| 173 | if (len < sublen) |
|---|
| 174 | return false; |
|---|
| 175 | else |
|---|
| 176 | return (strncasecmp(str, sub, sublen) == 0); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | bool NewtEndsWith(const char * str, const char * sub) |
|---|
| 191 | { |
|---|
| 192 | int32_t st; |
|---|
| 193 | |
|---|
| 194 | st = strlen(str) - strlen(sub); |
|---|
| 195 | |
|---|
| 196 | if (st < 0) |
|---|
| 197 | return false; |
|---|
| 198 | else |
|---|
| 199 | return (strcasecmp(str + st, sub) == 0); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | #if 0 |
|---|
| 204 | #pragma mark - |
|---|
| 205 | #endif |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | newtRef NsChr(newtRefArg rcvr, newtRefArg r) |
|---|
| 218 | { |
|---|
| 219 | if (! NewtRefIsInteger(r)) |
|---|
| 220 | return NewtThrow(kNErrNotAnInteger, r); |
|---|
| 221 | |
|---|
| 222 | return NewtMakeCharacter(NewtRefToInteger(r)); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | newtRef NsOrd(newtRefArg rcvr, newtRefArg r) |
|---|
| 238 | { |
|---|
| 239 | if (! NewtRefIsCharacter(r)) |
|---|
| 240 | return NewtThrow(kNErrNotAnInteger, r); |
|---|
| 241 | |
|---|
| 242 | return NewtMakeInteger(NewtRefToCharacter(r)); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | #if 0 |
|---|
| 247 | #pragma mark - |
|---|
| 248 | #endif |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | newtRef NsStrLen(newtRefArg rcvr, newtRefArg r) |
|---|
| 261 | { |
|---|
| 262 | if (! NewtRefIsString(r)) |
|---|
| 263 | return NewtThrow(kNErrNotAString, r); |
|---|
| 264 | |
|---|
| 265 | return NewtMakeInteger(NewtStringLength(r)); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | newtRef NsSPrintObject(newtRefArg rcvr, newtRefArg r) |
|---|
| 281 | { |
|---|
| 282 | newtRefVar str; |
|---|
| 283 | |
|---|
| 284 | str = NSSTR(""); |
|---|
| 285 | |
|---|
| 286 | NcStrCat(str, r); |
|---|
| 287 | |
|---|
| 288 | return str; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | newtRef NsSplit(newtRefArg rcvr, newtRefArg r, newtRefArg sep) |
|---|
| 305 | { |
|---|
| 306 | newtRefVar result; |
|---|
| 307 | |
|---|
| 308 | if (! NewtRefIsString(r)) |
|---|
| 309 | return NewtThrow(kNErrNotAString, r); |
|---|
| 310 | |
|---|
| 311 | switch (NewtGetRefType(sep, true)) |
|---|
| 312 | { |
|---|
| 313 | case kNewtCharacter: |
|---|
| 314 | { |
|---|
| 315 | newtRefVar v; |
|---|
| 316 | char * next; |
|---|
| 317 | char * s; |
|---|
| 318 | int c; |
|---|
| 319 | |
|---|
| 320 | s = NewtRefToString(r); |
|---|
| 321 | c = NewtRefToCharacter(sep); |
|---|
| 322 | |
|---|
| 323 | result = NewtMakeArray(kNewtRefUnbind, 0); |
|---|
| 324 | |
|---|
| 325 | while (*s) |
|---|
| 326 | { |
|---|
| 327 | next = strchr(s, c); |
|---|
| 328 | if (next == NULL) break; |
|---|
| 329 | |
|---|
| 330 | v = NewtMakeString2(s, next - s, false); |
|---|
| 331 | NcAddArraySlot(result, v); |
|---|
| 332 | s = next + 1; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | if (s == NewtRefToString(r)) |
|---|
| 336 | v = r; |
|---|
| 337 | else |
|---|
| 338 | v = NSSTR(s); |
|---|
| 339 | |
|---|
| 340 | NcAddArraySlot(result, v); |
|---|
| 341 | } |
|---|
| 342 | break; |
|---|
| 343 | |
|---|
| 344 | default: |
|---|
| 345 | { |
|---|
| 346 | newtRefVar initObj[] = {r}; |
|---|
| 347 | |
|---|
| 348 | result = NewtMakeArray2(kNewtRefNIL, sizeof(initObj) / sizeof(newtRefVar), initObj); |
|---|
| 349 | } |
|---|
| 350 | break; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | return result; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | newtRef NsParamStr(newtRefArg rcvr, newtRefArg baseString, newtRefArg paramStrArray) |
|---|
| 370 | { |
|---|
| 371 | if (! NewtRefIsString(baseString)) |
|---|
| 372 | return NewtThrow(kNErrNotAString, baseString); |
|---|
| 373 | |
|---|
| 374 | if (! NewtRefIsArray(paramStrArray)) |
|---|
| 375 | return NewtThrow(kNErrNotAnArray, paramStrArray); |
|---|
| 376 | |
|---|
| 377 | return NewtParamStr(NewtRefToString(baseString), NewtStringLength(baseString), paramStrArray, true); |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | newtRef NsSubStr(newtRefArg rcvr, newtRefArg r, newtRefArg start, newtRefArg count) |
|---|
| 394 | { |
|---|
| 395 | char* theString; |
|---|
| 396 | char* theBuffer; |
|---|
| 397 | int theStart, theEnd; |
|---|
| 398 | size_t theLen; |
|---|
| 399 | newtRefVar theResult; |
|---|
| 400 | |
|---|
| 401 | (void) rcvr; |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | if (! NewtRefIsString(r)) |
|---|
| 405 | return NewtThrow(kNErrNotAString, r); |
|---|
| 406 | if (! NewtRefIsInteger(start)) |
|---|
| 407 | return NewtThrow(kNErrNotAnInteger, start); |
|---|
| 408 | |
|---|
| 409 | theString = NewtRefToString(r); |
|---|
| 410 | theLen = strlen(theString); |
|---|
| 411 | |
|---|
| 412 | theStart = NewtRefToInteger(start); |
|---|
| 413 | |
|---|
| 414 | if (!NewtRefIsNIL(count)) |
|---|
| 415 | { |
|---|
| 416 | if (!NewtRefIsInteger(count)) |
|---|
| 417 | { |
|---|
| 418 | return NewtThrow(kNErrNotAnInteger, count); |
|---|
| 419 | } |
|---|
| 420 | theEnd = theStart + NewtRefToInteger(count); |
|---|
| 421 | if (theEnd > theLen) |
|---|
| 422 | { |
|---|
| 423 | return NewtThrow(kNErrOutOfRange, count); |
|---|
| 424 | } |
|---|
| 425 | } else { |
|---|
| 426 | theEnd = theLen; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | theLen = theEnd - theStart; |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | theBuffer = (char*) malloc(theLen + 1); |
|---|
| 434 | (void) memcpy(theBuffer, (const char*) &theString[theStart], theLen); |
|---|
| 435 | theBuffer[theLen] = 0; |
|---|
| 436 | theResult = NewtMakeString(theBuffer, false); |
|---|
| 437 | free(theBuffer); |
|---|
| 438 | |
|---|
| 439 | return theResult; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | newtRef NsStrEqual(newtRefArg rcvr, newtRefArg a, newtRefArg b) |
|---|
| 453 | { |
|---|
| 454 | char* aString; |
|---|
| 455 | char* bString; |
|---|
| 456 | newtRefVar theResult = kNewtRefNIL; |
|---|
| 457 | |
|---|
| 458 | (void) rcvr; |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | if (! NewtRefIsString(a)) |
|---|
| 462 | { |
|---|
| 463 | theResult = NewtThrow(kNErrNotAString, a); |
|---|
| 464 | } else if (! NewtRefIsString(b)) { |
|---|
| 465 | theResult = NewtThrow(kNErrNotAString, b); |
|---|
| 466 | } else if (a == b) { |
|---|
| 467 | theResult = kNewtRefTRUE; |
|---|
| 468 | } else { |
|---|
| 469 | aString = NewtRefToString(a); |
|---|
| 470 | bString = NewtRefToString(b); |
|---|
| 471 | |
|---|
| 472 | if (strcasecmp(aString, bString) == 0) |
|---|
| 473 | { |
|---|
| 474 | theResult = kNewtRefTRUE; |
|---|
| 475 | } |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | return theResult; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | newtRef NsStrExactCompare(newtRefArg rcvr, newtRefArg a, newtRefArg b) |
|---|
| 494 | { |
|---|
| 495 | char* aString; |
|---|
| 496 | char* bString; |
|---|
| 497 | newtRefVar theResult; |
|---|
| 498 | |
|---|
| 499 | (void) rcvr; |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | if (! NewtRefIsString(a)) |
|---|
| 503 | { |
|---|
| 504 | theResult = NewtThrow(kNErrNotAString, a); |
|---|
| 505 | } else if (! NewtRefIsString(b)) { |
|---|
| 506 | theResult = NewtThrow(kNErrNotAString, b); |
|---|
| 507 | } else if (a == b) { |
|---|
| 508 | theResult = NewtMakeInteger(0); |
|---|
| 509 | } else { |
|---|
| 510 | aString = NewtRefToString(a); |
|---|
| 511 | bString = NewtRefToString(b); |
|---|
| 512 | |
|---|
| 513 | theResult = NewtMakeInteger(strcmp(aString, bString)); |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | return theResult; |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | newtRef NsBeginsWith(newtRefArg rcvr, newtRefArg str, newtRefArg sub) |
|---|
| 532 | { |
|---|
| 533 | bool result; |
|---|
| 534 | |
|---|
| 535 | if (! NewtRefIsString(str)) |
|---|
| 536 | return NewtThrow(kNErrNotAString, str); |
|---|
| 537 | |
|---|
| 538 | if (! NewtRefIsString(sub)) |
|---|
| 539 | return NewtThrow(kNErrNotAString, sub); |
|---|
| 540 | |
|---|
| 541 | result = NewtBeginsWith(NewtRefToString(str), NewtRefToString(sub)); |
|---|
| 542 | |
|---|
| 543 | return NewtMakeBoolean(result); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | newtRef NsEndsWith(newtRefArg rcvr, newtRefArg str, newtRefArg sub) |
|---|
| 559 | { |
|---|
| 560 | bool result; |
|---|
| 561 | |
|---|
| 562 | if (! NewtRefIsString(str)) |
|---|
| 563 | return NewtThrow(kNErrNotAString, str); |
|---|
| 564 | |
|---|
| 565 | if (! NewtRefIsString(sub)) |
|---|
| 566 | return NewtThrow(kNErrNotAString, sub); |
|---|
| 567 | |
|---|
| 568 | result = NewtEndsWith(NewtRefToString(str), NewtRefToString(sub)); |
|---|
| 569 | |
|---|
| 570 | return NewtMakeBoolean(result); |
|---|
| 571 | } |
|---|