1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
|
#include "scriptstdstring.h"
#include <assert.h> // assert()
#include <sstream> // std::stringstream
#include <string.h> // strstr()
#include <stdio.h> // sprintf()
#include <stdlib.h> // strtod()
#ifndef __psp2__
#include <locale.h> // setlocale()
#endif
using namespace std;
// This macro is used to avoid warnings about unused variables.
// Usually where the variables are only used in debug mode.
#define UNUSED_VAR(x) (void)(x)
#ifdef AS_CAN_USE_CPP11
// The string factory doesn't need to keep a specific order in the
// cache, so the unordered_map is faster than the ordinary map
#include <unordered_map> // std::unordered_map
BEGIN_AS_NAMESPACE
typedef unordered_map<string, int> map_t;
END_AS_NAMESPACE
#else
#include <map> // std::map
BEGIN_AS_NAMESPACE
typedef map<string, int> map_t;
END_AS_NAMESPACE
#endif
BEGIN_AS_NAMESPACE
class CStdStringFactory : public asIStringFactory
{
public:
CStdStringFactory() {}
~CStdStringFactory()
{
// The script engine must release each string
// constant that it has requested
assert(stringCache.size() == 0);
}
const void *GetStringConstant(const char *data, asUINT length)
{
// The string factory might be modified from multiple
// threads, so it is necessary to use a mutex.
asAcquireExclusiveLock();
string str(data, length);
map_t::iterator it = stringCache.find(str);
if (it != stringCache.end())
it->second++;
else
it = stringCache.insert(map_t::value_type(str, 1)).first;
asReleaseExclusiveLock();
return reinterpret_cast<const void*>(&it->first);
}
int ReleaseStringConstant(const void *str)
{
if (str == 0)
return asERROR;
int ret = asSUCCESS;
// The string factory might be modified from multiple
// threads, so it is necessary to use a mutex.
asAcquireExclusiveLock();
map_t::iterator it = stringCache.find(*reinterpret_cast<const string*>(str));
if (it == stringCache.end())
ret = asERROR;
else
{
it->second--;
if (it->second == 0)
stringCache.erase(it);
}
asReleaseExclusiveLock();
return ret;
}
int GetRawStringData(const void *str, char *data, asUINT *length) const
{
if (str == 0)
return asERROR;
if (length)
*length = (asUINT)reinterpret_cast<const string*>(str)->length();
if (data)
memcpy(data, reinterpret_cast<const string*>(str)->c_str(), reinterpret_cast<const string*>(str)->length());
return asSUCCESS;
}
// THe access to the string cache is protected with the common mutex provided by AngelScript
map_t stringCache;
};
static CStdStringFactory *stringFactory = 0;
// TODO: Make this public so the application can also use the string
// factory and share the string constants if so desired, or to
// monitor the size of the string factory cache.
CStdStringFactory *GetStdStringFactorySingleton()
{
if( stringFactory == 0 )
{
// The following instance will be destroyed by the global
// CStdStringFactoryCleaner instance upon application shutdown
stringFactory = new CStdStringFactory();
}
return stringFactory;
}
class CStdStringFactoryCleaner
{
public:
~CStdStringFactoryCleaner()
{
if (stringFactory)
{
// Only delete the string factory if the stringCache is empty
// If it is not empty, it means that someone might still attempt
// to release string constants, so if we delete the string factory
// the application might crash. Not deleting the cache would
// lead to a memory leak, but since this is only happens when the
// application is shutting down anyway, it is not important.
if (stringFactory->stringCache.empty())
{
delete stringFactory;
stringFactory = 0;
}
}
}
};
static CStdStringFactoryCleaner cleaner;
static void ConstructString(string *thisPointer)
{
new(thisPointer) string();
}
static void CopyConstructString(const string &other, string *thisPointer)
{
new(thisPointer) string(other);
}
static void DestructString(string *thisPointer)
{
thisPointer->~string();
}
static string &AddAssignStringToString(const string &str, string &dest)
{
// We don't register the method directly because some compilers
// and standard libraries inline the definition, resulting in the
// linker being unable to find the declaration.
// Example: CLang/LLVM with XCode 4.3 on OSX 10.7
dest += str;
return dest;
}
// bool string::isEmpty()
// bool string::empty() // if AS_USE_STLNAMES == 1
static bool StringIsEmpty(const string &str)
{
// We don't register the method directly because some compilers
// and standard libraries inline the definition, resulting in the
// linker being unable to find the declaration
// Example: CLang/LLVM with XCode 4.3 on OSX 10.7
return str.empty();
}
static string &AssignUInt64ToString(asQWORD i, string &dest)
{
ostringstream stream;
stream << i;
dest = stream.str();
return dest;
}
static string &AddAssignUInt64ToString(asQWORD i, string &dest)
{
ostringstream stream;
stream << i;
dest += stream.str();
return dest;
}
static string AddStringUInt64(const string &str, asQWORD i)
{
ostringstream stream;
stream << i;
return str + stream.str();
}
static string AddInt64String(asINT64 i, const string &str)
{
ostringstream stream;
stream << i;
return stream.str() + str;
}
static string &AssignInt64ToString(asINT64 i, string &dest)
{
ostringstream stream;
stream << i;
dest = stream.str();
return dest;
}
static string &AddAssignInt64ToString(asINT64 i, string &dest)
{
ostringstream stream;
stream << i;
dest += stream.str();
return dest;
}
static string AddStringInt64(const string &str, asINT64 i)
{
ostringstream stream;
stream << i;
return str + stream.str();
}
static string AddUInt64String(asQWORD i, const string &str)
{
ostringstream stream;
stream << i;
return stream.str() + str;
}
static string &AssignDoubleToString(double f, string &dest)
{
ostringstream stream;
stream << f;
dest = stream.str();
return dest;
}
static string &AddAssignDoubleToString(double f, string &dest)
{
ostringstream stream;
stream << f;
dest += stream.str();
return dest;
}
static string &AssignFloatToString(float f, string &dest)
{
ostringstream stream;
stream << f;
dest = stream.str();
return dest;
}
static string &AddAssignFloatToString(float f, string &dest)
{
ostringstream stream;
stream << f;
dest += stream.str();
return dest;
}
static string &AssignBoolToString(bool b, string &dest)
{
ostringstream stream;
stream << (b ? "true" : "false");
dest = stream.str();
return dest;
}
static string &AddAssignBoolToString(bool b, string &dest)
{
ostringstream stream;
stream << (b ? "true" : "false");
dest += stream.str();
return dest;
}
static string AddStringDouble(const string &str, double f)
{
ostringstream stream;
stream << f;
return str + stream.str();
}
static string AddDoubleString(double f, const string &str)
{
ostringstream stream;
stream << f;
return stream.str() + str;
}
static string AddStringFloat(const string &str, float f)
{
ostringstream stream;
stream << f;
return str + stream.str();
}
static string AddFloatString(float f, const string &str)
{
ostringstream stream;
stream << f;
return stream.str() + str;
}
static string AddStringBool(const string &str, bool b)
{
ostringstream stream;
stream << (b ? "true" : "false");
return str + stream.str();
}
static string AddBoolString(bool b, const string &str)
{
ostringstream stream;
stream << (b ? "true" : "false");
return stream.str() + str;
}
static char *StringCharAt(unsigned int i, string &str)
{
if( i >= str.size() )
{
// Set a script exception
asIScriptContext *ctx = asGetActiveContext();
ctx->SetException("Out of range");
// Return a null pointer
return 0;
}
return &str[i];
}
// AngelScript signature:
// int string::opCmp(const string &in) const
static int StringCmp(const string &a, const string &b)
{
int cmp = 0;
if( a < b ) cmp = -1;
else if( a > b ) cmp = 1;
return cmp;
}
// This function returns the index of the first position where the substring
// exists in the input string. If the substring doesn't exist in the input
// string -1 is returned.
//
// AngelScript signature:
// int string::findFirst(const string &in sub, uint start = 0) const
static int StringFindFirst(const string &sub, asUINT start, const string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
return (int)str.find(sub, (size_t)(start < 0 ? string::npos : start));
}
// This function returns the index of the first position where the one of the bytes in substring
// exists in the input string. If the characters in the substring doesn't exist in the input
// string -1 is returned.
//
// AngelScript signature:
// int string::findFirstOf(const string &in sub, uint start = 0) const
static int StringFindFirstOf(const string &sub, asUINT start, const string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
return (int)str.find_first_of(sub, (size_t)(start < 0 ? string::npos : start));
}
// This function returns the index of the last position where the one of the bytes in substring
// exists in the input string. If the characters in the substring doesn't exist in the input
// string -1 is returned.
//
// AngelScript signature:
// int string::findLastOf(const string &in sub, uint start = -1) const
static int StringFindLastOf(const string &sub, asUINT start, const string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
return (int)str.find_last_of(sub, (size_t)(start < 0 ? string::npos : start));
}
// This function returns the index of the first position where a byte other than those in substring
// exists in the input string. If none is found -1 is returned.
//
// AngelScript signature:
// int string::findFirstNotOf(const string &in sub, uint start = 0) const
static int StringFindFirstNotOf(const string &sub, asUINT start, const string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
return (int)str.find_first_not_of(sub, (size_t)(start < 0 ? string::npos : start));
}
// This function returns the index of the last position where a byte other than those in substring
// exists in the input string. If none is found -1 is returned.
//
// AngelScript signature:
// int string::findLastNotOf(const string &in sub, uint start = -1) const
static int StringFindLastNotOf(const string &sub, asUINT start, const string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
return (int)str.find_last_not_of(sub, (size_t)(start < 0 ? string::npos : start));
}
// This function returns the index of the last position where the substring
// exists in the input string. If the substring doesn't exist in the input
// string -1 is returned.
//
// AngelScript signature:
// int string::findLast(const string &in sub, int start = -1) const
static int StringFindLast(const string &sub, int start, const string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
return (int)str.rfind(sub, (size_t)(start < 0 ? string::npos : start));
}
// AngelScript signature:
// void string::insert(uint pos, const string &in other)
static void StringInsert(unsigned int pos, const string &other, string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
str.insert(pos, other);
}
// AngelScript signature:
// void string::erase(uint pos, int count = -1)
static void StringErase(unsigned int pos, int count, string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
str.erase(pos, (size_t)(count < 0 ? string::npos : count));
}
// AngelScript signature:
// uint string::length() const
static asUINT StringLength(const string &str)
{
// We don't register the method directly because the return type changes between 32bit and 64bit platforms
return (asUINT)str.length();
}
// AngelScript signature:
// void string::resize(uint l)
static void StringResize(asUINT l, string &str)
{
// We don't register the method directly because the argument types change between 32bit and 64bit platforms
str.resize(l);
}
// AngelScript signature:
// string formatInt(int64 val, const string &in options, uint width)
static string formatInt(asINT64 value, const string &options, asUINT width)
{
bool leftJustify = options.find("l") != string::npos;
bool padWithZero = options.find("0") != string::npos;
bool alwaysSign = options.find("+") != string::npos;
bool spaceOnSign = options.find(" ") != string::npos;
bool hexSmall = options.find("h") != string::npos;
bool hexLarge = options.find("H") != string::npos;
string fmt = "%";
if( leftJustify ) fmt += "-";
if( alwaysSign ) fmt += "+";
if( spaceOnSign ) fmt += " ";
if( padWithZero ) fmt += "0";
#ifdef _WIN32
fmt += "*I64";
#else
#ifdef _LP64
fmt += "*l";
#else
fmt += "*ll";
#endif
#endif
if( hexSmall ) fmt += "x";
else if( hexLarge ) fmt += "X";
else fmt += "d";
string buf;
buf.resize(width+30);
#if _MSC_VER >= 1400 && !defined(__S3E__)
// MSVC 8.0 / 2005 or newer
sprintf_s(&buf[0], buf.size(), fmt.c_str(), width, value);
#else
sprintf(&buf[0], fmt.c_str(), width, value);
#endif
buf.resize(strlen(&buf[0]));
return buf;
}
// AngelScript signature:
// string formatUInt(uint64 val, const string &in options, uint width)
static string formatUInt(asQWORD value, const string &options, asUINT width)
{
bool leftJustify = options.find("l") != string::npos;
bool padWithZero = options.find("0") != string::npos;
bool alwaysSign = options.find("+") != string::npos;
bool spaceOnSign = options.find(" ") != string::npos;
bool hexSmall = options.find("h") != string::npos;
bool hexLarge = options.find("H") != string::npos;
string fmt = "%";
if( leftJustify ) fmt += "-";
if( alwaysSign ) fmt += "+";
if( spaceOnSign ) fmt += " ";
if( padWithZero ) fmt += "0";
#ifdef _WIN32
fmt += "*I64";
#else
#ifdef _LP64
fmt += "*l";
#else
fmt += "*ll";
#endif
#endif
if( hexSmall ) fmt += "x";
else if( hexLarge ) fmt += "X";
else fmt += "u";
string buf;
buf.resize(width+30);
#if _MSC_VER >= 1400 && !defined(__S3E__)
// MSVC 8.0 / 2005 or newer
sprintf_s(&buf[0], buf.size(), fmt.c_str(), width, value);
#else
sprintf(&buf[0], fmt.c_str(), width, value);
#endif
buf.resize(strlen(&buf[0]));
return buf;
}
// AngelScript signature:
// string formatFloat(double val, const string &in options, uint width, uint precision)
static string formatFloat(double value, const string &options, asUINT width, asUINT precision)
{
bool leftJustify = options.find("l") != string::npos;
bool padWithZero = options.find("0") != string::npos;
bool alwaysSign = options.find("+") != string::npos;
bool spaceOnSign = options.find(" ") != string::npos;
bool expSmall = options.find("e") != string::npos;
bool expLarge = options.find("E") != string::npos;
string fmt = "%";
if( leftJustify ) fmt += "-";
if( alwaysSign ) fmt += "+";
if( spaceOnSign ) fmt += " ";
if( padWithZero ) fmt += "0";
fmt += "*.*";
if( expSmall ) fmt += "e";
else if( expLarge ) fmt += "E";
else fmt += "f";
string buf;
buf.resize(width+precision+50);
#if _MSC_VER >= 1400 && !defined(__S3E__)
// MSVC 8.0 / 2005 or newer
sprintf_s(&buf[0], buf.size(), fmt.c_str(), width, precision, value);
#else
sprintf(&buf[0], fmt.c_str(), width, precision, value);
#endif
buf.resize(strlen(&buf[0]));
return buf;
}
// AngelScript signature:
// int64 parseInt(const string &in val, uint base = 10, uint &out byteCount = 0)
static asINT64 parseInt(const string &val, asUINT base, asUINT *byteCount)
{
// Only accept base 10 and 16
if( base != 10 && base != 16 )
{
if( byteCount ) *byteCount = 0;
return 0;
}
const char *end = &val[0];
// Determine the sign
bool sign = false;
if( *end == '-' )
{
sign = true;
end++;
}
else if( *end == '+' )
end++;
asINT64 res = 0;
if( base == 10 )
{
while( *end >= '0' && *end <= '9' )
{
res *= 10;
res += *end++ - '0';
}
}
else if( base == 16 )
{
while( (*end >= '0' && *end <= '9') ||
(*end >= 'a' && *end <= 'f') ||
(*end >= 'A' && *end <= 'F') )
{
res *= 16;
if( *end >= '0' && *end <= '9' )
res += *end++ - '0';
else if( *end >= 'a' && *end <= 'f' )
res += *end++ - 'a' + 10;
else if( *end >= 'A' && *end <= 'F' )
res += *end++ - 'A' + 10;
}
}
if( byteCount )
*byteCount = asUINT(size_t(end - val.c_str()));
if( sign )
res = -res;
return res;
}
// AngelScript signature:
// uint64 parseUInt(const string &in val, uint base = 10, uint &out byteCount = 0)
static asQWORD parseUInt(const string &val, asUINT base, asUINT *byteCount)
{
// Only accept base 10 and 16
if (base != 10 && base != 16)
{
if (byteCount) *byteCount = 0;
return 0;
}
const char *end = &val[0];
asQWORD res = 0;
if (base == 10)
{
while (*end >= '0' && *end <= '9')
{
res *= 10;
res += *end++ - '0';
}
}
else if (base == 16)
{
while ((*end >= '0' && *end <= '9') ||
(*end >= 'a' && *end <= 'f') ||
(*end >= 'A' && *end <= 'F'))
{
res *= 16;
if (*end >= '0' && *end <= '9')
res += *end++ - '0';
else if (*end >= 'a' && *end <= 'f')
res += *end++ - 'a' + 10;
else if (*end >= 'A' && *end <= 'F')
res += *end++ - 'A' + 10;
}
}
if (byteCount)
*byteCount = asUINT(size_t(end - val.c_str()));
return res;
}
// AngelScript signature:
// double parseFloat(const string &in val, uint &out byteCount = 0)
double parseFloat(const string &val, asUINT *byteCount)
{
char *end;
// WinCE doesn't have setlocale. Some quick testing on my current platform
// still manages to parse the numbers such as "3.14" even if the decimal for the
// locale is ",".
#if !defined(_WIN32_WCE) && !defined(ANDROID) && !defined(__psp2__)
// Set the locale to C so that we are guaranteed to parse the float value correctly
char *tmp = setlocale(LC_NUMERIC, 0);
string orig = tmp ? tmp : "C";
setlocale(LC_NUMERIC, "C");
#endif
double res = strtod(val.c_str(), &end);
#if !defined(_WIN32_WCE) && !defined(ANDROID) && !defined(__psp2__)
// Restore the locale
setlocale(LC_NUMERIC, orig.c_str());
#endif
if( byteCount )
*byteCount = asUINT(size_t(end - val.c_str()));
return res;
}
// This function returns a string containing the substring of the input string
// determined by the starting index and count of characters.
//
// AngelScript signature:
// string string::substr(uint start = 0, int count = -1) const
static string StringSubString(asUINT start, int count, const string &str)
{
// Check for out-of-bounds
string ret;
if( start < str.length() && count != 0 )
ret = str.substr(start, (size_t)(count < 0 ? string::npos : count));
return ret;
}
// String equality comparison.
// Returns true iff lhs is equal to rhs.
//
// For some reason gcc 4.7 has difficulties resolving the
// asFUNCTIONPR(operator==, (const string &, const string &)
// makro, so this wrapper was introduced as work around.
static bool StringEquals(const std::string& lhs, const std::string& rhs)
{
return lhs == rhs;
}
void RegisterStdString_Native(asIScriptEngine *engine)
{
int r = 0;
UNUSED_VAR(r);
// Register the string type
#if AS_CAN_USE_CPP11
// With C++11 it is possible to use asGetTypeTraits to automatically determine the correct flags to use
r = engine->RegisterObjectType("string", sizeof(string), asOBJ_VALUE | asGetTypeTraits<string>()); assert( r >= 0 );
#else
r = engine->RegisterObjectType("string", sizeof(string), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
#endif
r = engine->RegisterStringFactory("string", GetStdStringFactorySingleton());
// Register the object operator overloads
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(const string &in)", asFUNCTION(CopyConstructString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(const string &in)", asMETHODPR(string, operator =, (const string&), string&), asCALL_THISCALL); assert( r >= 0 );
// Need to use a wrapper on Mac OS X 10.7/XCode 4.3 and CLang/LLVM, otherwise the linker fails
r = engine->RegisterObjectMethod("string", "string &opAddAssign(const string &in)", asFUNCTION(AddAssignStringToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// r = engine->RegisterObjectMethod("string", "string &opAddAssign(const string &in)", asMETHODPR(string, operator+=, (const string&), string&), asCALL_THISCALL); assert( r >= 0 );
// Need to use a wrapper for operator== otherwise gcc 4.7 fails to compile
r = engine->RegisterObjectMethod("string", "bool opEquals(const string &in) const", asFUNCTIONPR(StringEquals, (const string &, const string &), bool), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int opCmp(const string &in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(const string &in) const", asFUNCTIONPR(operator +, (const string &, const string &), string), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
// The string length can be accessed through methods or through virtual property
// TODO: Register as size() for consistency with other types
#if AS_USE_ACCESSORS != 1
r = engine->RegisterObjectMethod("string", "uint length() const", asFUNCTION(StringLength), asCALL_CDECL_OBJLAST); assert( r >= 0 );
#endif
r = engine->RegisterObjectMethod("string", "void resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST); assert( r >= 0 );
#if AS_USE_STLNAMES != 1 && AS_USE_ACCESSORS == 1
// Don't register these if STL names is used, as they conflict with the method size()
r = engine->RegisterObjectMethod("string", "uint get_length() const property", asFUNCTION(StringLength), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "void set_length(uint) property", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST); assert( r >= 0 );
#endif
// Need to use a wrapper on Mac OS X 10.7/XCode 4.3 and CLang/LLVM, otherwise the linker fails
// r = engine->RegisterObjectMethod("string", "bool isEmpty() const", asMETHOD(string, empty), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "bool isEmpty() const", asFUNCTION(StringIsEmpty), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// Register the index operator, both as a mutator and as an inspector
// Note that we don't register the operator[] directly, as it doesn't do bounds checking
r = engine->RegisterObjectMethod("string", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// Automatic conversion from values
r = engine->RegisterObjectMethod("string", "string &opAssign(double)", asFUNCTION(AssignDoubleToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(double)", asFUNCTION(AddAssignDoubleToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(double) const", asFUNCTION(AddStringDouble), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(double) const", asFUNCTION(AddDoubleString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(float)", asFUNCTION(AssignFloatToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(float)", asFUNCTION(AddAssignFloatToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(float) const", asFUNCTION(AddStringFloat), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(float) const", asFUNCTION(AddFloatString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(int64)", asFUNCTION(AssignInt64ToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(int64)", asFUNCTION(AddAssignInt64ToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(int64) const", asFUNCTION(AddStringInt64), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(int64) const", asFUNCTION(AddInt64String), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(uint64)", asFUNCTION(AssignUInt64ToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(uint64)", asFUNCTION(AddAssignUInt64ToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(uint64) const", asFUNCTION(AddStringUInt64), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(uint64) const", asFUNCTION(AddUInt64String), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(bool)", asFUNCTION(AssignBoolToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(bool)", asFUNCTION(AddAssignBoolToString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(bool) const", asFUNCTION(AddStringBool), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(bool) const", asFUNCTION(AddBoolString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// Utilities
r = engine->RegisterObjectMethod("string", "string substr(uint start = 0, int count = -1) const", asFUNCTION(StringSubString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int findFirst(const string &in, uint start = 0) const", asFUNCTION(StringFindFirst), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int findFirstOf(const string &in, uint start = 0) const", asFUNCTION(StringFindFirstOf), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findFirstNotOf(const string &in, uint start = 0) const", asFUNCTION(StringFindFirstNotOf), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findLast(const string &in, int start = -1) const", asFUNCTION(StringFindLast), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int findLastOf(const string &in, int start = -1) const", asFUNCTION(StringFindLastOf), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findLastNotOf(const string &in, int start = -1) const", asFUNCTION(StringFindLastNotOf), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "void insert(uint pos, const string &in other)", asFUNCTION(StringInsert), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "void erase(uint pos, int count = -1)", asFUNCTION(StringErase), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterGlobalFunction("string formatInt(int64 val, const string &in options = \"\", uint width = 0)", asFUNCTION(formatInt), asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalFunction("string formatUInt(uint64 val, const string &in options = \"\", uint width = 0)", asFUNCTION(formatUInt), asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalFunction("string formatFloat(double val, const string &in options = \"\", uint width = 0, uint precision = 0)", asFUNCTION(formatFloat), asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalFunction("int64 parseInt(const string &in, uint base = 10, uint &out byteCount = 0)", asFUNCTION(parseInt), asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalFunction("uint64 parseUInt(const string &in, uint base = 10, uint &out byteCount = 0)", asFUNCTION(parseUInt), asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalFunction("double parseFloat(const string &in, uint &out byteCount = 0)", asFUNCTION(parseFloat), asCALL_CDECL); assert(r >= 0);
#if AS_USE_STLNAMES == 1
// Same as length
r = engine->RegisterObjectMethod("string", "uint size() const", asFUNCTION(StringLength), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// Same as isEmpty
r = engine->RegisterObjectMethod("string", "bool empty() const", asFUNCTION(StringIsEmpty), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// Same as findFirst
r = engine->RegisterObjectMethod("string", "int find(const string &in, uint start = 0) const", asFUNCTION(StringFindFirst), asCALL_CDECL_OBJLAST); assert( r >= 0 );
// Same as findLast
r = engine->RegisterObjectMethod("string", "int rfind(const string &in, int start = -1) const", asFUNCTION(StringFindLast), asCALL_CDECL_OBJLAST); assert( r >= 0 );
#endif
// TODO: Implement the following
// findAndReplace - replaces a text found in the string
// replaceRange - replaces a range of bytes in the string
// multiply/times/opMul/opMul_r - takes the string and multiplies it n times, e.g. "-".multiply(5) returns "-----"
}
static void ConstructStringGeneric(asIScriptGeneric * gen)
{
new (gen->GetObject()) string();
}
static void CopyConstructStringGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetArgObject(0));
new (gen->GetObject()) string(*a);
}
static void DestructStringGeneric(asIScriptGeneric * gen)
{
string * ptr = static_cast<string *>(gen->GetObject());
ptr->~string();
}
static void AssignStringGeneric(asIScriptGeneric *gen)
{
string * a = static_cast<string *>(gen->GetArgObject(0));
string * self = static_cast<string *>(gen->GetObject());
*self = *a;
gen->SetReturnAddress(self);
}
static void AddAssignStringGeneric(asIScriptGeneric *gen)
{
string * a = static_cast<string *>(gen->GetArgObject(0));
string * self = static_cast<string *>(gen->GetObject());
*self += *a;
gen->SetReturnAddress(self);
}
static void StringEqualsGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
string * b = static_cast<string *>(gen->GetArgAddress(0));
*(bool*)gen->GetAddressOfReturnLocation() = (*a == *b);
}
static void StringCmpGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
string * b = static_cast<string *>(gen->GetArgAddress(0));
int cmp = 0;
if( *a < *b ) cmp = -1;
else if( *a > *b ) cmp = 1;
*(int*)gen->GetAddressOfReturnLocation() = cmp;
}
static void StringAddGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
string * b = static_cast<string *>(gen->GetArgAddress(0));
string ret_val = *a + *b;
gen->SetReturnObject(&ret_val);
}
static void StringLengthGeneric(asIScriptGeneric * gen)
{
string * self = static_cast<string *>(gen->GetObject());
*static_cast<asUINT *>(gen->GetAddressOfReturnLocation()) = (asUINT)self->length();
}
static void StringIsEmptyGeneric(asIScriptGeneric * gen)
{
string * self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<bool *>(gen->GetAddressOfReturnLocation()) = StringIsEmpty(*self);
}
static void StringResizeGeneric(asIScriptGeneric * gen)
{
string * self = static_cast<string *>(gen->GetObject());
self->resize(*static_cast<asUINT *>(gen->GetAddressOfArg(0)));
}
static void StringInsert_Generic(asIScriptGeneric *gen)
{
string * self = static_cast<string *>(gen->GetObject());
asUINT pos = gen->GetArgDWord(0);
string *other = reinterpret_cast<string*>(gen->GetArgAddress(1));
StringInsert(pos, *other, *self);
}
static void StringErase_Generic(asIScriptGeneric *gen)
{
string * self = static_cast<string *>(gen->GetObject());
asUINT pos = gen->GetArgDWord(0);
int count = int(gen->GetArgDWord(1));
StringErase(pos, count, *self);
}
static void StringFindFirst_Generic(asIScriptGeneric * gen)
{
string *find = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT start = gen->GetArgDWord(1);
string *self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<int *>(gen->GetAddressOfReturnLocation()) = StringFindFirst(*find, start, *self);
}
static void StringFindLast_Generic(asIScriptGeneric * gen)
{
string *find = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT start = gen->GetArgDWord(1);
string *self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<int *>(gen->GetAddressOfReturnLocation()) = StringFindLast(*find, start, *self);
}
static void StringFindFirstOf_Generic(asIScriptGeneric * gen)
{
string *find = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT start = gen->GetArgDWord(1);
string *self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<int *>(gen->GetAddressOfReturnLocation()) = StringFindFirstOf(*find, start, *self);
}
static void StringFindLastOf_Generic(asIScriptGeneric * gen)
{
string *find = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT start = gen->GetArgDWord(1);
string *self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<int *>(gen->GetAddressOfReturnLocation()) = StringFindLastOf(*find, start, *self);
}
static void StringFindFirstNotOf_Generic(asIScriptGeneric * gen)
{
string *find = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT start = gen->GetArgDWord(1);
string *self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<int *>(gen->GetAddressOfReturnLocation()) = StringFindFirstNotOf(*find, start, *self);
}
static void StringFindLastNotOf_Generic(asIScriptGeneric * gen)
{
string *find = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT start = gen->GetArgDWord(1);
string *self = reinterpret_cast<string *>(gen->GetObject());
*reinterpret_cast<int *>(gen->GetAddressOfReturnLocation()) = StringFindLastNotOf(*find, start, *self);
}
static void formatInt_Generic(asIScriptGeneric * gen)
{
asINT64 val = gen->GetArgQWord(0);
string *options = reinterpret_cast<string*>(gen->GetArgAddress(1));
asUINT width = gen->GetArgDWord(2);
new(gen->GetAddressOfReturnLocation()) string(formatInt(val, *options, width));
}
static void formatUInt_Generic(asIScriptGeneric * gen)
{
asQWORD val = gen->GetArgQWord(0);
string *options = reinterpret_cast<string*>(gen->GetArgAddress(1));
asUINT width = gen->GetArgDWord(2);
new(gen->GetAddressOfReturnLocation()) string(formatUInt(val, *options, width));
}
static void formatFloat_Generic(asIScriptGeneric *gen)
{
double val = gen->GetArgDouble(0);
string *options = reinterpret_cast<string*>(gen->GetArgAddress(1));
asUINT width = gen->GetArgDWord(2);
asUINT precision = gen->GetArgDWord(3);
new(gen->GetAddressOfReturnLocation()) string(formatFloat(val, *options, width, precision));
}
static void parseInt_Generic(asIScriptGeneric *gen)
{
string *str = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT base = gen->GetArgDWord(1);
asUINT *byteCount = reinterpret_cast<asUINT*>(gen->GetArgAddress(2));
gen->SetReturnQWord(parseInt(*str,base,byteCount));
}
static void parseUInt_Generic(asIScriptGeneric *gen)
{
string *str = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT base = gen->GetArgDWord(1);
asUINT *byteCount = reinterpret_cast<asUINT*>(gen->GetArgAddress(2));
gen->SetReturnQWord(parseUInt(*str, base, byteCount));
}
static void parseFloat_Generic(asIScriptGeneric *gen)
{
string *str = reinterpret_cast<string*>(gen->GetArgAddress(0));
asUINT *byteCount = reinterpret_cast<asUINT*>(gen->GetArgAddress(1));
gen->SetReturnDouble(parseFloat(*str,byteCount));
}
static void StringCharAtGeneric(asIScriptGeneric * gen)
{
unsigned int index = gen->GetArgDWord(0);
string * self = static_cast<string *>(gen->GetObject());
if (index >= self->size())
{
// Set a script exception
asIScriptContext *ctx = asGetActiveContext();
ctx->SetException("Out of range");
gen->SetReturnAddress(0);
}
else
{
gen->SetReturnAddress(&(self->operator [](index)));
}
}
static void AssignInt2StringGeneric(asIScriptGeneric *gen)
{
asINT64 *a = static_cast<asINT64*>(gen->GetAddressOfArg(0));
string *self = static_cast<string*>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self = sstr.str();
gen->SetReturnAddress(self);
}
static void AssignUInt2StringGeneric(asIScriptGeneric *gen)
{
asQWORD *a = static_cast<asQWORD*>(gen->GetAddressOfArg(0));
string *self = static_cast<string*>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self = sstr.str();
gen->SetReturnAddress(self);
}
static void AssignDouble2StringGeneric(asIScriptGeneric *gen)
{
double *a = static_cast<double*>(gen->GetAddressOfArg(0));
string *self = static_cast<string*>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self = sstr.str();
gen->SetReturnAddress(self);
}
static void AssignFloat2StringGeneric(asIScriptGeneric *gen)
{
float *a = static_cast<float*>(gen->GetAddressOfArg(0));
string *self = static_cast<string*>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self = sstr.str();
gen->SetReturnAddress(self);
}
static void AssignBool2StringGeneric(asIScriptGeneric *gen)
{
bool *a = static_cast<bool*>(gen->GetAddressOfArg(0));
string *self = static_cast<string*>(gen->GetObject());
std::stringstream sstr;
sstr << (*a ? "true" : "false");
*self = sstr.str();
gen->SetReturnAddress(self);
}
static void AddAssignDouble2StringGeneric(asIScriptGeneric * gen)
{
double * a = static_cast<double *>(gen->GetAddressOfArg(0));
string * self = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self += sstr.str();
gen->SetReturnAddress(self);
}
static void AddAssignFloat2StringGeneric(asIScriptGeneric * gen)
{
float * a = static_cast<float *>(gen->GetAddressOfArg(0));
string * self = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self += sstr.str();
gen->SetReturnAddress(self);
}
static void AddAssignInt2StringGeneric(asIScriptGeneric * gen)
{
asINT64 * a = static_cast<asINT64 *>(gen->GetAddressOfArg(0));
string * self = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self += sstr.str();
gen->SetReturnAddress(self);
}
static void AddAssignUInt2StringGeneric(asIScriptGeneric * gen)
{
asQWORD * a = static_cast<asQWORD *>(gen->GetAddressOfArg(0));
string * self = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a;
*self += sstr.str();
gen->SetReturnAddress(self);
}
static void AddAssignBool2StringGeneric(asIScriptGeneric * gen)
{
bool * a = static_cast<bool *>(gen->GetAddressOfArg(0));
string * self = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << (*a ? "true" : "false");
*self += sstr.str();
gen->SetReturnAddress(self);
}
static void AddString2DoubleGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
double * b = static_cast<double *>(gen->GetAddressOfArg(0));
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddString2FloatGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
float * b = static_cast<float *>(gen->GetAddressOfArg(0));
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddString2IntGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
asINT64 * b = static_cast<asINT64 *>(gen->GetAddressOfArg(0));
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddString2UIntGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
asQWORD * b = static_cast<asQWORD *>(gen->GetAddressOfArg(0));
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddString2BoolGeneric(asIScriptGeneric * gen)
{
string * a = static_cast<string *>(gen->GetObject());
bool * b = static_cast<bool *>(gen->GetAddressOfArg(0));
std::stringstream sstr;
sstr << *a << (*b ? "true" : "false");
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddDouble2StringGeneric(asIScriptGeneric * gen)
{
double* a = static_cast<double *>(gen->GetAddressOfArg(0));
string * b = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddFloat2StringGeneric(asIScriptGeneric * gen)
{
float* a = static_cast<float *>(gen->GetAddressOfArg(0));
string * b = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddInt2StringGeneric(asIScriptGeneric * gen)
{
asINT64* a = static_cast<asINT64 *>(gen->GetAddressOfArg(0));
string * b = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddUInt2StringGeneric(asIScriptGeneric * gen)
{
asQWORD* a = static_cast<asQWORD *>(gen->GetAddressOfArg(0));
string * b = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << *a << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void AddBool2StringGeneric(asIScriptGeneric * gen)
{
bool* a = static_cast<bool *>(gen->GetAddressOfArg(0));
string * b = static_cast<string *>(gen->GetObject());
std::stringstream sstr;
sstr << (*a ? "true" : "false") << *b;
std::string ret_val = sstr.str();
gen->SetReturnObject(&ret_val);
}
static void StringSubString_Generic(asIScriptGeneric *gen)
{
// Get the arguments
string *str = (string*)gen->GetObject();
asUINT start = *(int*)gen->GetAddressOfArg(0);
int count = *(int*)gen->GetAddressOfArg(1);
// Return the substring
new(gen->GetAddressOfReturnLocation()) string(StringSubString(start, count, *str));
}
void RegisterStdString_Generic(asIScriptEngine *engine)
{
int r = 0;
UNUSED_VAR(r);
// Register the string type
r = engine->RegisterObjectType("string", sizeof(string), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
r = engine->RegisterStringFactory("string", GetStdStringFactorySingleton());
// Register the object operator overloads
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructStringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(const string &in)", asFUNCTION(CopyConstructStringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructStringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(const string &in)", asFUNCTION(AssignStringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(const string &in)", asFUNCTION(AddAssignStringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "bool opEquals(const string &in) const", asFUNCTION(StringEqualsGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int opCmp(const string &in) const", asFUNCTION(StringCmpGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(const string &in) const", asFUNCTION(StringAddGeneric), asCALL_GENERIC); assert( r >= 0 );
// Register the object methods
#if AS_USE_ACCESSORS != 1
r = engine->RegisterObjectMethod("string", "uint length() const", asFUNCTION(StringLengthGeneric), asCALL_GENERIC); assert( r >= 0 );
#endif
r = engine->RegisterObjectMethod("string", "void resize(uint)", asFUNCTION(StringResizeGeneric), asCALL_GENERIC); assert( r >= 0 );
#if AS_USE_STLNAMES != 1 && AS_USE_ACCESSORS == 1
r = engine->RegisterObjectMethod("string", "uint get_length() const property", asFUNCTION(StringLengthGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "void set_length(uint) property", asFUNCTION(StringResizeGeneric), asCALL_GENERIC); assert( r >= 0 );
#endif
r = engine->RegisterObjectMethod("string", "bool isEmpty() const", asFUNCTION(StringIsEmptyGeneric), asCALL_GENERIC); assert( r >= 0 );
// Register the index operator, both as a mutator and as an inspector
r = engine->RegisterObjectMethod("string", "uint8 &opIndex(uint)", asFUNCTION(StringCharAtGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAtGeneric), asCALL_GENERIC); assert( r >= 0 );
// Automatic conversion from values
r = engine->RegisterObjectMethod("string", "string &opAssign(double)", asFUNCTION(AssignDouble2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(double)", asFUNCTION(AddAssignDouble2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(double) const", asFUNCTION(AddString2DoubleGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(double) const", asFUNCTION(AddDouble2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(float)", asFUNCTION(AssignFloat2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(float)", asFUNCTION(AddAssignFloat2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(float) const", asFUNCTION(AddString2FloatGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(float) const", asFUNCTION(AddFloat2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(int64)", asFUNCTION(AssignInt2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(int64)", asFUNCTION(AddAssignInt2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(int64) const", asFUNCTION(AddString2IntGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(int64) const", asFUNCTION(AddInt2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(uint64)", asFUNCTION(AssignUInt2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(uint64)", asFUNCTION(AddAssignUInt2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(uint64) const", asFUNCTION(AddString2UIntGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(uint64) const", asFUNCTION(AddUInt2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAssign(bool)", asFUNCTION(AssignBool2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &opAddAssign(bool)", asFUNCTION(AddAssignBool2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd(bool) const", asFUNCTION(AddString2BoolGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string opAdd_r(bool) const", asFUNCTION(AddBool2StringGeneric), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string substr(uint start = 0, int count = -1) const", asFUNCTION(StringSubString_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findFirst(const string &in, uint start = 0) const", asFUNCTION(StringFindFirst_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findFirstOf(const string &in, uint start = 0) const", asFUNCTION(StringFindFirstOf_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findFirstNotOf(const string &in, uint start = 0) const", asFUNCTION(StringFindFirstNotOf_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findLast(const string &in, int start = -1) const", asFUNCTION(StringFindLast_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findLastOf(const string &in, int start = -1) const", asFUNCTION(StringFindLastOf_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "int findLastNotOf(const string &in, int start = -1) const", asFUNCTION(StringFindLastNotOf_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "void insert(uint pos, const string &in other)", asFUNCTION(StringInsert_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("string", "void erase(uint pos, int count = -1)", asFUNCTION(StringErase_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterGlobalFunction("string formatInt(int64 val, const string &in options = \"\", uint width = 0)", asFUNCTION(formatInt_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterGlobalFunction("string formatUInt(uint64 val, const string &in options = \"\", uint width = 0)", asFUNCTION(formatUInt_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterGlobalFunction("string formatFloat(double val, const string &in options = \"\", uint width = 0, uint precision = 0)", asFUNCTION(formatFloat_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterGlobalFunction("int64 parseInt(const string &in, uint base = 10, uint &out byteCount = 0)", asFUNCTION(parseInt_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterGlobalFunction("uint64 parseUInt(const string &in, uint base = 10, uint &out byteCount = 0)", asFUNCTION(parseUInt_Generic), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterGlobalFunction("double parseFloat(const string &in, uint &out byteCount = 0)", asFUNCTION(parseFloat_Generic), asCALL_GENERIC); assert(r >= 0);
}
void RegisterStdString(asIScriptEngine * engine)
{
if (strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY"))
RegisterStdString_Generic(engine);
else
RegisterStdString_Native(engine);
}
END_AS_NAMESPACE
|