暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 Packages/com.unity.collections/Unity.Collections/NativeText.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.Burst;
  21. using Unity.Collections.LowLevel.Unsafe;
  22. using Unity.Jobs;
  23. using UnityEngine.Internal;
  24. using Unity.Properties;
  25. <#
  26. {
  27. var SIZES = new [] {32,64,128,512,4096};
  28. #>
  29. namespace Unity.Collections
  30. {
  31. /// <summary>
  32. /// An unmanaged, 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. [StructLayout(LayoutKind.Sequential)]
  38. [NativeContainer]
  39. [DebuggerDisplay("Length = {Length}")]
  40. [GenerateTestsForBurstCompatibility]
  41. public unsafe partial struct NativeText
  42. : INativeList<byte>
  43. , INativeDisposable
  44. , IUTF8Bytes
  45. , IComparable<String>
  46. , IEquatable<String>
  47. , IComparable<NativeText>
  48. , IEquatable<NativeText>
  49. <#
  50. foreach (var OTHERBYTES in SIZES)
  51. {
  52. #>
  53. , IComparable<FixedString<#=OTHERBYTES#>Bytes>
  54. , IEquatable<FixedString<#=OTHERBYTES#>Bytes>
  55. <#
  56. }
  57. #>
  58. {
  59. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  60. internal AtomicSafetyHandle m_Safety;
  61. internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<NativeText>();
  62. #endif
  63. // NOTE! This Length is always > 0, because we have a null terminating byte.
  64. // We hide this byte from NativeText users.
  65. [NativeDisableUnsafePtrRestriction]
  66. internal UnsafeText* m_Data;
  67. /// <summary>
  68. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  69. /// </summary>
  70. /// <param name="source">A string to copy characters from.</param>
  71. /// <param name="allocator">The allocator to use.</param>
  72. [ExcludeFromBurstCompatTesting("Takes managed string")]
  73. public NativeText(String source, Allocator allocator) : this(source, (AllocatorManager.AllocatorHandle)allocator)
  74. {
  75. }
  76. /// <summary>
  77. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  78. /// </summary>
  79. /// <param name="source">A string to copy characters from.</param>
  80. /// <param name="allocator">The allocator to use.</param>
  81. [ExcludeFromBurstCompatTesting("Takes managed string")]
  82. public NativeText(String source, AllocatorManager.AllocatorHandle allocator) : this(source.Length * 2, allocator)
  83. {
  84. Length = source.Length * 2;
  85. unsafe
  86. {
  87. fixed (char* sourceptr = source)
  88. {
  89. var error = UTF8ArrayUnsafeUtility.Copy(GetUnsafePtr(), out var actualBytes, Capacity, sourceptr, source.Length);
  90. if (error != CopyError.None)
  91. {
  92. CheckNull(m_Data);
  93. m_Data->Dispose();
  94. m_Data = UnsafeText.Alloc(allocator);
  95. *m_Data = default(UnsafeText);
  96. ThrowCopyError(error, source);
  97. }
  98. Length = actualBytes;
  99. }
  100. }
  101. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  102. CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText");
  103. AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
  104. #endif
  105. }
  106. /// <summary>
  107. /// Initializes and returns an instance of NativeText.
  108. /// </summary>
  109. /// <param name="capacity">The initial capacity in bytes.</param>
  110. /// <param name="allocator">The allocator to use.</param>
  111. public NativeText(int capacity, Allocator allocator) : this(capacity, (AllocatorManager.AllocatorHandle)allocator)
  112. {
  113. }
  114. /// <summary>
  115. /// Initializes and returns an instance of NativeText.
  116. /// </summary>
  117. /// <param name="capacity">The initial capacity in bytes.</param>
  118. /// <param name="allocator">The allocator to use.</param>
  119. public NativeText(int capacity, AllocatorManager.AllocatorHandle allocator)
  120. {
  121. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  122. CollectionHelper.CheckAllocator(allocator);
  123. m_Safety = CollectionHelper.CreateSafetyHandle(allocator);
  124. CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText");
  125. AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
  126. #endif
  127. m_Data = UnsafeText.Alloc(allocator);
  128. *m_Data = new UnsafeText(capacity, allocator);
  129. }
  130. /// <summary>
  131. /// Initializes and returns an instance of NativeText with an initial capacity of 512 bytes.
  132. /// </summary>
  133. /// <param name="allocator">The allocator to use.</param>
  134. public NativeText(Allocator allocator) : this((AllocatorManager.AllocatorHandle)allocator)
  135. {
  136. }
  137. /// <summary>
  138. /// Initializes and returns an instance of NativeText with an initial capacity of 512 bytes.
  139. /// </summary>
  140. /// <param name="allocator">The allocator to use.</param>
  141. public NativeText(AllocatorManager.AllocatorHandle allocator) : this(512, allocator)
  142. {
  143. }
  144. <#
  145. foreach(var OTHERBYTES in SIZES)
  146. {
  147. #>
  148. /// <summary>
  149. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  150. /// </summary>
  151. /// <param name="source">A string to copy characters from.</param>
  152. /// <param name="allocator">The allocator to use.</param>
  153. public NativeText(in FixedString<#=OTHERBYTES#>Bytes source, AllocatorManager.AllocatorHandle allocator)
  154. : this(source.utf8LengthInBytes, allocator)
  155. {
  156. Length = source.utf8LengthInBytes;
  157. unsafe {
  158. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  159. byte* dbytes = (byte*) m_Data->GetUnsafePtr();
  160. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  161. }
  162. }
  163. /// <summary>
  164. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  165. /// </summary>
  166. /// <param name="source">A string to copy characters from.</param>
  167. /// <param name="allocator">The allocator to use.</param>
  168. public NativeText(in FixedString<#=OTHERBYTES#>Bytes source, Allocator allocator)
  169. : this(source, (AllocatorManager.AllocatorHandle)allocator)
  170. {
  171. }
  172. <#
  173. }
  174. #>
  175. /// <summary>
  176. /// The current length in bytes of this string.
  177. /// </summary>
  178. /// <remarks>
  179. /// The length does not include the null terminator byte.
  180. /// </remarks>
  181. /// <value>The current length in bytes of the UTF-8 encoded string.</value>
  182. public int Length
  183. {
  184. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  185. readonly get
  186. {
  187. CheckNull(m_Data);
  188. CheckRead();
  189. return m_Data->Length;
  190. }
  191. set
  192. {
  193. CheckNull(m_Data);
  194. CheckWriteAndBumpSecondaryVersion();
  195. m_Data->Length = value;
  196. }
  197. }
  198. /// <summary>
  199. /// The current capacity in bytes of this string.
  200. /// </summary>
  201. /// <remarks>
  202. /// The null-terminator byte is not included in the capacity, so the string's character buffer is `Capacity + 1` in size.
  203. /// </remarks>
  204. /// <value>The current capacity in bytes of the string.</value>
  205. public int Capacity
  206. {
  207. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  208. readonly get
  209. {
  210. CheckNull(m_Data);
  211. CheckRead();
  212. return m_Data->Capacity;
  213. }
  214. set
  215. {
  216. CheckNull(m_Data);
  217. CheckWriteAndBumpSecondaryVersion();
  218. m_Data->Capacity = value;
  219. }
  220. }
  221. /// <summary>
  222. /// Attempt to set the length in bytes of this string.
  223. /// </summary>
  224. /// <param name="newLength">The new length in bytes of the string.</param>
  225. /// <param name="clearOptions">Whether any bytes added should be zeroed out.</param>
  226. /// <returns>Always true.</returns>
  227. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
  228. {
  229. CheckWrite();
  230. // this can't ever fail, because if we can't resize malloc will abort
  231. Length = newLength;
  232. return true;
  233. }
  234. /// <summary>
  235. /// Whether this string has no characters.
  236. /// </summary>
  237. /// <value>True if this string has no characters or the string has not been constructed.</value>
  238. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  239. public readonly bool IsEmpty
  240. {
  241. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  242. get
  243. {
  244. if (!IsCreated)
  245. {
  246. return true;
  247. }
  248. CheckRead();
  249. return m_Data->IsEmpty;
  250. }
  251. }
  252. /// <summary>
  253. /// Whether this string's character buffer has been allocated (and not yet deallocated).
  254. /// </summary>
  255. /// <value>Whether this string's character buffer has been allocated (and not yet deallocated).</value>
  256. public readonly bool IsCreated
  257. {
  258. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  259. get => m_Data != null;
  260. }
  261. /// <summary>
  262. /// Returns a pointer to this string's character buffer.
  263. /// </summary>
  264. /// <remarks>
  265. /// The pointer is made invalid by operations that reallocate the character buffer, such as setting <see cref="Capacity"/>.
  266. /// </remarks>
  267. /// <returns>A pointer to this string's character buffer.</returns>
  268. public unsafe byte* GetUnsafePtr()
  269. {
  270. CheckNull(m_Data);
  271. CheckRead();
  272. return m_Data->GetUnsafePtr();
  273. }
  274. /// <summary>
  275. /// The byte at an index. Note that writing to a NativeText.Readonly is not supported; the setter of this property throws when safety checks are enabled.
  276. /// </summary>
  277. /// <param name="index">A zero-based byte index.</param>
  278. /// <value>The byte at the index.</value>
  279. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  280. public byte this[int index]
  281. {
  282. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  283. get
  284. {
  285. CheckNull(m_Data);
  286. CheckRead();
  287. return m_Data->ElementAt(index);
  288. }
  289. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  290. set
  291. {
  292. CheckNull(m_Data);
  293. CheckWrite();
  294. m_Data->ElementAt(index) = value;
  295. }
  296. }
  297. /// <summary>
  298. /// Returns a reference to the byte (not character) at an index.
  299. /// </summary>
  300. /// <remarks>
  301. /// Deallocating or reallocating this string's character buffer makes the reference invalid.
  302. /// </remarks>
  303. /// <param name="index">A byte index.</param>
  304. /// <returns>A reference to the byte at the index.</returns>
  305. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  306. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  307. public ref byte ElementAt(int index)
  308. {
  309. CheckNull(m_Data);
  310. CheckWrite();
  311. return ref m_Data->ElementAt(index);
  312. }
  313. /// <summary>
  314. /// Sets the length to 0.
  315. /// </summary>
  316. public void Clear()
  317. {
  318. Length = 0;
  319. }
  320. /// <summary>
  321. /// Appends a byte.
  322. /// </summary>
  323. /// <remarks>
  324. /// A zero byte will always follow the newly appended byte.
  325. ///
  326. /// 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.
  327. /// </remarks>
  328. /// <param name="value">A byte to append.</param>
  329. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  330. public void Add(in byte value)
  331. {
  332. CheckWrite();
  333. this[Length++] = value;
  334. }
  335. /// <summary>
  336. /// Returns the lexicographical sort order of this string relative to another.
  337. /// </summary>
  338. /// <param name="other">Another string to compare with.</param>
  339. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  340. ///
  341. /// 0 denotes both strings have the same sort position.<br/>
  342. /// -1 denotes that this string should be sorted to precede the other.<br/>
  343. /// +1 denotes that this string should be sorted to follow the other.<br/>
  344. /// </returns>
  345. public int CompareTo(NativeText other)
  346. {
  347. CheckRead();
  348. return FixedStringMethods.CompareTo(ref this, *other.m_Data);
  349. }
  350. /// <summary>
  351. /// Returns true if this string and another are equal.
  352. /// </summary>
  353. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  354. /// <param name="other">Another string to compare with.</param>
  355. /// <returns>True if the two strings are equal.</returns>
  356. public bool Equals(NativeText other)
  357. {
  358. CheckRead();
  359. return FixedStringMethods.Equals(ref this, *other.m_Data);
  360. }
  361. /// <summary>
  362. /// Returns the lexicographical sort order of this string relative to another.
  363. /// </summary>
  364. /// <param name="other">Another string to compare with.</param>
  365. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  366. ///
  367. /// 0 denotes both strings have the same sort position.<br/>
  368. /// -1 denotes that this string should be sorted to precede the other.<br/>
  369. /// +1 denotes that this string should be sorted to follow the other.<br/>
  370. /// </returns>
  371. public int CompareTo(NativeText.ReadOnly other)
  372. {
  373. CheckRead();
  374. return FixedStringMethods.CompareTo(ref this, other);
  375. }
  376. /// <summary>
  377. /// Returns true if this string and another are equal.
  378. /// </summary>
  379. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  380. /// <param name="other">Another string to compare with.</param>
  381. /// <returns>True if the two strings are equal.</returns>
  382. public bool Equals(NativeText.ReadOnly other)
  383. {
  384. CheckRead();
  385. return FixedStringMethods.Equals(ref this, *other.m_Data);
  386. }
  387. /// <summary>
  388. /// Releases all resources (memory and safety handles).
  389. /// </summary>
  390. public void Dispose()
  391. {
  392. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  393. if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
  394. {
  395. AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
  396. }
  397. #endif
  398. if (!IsCreated)
  399. {
  400. return;
  401. }
  402. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  403. CollectionHelper.DisposeSafetyHandle(ref m_Safety);
  404. #endif
  405. UnsafeText.Free(m_Data);
  406. m_Data = null;
  407. }
  408. /// <summary>
  409. /// Creates and schedules a job that releases all resources (memory and safety handles) of this NativeText.
  410. /// </summary>
  411. /// <param name="inputDeps">The dependency for the new job.</param>
  412. /// <returns>The handle of the new job. The job depends upon `inputDeps` and releases all resources (memory and safety handles) of this NativeText.</returns>
  413. public JobHandle Dispose(JobHandle inputDeps)
  414. {
  415. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  416. if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
  417. {
  418. AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
  419. }
  420. #endif
  421. if (!IsCreated)
  422. {
  423. return inputDeps;
  424. }
  425. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  426. var jobHandle = new NativeTextDisposeJob { Data = new NativeTextDispose { m_TextData = m_Data, m_Safety = m_Safety } }.Schedule(inputDeps);
  427. AtomicSafetyHandle.Release(m_Safety);
  428. #else
  429. var jobHandle = new NativeTextDisposeJob { Data = new NativeTextDispose { m_TextData = m_Data } }.Schedule(inputDeps);
  430. #endif
  431. m_Data = null;
  432. return jobHandle;
  433. }
  434. /// <summary>
  435. /// A copy of this string as a managed string.
  436. /// </summary>
  437. /// <remarks>
  438. /// For internal use only. Use <see cref="ToString"/> instead.
  439. /// </remarks>
  440. /// <value>A copy of this string as a managed string.</value>
  441. [CreateProperty]
  442. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  443. [ExcludeFromBurstCompatTesting("Returns managed string")]
  444. public string Value => ToString();
  445. /// <summary>
  446. /// An enumerator over the characters (not bytes) of a NativeText.
  447. /// </summary>
  448. /// <remarks>
  449. /// 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.
  450. /// </remarks>
  451. public struct Enumerator : IEnumerator<Unicode.Rune>
  452. {
  453. NativeText.ReadOnly target;
  454. int offset;
  455. Unicode.Rune current;
  456. /// <summary>
  457. /// Initializes and returns an instance of NativeText.Enumerator.
  458. /// </summary>
  459. /// <param name="source">A NativeText for which to create an enumerator.</param>
  460. public Enumerator(NativeText source)
  461. {
  462. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  463. AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(source.m_Safety);
  464. var ash = source.m_Safety;
  465. AtomicSafetyHandle.UseSecondaryVersion(ref ash);
  466. target = new ReadOnly(source.m_Data, ash);
  467. #else
  468. target = source.AsReadOnly();
  469. #endif
  470. offset = 0;
  471. current = default;
  472. }
  473. /// <summary>
  474. /// Initializes and returns an instance of NativeText.Enumerator.
  475. /// </summary>
  476. /// <param name="source">A NativeText.ReadOnly for which to create an enumerator.</param>
  477. public Enumerator(NativeText.ReadOnly source)
  478. {
  479. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  480. AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(source.m_Safety);
  481. var ash = source.m_Safety;
  482. AtomicSafetyHandle.UseSecondaryVersion(ref ash);
  483. target = new ReadOnly(source.m_Data, ash);
  484. #else
  485. target = source;
  486. #endif
  487. offset = 0;
  488. current = default;
  489. }
  490. /// <summary>
  491. /// Does nothing.
  492. /// </summary>
  493. public void Dispose()
  494. {
  495. }
  496. /// <summary>
  497. /// Advances the enumerator to the next character, returning true if <see cref="Current"/> is valid to read afterwards.
  498. /// </summary>
  499. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  500. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  501. public bool MoveNext()
  502. {
  503. if (offset >= target.Length)
  504. return false;
  505. unsafe
  506. {
  507. Unicode.Utf8ToUcs(out current, target.GetUnsafePtr(), ref offset, target.Length);
  508. }
  509. return true;
  510. }
  511. /// <summary>
  512. /// Resets the enumerator to its initial state.
  513. /// </summary>
  514. public void Reset()
  515. {
  516. offset = 0;
  517. current = default;
  518. }
  519. object IEnumerator.Current
  520. {
  521. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  522. get => Current;
  523. }
  524. /// <summary>
  525. /// The current character.
  526. /// </summary>
  527. /// <value>The current character.</value>
  528. public Unicode.Rune Current => current;
  529. }
  530. /// <summary>
  531. /// Returns an enumerator for iterating over the characters of the NativeText.
  532. /// </summary>
  533. /// <returns>An enumerator for iterating over the characters of the NativeText.</returns>
  534. public Enumerator GetEnumerator()
  535. {
  536. return new Enumerator(this);
  537. }
  538. /// <summary>
  539. /// Returns the lexicographical sort order of this string relative to another.
  540. /// </summary>
  541. /// <param name="other">Another string to compare with.</param>
  542. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  543. ///
  544. /// 0 denotes both strings have the same sort position.<br/>
  545. /// -1 denotes that this string should be sorted to precede the other.<br/>
  546. /// +1 denotes that this string should be sorted to follow the other.<br/>
  547. /// </returns>
  548. [ExcludeFromBurstCompatTesting("Takes managed string")]
  549. public int CompareTo(String other)
  550. {
  551. return ToString().CompareTo(other);
  552. }
  553. /// <summary>
  554. /// Returns true if this string and another are equal.
  555. /// </summary>
  556. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  557. /// <param name="other">Another string to compare with.</param>
  558. /// <returns>True if the two strings are equal.</returns>
  559. [ExcludeFromBurstCompatTesting("Takes managed string")]
  560. public bool Equals(String other)
  561. {
  562. return ToString().Equals(other);
  563. }
  564. <#
  565. //
  566. // Generate easy conversion and comparison between this and other FixedString types
  567. //
  568. foreach (var OTHERBYTES in SIZES)
  569. {
  570. #>
  571. /// <summary>
  572. /// Returns the lexicographical sort order of this string relative to another.
  573. /// </summary>
  574. /// <param name="other">Another string to compare with.</param>
  575. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  576. ///
  577. /// 0 denotes both strings have the same sort position.<br/>
  578. /// -1 denotes that this string should be sorted to precede the other.<br/>
  579. /// +1 denotes that this string should be sorted to follow the other.<br/>
  580. /// </returns>
  581. public int CompareTo(FixedString<#=OTHERBYTES#>Bytes other)
  582. {
  583. return FixedStringMethods.CompareTo(ref this, other);
  584. }
  585. /// <summary>
  586. /// Returns true if two strings are equal.
  587. /// </summary>
  588. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  589. /// <param name="a">A string to compare.</param>
  590. /// <param name="b">Another string to compare.</param>
  591. /// <returns>True if the two strings are equal.</returns>
  592. public static bool operator ==(in NativeText a, in FixedString<#=OTHERBYTES#>Bytes b)
  593. {
  594. unsafe {
  595. var aref = UnsafeUtilityExtensions.AsRef(a);
  596. int alen = aref.Length;
  597. int blen = b.utf8LengthInBytes;
  598. byte* aptr = (byte*) aref.GetUnsafePtr();
  599. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  600. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  601. }
  602. }
  603. /// <summary>
  604. /// Returns true if two strings are unequal.
  605. /// </summary>
  606. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  607. /// <param name="a">A string to compare.</param>
  608. /// <param name="b">Another string to compare.</param>
  609. /// <returns>True if the two strings are unequal.</returns>
  610. public static bool operator !=(in NativeText a, in FixedString<#=OTHERBYTES#>Bytes b)
  611. {
  612. return !(a == b);
  613. }
  614. /// <summary>
  615. /// Returns true if this string and another are equal.
  616. /// </summary>
  617. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  618. /// <param name="other">Another string to compare with.</param>
  619. /// <returns>True if the two strings are equal.</returns>
  620. public bool Equals(FixedString<#=OTHERBYTES#>Bytes other)
  621. {
  622. return this == other;
  623. }
  624. <#
  625. }
  626. #>
  627. /// <summary>
  628. /// Returns a managed string copy of this string.
  629. /// </summary>
  630. /// <returns>A managed string copy of this string.</returns>
  631. [ExcludeFromBurstCompatTesting("Returns managed string")]
  632. public override String ToString()
  633. {
  634. if (m_Data == null)
  635. return "";
  636. CheckRead();
  637. return this.ConvertToString();
  638. }
  639. /// <summary>
  640. /// Returns a hash code of this string.
  641. /// </summary>
  642. /// <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>
  643. /// <returns>A hash code of this string.</returns>
  644. public override int GetHashCode()
  645. {
  646. return this.ComputeHashCode();
  647. }
  648. /// <summary>
  649. /// Returns true if this string and another object are equal.
  650. /// </summary>
  651. /// <remarks>For the object to be equal, it must itself be a managed string, NativeText, or FixedString*N*Bytes.
  652. ///
  653. /// Two strings are equal if they have equal length and all their characters match.</remarks>
  654. /// <param name="other">Another string to compare with.</param>
  655. /// <returns>True if this string and the object are equal.</returns>
  656. [ExcludeFromBurstCompatTesting("Takes managed object")]
  657. public override bool Equals(object other)
  658. {
  659. if(ReferenceEquals(null, other)) return false;
  660. if(other is String aString) return Equals(aString);
  661. if(other is NativeText aNativeText) return Equals(aNativeText);
  662. if(other is ReadOnly aReadOnly) return Equals(aReadOnly);
  663. <#
  664. foreach(var OTHERBYTES in SIZES)
  665. {
  666. #>
  667. if(other is FixedString<#=OTHERBYTES#>Bytes a<#=OTHERBYTES#>) return Equals(a<#=OTHERBYTES#>);
  668. <#
  669. }
  670. #>
  671. return false;
  672. }
  673. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  674. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  675. internal static void CheckNull(void* dataPtr)
  676. {
  677. if (dataPtr == null)
  678. {
  679. throw new InvalidOperationException("NativeText has yet to be created or has been destroyed!");
  680. }
  681. }
  682. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  683. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  684. readonly void CheckRead()
  685. {
  686. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  687. AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
  688. #endif
  689. }
  690. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  691. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  692. void CheckWrite()
  693. {
  694. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  695. AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
  696. #endif
  697. }
  698. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  699. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  700. readonly void CheckWriteAndBumpSecondaryVersion()
  701. {
  702. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  703. AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety);
  704. #endif
  705. }
  706. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  707. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  708. void CheckIndexInRange(int index)
  709. {
  710. if (index < 0)
  711. throw new IndexOutOfRangeException($"Index {index} must be positive.");
  712. if (index >= Length)
  713. throw new IndexOutOfRangeException($"Index {index} is out of range in NativeText of {Length} length.");
  714. }
  715. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  716. void ThrowCopyError(CopyError error, String source)
  717. {
  718. throw new ArgumentException($"NativeText: {error} while copying \"{source}\"");
  719. }
  720. /// <summary>
  721. /// A read-only alias for the value of a NativeText. Does not have its own allocated storage.
  722. /// </summary>
  723. [NativeContainer]
  724. [NativeContainerIsReadOnly]
  725. public unsafe struct ReadOnly
  726. : INativeList<byte>
  727. , IUTF8Bytes
  728. , IComparable<String>
  729. , IEquatable<String>
  730. , IComparable<NativeText>
  731. , IEquatable<NativeText>
  732. <#
  733. foreach (var OTHERBYTES in SIZES)
  734. {
  735. #>
  736. , IComparable<FixedString<#=OTHERBYTES#>Bytes>
  737. , IEquatable<FixedString<#=OTHERBYTES#>Bytes>
  738. <#
  739. }
  740. #>
  741. {
  742. [NativeDisableUnsafePtrRestriction]
  743. internal UnsafeText* m_Data;
  744. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  745. internal AtomicSafetyHandle m_Safety;
  746. internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<NativeText.ReadOnly>();
  747. internal ReadOnly(UnsafeText* text, AtomicSafetyHandle safety)
  748. {
  749. m_Data = text;
  750. m_Safety = safety;
  751. CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText.ReadOnly");
  752. }
  753. #else
  754. internal ReadOnly(UnsafeText* text)
  755. {
  756. m_Data = text;
  757. }
  758. #endif
  759. /// <summary>
  760. /// The current capacity in bytes of this string.
  761. /// </summary>
  762. /// <remarks>
  763. /// The null-terminator byte is not included in the capacity, so the string's character buffer is `Capacity + 1` in size.
  764. /// </remarks>
  765. /// <value>The current capacity in bytes of the string.</value>
  766. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  767. public int Capacity
  768. {
  769. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  770. readonly get
  771. {
  772. CheckNull(m_Data);
  773. CheckRead();
  774. return m_Data->Capacity;
  775. }
  776. set
  777. {
  778. ErrorWrite();
  779. }
  780. }
  781. /// <summary>
  782. /// Whether this string has no characters.
  783. /// </summary>
  784. /// <value>True if this string has no characters or if the string has not been constructed.</value>
  785. public bool IsEmpty
  786. {
  787. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  788. readonly get
  789. {
  790. if (m_Data == null)
  791. {
  792. return true;
  793. }
  794. CheckRead();
  795. return m_Data->IsEmpty;
  796. }
  797. set
  798. {
  799. ErrorWrite();
  800. }
  801. }
  802. /// <summary>
  803. /// The current length in bytes of this string.
  804. /// </summary>
  805. /// <remarks>
  806. /// The length does not include the null terminator byte.
  807. /// </remarks>
  808. /// <value>The current length in bytes of the UTF-8 encoded string.</value>
  809. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  810. public int Length
  811. {
  812. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  813. readonly get
  814. {
  815. CheckNull(m_Data);
  816. CheckRead();
  817. return m_Data->Length;
  818. }
  819. set
  820. {
  821. ErrorWrite();
  822. }
  823. }
  824. /// <summary>
  825. /// The byte at an index.
  826. /// </summary>
  827. /// <param name="index">A zero-based byte index.</param>
  828. /// <value>The byte at the index.</value>
  829. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  830. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  831. public byte this[int index]
  832. {
  833. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  834. readonly get
  835. {
  836. CheckNull(m_Data);
  837. CheckRead();
  838. return m_Data->ElementAt(index);
  839. }
  840. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  841. set
  842. {
  843. ErrorWrite();
  844. }
  845. }
  846. /// <summary>
  847. /// Sets the length to 0. For a NativeText.Readonly this function does nothing, unless safety checks are enabled (in which case it throws).
  848. /// </summary>
  849. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined.</exception>
  850. public void Clear()
  851. {
  852. ErrorWrite();
  853. }
  854. /// <summary>
  855. /// Returns a reference to the byte (not character) at an index. Unsupported by NativeText.ReadOnly.
  856. /// </summary>
  857. /// <remarks>
  858. /// This function is a no-op when ENABLE_UNITY_COLLECTIONS_CHECKS is not defined, throws otherwise.
  859. /// </remarks>
  860. /// <param name="index">A byte index.</param>
  861. /// <returns>A reference to the byte at the index.</returns>
  862. /// <exception cref="NotSupportedException">Thrown when called. This operation is not supported.</exception>
  863. public ref byte ElementAt(int index)
  864. {
  865. throw new NotSupportedException("Trying to retrieve non-readonly ref to NativeText.ReadOnly data. This is not permitted.");
  866. }
  867. /// <summary>
  868. /// Returns a pointer to this string's character buffer.
  869. /// </summary>
  870. /// <remarks>
  871. /// The pointer is made invalid by operations that reallocate the character buffer, such as setting <see cref="Capacity"/>.
  872. /// </remarks>
  873. /// <returns>A pointer to this string's character buffer.</returns>
  874. public byte* GetUnsafePtr()
  875. {
  876. CheckNull(m_Data);
  877. CheckRead();
  878. return m_Data->GetUnsafePtr();
  879. }
  880. /// <summary>
  881. /// Attempt to set the length in bytes of this string. For NativeText.ReadOnly this function is a no-op and always returns false.
  882. /// </summary>
  883. /// <param name="newLength">The new length in bytes of the string.</param>
  884. /// <param name="clearOptions">Whether any bytes added should be zeroed out.</param>
  885. /// <returns>Always false.</returns>
  886. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined.</exception>
  887. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
  888. {
  889. ErrorWrite();
  890. return false;
  891. }
  892. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  893. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  894. internal static void CheckNull(void* dataPtr)
  895. {
  896. if (dataPtr == null)
  897. {
  898. throw new InvalidOperationException("NativeText.ReadOnly has yet to be created or has been destroyed!");
  899. }
  900. }
  901. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  902. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  903. readonly void CheckRead()
  904. {
  905. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  906. // Ensure we are allowed to read
  907. AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
  908. #endif
  909. }
  910. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  911. void ErrorWrite()
  912. {
  913. throw new NotSupportedException("Trying to write to a NativeText.ReadOnly. Write operations are not permitted and are ignored.");
  914. }
  915. /// <summary>
  916. /// Returns the lexicographical sort order of this string relative to another.
  917. /// </summary>
  918. /// <param name="other">Another string to compare with.</param>
  919. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  920. ///
  921. /// 0 denotes both strings have the same sort position.<br/>
  922. /// -1 denotes that this string should be sorted to precede the other.<br/>
  923. /// +1 denotes that this string should be sorted to follow the other.<br/>
  924. /// </returns>
  925. [ExcludeFromBurstCompatTesting("Takes managed string")]
  926. public int CompareTo(String other)
  927. {
  928. CheckNull(m_Data);
  929. CheckRead();
  930. return m_Data->ToString().CompareTo(other);
  931. }
  932. /// <summary>
  933. /// Returns true if this string and another are equal.
  934. /// </summary>
  935. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  936. /// <param name="other">Another string to compare with.</param>
  937. /// <returns>True if the two strings are equal.</returns>
  938. [ExcludeFromBurstCompatTesting("Takes managed string")]
  939. public bool Equals(String other)
  940. {
  941. CheckNull(m_Data);
  942. CheckRead();
  943. return m_Data->ToString().Equals(other);
  944. }
  945. /// <summary>
  946. /// Returns the lexicographical sort order of this string relative to another.
  947. /// </summary>
  948. /// <param name="other">Another string to compare with.</param>
  949. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  950. ///
  951. /// 0 denotes both strings have the same sort position.<br/>
  952. /// -1 denotes that this string should be sorted to precede the other.<br/>
  953. /// +1 denotes that this string should be sorted to follow the other.<br/>
  954. /// </returns>
  955. public int CompareTo(ReadOnly other)
  956. {
  957. CheckNull(m_Data);
  958. CheckRead();
  959. return FixedStringMethods.CompareTo(ref *m_Data, *other.m_Data);
  960. }
  961. /// <summary>
  962. /// Returns true if this string and another are equal.
  963. /// </summary>
  964. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  965. /// <param name="other">Another string to compare with.</param>
  966. /// <returns>True if the two strings are equal.</returns>
  967. public bool Equals(ReadOnly other)
  968. {
  969. CheckNull(m_Data);
  970. CheckRead();
  971. return FixedStringMethods.Equals(ref *m_Data, *other.m_Data);
  972. }
  973. /// <summary>
  974. /// Returns the lexicographical sort order of this string relative to another.
  975. /// </summary>
  976. /// <param name="other">Another string to compare with.</param>
  977. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  978. ///
  979. /// 0 denotes both strings have the same sort position.<br/>
  980. /// -1 denotes that this string should be sorted to precede the other.<br/>
  981. /// +1 denotes that this string should be sorted to follow the other.<br/>
  982. /// </returns>
  983. public int CompareTo(NativeText other)
  984. {
  985. CheckNull(m_Data);
  986. CheckRead();
  987. return FixedStringMethods.CompareTo(ref this, *other.m_Data);
  988. }
  989. /// <summary>
  990. /// Returns true if this string and another are equal.
  991. /// </summary>
  992. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  993. /// <param name="other">Another string to compare with.</param>
  994. /// <returns>True if the two strings are equal.</returns>
  995. public bool Equals(NativeText other)
  996. {
  997. CheckNull(m_Data);
  998. CheckRead();
  999. return FixedStringMethods.Equals(ref this, *other.m_Data);
  1000. }
  1001. <#
  1002. //
  1003. // Generate easy conversion and comparison between this and other FixedString types
  1004. //
  1005. foreach (var OTHERBYTES in SIZES)
  1006. {
  1007. #>
  1008. /// <summary>
  1009. /// Returns the lexicographical sort order of this string relative to another.
  1010. /// </summary>
  1011. /// <param name="other">Another string to compare with.</param>
  1012. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1013. ///
  1014. /// 0 denotes both strings have the same sort position.<br/>
  1015. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1016. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1017. /// </returns>
  1018. public int CompareTo(FixedString<#=OTHERBYTES#>Bytes other)
  1019. {
  1020. return FixedStringMethods.CompareTo(ref this, other);
  1021. }
  1022. /// <summary>
  1023. /// Returns true if two strings are equal.
  1024. /// </summary>
  1025. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1026. /// <param name="a">A string to compare.</param>
  1027. /// <param name="b">Another string to compare.</param>
  1028. /// <returns>True if the two strings are equal.</returns>
  1029. public static bool operator ==(in ReadOnly a, in FixedString<#=OTHERBYTES#>Bytes b)
  1030. {
  1031. CheckNull(a.m_Data);
  1032. a.CheckRead();
  1033. unsafe {
  1034. var aref = *a.m_Data;
  1035. int alen = aref.Length;
  1036. int blen = b.utf8LengthInBytes;
  1037. byte* aptr = (byte*) aref.GetUnsafePtr();
  1038. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  1039. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  1040. }
  1041. }
  1042. /// <summary>
  1043. /// Returns true if two strings are unequal.
  1044. /// </summary>
  1045. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1046. /// <param name="a">A string to compare.</param>
  1047. /// <param name="b">Another string to compare.</param>
  1048. /// <returns>True if the two strings are unequal.</returns>
  1049. public static bool operator !=(in ReadOnly a, in FixedString<#=OTHERBYTES#>Bytes b)
  1050. {
  1051. return !(a == b);
  1052. }
  1053. /// <summary>
  1054. /// Returns true if this string and another are equal.
  1055. /// </summary>
  1056. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1057. /// <param name="other">Another string to compare with.</param>
  1058. /// <returns>True if the two strings are equal.</returns>
  1059. public bool Equals(FixedString<#=OTHERBYTES#>Bytes other)
  1060. {
  1061. return this == other;
  1062. }
  1063. <#
  1064. }
  1065. #>
  1066. /// <summary>
  1067. /// Returns a managed string copy of this string.
  1068. /// </summary>
  1069. /// <returns>A managed string copy of this string.</returns>
  1070. [ExcludeFromBurstCompatTesting("Returns managed string")]
  1071. public override String ToString()
  1072. {
  1073. if (m_Data == null)
  1074. return "";
  1075. CheckRead();
  1076. return this.ConvertToString();
  1077. }
  1078. /// <summary>
  1079. /// Returns a hash code of this string.
  1080. /// </summary>
  1081. /// <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>
  1082. /// <returns>A hash code of this string.</returns>
  1083. public override int GetHashCode()
  1084. {
  1085. CheckRead();
  1086. return this.ComputeHashCode();
  1087. }
  1088. /// <summary>
  1089. /// Returns true if this string and another object are equal.
  1090. /// </summary>
  1091. /// <remarks>For the object to be equal, it must itself be a managed string, NativeText, or FixedString*N*Bytes.
  1092. ///
  1093. /// Two strings are equal if they have equal length and all their characters match.</remarks>
  1094. /// <param name="other">Another string to compare with.</param>
  1095. /// <returns>True if this string and the object are equal.</returns>
  1096. [ExcludeFromBurstCompatTesting("Takes managed object")]
  1097. public override bool Equals(object other)
  1098. {
  1099. if(ReferenceEquals(null, other)) return false;
  1100. if(other is String aString) return Equals(aString);
  1101. if(other is NativeText aNativeText) return Equals(aNativeText);
  1102. if(other is ReadOnly aReadOnly) return Equals(aReadOnly);
  1103. <#
  1104. foreach(var OTHERBYTES in SIZES)
  1105. {
  1106. #>
  1107. if(other is FixedString<#=OTHERBYTES#>Bytes a<#=OTHERBYTES#>) return Equals(a<#=OTHERBYTES#>);
  1108. <#
  1109. }
  1110. #>
  1111. return false;
  1112. }
  1113. /// <summary>
  1114. /// A copy of this string as a managed string.
  1115. /// </summary>
  1116. /// <remarks>
  1117. /// For internal use only. Use <see cref="ToString"/> instead.
  1118. /// </remarks>
  1119. /// <value>A copy of this string as a managed string.</value>
  1120. [CreateProperty]
  1121. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  1122. [ExcludeFromBurstCompatTesting("Returns managed string")]
  1123. public string Value => ToString();
  1124. /// <summary>
  1125. /// Returns an enumerator for iterating over the characters of the NativeText.
  1126. /// </summary>
  1127. /// <returns>An enumerator for iterating over the characters of the NativeText.</returns>
  1128. public Enumerator GetEnumerator()
  1129. {
  1130. return new Enumerator(this);
  1131. }
  1132. }
  1133. /// <summary>
  1134. /// Returns a readonly version of this NativeText instance.
  1135. /// </summary>
  1136. /// <remarks>ReadOnly containers point to the same underlying data as the NativeText it is made from. Note while ReadOnly contains methods that would write to the string data these methods will perform no writes and/or throw a NotSupportedException.</remarks>
  1137. /// <returns>ReadOnly instance for this.</returns>
  1138. public ReadOnly AsReadOnly()
  1139. {
  1140. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  1141. var ash = m_Safety;
  1142. return new ReadOnly(m_Data, ash);
  1143. #else
  1144. return new ReadOnly(m_Data);
  1145. #endif
  1146. }
  1147. }
  1148. [NativeContainer]
  1149. [GenerateTestsForBurstCompatibility]
  1150. internal unsafe struct NativeTextDispose
  1151. {
  1152. [NativeDisableUnsafePtrRestriction]
  1153. public UnsafeText* m_TextData;
  1154. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  1155. public AtomicSafetyHandle m_Safety;
  1156. #endif
  1157. public void Dispose()
  1158. {
  1159. UnsafeText.Free(m_TextData);
  1160. }
  1161. }
  1162. [BurstCompile]
  1163. internal unsafe struct NativeTextDisposeJob : IJob
  1164. {
  1165. public NativeTextDispose Data;
  1166. public void Execute()
  1167. {
  1168. Data.Dispose();
  1169. }
  1170. }
  1171. }
  1172. <#}#>