Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HeapString.tt 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <#@ template language="C#" debug="True" #>
  2. <#@ output extension=".gen.cs" encoding="utf-8" #>
  3. <#@ assembly name="System.Core" #>
  4. //------------------------------------------------------------------------------
  5. // <auto-generated>
  6. // This code was generated by a tool.
  7. //
  8. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/HeapString.tt
  9. //
  10. // Changes to this file may cause incorrect behavior and will be lost if
  11. // the code is regenerated.
  12. // </auto-generated>
  13. //------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Runtime.InteropServices;
  19. using System.Runtime.CompilerServices;
  20. using Unity.Collections.LowLevel.Unsafe;
  21. using UnityEngine.Internal;
  22. #if UNITY_PROPERTIES_EXISTS
  23. using Unity.Properties;
  24. #endif
  25. <#
  26. {
  27. var SIZES = new [] {32,64,128,512,4096};
  28. #>
  29. namespace Unity.Collections
  30. {
  31. /// <summary>
  32. /// An unmanaged, allocated, mutable, resizable UTF-8 string.
  33. /// </summary>
  34. /// <remarks>
  35. /// The string is always null-terminated, meaning a zero byte always immediately follows the last character.
  36. /// </remarks>
  37. [BurstCompatible]
  38. [Obsolete("HeapString has been removed and replaced with NativeText (RemovedAfter 2021-07-21) (UnityUpgradable) -> NativeText", false)]
  39. public partial struct HeapString
  40. : INativeList<byte>
  41. , IDisposable
  42. , IUTF8Bytes
  43. , IComparable<String>
  44. , IEquatable<String>
  45. , IComparable<HeapString>
  46. , IEquatable<HeapString>
  47. <#
  48. foreach (var OTHERBYTES in SIZES)
  49. {
  50. #>
  51. , IComparable<FixedString<#=OTHERBYTES#>Bytes>
  52. , IEquatable<FixedString<#=OTHERBYTES#>Bytes>
  53. <#
  54. }
  55. #>
  56. {
  57. // NOTE! This Length is always > 0, because we have a null terminating byte.
  58. // We hide this byte from HeapString users.
  59. private NativeList<byte> m_Data;
  60. /// <summary>
  61. /// The current length in bytes of this string.
  62. /// </summary>
  63. /// <remarks>
  64. /// The length does not include the null terminator byte.
  65. /// </remarks>
  66. /// <value>The current length in bytes of the UTF-8 encoded string.</value>
  67. public int Length
  68. {
  69. get {
  70. return m_Data.Length - 1;
  71. }
  72. set {
  73. m_Data.Resize(value + 1, NativeArrayOptions.UninitializedMemory);
  74. m_Data[value] = 0;
  75. }
  76. }
  77. /// <summary>
  78. /// The current capacity of this string.
  79. /// </summary>
  80. /// <remarks>
  81. /// The null-terminator byte is not included in the Capacity, so the string's character buffer is `Capacity + 1` in size.
  82. /// </remarks>
  83. /// <value>The current capacity of the string.</value>
  84. public int Capacity
  85. {
  86. get {
  87. return m_Data.Capacity - 1;
  88. }
  89. set {
  90. m_Data.Capacity = value + 1;
  91. }
  92. }
  93. /// <summary>
  94. /// Attempt to set the length in bytes of this string.
  95. /// </summary>
  96. /// <param name="newLength">The new length in bytes of the string.</param>
  97. /// <param name="clearOptions">Whether any bytes added should be zeroed out.</param>
  98. /// <returns>Always true.</returns>
  99. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
  100. {
  101. // this can't ever fail, because if we can't resize malloc will abort
  102. Length = newLength;
  103. return true;
  104. }
  105. /// <summary>
  106. /// Whether this string has no characters.
  107. /// </summary>
  108. /// <value>True if this string has no characters.</value>
  109. public bool IsEmpty => m_Data.Length == 1;
  110. /// <summary>
  111. /// Whether this string's character buffer has been allocated and not yet deallocated.
  112. /// </summary>
  113. /// <value>Whether this string's character buffer has been allocated and not yet deallocated.</value>
  114. public bool IsCreated => m_Data.IsCreated;
  115. /// <summary>
  116. /// Returns a pointer to this string's character buffer.
  117. /// </summary>
  118. /// <remarks>
  119. /// The pointer is made invalid by operations that reallocate the character buffer, such as setting <see cref="Capacity"/>.
  120. /// </remarks>
  121. /// <returns>A pointer to this string's character buffer.</returns>
  122. public unsafe byte* GetUnsafePtr()
  123. {
  124. return (byte*) m_Data.GetUnsafePtr();
  125. }
  126. /// <summary>
  127. /// The byte at an index.
  128. /// </summary>
  129. /// <param name="index">A zero-based byte index.</param>
  130. /// <value>The byte at the index.</value>
  131. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  132. public byte this[int index]
  133. {
  134. get {
  135. CheckIndexInRange(index);
  136. return m_Data[index];
  137. }
  138. set {
  139. CheckIndexInRange(index);
  140. m_Data[index] = value;
  141. }
  142. }
  143. /// <summary>
  144. /// Returns the reference to the byte (not character) at an index.
  145. /// </summary>
  146. /// <remarks>
  147. /// Deallocating or reallocating this string's character buffer makes the reference invalid.
  148. /// </remarks>
  149. /// <param name="index">A byte index.</param>
  150. /// <returns>A reference to the byte at the index.</returns>
  151. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  152. public ref byte ElementAt(int index)
  153. {
  154. CheckIndexInRange(index);
  155. return ref m_Data.ElementAt(index);
  156. }
  157. /// <summary>
  158. /// Sets the length to 0.
  159. /// </summary>
  160. public void Clear()
  161. {
  162. Length = 0;
  163. }
  164. /// <summary>
  165. /// Appends a byte.
  166. /// </summary>
  167. /// <remarks>
  168. /// A zero byte will always follow the newly appended byte.
  169. ///
  170. /// 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.
  171. /// </remarks>
  172. /// <param name="value">A byte to append.</param>
  173. public void Add(in byte value)
  174. {
  175. this[Length++] = value;
  176. }
  177. /// <summary>
  178. /// Returns the lexicographical sort order of this string relative to another.
  179. /// </summary>
  180. /// <param name="other">Another string to compare with.</param>
  181. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  182. ///
  183. /// 0 denotes both strings have the same sort position.<br/>
  184. /// -1 denotes that this string should be sorted to precede the other.<br/>
  185. /// +1 denotes that this string should be sorted to follow the other.<br/>
  186. /// </returns>
  187. public int CompareTo(HeapString other)
  188. {
  189. return FixedStringMethods.CompareTo(ref this, other);
  190. }
  191. /// <summary>
  192. /// Returns true if this string and another are equal.
  193. /// </summary>
  194. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  195. /// <param name="other">Another string to compare with.</param>
  196. /// <returns>True if the two strings are equal.</returns>
  197. public bool Equals(HeapString other)
  198. {
  199. return FixedStringMethods.Equals(ref this, other);
  200. }
  201. /// <summary>
  202. /// Releases all resources (memory and safety handles).
  203. /// </summary>
  204. public void Dispose()
  205. {
  206. m_Data.Dispose();
  207. }
  208. /// <summary>
  209. /// A copy of this string as a managed string.
  210. /// </summary>
  211. /// <remarks>
  212. /// For internal use only. Use <see cref="ToString"/> instead.
  213. /// </remarks>
  214. /// <value>A copy of this string as a managed string.</value>
  215. [CreateProperty]
  216. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  217. [NotBurstCompatible]
  218. public string Value => ToString();
  219. /// <summary>
  220. /// An enumerator over the characters (not bytes) of a HeapString.
  221. /// </summary>
  222. /// <remarks>
  223. /// 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.
  224. /// </remarks>
  225. public struct Enumerator : IEnumerator<Unicode.Rune>
  226. {
  227. HeapString target;
  228. int offset;
  229. Unicode.Rune current;
  230. /// <summary>
  231. /// Initializes and returns an instance of HeapString.Enumerator.
  232. /// </summary>
  233. /// <param name="source">A HeapString for which to create an enumerator.</param>
  234. public Enumerator(HeapString source)
  235. {
  236. target = source;
  237. offset = 0;
  238. current = default;
  239. }
  240. /// <summary>
  241. /// Does nothing.
  242. /// </summary>
  243. public void Dispose()
  244. {
  245. }
  246. /// <summary>
  247. /// Advances the enumerator to the next character, returning true if <see cref="Current"/> is valid to read afterwards.
  248. /// </summary>
  249. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  250. public bool MoveNext()
  251. {
  252. if (offset >= target.Length)
  253. return false;
  254. unsafe
  255. {
  256. Unicode.Utf8ToUcs(out current, target.GetUnsafePtr(), ref offset, target.Length);
  257. }
  258. return true;
  259. }
  260. /// <summary>
  261. /// Resets the enumerator to its initial state.
  262. /// </summary>
  263. public void Reset()
  264. {
  265. offset = 0;
  266. current = default;
  267. }
  268. object IEnumerator.Current => Current;
  269. /// <summary>
  270. /// The current character.
  271. /// </summary>
  272. /// <value>The current character.</value>
  273. public Unicode.Rune Current => current;
  274. }
  275. /// <summary>
  276. /// Returns an enumerator for iterating over the characters of the HeapString.
  277. /// </summary>
  278. /// <returns>An enumerator for iterating over the characters of the HeapString.</returns>
  279. public Enumerator GetEnumerator()
  280. {
  281. return new Enumerator(this);
  282. }
  283. /// <summary>
  284. /// Returns the lexicographical sort order of this string relative to another.
  285. /// </summary>
  286. /// <param name="other">Another string to compare with.</param>
  287. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  288. ///
  289. /// 0 denotes both strings have the same sort position.<br/>
  290. /// -1 denotes that this string should be sorted to precede the other.<br/>
  291. /// +1 denotes that this string should be sorted to follow the other.<br/>
  292. /// </returns>
  293. [NotBurstCompatible]
  294. public int CompareTo(String other)
  295. {
  296. return ToString().CompareTo(other);
  297. }
  298. /// <summary>
  299. /// Returns true if this string and another are equal.
  300. /// </summary>
  301. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  302. /// <param name="other">Another string to compare with.</param>
  303. /// <returns>True if the two strings are equal.</returns>
  304. [NotBurstCompatible]
  305. public bool Equals(String other)
  306. {
  307. return ToString().Equals(other);
  308. }
  309. /// <summary>
  310. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  311. /// </summary>
  312. /// <param name="source">A string to copy characters from.</param>
  313. /// <param name="allocator">The allocator to use.</param>
  314. [NotBurstCompatible]
  315. public HeapString(String source, Allocator allocator)
  316. {
  317. m_Data = new NativeList<byte>(source.Length * 2 + 1, allocator);
  318. Length = source.Length * 2; // maximum possible
  319. unsafe
  320. {
  321. fixed (char* sourceptr = source)
  322. {
  323. var error = UTF8ArrayUnsafeUtility.Copy(GetUnsafePtr(), out var actualBytes, Capacity, sourceptr, source.Length);
  324. if (error != CopyError.None)
  325. {
  326. m_Data.Dispose();
  327. m_Data = default;
  328. ThrowCopyError(error, source);
  329. }
  330. this.Length = actualBytes;
  331. }
  332. }
  333. }
  334. /// <summary>
  335. /// Initializes and returns an instance of HeapString with a specified initial capacity.
  336. /// </summary>
  337. /// <param name="capacity">The initial capacity in bytes.</param>
  338. /// <param name="allocator">The allocator to use.</param>
  339. public HeapString(int capacity, Allocator allocator)
  340. {
  341. m_Data = new NativeList<byte>(capacity + 1, allocator);
  342. this.Length = 0;
  343. }
  344. /// <summary>
  345. /// Initializes and returns an instance of HeapString with an initial capacity of 128 bytes.
  346. /// </summary>
  347. /// <param name="allocator">The allocator to use.</param>
  348. public HeapString(Allocator allocator)
  349. {
  350. m_Data = new NativeList<byte>(128 + 1, allocator);
  351. this.Length = 0;
  352. }
  353. <#
  354. //
  355. // Generate easy conversion and comparison between this and other FixedString types
  356. //
  357. foreach (var OTHERBYTES in SIZES)
  358. {
  359. #>
  360. /// <summary>
  361. /// Returns the lexicographical sort order of this string relative to another.
  362. /// </summary>
  363. /// <param name="other">Another string to compare with.</param>
  364. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  365. ///
  366. /// 0 denotes both strings have the same sort position.<br/>
  367. /// -1 denotes that this string should be sorted to precede the other.<br/>
  368. /// +1 denotes that this string should be sorted to follow the other.<br/>
  369. /// </returns>
  370. public int CompareTo(FixedString<#=OTHERBYTES#>Bytes other)
  371. {
  372. return FixedStringMethods.CompareTo(ref this, other);
  373. }
  374. /// <summary>
  375. /// Initializes and returns an instance of HeapString with the characters copied from another string.
  376. /// </summary>
  377. /// <param name="source">A string to copy characters from.</param>
  378. /// <param name="allocator">The allocator to use.</param>
  379. public HeapString(in FixedString<#=OTHERBYTES#>Bytes source, Allocator allocator)
  380. {
  381. m_Data = new NativeList<byte>(source.utf8LengthInBytes + 1, allocator);
  382. Length = source.utf8LengthInBytes;
  383. unsafe {
  384. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  385. byte* dbytes = (byte*) m_Data.GetUnsafePtr();
  386. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  387. }
  388. }
  389. /// <summary>
  390. /// Returns true if two strings are equal.
  391. /// </summary>
  392. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  393. /// <param name="a">A string to compare.</param>
  394. /// <param name="b">Another string to compare.</param>
  395. /// <returns>True if the two strings are equal.</returns>
  396. public static bool operator ==(in HeapString a, in FixedString<#=OTHERBYTES#>Bytes b)
  397. {
  398. unsafe {
  399. var aref = UnsafeUtilityExtensions.AsRef(a);
  400. int alen = aref.Length;
  401. int blen = b.utf8LengthInBytes;
  402. byte* aptr = (byte*) aref.GetUnsafePtr();
  403. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  404. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  405. }
  406. }
  407. /// <summary>
  408. /// Returns true if two strings are unequal.
  409. /// </summary>
  410. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  411. /// <param name="a">A string to compare.</param>
  412. /// <param name="b">Another string to compare.</param>
  413. /// <returns>True if the two strings are unequal.</returns>
  414. public static bool operator !=(in HeapString a, in FixedString<#=OTHERBYTES#>Bytes b)
  415. {
  416. return !(a == b);
  417. }
  418. /// <summary>
  419. /// Returns true if this string and another are equal.
  420. /// </summary>
  421. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  422. /// <param name="other">Another string to compare with.</param>
  423. /// <returns>True if the two strings are equal.</returns>
  424. public bool Equals(FixedString<#=OTHERBYTES#>Bytes other)
  425. {
  426. return this == other;
  427. }
  428. <#
  429. }
  430. #>
  431. /// <summary>
  432. /// Returns a managed string copy of this string.
  433. /// </summary>
  434. /// <returns>A managed string copy of this string.</returns>>
  435. [NotBurstCompatible]
  436. public override String ToString()
  437. {
  438. if (!m_Data.IsCreated)
  439. return "";
  440. return this.ConvertToString();
  441. }
  442. /// <summary>
  443. /// Returns a hash code of this string.
  444. /// </summary>
  445. /// <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>
  446. /// <returns>A hash code of this string.</returns>
  447. public override int GetHashCode()
  448. {
  449. return this.ComputeHashCode();
  450. }
  451. /// <summary>
  452. /// Returns true if this string and another object are equal.
  453. /// </summary>
  454. /// <remarks>For the object to be equal, it must itself be a managed string, HeapString, or FixedString*N*Bytes.
  455. ///
  456. /// Two strings are equal if they have equal length and all their characters match.</remarks>
  457. /// <param name="other">Another string to compare with.</param>
  458. /// <returns>True if this string and the object are equal.</returns>
  459. [NotBurstCompatible]
  460. public override bool Equals(object other)
  461. {
  462. if(ReferenceEquals(null, other)) return false;
  463. if(other is String aString) return Equals(aString);
  464. if(other is HeapString aHeapString) return Equals(aHeapString);
  465. <#
  466. foreach(var OTHERBYTES in SIZES)
  467. {
  468. #>
  469. if(other is FixedString<#=OTHERBYTES#>Bytes a<#=OTHERBYTES#>) return Equals(a<#=OTHERBYTES#>);
  470. <#
  471. }
  472. #>
  473. return false;
  474. }
  475. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  476. void CheckIndexInRange(int index)
  477. {
  478. if (index < 0)
  479. throw new IndexOutOfRangeException($"Index {index} must be positive.");
  480. if (index >= Length)
  481. throw new IndexOutOfRangeException($"Index {index} is out of range in HeapString of {Length} length.");
  482. }
  483. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  484. void ThrowCopyError(CopyError error, String source)
  485. {
  486. throw new ArgumentException($"HeapString: {error} while copying \"{source}\"");
  487. }
  488. }
  489. <#}#>
  490. }