StringExtensions.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. //using System;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Security;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace Godot
  9. {
  10. public static class StringExtensions
  11. {
  12. private static int GetSliceCount(this string instance, string splitter)
  13. {
  14. if (instance.Empty() || splitter.Empty())
  15. return 0;
  16. int pos = 0;
  17. int slices = 1;
  18. while ((pos = instance.Find(splitter, pos)) >= 0)
  19. {
  20. slices++;
  21. pos += splitter.Length;
  22. }
  23. return slices;
  24. }
  25. private static string GetSlicec(this string instance, char splitter, int slice)
  26. {
  27. if (!instance.Empty() && slice >= 0)
  28. {
  29. int i = 0;
  30. int prev = 0;
  31. int count = 0;
  32. while (true)
  33. {
  34. if (instance[i] == 0 || instance[i] == splitter)
  35. {
  36. if (slice == count)
  37. {
  38. return instance.Substring(prev, i - prev);
  39. }
  40. count++;
  41. prev = i + 1;
  42. }
  43. i++;
  44. }
  45. }
  46. return string.Empty;
  47. }
  48. // <summary>
  49. // If the string is a path to a file, return the path to the file without the extension.
  50. // </summary>
  51. public static string Basename(this string instance)
  52. {
  53. int index = instance.LastIndexOf('.');
  54. if (index > 0)
  55. return instance.Substring(0, index);
  56. return instance;
  57. }
  58. // <summary>
  59. // Return true if the strings begins with the given string.
  60. // </summary>
  61. public static bool BeginsWith(this string instance, string text)
  62. {
  63. return instance.StartsWith(text);
  64. }
  65. // <summary>
  66. // Return the bigrams (pairs of consecutive letters) of this string.
  67. // </summary>
  68. public static string[] Bigrams(this string instance)
  69. {
  70. var b = new string[instance.Length - 1];
  71. for (int i = 0; i < b.Length; i++)
  72. {
  73. b[i] = instance.Substring(i, 2);
  74. }
  75. return b;
  76. }
  77. // <summary>
  78. // Return a copy of the string with special characters escaped using the C language standard.
  79. // </summary>
  80. public static string CEscape(this string instance)
  81. {
  82. var sb = new StringBuilder(string.Copy(instance));
  83. sb.Replace("\\", "\\\\");
  84. sb.Replace("\a", "\\a");
  85. sb.Replace("\b", "\\b");
  86. sb.Replace("\f", "\\f");
  87. sb.Replace("\n", "\\n");
  88. sb.Replace("\r", "\\r");
  89. sb.Replace("\t", "\\t");
  90. sb.Replace("\v", "\\v");
  91. sb.Replace("\'", "\\'");
  92. sb.Replace("\"", "\\\"");
  93. sb.Replace("?", "\\?");
  94. return sb.ToString();
  95. }
  96. // <summary>
  97. // Return a copy of the string with escaped characters replaced by their meanings according to the C language standard.
  98. // </summary>
  99. public static string CUnescape(this string instance)
  100. {
  101. var sb = new StringBuilder(string.Copy(instance));
  102. sb.Replace("\\a", "\a");
  103. sb.Replace("\\b", "\b");
  104. sb.Replace("\\f", "\f");
  105. sb.Replace("\\n", "\n");
  106. sb.Replace("\\r", "\r");
  107. sb.Replace("\\t", "\t");
  108. sb.Replace("\\v", "\v");
  109. sb.Replace("\\'", "\'");
  110. sb.Replace("\\\"", "\"");
  111. sb.Replace("\\?", "?");
  112. sb.Replace("\\\\", "\\");
  113. return sb.ToString();
  114. }
  115. // <summary>
  116. // Change the case of some letters. Replace underscores with spaces, convert all letters to lowercase then capitalize first and every letter following the space character. For [code]capitalize camelCase mixed_with_underscores[/code] it will return [code]Capitalize Camelcase Mixed With Underscores[/code].
  117. // </summary>
  118. public static string Capitalize(this string instance)
  119. {
  120. string aux = instance.Replace("_", " ").ToLower();
  121. var cap = string.Empty;
  122. for (int i = 0; i < aux.GetSliceCount(" "); i++)
  123. {
  124. string slice = aux.GetSlicec(' ', i);
  125. if (slice.Length > 0)
  126. {
  127. slice = char.ToUpper(slice[0]) + slice.Substring(1);
  128. if (i > 0)
  129. cap += " ";
  130. cap += slice;
  131. }
  132. }
  133. return cap;
  134. }
  135. // <summary>
  136. // Perform a case-sensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.
  137. // </summary>
  138. public static int CasecmpTo(this string instance, string to)
  139. {
  140. if (instance.Empty())
  141. return to.Empty() ? 0 : -1;
  142. if (to.Empty())
  143. return 1;
  144. int instance_idx = 0;
  145. int to_idx = 0;
  146. while (true)
  147. {
  148. if (to[to_idx] == 0 && instance[instance_idx] == 0)
  149. return 0; // We're equal
  150. if (instance[instance_idx] == 0)
  151. return -1; // If this is empty, and the other one is not, then we're less... I think?
  152. if (to[to_idx] == 0)
  153. return 1; // Otherwise the other one is smaller...
  154. if (instance[instance_idx] < to[to_idx]) // More than
  155. return -1;
  156. if (instance[instance_idx] > to[to_idx]) // Less than
  157. return 1;
  158. instance_idx++;
  159. to_idx++;
  160. }
  161. }
  162. // <summary>
  163. // Return true if the string is empty.
  164. // </summary>
  165. public static bool Empty(this string instance)
  166. {
  167. return string.IsNullOrEmpty(instance);
  168. }
  169. // <summary>
  170. // Return true if the strings ends with the given string.
  171. // </summary>
  172. public static bool EndsWith(this string instance, string text)
  173. {
  174. return instance.EndsWith(text);
  175. }
  176. // <summary>
  177. // Erase [code]chars[/code] characters from the string starting from [code]pos[/code].
  178. // </summary>
  179. public static void Erase(this StringBuilder instance, int pos, int chars)
  180. {
  181. instance.Remove(pos, chars);
  182. }
  183. // <summary>
  184. // If the string is a path to a file, return the extension.
  185. // </summary>
  186. public static string Extension(this string instance)
  187. {
  188. int pos = instance.FindLast(".");
  189. if (pos < 0)
  190. return instance;
  191. return instance.Substring(pos + 1);
  192. }
  193. // <summary>
  194. // Find the first occurrence of a substring, return the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed.
  195. // </summary>
  196. public static int Find(this string instance, string what, int from = 0)
  197. {
  198. return instance.IndexOf(what, StringComparison.OrdinalIgnoreCase);
  199. }
  200. // <summary>
  201. // Find the last occurrence of a substring, return the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed.
  202. // </summary>
  203. public static int FindLast(this string instance, string what)
  204. {
  205. return instance.LastIndexOf(what, StringComparison.OrdinalIgnoreCase);
  206. }
  207. // <summary>
  208. // Find the first occurrence of a substring but search as case-insensitive, return the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed.
  209. // </summary>
  210. public static int FindN(this string instance, string what, int from = 0)
  211. {
  212. return instance.IndexOf(what, StringComparison.Ordinal);
  213. }
  214. // <summary>
  215. // If the string is a path to a file, return the base directory.
  216. // </summary>
  217. public static string GetBaseDir(this string instance)
  218. {
  219. int basepos = instance.Find("://");
  220. string rs;
  221. var @base = string.Empty;
  222. if (basepos != -1)
  223. {
  224. var end = basepos + 3;
  225. rs = instance.Substring(end, instance.Length);
  226. @base = instance.Substring(0, end);
  227. }
  228. else
  229. {
  230. if (instance.BeginsWith("/"))
  231. {
  232. rs = instance.Substring(1, instance.Length);
  233. @base = "/";
  234. }
  235. else
  236. {
  237. rs = instance;
  238. }
  239. }
  240. int sep = Mathf.Max(rs.FindLast("/"), rs.FindLast("\\"));
  241. if (sep == -1)
  242. return @base;
  243. return @base + rs.Substr(0, sep);
  244. }
  245. // <summary>
  246. // If the string is a path to a file, return the file and ignore the base directory.
  247. // </summary>
  248. public static string GetFile(this string instance)
  249. {
  250. int sep = Mathf.Max(instance.FindLast("/"), instance.FindLast("\\"));
  251. if (sep == -1)
  252. return instance;
  253. return instance.Substring(sep + 1, instance.Length);
  254. }
  255. // <summary>
  256. // Hash the string and return a 32 bits integer.
  257. // </summary>
  258. public static int Hash(this string instance)
  259. {
  260. int index = 0;
  261. int hashv = 5381;
  262. int c;
  263. while ((c = instance[index++]) != 0)
  264. hashv = (hashv << 5) + hashv + c; // hash * 33 + c
  265. return hashv;
  266. }
  267. // <summary>
  268. // Convert a string containing an hexadecimal number into an int.
  269. // </summary>
  270. public static int HexToInt(this string instance)
  271. {
  272. int sign = 1;
  273. if (instance[0] == '-')
  274. {
  275. sign = -1;
  276. instance = instance.Substring(1);
  277. }
  278. if (!instance.StartsWith("0x"))
  279. return 0;
  280. return sign * int.Parse(instance.Substring(2), NumberStyles.HexNumber);
  281. }
  282. // <summary>
  283. // Insert a substring at a given position.
  284. // </summary>
  285. public static string Insert(this string instance, int pos, string what)
  286. {
  287. return instance.Insert(pos, what);
  288. }
  289. // <summary>
  290. // If the string is a path to a file or directory, return true if the path is absolute.
  291. // </summary>
  292. public static bool IsAbsPath(this string instance)
  293. {
  294. return System.IO.Path.IsPathRooted(instance);
  295. }
  296. // <summary>
  297. // If the string is a path to a file or directory, return true if the path is relative.
  298. // </summary>
  299. public static bool IsRelPath(this string instance)
  300. {
  301. return !System.IO.Path.IsPathRooted(instance);
  302. }
  303. // <summary>
  304. // Check whether this string is a subsequence of the given string.
  305. // </summary>
  306. public static bool IsSubsequenceOf(this string instance, string text, bool case_insensitive)
  307. {
  308. int len = instance.Length;
  309. if (len == 0)
  310. return true; // Technically an empty string is subsequence of any string
  311. if (len > text.Length)
  312. return false;
  313. int src = 0;
  314. int tgt = 0;
  315. while (instance[src] != 0 && text[tgt] != 0)
  316. {
  317. bool match;
  318. if (case_insensitive)
  319. {
  320. char srcc = char.ToLower(instance[src]);
  321. char tgtc = char.ToLower(text[tgt]);
  322. match = srcc == tgtc;
  323. }
  324. else
  325. {
  326. match = instance[src] == text[tgt];
  327. }
  328. if (match)
  329. {
  330. src++;
  331. if (instance[src] == 0)
  332. return true;
  333. }
  334. tgt++;
  335. }
  336. return false;
  337. }
  338. // <summary>
  339. // Check whether this string is a subsequence of the given string, considering case.
  340. // </summary>
  341. public static bool IsSubsequenceOf(this string instance, string text)
  342. {
  343. return instance.IsSubsequenceOf(text, false);
  344. }
  345. // <summary>
  346. // Check whether this string is a subsequence of the given string, without considering case.
  347. // </summary>
  348. public static bool IsSubsequenceOfI(this string instance, string text)
  349. {
  350. return instance.IsSubsequenceOf(text, true);
  351. }
  352. // <summary>
  353. // Check whether the string contains a valid float.
  354. // </summary>
  355. public static bool IsValidFloat(this string instance)
  356. {
  357. float f;
  358. return float.TryParse(instance, out f);
  359. }
  360. // <summary>
  361. // Check whether the string contains a valid color in HTML notation.
  362. // </summary>
  363. public static bool IsValidHtmlColor(this string instance)
  364. {
  365. return Color.HtmlIsValid(instance);
  366. }
  367. // <summary>
  368. // Check whether the string is a valid identifier. As is common in programming languages, a valid identifier may contain only letters, digits and underscores (_) and the first character may not be a digit.
  369. // </summary>
  370. public static bool IsValidIdentifier(this string instance)
  371. {
  372. int len = instance.Length;
  373. if (len == 0)
  374. return false;
  375. for (int i = 0; i < len; i++)
  376. {
  377. if (i == 0)
  378. {
  379. if (instance[0] >= '0' && instance[0] <= '9')
  380. return false; // Don't start with number plz
  381. }
  382. bool valid_char = instance[i] >= '0' &&
  383. instance[i] <= '9' || instance[i] >= 'a' &&
  384. instance[i] <= 'z' || instance[i] >= 'A' &&
  385. instance[i] <= 'Z' || instance[i] == '_';
  386. if (!valid_char)
  387. return false;
  388. }
  389. return true;
  390. }
  391. // <summary>
  392. // Check whether the string contains a valid integer.
  393. // </summary>
  394. public static bool IsValidInteger(this string instance)
  395. {
  396. int f;
  397. return int.TryParse(instance, out f);
  398. }
  399. // <summary>
  400. // Check whether the string contains a valid IP address.
  401. // </summary>
  402. public static bool IsValidIpAddress(this string instance)
  403. {
  404. string[] ip = instance.Split(".");
  405. if (ip.Length != 4)
  406. return false;
  407. for (int i = 0; i < ip.Length; i++)
  408. {
  409. string n = ip[i];
  410. if (!n.IsValidInteger())
  411. return false;
  412. int val = n.ToInt();
  413. if (val < 0 || val > 255)
  414. return false;
  415. }
  416. return true;
  417. }
  418. // <summary>
  419. // Return a copy of the string with special characters escaped using the JSON standard.
  420. // </summary>
  421. public static string JsonEscape(this string instance)
  422. {
  423. var sb = new StringBuilder(string.Copy(instance));
  424. sb.Replace("\\", "\\\\");
  425. sb.Replace("\b", "\\b");
  426. sb.Replace("\f", "\\f");
  427. sb.Replace("\n", "\\n");
  428. sb.Replace("\r", "\\r");
  429. sb.Replace("\t", "\\t");
  430. sb.Replace("\v", "\\v");
  431. sb.Replace("\"", "\\\"");
  432. return sb.ToString();
  433. }
  434. // <summary>
  435. // Return an amount of characters from the left of the string.
  436. // </summary>
  437. public static string Left(this string instance, int pos)
  438. {
  439. if (pos <= 0)
  440. return string.Empty;
  441. if (pos >= instance.Length)
  442. return instance;
  443. return instance.Substring(0, pos);
  444. }
  445. /// <summary>
  446. /// Return the length of the string in characters.
  447. /// </summary>
  448. public static int Length(this string instance)
  449. {
  450. return instance.Length;
  451. }
  452. // <summary>
  453. // Do a simple expression match, where '*' matches zero or more arbitrary characters and '?' matches any single character except '.'.
  454. // </summary>
  455. public static bool ExprMatch(this string instance, string expr, bool caseSensitive)
  456. {
  457. if (expr.Length == 0 || instance.Length == 0)
  458. return false;
  459. switch (expr[0])
  460. {
  461. case '\0':
  462. return instance[0] == 0;
  463. case '*':
  464. return ExprMatch(expr + 1, instance, caseSensitive) || instance[0] != 0 && ExprMatch(expr, instance + 1, caseSensitive);
  465. case '?':
  466. return instance[0] != 0 && instance[0] != '.' && ExprMatch(expr + 1, instance + 1, caseSensitive);
  467. default:
  468. return (caseSensitive ? instance[0] == expr[0] : char.ToUpper(instance[0]) == char.ToUpper(expr[0])) &&
  469. ExprMatch(expr + 1, instance + 1, caseSensitive);
  470. }
  471. }
  472. // <summary>
  473. // Do a simple case sensitive expression match, using ? and * wildcards (see [method expr_match]).
  474. // </summary>
  475. public static bool Match(this string instance, string expr)
  476. {
  477. return instance.ExprMatch(expr, true);
  478. }
  479. // <summary>
  480. // Do a simple case insensitive expression match, using ? and * wildcards (see [method expr_match]).
  481. // </summary>
  482. public static bool Matchn(this string instance, string expr)
  483. {
  484. return instance.ExprMatch(expr, false);
  485. }
  486. // <summary>
  487. // Return the MD5 hash of the string as an array of bytes.
  488. // </summary>
  489. public static byte[] Md5Buffer(this string instance)
  490. {
  491. return NativeCalls.godot_icall_String_md5_buffer(instance);
  492. }
  493. // <summary>
  494. // Return the MD5 hash of the string as a string.
  495. // </summary>
  496. public static string Md5Text(this string instance)
  497. {
  498. return NativeCalls.godot_icall_String_md5_text(instance);
  499. }
  500. // <summary>
  501. // Perform a case-insensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.
  502. // </summary>
  503. public static int NocasecmpTo(this string instance, string to)
  504. {
  505. if (instance.Empty())
  506. return to.Empty() ? 0 : -1;
  507. if (to.Empty())
  508. return 1;
  509. int instance_idx = 0;
  510. int to_idx = 0;
  511. while (true)
  512. {
  513. if (to[to_idx] == 0 && instance[instance_idx] == 0)
  514. return 0; // We're equal
  515. if (instance[instance_idx] == 0)
  516. return -1; // If this is empty, and the other one is not, then we're less... I think?
  517. if (to[to_idx] == 0)
  518. return 1; // Otherwise the other one is smaller..
  519. if (char.ToUpper(instance[instance_idx]) < char.ToUpper(to[to_idx])) // More than
  520. return -1;
  521. if (char.ToUpper(instance[instance_idx]) > char.ToUpper(to[to_idx])) // Less than
  522. return 1;
  523. instance_idx++;
  524. to_idx++;
  525. }
  526. }
  527. // <summary>
  528. // Return the character code at position [code]at[/code].
  529. // </summary>
  530. public static int OrdAt(this string instance, int at)
  531. {
  532. return instance[at];
  533. }
  534. // <summary>
  535. // Format a number to have an exact number of [code]digits[/code] after the decimal point.
  536. // </summary>
  537. public static string PadDecimals(this string instance, int digits)
  538. {
  539. int c = instance.Find(".");
  540. if (c == -1)
  541. {
  542. if (digits <= 0)
  543. return instance;
  544. instance += ".";
  545. c = instance.Length - 1;
  546. }
  547. else
  548. {
  549. if (digits <= 0)
  550. return instance.Substring(0, c);
  551. }
  552. if (instance.Length - (c + 1) > digits)
  553. {
  554. instance = instance.Substring(0, c + digits + 1);
  555. }
  556. else
  557. {
  558. while (instance.Length - (c + 1) < digits)
  559. {
  560. instance += "0";
  561. }
  562. }
  563. return instance;
  564. }
  565. // <summary>
  566. // Format a number to have an exact number of [code]digits[/code] before the decimal point.
  567. // </summary>
  568. public static string PadZeros(this string instance, int digits)
  569. {
  570. string s = instance;
  571. int end = s.Find(".");
  572. if (end == -1)
  573. end = s.Length;
  574. if (end == 0)
  575. return s;
  576. int begin = 0;
  577. while (begin < end && (s[begin] < '0' || s[begin] > '9'))
  578. {
  579. begin++;
  580. }
  581. if (begin >= end)
  582. return s;
  583. while (end - begin < digits)
  584. {
  585. s = s.Insert(begin, "0");
  586. end++;
  587. }
  588. return s;
  589. }
  590. // <summary>
  591. // Decode a percent-encoded string. See [method percent_encode].
  592. // </summary>
  593. public static string PercentDecode(this string instance)
  594. {
  595. return Uri.UnescapeDataString(instance);
  596. }
  597. // <summary>
  598. // Percent-encode a string. This is meant to encode parameters in a URL when sending a HTTP GET request and bodies of form-urlencoded POST request.
  599. // </summary>
  600. public static string PercentEncode(this string instance)
  601. {
  602. return Uri.EscapeDataString(instance);
  603. }
  604. // <summary>
  605. // If the string is a path, this concatenates [code]file[/code] at the end of the string as a subpath. E.g. [code]"this/is".plus_file("path") == "this/is/path"[/code].
  606. // </summary>
  607. public static string PlusFile(this string instance, string file)
  608. {
  609. if (instance.Length > 0 && instance[instance.Length - 1] == '/')
  610. return instance + file;
  611. return instance + "/" + file;
  612. }
  613. // <summary>
  614. // Replace occurrences of a substring for different ones inside the string.
  615. // </summary>
  616. public static string Replace(this string instance, string what, string forwhat)
  617. {
  618. return instance.Replace(what, forwhat);
  619. }
  620. // <summary>
  621. // Replace occurrences of a substring for different ones inside the string, but search case-insensitive.
  622. // </summary>
  623. public static string Replacen(this string instance, string what, string forwhat)
  624. {
  625. return Regex.Replace(instance, what, forwhat, RegexOptions.IgnoreCase);
  626. }
  627. // <summary>
  628. // Perform a search for a substring, but start from the end of the string instead of the beginning.
  629. // </summary>
  630. public static int Rfind(this string instance, string what, int from = -1)
  631. {
  632. return NativeCalls.godot_icall_String_rfind(instance, what, from);
  633. }
  634. // <summary>
  635. // Perform a search for a substring, but start from the end of the string instead of the beginning. Also search case-insensitive.
  636. // </summary>
  637. public static int Rfindn(this string instance, string what, int from = -1)
  638. {
  639. return NativeCalls.godot_icall_String_rfindn(instance, what, from);
  640. }
  641. // <summary>
  642. // Return the right side of the string from a given position.
  643. // </summary>
  644. public static string Right(this string instance, int pos)
  645. {
  646. if (pos >= instance.Length)
  647. return instance;
  648. if (pos < 0)
  649. return string.Empty;
  650. return instance.Substring(pos, instance.Length - pos);
  651. }
  652. public static byte[] Sha256Buffer(this string instance)
  653. {
  654. return NativeCalls.godot_icall_String_sha256_buffer(instance);
  655. }
  656. // <summary>
  657. // Return the SHA-256 hash of the string as a string.
  658. // </summary>
  659. public static string Sha256Text(this string instance)
  660. {
  661. return NativeCalls.godot_icall_String_sha256_text(instance);
  662. }
  663. // <summary>
  664. // Return the similarity index of the text compared to this string. 1 means totally similar and 0 means totally dissimilar.
  665. // </summary>
  666. public static float Similarity(this string instance, string text)
  667. {
  668. if (instance == text)
  669. {
  670. // Equal strings are totally similar
  671. return 1.0f;
  672. }
  673. if (instance.Length < 2 || text.Length < 2)
  674. {
  675. // No way to calculate similarity without a single bigram
  676. return 0.0f;
  677. }
  678. string[] srcBigrams = instance.Bigrams();
  679. string[] tgtBigrams = text.Bigrams();
  680. int src_size = srcBigrams.Length;
  681. int tgt_size = tgtBigrams.Length;
  682. float sum = src_size + tgt_size;
  683. float inter = 0;
  684. for (int i = 0; i < src_size; i++)
  685. {
  686. for (int j = 0; j < tgt_size; j++)
  687. {
  688. if (srcBigrams[i] == tgtBigrams[j])
  689. {
  690. inter++;
  691. break;
  692. }
  693. }
  694. }
  695. return 2.0f * inter / sum;
  696. }
  697. // <summary>
  698. // Split the string by a divisor string, return an array of the substrings. Example "One,Two,Three" will return ["One","Two","Three"] if split by ",".
  699. // </summary>
  700. public static string[] Split(this string instance, string divisor, bool allow_empty = true)
  701. {
  702. return instance.Split(new[] { divisor }, StringSplitOptions.RemoveEmptyEntries);
  703. }
  704. // <summary>
  705. // Split the string in floats by using a divisor string, return an array of the substrings. Example "1,2.5,3" will return [1,2.5,3] if split by ",".
  706. // </summary>
  707. public static float[] SplitFloats(this string instance, string divisor, bool allow_empty = true)
  708. {
  709. var ret = new List<float>();
  710. int from = 0;
  711. int len = instance.Length;
  712. while (true)
  713. {
  714. int end = instance.Find(divisor, from);
  715. if (end < 0)
  716. end = len;
  717. if (allow_empty || end > from)
  718. ret.Add(float.Parse(instance.Substring(from)));
  719. if (end == len)
  720. break;
  721. from = end + divisor.Length;
  722. }
  723. return ret.ToArray();
  724. }
  725. private static readonly char[] non_printable = {
  726. (char)00, (char)01, (char)02, (char)03, (char)04, (char)05,
  727. (char)06, (char)07, (char)08, (char)09, (char)10, (char)11,
  728. (char)12, (char)13, (char)14, (char)15, (char)16, (char)17,
  729. (char)18, (char)19, (char)20, (char)21, (char)22, (char)23,
  730. (char)24, (char)25, (char)26, (char)27, (char)28, (char)29,
  731. (char)30, (char)31, (char)32
  732. };
  733. // <summary>
  734. // Return a copy of the string stripped of any non-printable character at the beginning and the end. The optional arguments are used to toggle stripping on the left and right edges respectively.
  735. // </summary>
  736. public static string StripEdges(this string instance, bool left = true, bool right = true)
  737. {
  738. if (left)
  739. {
  740. if (right)
  741. return instance.Trim(non_printable);
  742. return instance.TrimStart(non_printable);
  743. }
  744. return instance.TrimEnd(non_printable);
  745. }
  746. // <summary>
  747. // Return part of the string from the position [code]from[/code], with length [code]len[/code].
  748. // </summary>
  749. public static string Substr(this string instance, int from, int len)
  750. {
  751. return instance.Substring(from, len);
  752. }
  753. // <summary>
  754. // Convert the String (which is a character array) to PoolByteArray (which is an array of bytes). The conversion is speeded up in comparison to to_utf8() with the assumption that all the characters the String contains are only ASCII characters.
  755. // </summary>
  756. public static byte[] ToAscii(this string instance)
  757. {
  758. return Encoding.ASCII.GetBytes(instance);
  759. }
  760. // <summary>
  761. // Convert a string, containing a decimal number, into a [code]float[/code].
  762. // </summary>
  763. public static float ToFloat(this string instance)
  764. {
  765. return float.Parse(instance);
  766. }
  767. // <summary>
  768. // Convert a string, containing an integer number, into an [code]int[/code].
  769. // </summary>
  770. public static int ToInt(this string instance)
  771. {
  772. return int.Parse(instance);
  773. }
  774. // <summary>
  775. // Return the string converted to lowercase.
  776. // </summary>
  777. public static string ToLower(this string instance)
  778. {
  779. return instance.ToLower();
  780. }
  781. // <summary>
  782. // Return the string converted to uppercase.
  783. // </summary>
  784. public static string ToUpper(this string instance)
  785. {
  786. return instance.ToUpper();
  787. }
  788. // <summary>
  789. // Convert the String (which is an array of characters) to PoolByteArray (which is an array of bytes). The conversion is a bit slower than to_ascii(), but supports all UTF-8 characters. Therefore, you should prefer this function over to_ascii().
  790. // </summary>
  791. public static byte[] ToUtf8(this string instance)
  792. {
  793. return Encoding.UTF8.GetBytes(instance);
  794. }
  795. // <summary>
  796. // Return a copy of the string with special characters escaped using the XML standard.
  797. // </summary>
  798. public static string XmlEscape(this string instance)
  799. {
  800. return SecurityElement.Escape(instance);
  801. }
  802. // <summary>
  803. // Return a copy of the string with escaped characters replaced by their meanings according to the XML standard.
  804. // </summary>
  805. public static string XmlUnescape(this string instance)
  806. {
  807. return SecurityElement.FromString(instance).Text;
  808. }
  809. }
  810. }