2
0

platformString_ScriptBinding.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. /*!
  23. @defgroup StringFunctions String
  24. @ingroup TorqueScriptFunctions
  25. @{
  26. */
  27. /*! Use the strcmp function to do a lexicographic case sensitive string comparison between string1 and string2.
  28. @param string1 String to be compared to string2.
  29. @param string2 String to be compared to string1.
  30. @return Returns a numeric value: <-1 (string1 is less than string2, including case), 0 (string1 is equal to string2, including case), 1 (string1 is greater than string2, including case)>.
  31. @sa see stricmp, strstr
  32. */
  33. ConsoleFunctionWithDocs(strcmp, ConsoleInt, 3, 3, ( string1 , string2 ))
  34. {
  35. TORQUE_UNUSED( argc );
  36. return dStrcmp(argv[1], argv[2]);
  37. }
  38. /*! Use the stricmp function to do a lexicographic case in-sensitive string comparison between string1 and string2.
  39. @param string1 String to be compared to string2.
  40. @param string2 String to be compared to string1.
  41. @return Returns a numeric value: <-1 (string1 is less than string2, ignoring case), 0 (string1 is equal to string2, ignoring case), 1 (string1 is greater than string2, ignoring case)>.
  42. @sa see strcmp, strstr
  43. */
  44. ConsoleFunctionWithDocs(stricmp, ConsoleInt, 3, 3, ( string1 , string2 ))
  45. {
  46. TORQUE_UNUSED( argc );
  47. return dStricmp(argv[1], argv[2]);
  48. }
  49. /*! Use the strlen function to determine how many characters there are in string.
  50. @param string The string to count characters for.
  51. @return Returns the number of characters in string, or 0 if string is invalid or a NULL string
  52. */
  53. ConsoleFunctionWithDocs(strlen, ConsoleInt, 2, 2, ( string ))
  54. {
  55. TORQUE_UNUSED( argc );
  56. return dStrlen(argv[1]);
  57. }
  58. /*! Use the strstr function to locate the first instance of searchString in sourceString.
  59. @param sourceString The string in which to search for searchString.
  60. @param searchString The string for which to search for in sourceString.
  61. @return Returns a numeric character index representing the position in sourceString at which searchString was found, or -1 to indicate that no instance of searchString was found.
  62. @sa strpos
  63. */
  64. ConsoleFunctionWithDocs(strstr, ConsoleInt, 3, 3, ( sourceString , searchString ))
  65. {
  66. TORQUE_UNUSED( argc );
  67. // returns the start of the sub string argv[2] in argv[1]
  68. // or -1 if not found.
  69. const char *retpos = dStrstr(argv[1], argv[2]);
  70. if(!retpos)
  71. return -1;
  72. return (S32)(retpos - argv[1]);
  73. }
  74. /*! Use the strPos function to locate the first instance of searchString in sourceString, starting at character 0, or at an optional offset.
  75. @param sourceString The string in which to search for searchString.
  76. @param searchString The string for which to search for in sourceString.
  77. @param offset An optional non-negative integer value representing the character offset within sourceString at which to begin the search.
  78. @return Returns a numeric character index representing the postion in sourceString at which searchString was found, or -1 to indicate that no instance of searchString was found.
  79. @sa strstr
  80. */
  81. ConsoleFunctionWithDocs(strpos, ConsoleInt, 3, 4, ( sourceString , searchString, [offset]? ))
  82. {
  83. S32 start = 0;
  84. if(argc == 4)
  85. start = dAtoi(argv[3]);
  86. U32 sublen = dStrlen(argv[2]);
  87. U32 strlen = dStrlen(argv[1]);
  88. if(start < 0)
  89. return -1;
  90. if(sublen + start > strlen)
  91. return -1;
  92. for(; start + sublen <= strlen; start++)
  93. if(!dStrncmp(argv[1] + start, argv[2], sublen))
  94. return start;
  95. return -1;
  96. }
  97. /*! Use the ltrim function to strip the leading white space from sourceString.
  98. White space is any character in this set: spaces, TABs, and NULL strings.
  99. @param sourceString The string to be trimmed.
  100. @return Returns sourceString with all the leading white spaces removed.
  101. @sa stripChars, stripMLControlChars, stripTrailingSpaces, rtrim, trim
  102. */
  103. ConsoleFunctionWithDocs(ltrim, ConsoleString,2,2, ( sourceString ))
  104. {
  105. TORQUE_UNUSED( argc );
  106. const char *ret = argv[1];
  107. while(*ret == ' ' || *ret == '\n' || *ret == '\t')
  108. ret++;
  109. return ret;
  110. }
  111. /*! Use the rtrim function to strip the trailing white space from sourceString.
  112. White space is any character in this set: spaces, TABs, and NULL strings.
  113. @param sourceString The string to be trimmed.
  114. @return Returns sourceString with all the trailing white spaces removed.
  115. @sa stripChars, stripMLControlChars, stripTrailingSpaces, ltrim, trim
  116. */
  117. ConsoleFunctionWithDocs(rtrim, ConsoleString,2,2, ( sourceString ))
  118. {
  119. TORQUE_UNUSED( argc );
  120. S32 firstWhitespace = 0;
  121. S32 pos = 0;
  122. const char *str = argv[1];
  123. while(str[pos])
  124. {
  125. if(str[pos] != ' ' && str[pos] != '\n' && str[pos] != '\t')
  126. firstWhitespace = pos + 1;
  127. pos++;
  128. }
  129. char *ret = Con::getReturnBuffer(firstWhitespace + 1);
  130. dStrncpy(ret, argv[1], firstWhitespace);
  131. ret[firstWhitespace] = 0;
  132. return ret;
  133. }
  134. /*! Use the trim function to strip the leading and trailing white space from sourceString.
  135. White space is any character in this set: spaces, TABs, and NULL strings.
  136. @param sourceString The string to be trimmed.
  137. @return Returns sourceString with all the leading and trailing white spaces removed.
  138. @sa stripChars, stripMLControlChars, stripTrailingSpaces, ltrim, rtrim
  139. */
  140. ConsoleFunctionWithDocs(trim, ConsoleString,2,2, ( sourceString ))
  141. {
  142. TORQUE_UNUSED( argc );
  143. const char *ptr = argv[1];
  144. while(*ptr == ' ' || *ptr == '\n' || *ptr == '\t')
  145. ptr++;
  146. S32 firstWhitespace = 0;
  147. S32 pos = 0;
  148. const char *str = ptr;
  149. while(str[pos])
  150. {
  151. if(str[pos] != ' ' && str[pos] != '\n' && str[pos] != '\t')
  152. firstWhitespace = pos + 1;
  153. pos++;
  154. }
  155. char *ret = Con::getReturnBuffer(firstWhitespace + 1);
  156. dStrncpy(ret, ptr, firstWhitespace);
  157. ret[firstWhitespace] = 0;
  158. return ret;
  159. }
  160. /*! Use the stripChars function to remove chars from sourceString.
  161. @param sourceString The string to be modified.
  162. @param chars The character or characters to search for and remove.
  163. @return Returns a copy of sourceString, from which all instances of chars have been removed. This may be the original sourceString, if chars was not found.
  164. @sa stripMLControlChars, stripTrailingSpaces
  165. */
  166. ConsoleFunctionWithDocs(stripChars, ConsoleString, 3, 3, ( sourceString , chars ))
  167. {
  168. TORQUE_UNUSED( argc );
  169. char* ret = Con::getReturnBuffer( dStrlen( argv[1] ) + 1 );
  170. dStrcpy( ret, argv[1] );
  171. U32 pos = dStrcspn( ret, argv[2] );
  172. while ( pos < dStrlen( ret ) )
  173. {
  174. dStrcpy( ret + pos, ret + pos + 1 );
  175. pos = dStrcspn( ret, argv[2] );
  176. }
  177. return( ret );
  178. }
  179. /*!
  180. remove TorqueML color codes from the string.
  181. @param stringtoString The string from which to remove TorqueML color codes
  182. @return A string consisting of the original string minus color codes
  183. */
  184. ConsoleFunctionWithDocs(stripColorCodes, ConsoleString, 2,2, (stringtoStrip))
  185. {
  186. char* ret = Con::getReturnBuffer( dStrlen( argv[1] ) + 1 );
  187. dStrcpy(ret, argv[1]);
  188. Con::stripColorChars(ret);
  189. return ret;
  190. }
  191. /*! Use the strlwr function to convert all alpha characters in sourceString to lower-case equivalents.
  192. @param sourceString The string to be modified.
  193. @return Returns a copy of sourceString in which all upper-case characters have been converted to lower-case letters.
  194. @sa strupr
  195. */
  196. ConsoleFunctionWithDocs(strlwr, ConsoleString,2,2, ( sourceString ))
  197. {
  198. TORQUE_UNUSED( argc );
  199. char *ret = Con::getReturnBuffer(dStrlen(argv[1]) + 1);
  200. dStrcpy(ret, argv[1]);
  201. return dStrlwr(ret);
  202. }
  203. /*! Use the strupr function to convert all alpha characters in sourceString to upper-case equivalents.
  204. @param sourceString The string to be modified.
  205. @return Returns a copy of sourceString in which all lower-case characters have been converted to upper-case letters.
  206. @sa strlwr
  207. */
  208. ConsoleFunctionWithDocs(strupr, ConsoleString,2,2, ( sourceString ))
  209. {
  210. TORQUE_UNUSED( argc );
  211. char *ret = Con::getReturnBuffer(dStrlen(argv[1]) + 1);
  212. dStrcpy(ret, argv[1]);
  213. return dStrupr(ret);
  214. }
  215. /*! Use the strchr function to extract a sub-string of sourceString, where the sub-string is equal to the first occurence of char in sourceString followed by the remainder of sourceString.
  216. @param sourceString The string from which to extract a sub-string.
  217. @param char The character to search for in sourceString.
  218. @return Returns a string composed of first instance of char in sourceString, and all of the characters after it. If char is not found, a NULL string is returned.
  219. @sa getSubStr
  220. */
  221. ConsoleFunctionWithDocs(strchr, ConsoleString,3,3, ( sourceString , char ))
  222. {
  223. TORQUE_UNUSED( argc );
  224. const char *ret = dStrchr(argv[1], argv[2][0]);
  225. return ret ? ret : "";
  226. }
  227. /*! strrchr searches the sourceString for the last occurance of the given char
  228. @param sourceString The string to search
  229. @return Either a string consisting of the given string from the last occurance of the given char on or an empty string if not found
  230. */
  231. ConsoleFunctionWithDocs(strrchr, ConsoleString,3,3, ( sourceString , char ))
  232. {
  233. TORQUE_UNUSED( argc );
  234. const char *ret = dStrrchr(argv[1], argv[2][0]);
  235. return ret ? ret : "";
  236. }
  237. /*! Use the strreplace function to replace every instance of from in sourceString with to.
  238. This function is case-sensitive and only does exact matching
  239. @param sourceString The string to do replacement operations on.
  240. @param from The old value to be replaced.
  241. @param to The new value to replace old values with.
  242. @return Returns a new version of sourceString in which every instance of the value in from was replaced with the value in to.
  243. */
  244. ConsoleFunctionWithDocs(strreplace, ConsoleString, 4, 4, ( sourceString , from , to ))
  245. {
  246. TORQUE_UNUSED( argc );
  247. S32 fromLen = dStrlen(argv[2]);
  248. if(!fromLen)
  249. return argv[1];
  250. S32 toLen = dStrlen(argv[3]);
  251. S32 count = 0;
  252. const char *scan = argv[1];
  253. while(scan)
  254. {
  255. scan = dStrstr(scan, argv[2]);
  256. if(scan)
  257. {
  258. scan += fromLen;
  259. count++;
  260. }
  261. }
  262. char *ret = Con::getReturnBuffer(dStrlen(argv[1]) + 1 + (toLen - fromLen) * count);
  263. U32 scanp = 0;
  264. U32 dstp = 0;
  265. for(;;)
  266. {
  267. const char *scan = dStrstr(argv[1] + scanp, argv[2]);
  268. if(!scan)
  269. {
  270. dStrcpy(ret + dstp, argv[1] + scanp);
  271. return ret;
  272. }
  273. U32 len = (U32)(scan - (argv[1] + scanp));
  274. dStrncpy(ret + dstp, argv[1] + scanp, len);
  275. dstp += len;
  276. dStrcpy(ret + dstp, argv[3]);
  277. dstp += toLen;
  278. scanp += len + fromLen;
  279. }
  280. return ret;
  281. }
  282. /*! Use the getSubStr function to get a sub-string of sourceString, starting at character index start and ending at character index start + count, or the end-of-string, which ever comes first.
  283. If start + count is greater than the length of sourceString, the extraction will return a string shorter than count.
  284. @param sourceString The string from which to extract a sub-string.
  285. @param start The character index at which the extraction starts.
  286. @param count The length of the sub-string to extract.
  287. @return Returns a string made up of the character at start in sourceString and ending at the end of the original sourceString, or start + count, whichever comes first.
  288. @sa strchr
  289. */
  290. ConsoleFunctionWithDocs(getSubStr, ConsoleString, 4, 4, ( sourceString , start , count ))
  291. {
  292. TORQUE_UNUSED( argc );
  293. // Returns the substring of argv[1], starting at argv[2], and continuing
  294. // to either the end of the string, or argv[3] characters, whichever
  295. // comes first.
  296. //
  297. S32 startPos = dAtoi(argv[2]);
  298. S32 desiredLen = dAtoi(argv[3]);
  299. if (startPos < 0 || desiredLen < 0) {
  300. Con::errorf(ConsoleLogEntry::Script, "getSubStr(...): error, starting position and desired length must be >= 0: (%d, %d)", startPos, desiredLen);
  301. return "";
  302. }
  303. S32 baseLen = dStrlen(argv[1]);
  304. if (baseLen < startPos)
  305. return "";
  306. U32 actualLen = desiredLen;
  307. if (startPos + desiredLen > baseLen)
  308. actualLen = baseLen - startPos;
  309. char *ret = Con::getReturnBuffer(actualLen + 1);
  310. dStrncpy(ret, argv[1] + startPos, actualLen);
  311. ret[actualLen] = '\0';
  312. return ret;
  313. }
  314. // Used?
  315. /*! Removes all spaces after the final
  316. @param string from which to remove trailing spaces
  317. @return the source string minus trailing spaces
  318. */
  319. ConsoleFunctionWithDocs( stripTrailingSpaces, ConsoleString, 2, 2, ( string ))
  320. {
  321. TORQUE_UNUSED( argc );
  322. S32 temp = S32(dStrlen( argv[1] ));
  323. if ( temp )
  324. {
  325. while ( temp >= 1 && ( argv[1][temp - 1] == ' ' || argv[1][temp - 1] == '_' ) )
  326. temp--;
  327. if ( temp )
  328. {
  329. char* returnString = Con::getReturnBuffer( temp + 1 );
  330. dStrncpy( returnString, argv[1], U32(temp) );
  331. returnString[temp] = '\0';
  332. return( returnString );
  333. }
  334. }
  335. return( "" );
  336. }
  337. /*! @} */ // group StringFunctions