enstring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /**
  2. * \defgroup Strings Strings
  3. * @{
  4. */
  5. class string
  6. {
  7. static const string Empty;
  8. /**
  9. \brief Converts string to integer
  10. \return \p int - Converted \p string.
  11. @code
  12. string str = "56";
  13. int i = str.ToInt();
  14. Print(i);
  15. >> i = 56
  16. @endcode
  17. */
  18. proto native int ToInt();
  19. /**
  20. \brief Converts string to integer
  21. \return \p int - Converted \p string.
  22. @code
  23. string str = "0xFF";
  24. int i = str.HexToInt();
  25. Print(i);
  26. >> i = 255
  27. @endcode
  28. */
  29. proto native int HexToInt();
  30. /**
  31. \brief Converts string to float
  32. \return \p float - Converted \p string \p in float.
  33. @code
  34. string str = "56.6";
  35. float f = str.ToFloat();
  36. Print(f);
  37. >> f = 56.6
  38. @endcode
  39. */
  40. proto native float ToFloat();
  41. /**
  42. \brief Returns a vector from a string
  43. \return \p vector Converted s as vector
  44. @code
  45. string str = "1 0 1";
  46. vector v = str.ToVector();
  47. Print(v);
  48. >> v = <1,0,1>
  49. @endcode
  50. */
  51. proto vector ToVector();
  52. /**
  53. \brief Convert beautified string into a vector
  54. \param vec \p beautified string
  55. \return \p vector resulting vector
  56. */
  57. vector BeautifiedToVector()
  58. {
  59. string copy = value;
  60. copy.Replace("<", "");
  61. copy.Replace(">", "");
  62. copy.Replace(",", " ");
  63. return copy.ToVector();
  64. }
  65. /**
  66. \brief Converts string's first character to ASCII code
  67. \param str String for convert to ASCII code
  68. \return \p ascii code \p int.
  69. @code
  70. int ascii = "M".ToAscii();
  71. Print(ascii);
  72. >> ascii = 77
  73. @endcode
  74. */
  75. proto native int ToAscii();
  76. /**
  77. \brief Returns internal type representation. Can be used in runtime, or cached in variables and used for faster inheritance checking
  78. \returns \p typename Type of class
  79. @code
  80. ???
  81. @endcode
  82. */
  83. proto native typename ToType();
  84. //! Return string representation of variable
  85. static proto string ToString(void var, bool type = false, bool name = false, bool quotes = true);
  86. /**
  87. \brief Substring of 'str' from 'start' position 'len' number of characters
  88. \param start Position in \p str
  89. \param len Count of characters
  90. \return \p string - Substring of \p str
  91. @code
  92. string str = "Hello World";
  93. string strSub = str.Substring(2, 5);
  94. Print(strSub);
  95. >> strSub = llo W
  96. @endcode
  97. */
  98. proto string Substring(int start, int len);
  99. //! Inverted SubString. This deletes everything in between position_start and position_end.
  100. string SubstringInverted( string string_to_split, int position_start, int position_end )
  101. {
  102. string first_half = string_to_split.Substring(0, position_start);
  103. string second_half = string_to_split.Substring( position_end, string_to_split.Length() - position_end );
  104. string result = first_half + second_half;
  105. return result;
  106. }
  107. /**
  108. \brief Substring of 'str' from 'startChar' position 'len' number of characters for UTF8 strings with multibyte chars
  109. \param startChar Position in \p str.
  110. \param len Count of characters
  111. \return \p string - Substring of \p str with \p startChar character and \p len characters
  112. @code
  113. string str = "こんにちは世界";
  114. string strSub = str.SubstringUtf8(2, 4);
  115. Print(strSub);
  116. >> strSub = にちは世
  117. @endcode
  118. */
  119. proto string SubstringUtf8(int startChar, int len);
  120. /**
  121. \brief Replace all occurrances of 'sample' in 'str' by 'replace'
  122. \param sample string to search in \p str
  123. \param replace string which replace \p sample in \p str
  124. \return \p int - number of occurrances of 'sample' in 'str'
  125. @code
  126. string test = "If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.";
  127. Print(test);
  128. int count = test.Replace("the", "*");
  129. Print(count);
  130. Print(test);
  131. >> string test = 'If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.';
  132. >> int count = 4
  133. >> string test = 'If * length of * C string in source is less than num, only * content up to * terminating null-character is copied.'
  134. @endcode
  135. */
  136. proto int Replace(string sample, string replace);
  137. /**
  138. \brief Changes string to lowercase. Returns length
  139. \return \p int - Length of changed string
  140. @code
  141. string str = "Hello World";
  142. int i = str.ToLower();
  143. Print(str);
  144. Print(i);
  145. >> str = hello world
  146. >> i = 11
  147. @endcode
  148. */
  149. proto int ToLower();
  150. /**
  151. \brief Changes string to uppercase. Returns length
  152. \return \p int - Length of changed string
  153. @code
  154. string str = "Hello World";
  155. int i = str.ToUpper();
  156. Print(str);
  157. Print(i);
  158. >> str = HELLO WORLD
  159. >> i = 11
  160. @endcode
  161. */
  162. proto int ToUpper();
  163. /**
  164. \brief Returns length of string
  165. \return \p int - Length of string
  166. @code
  167. string str = "Hello World";
  168. int i = str.Length();
  169. Print(i);
  170. >> i = 11
  171. @endcode
  172. */
  173. proto native int Length();
  174. /**
  175. \brief Returns number of characters in UTF8 string
  176. \return \p int - Number of characters in UTF8 string
  177. @code
  178. string str = "こんにちは世界";
  179. int i = str.LengthUtf8();
  180. Print(i);
  181. >> i = 7
  182. @endcode
  183. */
  184. proto native int LengthUtf8();
  185. /**
  186. \brief Returns hash of string
  187. \return \p int - Hash of string
  188. @code
  189. string str = "Hello World";
  190. int hash = str.Hash();
  191. Print(hash);
  192. @endcode
  193. */
  194. proto native int Hash();
  195. /**
  196. \brief Finds 'sample' in 'str'. Returns -1 when not found
  197. \param sample \p string Finding string
  198. \return \p int - Returns position where \p sample starts, or -1 when \p sample not found
  199. @code
  200. string str = "Hello World";
  201. Print( str.IndexOf( "H" ) );
  202. Print( str.IndexOf( "W" ) );
  203. Print( str.IndexOf( "Q" ) );
  204. >> 0
  205. >> 6
  206. >> -1
  207. @endcode
  208. */
  209. proto native int IndexOf(string sample);
  210. /**
  211. \brief Finds last 'sample' in 'str'. Returns -1 when not found
  212. \param sample \p string Finding string
  213. \return \p int - Returns position where \p sample starts, or -1 when \p sample not found
  214. @code
  215. string str = "Hello World";
  216. Print( str.IndexOf( "l" ) );
  217. >> 9
  218. @endcode
  219. */
  220. proto native int LastIndexOf(string sample);
  221. /**
  222. \brief Finds 'sample' in 'str' from 'start' position. Returns -1 when not found
  223. \param start \p int Start from position
  224. \param sample \p string Finding string expression
  225. \return \p int - Length of string \p s
  226. @code
  227. string str = "Hello World";
  228. Print( str.IndexOfFrom( 3, "H" ) );
  229. Print( str.IndexOfFrom( 3, "W" ) );
  230. Print( str.IndexOfFrom( 3, "Q" ) );
  231. >> -1
  232. >> 6
  233. >> -1
  234. @endcode
  235. */
  236. proto native int IndexOfFrom(int start, string sample);
  237. /**
  238. \brief Returns true if sample is substring of string
  239. \param sample \p string Finding string expression
  240. \return \p bool true if sample is substring of string
  241. @code
  242. string str = "Hello World";
  243. Print( str.IndexOfFrom( 3, "Hello" ) );
  244. Print( str.IndexOfFrom( 3, "Mexico" ) );
  245. >> true
  246. >> false
  247. @endcode
  248. */
  249. bool Contains(string sample)
  250. {
  251. return value.IndexOf(sample) != -1;
  252. }
  253. /**
  254. \brief Returns trimmed string with removed leading and trailing whitespaces
  255. \return \p string - Trimmed string
  256. @code
  257. string str = " Hello World "
  258. Print( str );
  259. Print( str.Trim() );
  260. >> ' Hello World '
  261. >> 'Hello World'
  262. @endcode
  263. */
  264. proto string Trim();
  265. /**
  266. \brief Removes leading and trailing whitespaces in string. Returns length
  267. \return \p int - Count of chars
  268. @code
  269. string str = " Hello World ";
  270. int i = str.TrimInPlace();
  271. Print(str);
  272. Print(i);
  273. >> str = 'Hello World'
  274. >> i = 11
  275. @endcode
  276. */
  277. proto int TrimInPlace();
  278. /**
  279. \brief Parses one token from input string. Result is put into token string, and type of token is returned. Input string is left-truncated by the resulting token length.
  280. \param[out] token \p string Founded string token
  281. \return \p int Type of token
  282. \verbatim
  283. Token types:
  284. 0 - error, no token
  285. 1 - defined token (special characters etc. . / * )
  286. 2 - quoted string. Quotes are removed -> TODO
  287. 3 - alphabetic string
  288. 4 - number
  289. 5 - end of line -> TODO
  290. \endverbatim
  291. @code
  292. string input = "Hello*World";
  293. string token1;
  294. string token2;
  295. int result1 = input.ParseStringEx(token1);
  296. int result2 = input.ParseStringEx(token2);
  297. Print( "Token1 = '" + token1 + "' Type = " + result1.ToString() ) );
  298. Print( "Token2 = '" + token2 + "' Type = " + result2.ToString() ) );
  299. >> 'Toke1 = 'Hello' Type = 3'
  300. >> 'Toke1 = '*' Type = 1'
  301. @endcode
  302. */
  303. proto int ParseStringEx(out string token);
  304. /**
  305. \brief Parses string into array of tokens returns number of tokens
  306. \param[out] tokens \p array[] Parsed string in array
  307. \return \p int Number of tokens
  308. @code
  309. string token[2];
  310. string str = "Hello World";
  311. int result = str.ParseString(token);
  312. for (int i = 0; i < 2; i++)
  313. {
  314. Print(token[i]);
  315. }
  316. >> 'Hello'
  317. >> 'World'
  318. @endcode
  319. */
  320. proto int ParseString(out string tokens[]);
  321. /**
  322. \brief Splits string into array of strings separated by 'sample'
  323. \param sample \p string Strings separator
  324. \return \p TStringArray Array with strings
  325. @code
  326. string example = "The quick brown fox jumps over the lazy dog";
  327. TStringArray strs = new TStringArray;
  328. example.Split(" ", strs);
  329. for (int i = 0; i < strs.Count(); i++)
  330. {
  331. Print(strs.Get(i));
  332. }
  333. >> 'The'
  334. >> 'quick'
  335. >> 'brown'
  336. >> 'fox'
  337. >> 'jumps'
  338. >> 'over'
  339. >> 'the'
  340. >> 'lazy'
  341. >> 'dog'
  342. @endcode
  343. */
  344. void Split(string sample, out array<string> output)
  345. {
  346. int txt_len = 0;
  347. int sep_pos = -1;
  348. int nxt_sep_pos = 0;
  349. string text = "";
  350. bool line_end = false;
  351. while (line_end == false)
  352. {
  353. sep_pos = sep_pos + txt_len + 1;
  354. nxt_sep_pos = value.IndexOfFrom(sep_pos, sample);
  355. if ( nxt_sep_pos == -1 )
  356. {
  357. nxt_sep_pos = value.Length();
  358. line_end = true;
  359. }
  360. txt_len = nxt_sep_pos - sep_pos;
  361. if ( txt_len > 0 )
  362. {
  363. text = value.Substring(sep_pos, txt_len);
  364. output.Insert(text);
  365. }
  366. }
  367. }
  368. // !Joins array of strings into a single string, separated by 'separator'. Inverse of Split
  369. static string Join(string separator, notnull TStringArray tokens)
  370. {
  371. string output;
  372. foreach (int i, string s: tokens)
  373. {
  374. if (i != 0)
  375. output += separator;
  376. output += s;
  377. }
  378. return output;
  379. }
  380. /**
  381. \brief Gets n-th character from string
  382. \param index character index
  383. \return \p string character on index-th position in string
  384. \warning VME When index less than 0 or greater than string length
  385. @code
  386. string str = "Hello World";
  387. Print( str[4] ); // Print( str.Get(4) );
  388. >> 'o'
  389. @endcode
  390. */
  391. proto string Get(int index);
  392. /**
  393. \brief Sets the n-th character in string with the input, replacing previous value
  394. \param index index to be replaced
  395. \param input single non-terminated character value to replace with
  396. \warning VME When index less than 0 or greater than string length
  397. \warning (Diag) VME When string is empty or greater than length of 1
  398. (Retail) Calls 'string.Insert' except it replaces only the initial character
  399. @code
  400. string str = "Hello World";
  401. str[4] = "O";
  402. Print( str );
  403. >> 'HellO World'
  404. @endcode
  405. @code
  406. string str = "Hello World";
  407. str[6] = "Test ";
  408. Print( str );
  409. >> 'Hello Test orld'
  410. @endcode
  411. */
  412. proto void Set(int index, string input);
  413. #ifdef DIAG_DEVELOPER
  414. /**
  415. \brief Do not use. Re-implemented for verification of new function due to design of previous function allowing for unexpected behavior that modders may depend on.
  416. \see string.Set warnings for handling existing mods in Retail
  417. @code
  418. string str = "Hello World";
  419. str.OldSet(6, "Test ");
  420. Print( str );
  421. >> 'Hello Test orld'
  422. @endcode
  423. */
  424. void OldSet(int n, string _value)
  425. {
  426. string pre = value.Substring(0, n);
  427. n += 1;
  428. int length = value.Length() - n;
  429. string post = value.Substring(n, length);
  430. value = pre + _value + post;
  431. }
  432. #endif
  433. /**
  434. \brief Inserts a string into the n-th index, increasing the string length by the size of the input
  435. \param index index to insert at
  436. \param input string value to insert with
  437. \warning VME When index less than 0 or greater than string length
  438. \warning VME When string is empty
  439. @code
  440. string str = "Hello World";
  441. str.Insert(6, "Test ");
  442. Print( str );
  443. >> 'Hello Test World'
  444. @endcode
  445. */
  446. proto void Insert(int index, string input);
  447. /**
  448. \brief Gets n-th character from string
  449. \param index character index
  450. \return \p string character on index-th position in string
  451. @code
  452. int a = 5;
  453. float b = 5.99;
  454. string c = "beta";
  455. string test = string.Format("Ahoj %1 = %3 , %2", a, b, c);
  456. Print(test);
  457. >> 'Ahoj 5 = 'beta' , 5.99'
  458. @endcode
  459. */
  460. static proto string Format(string fmt, void param1 = NULL, void param2 = NULL, void param3 = NULL, void param4 = NULL, void param5 = NULL, void param6 = NULL, void param7 = NULL, void param8 = NULL, void param9 = NULL);
  461. };
  462. //@}