No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HeapString.gen.cs 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. 
  2. //------------------------------------------------------------------------------
  3. // <auto-generated>
  4. // This code was generated by a tool.
  5. //
  6. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/HeapString.tt
  7. //
  8. // Changes to this file may cause incorrect behavior and will be lost if
  9. // the code is regenerated.
  10. // </auto-generated>
  11. //------------------------------------------------------------------------------
  12. using System;
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Runtime.InteropServices;
  17. using System.Runtime.CompilerServices;
  18. using Unity.Collections.LowLevel.Unsafe;
  19. using UnityEngine.Internal;
  20. #if UNITY_PROPERTIES_EXISTS
  21. using Unity.Properties;
  22. #endif
  23. namespace Unity.Collections
  24. {
  25. /// <summary>
  26. /// An unmanaged, allocated, mutable, resizable UTF-8 string.
  27. /// </summary>
  28. /// <remarks>
  29. /// The string is always null-terminated, meaning a zero byte always immediately follows the last character.
  30. /// </remarks>
  31. [BurstCompatible]
  32. [Obsolete("HeapString has been removed and replaced with NativeText (RemovedAfter 2021-07-21) (UnityUpgradable) -> NativeText", false)]
  33. public partial struct HeapString
  34. : INativeList<byte>
  35. , IDisposable
  36. , IUTF8Bytes
  37. , IComparable<String>
  38. , IEquatable<String>
  39. , IComparable<HeapString>
  40. , IEquatable<HeapString>
  41. , IComparable<FixedString32Bytes>
  42. , IEquatable<FixedString32Bytes>
  43. , IComparable<FixedString64Bytes>
  44. , IEquatable<FixedString64Bytes>
  45. , IComparable<FixedString128Bytes>
  46. , IEquatable<FixedString128Bytes>
  47. , IComparable<FixedString512Bytes>
  48. , IEquatable<FixedString512Bytes>
  49. , IComparable<FixedString4096Bytes>
  50. , IEquatable<FixedString4096Bytes>
  51. {
  52. // NOTE! This Length is always > 0, because we have a null terminating byte.
  53. // We hide this byte from HeapString users.
  54. private NativeList<byte> m_Data;
  55. /// <summary>
  56. /// The current length in bytes of this string.
  57. /// </summary>
  58. /// <remarks>
  59. /// The length does not include the null terminator byte.
  60. /// </remarks>
  61. /// <value>The current length in bytes of the UTF-8 encoded string.</value>
  62. public int Length
  63. {
  64. get {
  65. return m_Data.Length - 1;
  66. }
  67. set {
  68. m_Data.Resize(value + 1, NativeArrayOptions.UninitializedMemory);
  69. m_Data[value] = 0;
  70. }
  71. }
  72. /// <summary>
  73. /// The current capacity of this string.
  74. /// </summary>
  75. /// <remarks>
  76. /// The null-terminator byte is not included in the Capacity, so the string's character buffer is `Capacity + 1` in size.
  77. /// </remarks>
  78. /// <value>The current capacity of the string.</value>
  79. public int Capacity
  80. {
  81. get {
  82. return m_Data.Capacity - 1;
  83. }
  84. set {
  85. m_Data.Capacity = value + 1;
  86. }
  87. }
  88. /// <summary>
  89. /// Attempt to set the length in bytes of this string.
  90. /// </summary>
  91. /// <param name="newLength">The new length in bytes of the string.</param>
  92. /// <param name="clearOptions">Whether any bytes added should be zeroed out.</param>
  93. /// <returns>Always true.</returns>
  94. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
  95. {
  96. // this can't ever fail, because if we can't resize malloc will abort
  97. Length = newLength;
  98. return true;
  99. }
  100. /// <summary>
  101. /// Whether this string has no characters.
  102. /// </summary>
  103. /// <value>True if this string has no characters.</value>
  104. public bool IsEmpty => m_Data.Length == 1;
  105. /// <summary>
  106. /// Whether this string's character buffer has been allocated and not yet deallocated.
  107. /// </summary>
  108. /// <value>Whether this string's character buffer has been allocated and not yet deallocated.</value>
  109. public bool IsCreated => m_Data.IsCreated;
  110. /// <summary>
  111. /// Returns a pointer to this string's character buffer.
  112. /// </summary>
  113. /// <remarks>
  114. /// The pointer is made invalid by operations that reallocate the character buffer, such as setting <see cref="Capacity"/>.
  115. /// </remarks>
  116. /// <returns>A pointer to this string's character buffer.</returns>
  117. public unsafe byte* GetUnsafePtr()
  118. {
  119. return (byte*) m_Data.GetUnsafePtr();
  120. }
  121. /// <summary>
  122. /// The byte at an index.
  123. /// </summary>
  124. /// <param name="index">A zero-based byte index.</param>
  125. /// <value>The byte at the index.</value>
  126. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  127. public byte this[int index]
  128. {
  129. get {
  130. CheckIndexInRange(index);
  131. return m_Data[index];
  132. }
  133. set {
  134. CheckIndexInRange(index);
  135. m_Data[index] = value;
  136. }
  137. }
  138. /// <summary>
  139. /// Returns the reference to the byte (not character) at an index.
  140. /// </summary>
  141. /// <remarks>
  142. /// Deallocating or reallocating this string's character buffer makes the reference invalid.
  143. /// </remarks>
  144. /// <param name="index">A byte index.</param>
  145. /// <returns>A reference to the byte at the index.</returns>
  146. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  147. public ref byte ElementAt(int index)
  148. {
  149. CheckIndexInRange(index);
  150. return ref m_Data.ElementAt(index);
  151. }
  152. /// <summary>
  153. /// Sets the length to 0.
  154. /// </summary>
  155. public void Clear()
  156. {
  157. Length = 0;
  158. }
  159. /// <summary>
  160. /// Appends a byte.
  161. /// </summary>
  162. /// <remarks>
  163. /// A zero byte will always follow the newly appended byte.
  164. ///
  165. /// No validation is performed: it is your responsibility for the bytes of the string to form valid UTF-8 when you're done appending bytes.
  166. /// </remarks>
  167. /// <param name="value">A byte to append.</param>
  168. public void Add(in byte value)
  169. {
  170. this[Length++] = value;
  171. }
  172. /// <summary>
  173. /// Returns the lexicographical sort order of this string relative to another.
  174. /// </summary>
  175. /// <param name="other">Another string to compare with.</param>
  176. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  177. ///
  178. /// 0 denotes both strings have the same sort position.<br/>
  179. /// -1 denotes that this string should be sorted to precede the other.<br/>
  180. /// +1 denotes that this string should be sorted to follow the other.<br/>
  181. /// </returns>
  182. public int CompareTo(HeapString other)
  183. {
  184. return FixedStringMethods.CompareTo(ref this, other);
  185. }
  186. /// <summary>
  187. /// Returns true if this string and another are equal.
  188. /// </summary>
  189. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  190. /// <param name="other">Another string to compare with.</param>
  191. /// <returns>True if the two strings are equal.</returns>
  192. public bool Equals(HeapString other)
  193. {
  194. return FixedStringMethods.Equals(ref this, other);
  195. }
  196. /// <summary>
  197. /// Releases all resources (memory and safety handles).
  198. /// </summary>
  199. public void Dispose()
  200. {
  201. m_Data.Dispose();
  202. }
  203. /// <summary>
  204. /// A copy of this string as a managed string.
  205. /// </summary>
  206. /// <remarks>
  207. /// For internal use only. Use <see cref="ToString"/> instead.
  208. /// </remarks>
  209. /// <value>A copy of this string as a managed string.</value>
  210. [CreateProperty]
  211. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  212. [NotBurstCompatible]
  213. public string Value => ToString();
  214. /// <summary>
  215. /// An enumerator over the characters (not bytes) of a HeapString.
  216. /// </summary>
  217. /// <remarks>
  218. /// In an enumerator's initial state, its index is invalid. The first <see cref="MoveNext"/> call advances the enumerator's index to the first character.
  219. /// </remarks>
  220. public struct Enumerator : IEnumerator<Unicode.Rune>
  221. {
  222. HeapString target;
  223. int offset;
  224. Unicode.Rune current;
  225. /// <summary>
  226. /// Initializes and returns an instance of HeapString.Enumerator.
  227. /// </summary>
  228. /// <param name="source">A HeapString for which to create an enumerator.</param>
  229. public Enumerator(HeapString source)
  230. {
  231. target = source;
  232. offset = 0;
  233. current = default;
  234. }
  235. /// <summary>
  236. /// Does nothing.
  237. /// </summary>
  238. public void Dispose()
  239. {
  240. }
  241. /// <summary>
  242. /// Advances the enumerator to the next character, returning true if <see cref="Current"/> is valid to read afterwards.
  243. /// </summary>
  244. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  245. public bool MoveNext()
  246. {
  247. if (offset >= target.Length)
  248. return false;
  249. unsafe
  250. {
  251. Unicode.Utf8ToUcs(out current, target.GetUnsafePtr(), ref offset, target.Length);
  252. }
  253. return true;
  254. }
  255. /// <summary>
  256. /// Resets the enumerator to its initial state.
  257. /// </summary>
  258. public void Reset()
  259. {
  260. offset = 0;
  261. current = default;
  262. }
  263. object IEnumerator.Current => Current;
  264. /// <summary>
  265. /// The current character.
  266. /// </summary>
  267. /// <value>The current character.</value>
  268. public Unicode.Rune Current => current;
  269. }
  270. /// <summary>
  271. /// Returns an enumerator for iterating over the characters of the HeapString.
  272. /// </summary>
  273. /// <returns>An enumerator for iterating over the characters of the HeapString.</returns>
  274. public Enumerator GetEnumerator()
  275. {
  276. return new Enumerator(this);
  277. }
  278. /// <summary>
  279. /// Returns the lexicographical sort order of this string relative to another.
  280. /// </summary>
  281. /// <param name="other">Another string to compare with.</param>
  282. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  283. ///
  284. /// 0 denotes both strings have the same sort position.<br/>
  285. /// -1 denotes that this string should be sorted to precede the other.<br/>
  286. /// +1 denotes that this string should be sorted to follow the other.<br/>
  287. /// </returns>
  288. [NotBurstCompatible]
  289. public int CompareTo(String other)
  290. {
  291. return ToString().CompareTo(other);
  292. }
  293. /// <summary>
  294. /// Returns true if this string and another are equal.
  295. /// </summary>
  296. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  297. /// <param name="other">Another string to compare with.</param>
  298. /// <returns>True if the two strings are equal.</returns>
  299. [NotBurstCompatible]
  300. public bool Equals(String other)
  301. {
  302. return ToString().Equals(other);
  303. }
  304. /// <summary>
  305. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  306. /// </summary>
  307. /// <param name="source">A string to copy characters from.</param>
  308. /// <param name="allocator">The allocator to use.</param>
  309. [NotBurstCompatible]
  310. public HeapString(String source, Allocator allocator)
  311. {
  312. m_Data = new NativeList<byte>(source.Length * 2 + 1, allocator);
  313. Length = source.Length * 2; // maximum possible
  314. unsafe
  315. {
  316. fixed (char* sourceptr = source)
  317. {
  318. var error = UTF8ArrayUnsafeUtility.Copy(GetUnsafePtr(), out var actualBytes, Capacity, sourceptr, source.Length);
  319. if (error != CopyError.None)
  320. {
  321. m_Data.Dispose();
  322. m_Data = default;
  323. ThrowCopyError(error, source);
  324. }
  325. this.Length = actualBytes;
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// Initializes and returns an instance of HeapString with a specified initial capacity.
  331. /// </summary>
  332. /// <param name="capacity">The initial capacity in bytes.</param>
  333. /// <param name="allocator">The allocator to use.</param>
  334. public HeapString(int capacity, Allocator allocator)
  335. {
  336. m_Data = new NativeList<byte>(capacity + 1, allocator);
  337. this.Length = 0;
  338. }
  339. /// <summary>
  340. /// Initializes and returns an instance of HeapString with an initial capacity of 128 bytes.
  341. /// </summary>
  342. /// <param name="allocator">The allocator to use.</param>
  343. public HeapString(Allocator allocator)
  344. {
  345. m_Data = new NativeList<byte>(128 + 1, allocator);
  346. this.Length = 0;
  347. }
  348. /// <summary>
  349. /// Returns the lexicographical sort order of this string relative to another.
  350. /// </summary>
  351. /// <param name="other">Another string to compare with.</param>
  352. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  353. ///
  354. /// 0 denotes both strings have the same sort position.<br/>
  355. /// -1 denotes that this string should be sorted to precede the other.<br/>
  356. /// +1 denotes that this string should be sorted to follow the other.<br/>
  357. /// </returns>
  358. public int CompareTo(FixedString32Bytes other)
  359. {
  360. return FixedStringMethods.CompareTo(ref this, other);
  361. }
  362. /// <summary>
  363. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  364. /// </summary>
  365. /// <param name="source">A string to copy characters from.</param>
  366. /// <param name="allocator">The allocator to use.</param>
  367. public HeapString(in FixedString32Bytes source, Allocator allocator)
  368. {
  369. m_Data = new NativeList<byte>(source.utf8LengthInBytes + 1, allocator);
  370. Length = source.utf8LengthInBytes;
  371. unsafe {
  372. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  373. byte* dbytes = (byte*) m_Data.GetUnsafePtr();
  374. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  375. }
  376. }
  377. /// <summary>
  378. /// Returns true if two strings are equal.
  379. /// </summary>
  380. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  381. /// <param name="a">A string to compare.</param>
  382. /// <param name="b">Another string to compare.</param>
  383. /// <returns>True if the two strings are equal.</returns>
  384. public static bool operator ==(in HeapString a, in FixedString32Bytes b)
  385. {
  386. unsafe {
  387. var aref = UnsafeUtilityExtensions.AsRef(a);
  388. int alen = aref.Length;
  389. int blen = b.utf8LengthInBytes;
  390. byte* aptr = (byte*) aref.GetUnsafePtr();
  391. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  392. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  393. }
  394. }
  395. /// <summary>
  396. /// Returns true if two strings are unequal.
  397. /// </summary>
  398. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  399. /// <param name="a">A string to compare.</param>
  400. /// <param name="b">Another string to compare.</param>
  401. /// <returns>True if the two strings are unequal.</returns>
  402. public static bool operator !=(in HeapString a, in FixedString32Bytes b)
  403. {
  404. return !(a == b);
  405. }
  406. /// <summary>
  407. /// Returns true if this string and another are equal.
  408. /// </summary>
  409. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  410. /// <param name="other">Another string to compare with.</param>
  411. /// <returns>True if the two strings are equal.</returns>
  412. public bool Equals(FixedString32Bytes other)
  413. {
  414. return this == other;
  415. }
  416. /// <summary>
  417. /// Returns the lexicographical sort order of this string relative to another.
  418. /// </summary>
  419. /// <param name="other">Another string to compare with.</param>
  420. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  421. ///
  422. /// 0 denotes both strings have the same sort position.<br/>
  423. /// -1 denotes that this string should be sorted to precede the other.<br/>
  424. /// +1 denotes that this string should be sorted to follow the other.<br/>
  425. /// </returns>
  426. public int CompareTo(FixedString64Bytes other)
  427. {
  428. return FixedStringMethods.CompareTo(ref this, other);
  429. }
  430. /// <summary>
  431. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  432. /// </summary>
  433. /// <param name="source">A string to copy characters from.</param>
  434. /// <param name="allocator">The allocator to use.</param>
  435. public HeapString(in FixedString64Bytes source, Allocator allocator)
  436. {
  437. m_Data = new NativeList<byte>(source.utf8LengthInBytes + 1, allocator);
  438. Length = source.utf8LengthInBytes;
  439. unsafe {
  440. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  441. byte* dbytes = (byte*) m_Data.GetUnsafePtr();
  442. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  443. }
  444. }
  445. /// <summary>
  446. /// Returns true if two strings are equal.
  447. /// </summary>
  448. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  449. /// <param name="a">A string to compare.</param>
  450. /// <param name="b">Another string to compare.</param>
  451. /// <returns>True if the two strings are equal.</returns>
  452. public static bool operator ==(in HeapString a, in FixedString64Bytes b)
  453. {
  454. unsafe {
  455. var aref = UnsafeUtilityExtensions.AsRef(a);
  456. int alen = aref.Length;
  457. int blen = b.utf8LengthInBytes;
  458. byte* aptr = (byte*) aref.GetUnsafePtr();
  459. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  460. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  461. }
  462. }
  463. /// <summary>
  464. /// Returns true if two strings are unequal.
  465. /// </summary>
  466. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  467. /// <param name="a">A string to compare.</param>
  468. /// <param name="b">Another string to compare.</param>
  469. /// <returns>True if the two strings are unequal.</returns>
  470. public static bool operator !=(in HeapString a, in FixedString64Bytes b)
  471. {
  472. return !(a == b);
  473. }
  474. /// <summary>
  475. /// Returns true if this string and another are equal.
  476. /// </summary>
  477. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  478. /// <param name="other">Another string to compare with.</param>
  479. /// <returns>True if the two strings are equal.</returns>
  480. public bool Equals(FixedString64Bytes other)
  481. {
  482. return this == other;
  483. }
  484. /// <summary>
  485. /// Returns the lexicographical sort order of this string relative to another.
  486. /// </summary>
  487. /// <param name="other">Another string to compare with.</param>
  488. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  489. ///
  490. /// 0 denotes both strings have the same sort position.<br/>
  491. /// -1 denotes that this string should be sorted to precede the other.<br/>
  492. /// +1 denotes that this string should be sorted to follow the other.<br/>
  493. /// </returns>
  494. public int CompareTo(FixedString128Bytes other)
  495. {
  496. return FixedStringMethods.CompareTo(ref this, other);
  497. }
  498. /// <summary>
  499. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  500. /// </summary>
  501. /// <param name="source">A string to copy characters from.</param>
  502. /// <param name="allocator">The allocator to use.</param>
  503. public HeapString(in FixedString128Bytes source, Allocator allocator)
  504. {
  505. m_Data = new NativeList<byte>(source.utf8LengthInBytes + 1, allocator);
  506. Length = source.utf8LengthInBytes;
  507. unsafe {
  508. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  509. byte* dbytes = (byte*) m_Data.GetUnsafePtr();
  510. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  511. }
  512. }
  513. /// <summary>
  514. /// Returns true if two strings are equal.
  515. /// </summary>
  516. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  517. /// <param name="a">A string to compare.</param>
  518. /// <param name="b">Another string to compare.</param>
  519. /// <returns>True if the two strings are equal.</returns>
  520. public static bool operator ==(in HeapString a, in FixedString128Bytes b)
  521. {
  522. unsafe {
  523. var aref = UnsafeUtilityExtensions.AsRef(a);
  524. int alen = aref.Length;
  525. int blen = b.utf8LengthInBytes;
  526. byte* aptr = (byte*) aref.GetUnsafePtr();
  527. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  528. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  529. }
  530. }
  531. /// <summary>
  532. /// Returns true if two strings are unequal.
  533. /// </summary>
  534. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  535. /// <param name="a">A string to compare.</param>
  536. /// <param name="b">Another string to compare.</param>
  537. /// <returns>True if the two strings are unequal.</returns>
  538. public static bool operator !=(in HeapString a, in FixedString128Bytes b)
  539. {
  540. return !(a == b);
  541. }
  542. /// <summary>
  543. /// Returns true if this string and another are equal.
  544. /// </summary>
  545. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  546. /// <param name="other">Another string to compare with.</param>
  547. /// <returns>True if the two strings are equal.</returns>
  548. public bool Equals(FixedString128Bytes other)
  549. {
  550. return this == other;
  551. }
  552. /// <summary>
  553. /// Returns the lexicographical sort order of this string relative to another.
  554. /// </summary>
  555. /// <param name="other">Another string to compare with.</param>
  556. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  557. ///
  558. /// 0 denotes both strings have the same sort position.<br/>
  559. /// -1 denotes that this string should be sorted to precede the other.<br/>
  560. /// +1 denotes that this string should be sorted to follow the other.<br/>
  561. /// </returns>
  562. public int CompareTo(FixedString512Bytes other)
  563. {
  564. return FixedStringMethods.CompareTo(ref this, other);
  565. }
  566. /// <summary>
  567. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  568. /// </summary>
  569. /// <param name="source">A string to copy characters from.</param>
  570. /// <param name="allocator">The allocator to use.</param>
  571. public HeapString(in FixedString512Bytes source, Allocator allocator)
  572. {
  573. m_Data = new NativeList<byte>(source.utf8LengthInBytes + 1, allocator);
  574. Length = source.utf8LengthInBytes;
  575. unsafe {
  576. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  577. byte* dbytes = (byte*) m_Data.GetUnsafePtr();
  578. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  579. }
  580. }
  581. /// <summary>
  582. /// Returns true if two strings are equal.
  583. /// </summary>
  584. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  585. /// <param name="a">A string to compare.</param>
  586. /// <param name="b">Another string to compare.</param>
  587. /// <returns>True if the two strings are equal.</returns>
  588. public static bool operator ==(in HeapString a, in FixedString512Bytes b)
  589. {
  590. unsafe {
  591. var aref = UnsafeUtilityExtensions.AsRef(a);
  592. int alen = aref.Length;
  593. int blen = b.utf8LengthInBytes;
  594. byte* aptr = (byte*) aref.GetUnsafePtr();
  595. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  596. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  597. }
  598. }
  599. /// <summary>
  600. /// Returns true if two strings are unequal.
  601. /// </summary>
  602. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  603. /// <param name="a">A string to compare.</param>
  604. /// <param name="b">Another string to compare.</param>
  605. /// <returns>True if the two strings are unequal.</returns>
  606. public static bool operator !=(in HeapString a, in FixedString512Bytes b)
  607. {
  608. return !(a == b);
  609. }
  610. /// <summary>
  611. /// Returns true if this string and another are equal.
  612. /// </summary>
  613. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  614. /// <param name="other">Another string to compare with.</param>
  615. /// <returns>True if the two strings are equal.</returns>
  616. public bool Equals(FixedString512Bytes other)
  617. {
  618. return this == other;
  619. }
  620. /// <summary>
  621. /// Returns the lexicographical sort order of this string relative to another.
  622. /// </summary>
  623. /// <param name="other">Another string to compare with.</param>
  624. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  625. ///
  626. /// 0 denotes both strings have the same sort position.<br/>
  627. /// -1 denotes that this string should be sorted to precede the other.<br/>
  628. /// +1 denotes that this string should be sorted to follow the other.<br/>
  629. /// </returns>
  630. public int CompareTo(FixedString4096Bytes other)
  631. {
  632. return FixedStringMethods.CompareTo(ref this, other);
  633. }
  634. /// <summary>
  635. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  636. /// </summary>
  637. /// <param name="source">A string to copy characters from.</param>
  638. /// <param name="allocator">The allocator to use.</param>
  639. public HeapString(in FixedString4096Bytes source, Allocator allocator)
  640. {
  641. m_Data = new NativeList<byte>(source.utf8LengthInBytes + 1, allocator);
  642. Length = source.utf8LengthInBytes;
  643. unsafe {
  644. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  645. byte* dbytes = (byte*) m_Data.GetUnsafePtr();
  646. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  647. }
  648. }
  649. /// <summary>
  650. /// Returns true if two strings are equal.
  651. /// </summary>
  652. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  653. /// <param name="a">A string to compare.</param>
  654. /// <param name="b">Another string to compare.</param>
  655. /// <returns>True if the two strings are equal.</returns>
  656. public static bool operator ==(in HeapString a, in FixedString4096Bytes b)
  657. {
  658. unsafe {
  659. var aref = UnsafeUtilityExtensions.AsRef(a);
  660. int alen = aref.Length;
  661. int blen = b.utf8LengthInBytes;
  662. byte* aptr = (byte*) aref.GetUnsafePtr();
  663. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  664. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  665. }
  666. }
  667. /// <summary>
  668. /// Returns true if two strings are unequal.
  669. /// </summary>
  670. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  671. /// <param name="a">A string to compare.</param>
  672. /// <param name="b">Another string to compare.</param>
  673. /// <returns>True if the two strings are unequal.</returns>
  674. public static bool operator !=(in HeapString a, in FixedString4096Bytes b)
  675. {
  676. return !(a == b);
  677. }
  678. /// <summary>
  679. /// Returns true if this string and another are equal.
  680. /// </summary>
  681. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  682. /// <param name="other">Another string to compare with.</param>
  683. /// <returns>True if the two strings are equal.</returns>
  684. public bool Equals(FixedString4096Bytes other)
  685. {
  686. return this == other;
  687. }
  688. /// <summary>
  689. /// Returns a managed string copy of this string.
  690. /// </summary>
  691. /// <returns>A managed string copy of this string.</returns>>
  692. [NotBurstCompatible]
  693. public override String ToString()
  694. {
  695. if (!m_Data.IsCreated)
  696. return "";
  697. return this.ConvertToString();
  698. }
  699. /// <summary>
  700. /// Returns a hash code of this string.
  701. /// </summary>
  702. /// <remarks>The hash code is an integer that is always the same for two equal strings but (very likely) different for two unequal strings.</remarks>
  703. /// <returns>A hash code of this string.</returns>
  704. public override int GetHashCode()
  705. {
  706. return this.ComputeHashCode();
  707. }
  708. /// <summary>
  709. /// Returns true if this string and another object are equal.
  710. /// </summary>
  711. /// <remarks>For the object to be equal, it must itself be a managed string, HeapString, or FixedString*N*Bytes.
  712. ///
  713. /// Two strings are equal if they have equal length and all their characters match.</remarks>
  714. /// <param name="other">Another string to compare with.</param>
  715. /// <returns>True if this string and the object are equal.</returns>
  716. [NotBurstCompatible]
  717. public override bool Equals(object other)
  718. {
  719. if(ReferenceEquals(null, other)) return false;
  720. if(other is String aString) return Equals(aString);
  721. if(other is HeapString aHeapString) return Equals(aHeapString);
  722. if(other is FixedString32Bytes a32) return Equals(a32);
  723. if(other is FixedString64Bytes a64) return Equals(a64);
  724. if(other is FixedString128Bytes a128) return Equals(a128);
  725. if(other is FixedString512Bytes a512) return Equals(a512);
  726. if(other is FixedString4096Bytes a4096) return Equals(a4096);
  727. return false;
  728. }
  729. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  730. void CheckIndexInRange(int index)
  731. {
  732. if (index < 0)
  733. throw new IndexOutOfRangeException($"Index {index} must be positive.");
  734. if (index >= Length)
  735. throw new IndexOutOfRangeException($"Index {index} is out of range in HeapString of {Length} length.");
  736. }
  737. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  738. void ThrowCopyError(CopyError error, String source)
  739. {
  740. throw new ArgumentException($"HeapString: {error} while copying \"{source}\"");
  741. }
  742. }
  743. }