Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. 
  2. //------------------------------------------------------------------------------
  3. // <auto-generated>
  4. // This code was generated by a tool.
  5. //
  6. // TextTransform Packages/com.unity.collections/Unity.Collections/NativeText.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.Burst;
  19. using Unity.Collections.LowLevel.Unsafe;
  20. using Unity.Jobs;
  21. using UnityEngine.Internal;
  22. using Unity.Properties;
  23. namespace Unity.Collections
  24. {
  25. /// <summary>
  26. /// An unmanaged, 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. [StructLayout(LayoutKind.Sequential)]
  32. [NativeContainer]
  33. [DebuggerDisplay("Length = {Length}")]
  34. [GenerateTestsForBurstCompatibility]
  35. public unsafe partial struct NativeText
  36. : INativeList<byte>
  37. , INativeDisposable
  38. , IUTF8Bytes
  39. , IComparable<String>
  40. , IEquatable<String>
  41. , IComparable<NativeText>
  42. , IEquatable<NativeText>
  43. , IComparable<FixedString32Bytes>
  44. , IEquatable<FixedString32Bytes>
  45. , IComparable<FixedString64Bytes>
  46. , IEquatable<FixedString64Bytes>
  47. , IComparable<FixedString128Bytes>
  48. , IEquatable<FixedString128Bytes>
  49. , IComparable<FixedString512Bytes>
  50. , IEquatable<FixedString512Bytes>
  51. , IComparable<FixedString4096Bytes>
  52. , IEquatable<FixedString4096Bytes>
  53. {
  54. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  55. internal AtomicSafetyHandle m_Safety;
  56. internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<NativeText>();
  57. #endif
  58. // NOTE! This Length is always > 0, because we have a null terminating byte.
  59. // We hide this byte from NativeText users.
  60. [NativeDisableUnsafePtrRestriction]
  61. internal UnsafeText* m_Data;
  62. /// <summary>
  63. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  64. /// </summary>
  65. /// <param name="source">A string to copy characters from.</param>
  66. /// <param name="allocator">The allocator to use.</param>
  67. [ExcludeFromBurstCompatTesting("Takes managed string")]
  68. public NativeText(String source, Allocator allocator) : this(source, (AllocatorManager.AllocatorHandle)allocator)
  69. {
  70. }
  71. /// <summary>
  72. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  73. /// </summary>
  74. /// <param name="source">A string to copy characters from.</param>
  75. /// <param name="allocator">The allocator to use.</param>
  76. [ExcludeFromBurstCompatTesting("Takes managed string")]
  77. public NativeText(String source, AllocatorManager.AllocatorHandle allocator) : this(source.Length * 2, allocator)
  78. {
  79. Length = source.Length * 2;
  80. unsafe
  81. {
  82. fixed (char* sourceptr = source)
  83. {
  84. var error = UTF8ArrayUnsafeUtility.Copy(GetUnsafePtr(), out var actualBytes, Capacity, sourceptr, source.Length);
  85. if (error != CopyError.None)
  86. {
  87. CheckNull(m_Data);
  88. m_Data->Dispose();
  89. m_Data = UnsafeText.Alloc(allocator);
  90. *m_Data = default(UnsafeText);
  91. ThrowCopyError(error, source);
  92. }
  93. Length = actualBytes;
  94. }
  95. }
  96. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  97. CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText");
  98. AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
  99. #endif
  100. }
  101. /// <summary>
  102. /// Initializes and returns an instance of NativeText.
  103. /// </summary>
  104. /// <param name="capacity">The initial capacity in bytes.</param>
  105. /// <param name="allocator">The allocator to use.</param>
  106. public NativeText(int capacity, Allocator allocator) : this(capacity, (AllocatorManager.AllocatorHandle)allocator)
  107. {
  108. }
  109. /// <summary>
  110. /// Initializes and returns an instance of NativeText.
  111. /// </summary>
  112. /// <param name="capacity">The initial capacity in bytes.</param>
  113. /// <param name="allocator">The allocator to use.</param>
  114. public NativeText(int capacity, AllocatorManager.AllocatorHandle allocator)
  115. {
  116. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  117. CollectionHelper.CheckAllocator(allocator);
  118. m_Safety = CollectionHelper.CreateSafetyHandle(allocator);
  119. CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText");
  120. AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
  121. #endif
  122. m_Data = UnsafeText.Alloc(allocator);
  123. *m_Data = new UnsafeText(capacity, allocator);
  124. }
  125. /// <summary>
  126. /// Initializes and returns an instance of NativeText with an initial capacity of 512 bytes.
  127. /// </summary>
  128. /// <param name="allocator">The allocator to use.</param>
  129. public NativeText(Allocator allocator) : this((AllocatorManager.AllocatorHandle)allocator)
  130. {
  131. }
  132. /// <summary>
  133. /// Initializes and returns an instance of NativeText with an initial capacity of 512 bytes.
  134. /// </summary>
  135. /// <param name="allocator">The allocator to use.</param>
  136. public NativeText(AllocatorManager.AllocatorHandle allocator) : this(512, allocator)
  137. {
  138. }
  139. /// <summary>
  140. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  141. /// </summary>
  142. /// <param name="source">A string to copy characters from.</param>
  143. /// <param name="allocator">The allocator to use.</param>
  144. public NativeText(in FixedString32Bytes source, AllocatorManager.AllocatorHandle allocator)
  145. : this(source.utf8LengthInBytes, allocator)
  146. {
  147. Length = source.utf8LengthInBytes;
  148. unsafe {
  149. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  150. byte* dbytes = (byte*) m_Data->GetUnsafePtr();
  151. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  152. }
  153. }
  154. /// <summary>
  155. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  156. /// </summary>
  157. /// <param name="source">A string to copy characters from.</param>
  158. /// <param name="allocator">The allocator to use.</param>
  159. public NativeText(in FixedString32Bytes source, Allocator allocator)
  160. : this(source, (AllocatorManager.AllocatorHandle)allocator)
  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 FixedString64Bytes source, AllocatorManager.AllocatorHandle allocator)
  169. : this(source.utf8LengthInBytes, allocator)
  170. {
  171. Length = source.utf8LengthInBytes;
  172. unsafe {
  173. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  174. byte* dbytes = (byte*) m_Data->GetUnsafePtr();
  175. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  176. }
  177. }
  178. /// <summary>
  179. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  180. /// </summary>
  181. /// <param name="source">A string to copy characters from.</param>
  182. /// <param name="allocator">The allocator to use.</param>
  183. public NativeText(in FixedString64Bytes source, Allocator allocator)
  184. : this(source, (AllocatorManager.AllocatorHandle)allocator)
  185. {
  186. }
  187. /// <summary>
  188. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  189. /// </summary>
  190. /// <param name="source">A string to copy characters from.</param>
  191. /// <param name="allocator">The allocator to use.</param>
  192. public NativeText(in FixedString128Bytes source, AllocatorManager.AllocatorHandle allocator)
  193. : this(source.utf8LengthInBytes, allocator)
  194. {
  195. Length = source.utf8LengthInBytes;
  196. unsafe {
  197. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  198. byte* dbytes = (byte*) m_Data->GetUnsafePtr();
  199. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  200. }
  201. }
  202. /// <summary>
  203. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  204. /// </summary>
  205. /// <param name="source">A string to copy characters from.</param>
  206. /// <param name="allocator">The allocator to use.</param>
  207. public NativeText(in FixedString128Bytes source, Allocator allocator)
  208. : this(source, (AllocatorManager.AllocatorHandle)allocator)
  209. {
  210. }
  211. /// <summary>
  212. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  213. /// </summary>
  214. /// <param name="source">A string to copy characters from.</param>
  215. /// <param name="allocator">The allocator to use.</param>
  216. public NativeText(in FixedString512Bytes source, AllocatorManager.AllocatorHandle allocator)
  217. : this(source.utf8LengthInBytes, allocator)
  218. {
  219. Length = source.utf8LengthInBytes;
  220. unsafe {
  221. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  222. byte* dbytes = (byte*) m_Data->GetUnsafePtr();
  223. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  224. }
  225. }
  226. /// <summary>
  227. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  228. /// </summary>
  229. /// <param name="source">A string to copy characters from.</param>
  230. /// <param name="allocator">The allocator to use.</param>
  231. public NativeText(in FixedString512Bytes source, Allocator allocator)
  232. : this(source, (AllocatorManager.AllocatorHandle)allocator)
  233. {
  234. }
  235. /// <summary>
  236. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  237. /// </summary>
  238. /// <param name="source">A string to copy characters from.</param>
  239. /// <param name="allocator">The allocator to use.</param>
  240. public NativeText(in FixedString4096Bytes source, AllocatorManager.AllocatorHandle allocator)
  241. : this(source.utf8LengthInBytes, allocator)
  242. {
  243. Length = source.utf8LengthInBytes;
  244. unsafe {
  245. byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
  246. byte* dbytes = (byte*) m_Data->GetUnsafePtr();
  247. UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
  248. }
  249. }
  250. /// <summary>
  251. /// Initializes and returns an instance of NativeText with the characters copied from another string.
  252. /// </summary>
  253. /// <param name="source">A string to copy characters from.</param>
  254. /// <param name="allocator">The allocator to use.</param>
  255. public NativeText(in FixedString4096Bytes source, Allocator allocator)
  256. : this(source, (AllocatorManager.AllocatorHandle)allocator)
  257. {
  258. }
  259. /// <summary>
  260. /// The current length in bytes of this string.
  261. /// </summary>
  262. /// <remarks>
  263. /// The length does not include the null terminator byte.
  264. /// </remarks>
  265. /// <value>The current length in bytes of the UTF-8 encoded string.</value>
  266. public int Length
  267. {
  268. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  269. readonly get
  270. {
  271. CheckNull(m_Data);
  272. CheckRead();
  273. return m_Data->Length;
  274. }
  275. set
  276. {
  277. CheckNull(m_Data);
  278. CheckWriteAndBumpSecondaryVersion();
  279. m_Data->Length = value;
  280. }
  281. }
  282. /// <summary>
  283. /// The current capacity in bytes of this string.
  284. /// </summary>
  285. /// <remarks>
  286. /// The null-terminator byte is not included in the capacity, so the string's character buffer is `Capacity + 1` in size.
  287. /// </remarks>
  288. /// <value>The current capacity in bytes of the string.</value>
  289. public int Capacity
  290. {
  291. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  292. readonly get
  293. {
  294. CheckNull(m_Data);
  295. CheckRead();
  296. return m_Data->Capacity;
  297. }
  298. set
  299. {
  300. CheckNull(m_Data);
  301. CheckWriteAndBumpSecondaryVersion();
  302. m_Data->Capacity = value;
  303. }
  304. }
  305. /// <summary>
  306. /// Attempt to set the length in bytes of this string.
  307. /// </summary>
  308. /// <param name="newLength">The new length in bytes of the string.</param>
  309. /// <param name="clearOptions">Whether any bytes added should be zeroed out.</param>
  310. /// <returns>Always true.</returns>
  311. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
  312. {
  313. CheckWrite();
  314. // this can't ever fail, because if we can't resize malloc will abort
  315. Length = newLength;
  316. return true;
  317. }
  318. /// <summary>
  319. /// Whether this string has no characters.
  320. /// </summary>
  321. /// <value>True if this string has no characters or the string has not been constructed.</value>
  322. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  323. public readonly bool IsEmpty
  324. {
  325. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  326. get
  327. {
  328. if (!IsCreated)
  329. {
  330. return true;
  331. }
  332. CheckRead();
  333. return m_Data->IsEmpty;
  334. }
  335. }
  336. /// <summary>
  337. /// Whether this string's character buffer has been allocated (and not yet deallocated).
  338. /// </summary>
  339. /// <value>Whether this string's character buffer has been allocated (and not yet deallocated).</value>
  340. public readonly bool IsCreated
  341. {
  342. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  343. get => m_Data != null;
  344. }
  345. /// <summary>
  346. /// Returns a pointer to this string's character buffer.
  347. /// </summary>
  348. /// <remarks>
  349. /// The pointer is made invalid by operations that reallocate the character buffer, such as setting <see cref="Capacity"/>.
  350. /// </remarks>
  351. /// <returns>A pointer to this string's character buffer.</returns>
  352. public unsafe byte* GetUnsafePtr()
  353. {
  354. CheckNull(m_Data);
  355. CheckRead();
  356. return m_Data->GetUnsafePtr();
  357. }
  358. /// <summary>
  359. /// 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.
  360. /// </summary>
  361. /// <param name="index">A zero-based byte index.</param>
  362. /// <value>The byte at the index.</value>
  363. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  364. public byte this[int index]
  365. {
  366. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  367. get
  368. {
  369. CheckNull(m_Data);
  370. CheckRead();
  371. return m_Data->ElementAt(index);
  372. }
  373. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  374. set
  375. {
  376. CheckNull(m_Data);
  377. CheckWrite();
  378. m_Data->ElementAt(index) = value;
  379. }
  380. }
  381. /// <summary>
  382. /// Returns a reference to the byte (not character) at an index.
  383. /// </summary>
  384. /// <remarks>
  385. /// Deallocating or reallocating this string's character buffer makes the reference invalid.
  386. /// </remarks>
  387. /// <param name="index">A byte index.</param>
  388. /// <returns>A reference to the byte at the index.</returns>
  389. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  390. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  391. public ref byte ElementAt(int index)
  392. {
  393. CheckNull(m_Data);
  394. CheckWrite();
  395. return ref m_Data->ElementAt(index);
  396. }
  397. /// <summary>
  398. /// Sets the length to 0.
  399. /// </summary>
  400. public void Clear()
  401. {
  402. Length = 0;
  403. }
  404. /// <summary>
  405. /// Appends a byte.
  406. /// </summary>
  407. /// <remarks>
  408. /// A zero byte will always follow the newly appended byte.
  409. ///
  410. /// 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.
  411. /// </remarks>
  412. /// <param name="value">A byte to append.</param>
  413. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  414. public void Add(in byte value)
  415. {
  416. CheckWrite();
  417. this[Length++] = value;
  418. }
  419. /// <summary>
  420. /// Returns the lexicographical sort order of this string relative to another.
  421. /// </summary>
  422. /// <param name="other">Another string to compare with.</param>
  423. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  424. ///
  425. /// 0 denotes both strings have the same sort position.<br/>
  426. /// -1 denotes that this string should be sorted to precede the other.<br/>
  427. /// +1 denotes that this string should be sorted to follow the other.<br/>
  428. /// </returns>
  429. public int CompareTo(NativeText other)
  430. {
  431. CheckRead();
  432. return FixedStringMethods.CompareTo(ref this, *other.m_Data);
  433. }
  434. /// <summary>
  435. /// Returns true if this string and another are equal.
  436. /// </summary>
  437. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  438. /// <param name="other">Another string to compare with.</param>
  439. /// <returns>True if the two strings are equal.</returns>
  440. public bool Equals(NativeText other)
  441. {
  442. CheckRead();
  443. return FixedStringMethods.Equals(ref this, *other.m_Data);
  444. }
  445. /// <summary>
  446. /// Returns the lexicographical sort order of this string relative to another.
  447. /// </summary>
  448. /// <param name="other">Another string to compare with.</param>
  449. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  450. ///
  451. /// 0 denotes both strings have the same sort position.<br/>
  452. /// -1 denotes that this string should be sorted to precede the other.<br/>
  453. /// +1 denotes that this string should be sorted to follow the other.<br/>
  454. /// </returns>
  455. public int CompareTo(NativeText.ReadOnly other)
  456. {
  457. CheckRead();
  458. return FixedStringMethods.CompareTo(ref this, other);
  459. }
  460. /// <summary>
  461. /// Returns true if this string and another are equal.
  462. /// </summary>
  463. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  464. /// <param name="other">Another string to compare with.</param>
  465. /// <returns>True if the two strings are equal.</returns>
  466. public bool Equals(NativeText.ReadOnly other)
  467. {
  468. CheckRead();
  469. return FixedStringMethods.Equals(ref this, *other.m_Data);
  470. }
  471. /// <summary>
  472. /// Releases all resources (memory and safety handles).
  473. /// </summary>
  474. public void Dispose()
  475. {
  476. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  477. if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
  478. {
  479. AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
  480. }
  481. #endif
  482. if (!IsCreated)
  483. {
  484. return;
  485. }
  486. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  487. CollectionHelper.DisposeSafetyHandle(ref m_Safety);
  488. #endif
  489. UnsafeText.Free(m_Data);
  490. m_Data = null;
  491. }
  492. /// <summary>
  493. /// Creates and schedules a job that releases all resources (memory and safety handles) of this NativeText.
  494. /// </summary>
  495. /// <param name="inputDeps">The dependency for the new job.</param>
  496. /// <returns>The handle of the new job. The job depends upon `inputDeps` and releases all resources (memory and safety handles) of this NativeText.</returns>
  497. public JobHandle Dispose(JobHandle inputDeps)
  498. {
  499. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  500. if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
  501. {
  502. AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
  503. }
  504. #endif
  505. if (!IsCreated)
  506. {
  507. return inputDeps;
  508. }
  509. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  510. var jobHandle = new NativeTextDisposeJob { Data = new NativeTextDispose { m_TextData = m_Data, m_Safety = m_Safety } }.Schedule(inputDeps);
  511. AtomicSafetyHandle.Release(m_Safety);
  512. #else
  513. var jobHandle = new NativeTextDisposeJob { Data = new NativeTextDispose { m_TextData = m_Data } }.Schedule(inputDeps);
  514. #endif
  515. m_Data = null;
  516. return jobHandle;
  517. }
  518. /// <summary>
  519. /// A copy of this string as a managed string.
  520. /// </summary>
  521. /// <remarks>
  522. /// For internal use only. Use <see cref="ToString"/> instead.
  523. /// </remarks>
  524. /// <value>A copy of this string as a managed string.</value>
  525. [CreateProperty]
  526. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  527. [ExcludeFromBurstCompatTesting("Returns managed string")]
  528. public string Value => ToString();
  529. /// <summary>
  530. /// An enumerator over the characters (not bytes) of a NativeText.
  531. /// </summary>
  532. /// <remarks>
  533. /// 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.
  534. /// </remarks>
  535. public struct Enumerator : IEnumerator<Unicode.Rune>
  536. {
  537. NativeText.ReadOnly target;
  538. int offset;
  539. Unicode.Rune current;
  540. /// <summary>
  541. /// Initializes and returns an instance of NativeText.Enumerator.
  542. /// </summary>
  543. /// <param name="source">A NativeText for which to create an enumerator.</param>
  544. public Enumerator(NativeText source)
  545. {
  546. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  547. AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(source.m_Safety);
  548. var ash = source.m_Safety;
  549. AtomicSafetyHandle.UseSecondaryVersion(ref ash);
  550. target = new ReadOnly(source.m_Data, ash);
  551. #else
  552. target = source.AsReadOnly();
  553. #endif
  554. offset = 0;
  555. current = default;
  556. }
  557. /// <summary>
  558. /// Initializes and returns an instance of NativeText.Enumerator.
  559. /// </summary>
  560. /// <param name="source">A NativeText.ReadOnly for which to create an enumerator.</param>
  561. public Enumerator(NativeText.ReadOnly source)
  562. {
  563. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  564. AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(source.m_Safety);
  565. var ash = source.m_Safety;
  566. AtomicSafetyHandle.UseSecondaryVersion(ref ash);
  567. target = new ReadOnly(source.m_Data, ash);
  568. #else
  569. target = source;
  570. #endif
  571. offset = 0;
  572. current = default;
  573. }
  574. /// <summary>
  575. /// Does nothing.
  576. /// </summary>
  577. public void Dispose()
  578. {
  579. }
  580. /// <summary>
  581. /// Advances the enumerator to the next character, returning true if <see cref="Current"/> is valid to read afterwards.
  582. /// </summary>
  583. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  584. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  585. public bool MoveNext()
  586. {
  587. if (offset >= target.Length)
  588. return false;
  589. unsafe
  590. {
  591. Unicode.Utf8ToUcs(out current, target.GetUnsafePtr(), ref offset, target.Length);
  592. }
  593. return true;
  594. }
  595. /// <summary>
  596. /// Resets the enumerator to its initial state.
  597. /// </summary>
  598. public void Reset()
  599. {
  600. offset = 0;
  601. current = default;
  602. }
  603. object IEnumerator.Current
  604. {
  605. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  606. get => Current;
  607. }
  608. /// <summary>
  609. /// The current character.
  610. /// </summary>
  611. /// <value>The current character.</value>
  612. public Unicode.Rune Current => current;
  613. }
  614. /// <summary>
  615. /// Returns an enumerator for iterating over the characters of the NativeText.
  616. /// </summary>
  617. /// <returns>An enumerator for iterating over the characters of the NativeText.</returns>
  618. public Enumerator GetEnumerator()
  619. {
  620. return new Enumerator(this);
  621. }
  622. /// <summary>
  623. /// Returns the lexicographical sort order of this string relative to another.
  624. /// </summary>
  625. /// <param name="other">Another string to compare with.</param>
  626. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  627. ///
  628. /// 0 denotes both strings have the same sort position.<br/>
  629. /// -1 denotes that this string should be sorted to precede the other.<br/>
  630. /// +1 denotes that this string should be sorted to follow the other.<br/>
  631. /// </returns>
  632. [ExcludeFromBurstCompatTesting("Takes managed string")]
  633. public int CompareTo(String other)
  634. {
  635. return ToString().CompareTo(other);
  636. }
  637. /// <summary>
  638. /// Returns true if this string and another are equal.
  639. /// </summary>
  640. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  641. /// <param name="other">Another string to compare with.</param>
  642. /// <returns>True if the two strings are equal.</returns>
  643. [ExcludeFromBurstCompatTesting("Takes managed string")]
  644. public bool Equals(String other)
  645. {
  646. return ToString().Equals(other);
  647. }
  648. /// <summary>
  649. /// Returns the lexicographical sort order of this string relative to another.
  650. /// </summary>
  651. /// <param name="other">Another string to compare with.</param>
  652. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  653. ///
  654. /// 0 denotes both strings have the same sort position.<br/>
  655. /// -1 denotes that this string should be sorted to precede the other.<br/>
  656. /// +1 denotes that this string should be sorted to follow the other.<br/>
  657. /// </returns>
  658. public int CompareTo(FixedString32Bytes other)
  659. {
  660. return FixedStringMethods.CompareTo(ref this, other);
  661. }
  662. /// <summary>
  663. /// Returns true if two strings are equal.
  664. /// </summary>
  665. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  666. /// <param name="a">A string to compare.</param>
  667. /// <param name="b">Another string to compare.</param>
  668. /// <returns>True if the two strings are equal.</returns>
  669. public static bool operator ==(in NativeText a, in FixedString32Bytes b)
  670. {
  671. unsafe {
  672. var aref = UnsafeUtilityExtensions.AsRef(a);
  673. int alen = aref.Length;
  674. int blen = b.utf8LengthInBytes;
  675. byte* aptr = (byte*) aref.GetUnsafePtr();
  676. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  677. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  678. }
  679. }
  680. /// <summary>
  681. /// Returns true if two strings are unequal.
  682. /// </summary>
  683. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  684. /// <param name="a">A string to compare.</param>
  685. /// <param name="b">Another string to compare.</param>
  686. /// <returns>True if the two strings are unequal.</returns>
  687. public static bool operator !=(in NativeText a, in FixedString32Bytes b)
  688. {
  689. return !(a == b);
  690. }
  691. /// <summary>
  692. /// Returns true if this string and another are equal.
  693. /// </summary>
  694. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  695. /// <param name="other">Another string to compare with.</param>
  696. /// <returns>True if the two strings are equal.</returns>
  697. public bool Equals(FixedString32Bytes other)
  698. {
  699. return this == other;
  700. }
  701. /// <summary>
  702. /// Returns the lexicographical sort order of this string relative to another.
  703. /// </summary>
  704. /// <param name="other">Another string to compare with.</param>
  705. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  706. ///
  707. /// 0 denotes both strings have the same sort position.<br/>
  708. /// -1 denotes that this string should be sorted to precede the other.<br/>
  709. /// +1 denotes that this string should be sorted to follow the other.<br/>
  710. /// </returns>
  711. public int CompareTo(FixedString64Bytes other)
  712. {
  713. return FixedStringMethods.CompareTo(ref this, other);
  714. }
  715. /// <summary>
  716. /// Returns true if two strings are equal.
  717. /// </summary>
  718. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  719. /// <param name="a">A string to compare.</param>
  720. /// <param name="b">Another string to compare.</param>
  721. /// <returns>True if the two strings are equal.</returns>
  722. public static bool operator ==(in NativeText a, in FixedString64Bytes b)
  723. {
  724. unsafe {
  725. var aref = UnsafeUtilityExtensions.AsRef(a);
  726. int alen = aref.Length;
  727. int blen = b.utf8LengthInBytes;
  728. byte* aptr = (byte*) aref.GetUnsafePtr();
  729. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  730. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  731. }
  732. }
  733. /// <summary>
  734. /// Returns true if two strings are unequal.
  735. /// </summary>
  736. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  737. /// <param name="a">A string to compare.</param>
  738. /// <param name="b">Another string to compare.</param>
  739. /// <returns>True if the two strings are unequal.</returns>
  740. public static bool operator !=(in NativeText a, in FixedString64Bytes b)
  741. {
  742. return !(a == b);
  743. }
  744. /// <summary>
  745. /// Returns true if this string and another are equal.
  746. /// </summary>
  747. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  748. /// <param name="other">Another string to compare with.</param>
  749. /// <returns>True if the two strings are equal.</returns>
  750. public bool Equals(FixedString64Bytes other)
  751. {
  752. return this == other;
  753. }
  754. /// <summary>
  755. /// Returns the lexicographical sort order of this string relative to another.
  756. /// </summary>
  757. /// <param name="other">Another string to compare with.</param>
  758. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  759. ///
  760. /// 0 denotes both strings have the same sort position.<br/>
  761. /// -1 denotes that this string should be sorted to precede the other.<br/>
  762. /// +1 denotes that this string should be sorted to follow the other.<br/>
  763. /// </returns>
  764. public int CompareTo(FixedString128Bytes other)
  765. {
  766. return FixedStringMethods.CompareTo(ref this, other);
  767. }
  768. /// <summary>
  769. /// Returns true if two strings are equal.
  770. /// </summary>
  771. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  772. /// <param name="a">A string to compare.</param>
  773. /// <param name="b">Another string to compare.</param>
  774. /// <returns>True if the two strings are equal.</returns>
  775. public static bool operator ==(in NativeText a, in FixedString128Bytes b)
  776. {
  777. unsafe {
  778. var aref = UnsafeUtilityExtensions.AsRef(a);
  779. int alen = aref.Length;
  780. int blen = b.utf8LengthInBytes;
  781. byte* aptr = (byte*) aref.GetUnsafePtr();
  782. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  783. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  784. }
  785. }
  786. /// <summary>
  787. /// Returns true if two strings are unequal.
  788. /// </summary>
  789. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  790. /// <param name="a">A string to compare.</param>
  791. /// <param name="b">Another string to compare.</param>
  792. /// <returns>True if the two strings are unequal.</returns>
  793. public static bool operator !=(in NativeText a, in FixedString128Bytes b)
  794. {
  795. return !(a == b);
  796. }
  797. /// <summary>
  798. /// Returns true if this string and another are equal.
  799. /// </summary>
  800. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  801. /// <param name="other">Another string to compare with.</param>
  802. /// <returns>True if the two strings are equal.</returns>
  803. public bool Equals(FixedString128Bytes other)
  804. {
  805. return this == other;
  806. }
  807. /// <summary>
  808. /// Returns the lexicographical sort order of this string relative to another.
  809. /// </summary>
  810. /// <param name="other">Another string to compare with.</param>
  811. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  812. ///
  813. /// 0 denotes both strings have the same sort position.<br/>
  814. /// -1 denotes that this string should be sorted to precede the other.<br/>
  815. /// +1 denotes that this string should be sorted to follow the other.<br/>
  816. /// </returns>
  817. public int CompareTo(FixedString512Bytes other)
  818. {
  819. return FixedStringMethods.CompareTo(ref this, other);
  820. }
  821. /// <summary>
  822. /// Returns true if two strings are equal.
  823. /// </summary>
  824. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  825. /// <param name="a">A string to compare.</param>
  826. /// <param name="b">Another string to compare.</param>
  827. /// <returns>True if the two strings are equal.</returns>
  828. public static bool operator ==(in NativeText a, in FixedString512Bytes b)
  829. {
  830. unsafe {
  831. var aref = UnsafeUtilityExtensions.AsRef(a);
  832. int alen = aref.Length;
  833. int blen = b.utf8LengthInBytes;
  834. byte* aptr = (byte*) aref.GetUnsafePtr();
  835. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  836. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  837. }
  838. }
  839. /// <summary>
  840. /// Returns true if two strings are unequal.
  841. /// </summary>
  842. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  843. /// <param name="a">A string to compare.</param>
  844. /// <param name="b">Another string to compare.</param>
  845. /// <returns>True if the two strings are unequal.</returns>
  846. public static bool operator !=(in NativeText a, in FixedString512Bytes b)
  847. {
  848. return !(a == b);
  849. }
  850. /// <summary>
  851. /// Returns true if this string and another are equal.
  852. /// </summary>
  853. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  854. /// <param name="other">Another string to compare with.</param>
  855. /// <returns>True if the two strings are equal.</returns>
  856. public bool Equals(FixedString512Bytes other)
  857. {
  858. return this == other;
  859. }
  860. /// <summary>
  861. /// Returns the lexicographical sort order of this string relative to another.
  862. /// </summary>
  863. /// <param name="other">Another string to compare with.</param>
  864. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  865. ///
  866. /// 0 denotes both strings have the same sort position.<br/>
  867. /// -1 denotes that this string should be sorted to precede the other.<br/>
  868. /// +1 denotes that this string should be sorted to follow the other.<br/>
  869. /// </returns>
  870. public int CompareTo(FixedString4096Bytes other)
  871. {
  872. return FixedStringMethods.CompareTo(ref this, other);
  873. }
  874. /// <summary>
  875. /// Returns true if two strings are equal.
  876. /// </summary>
  877. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  878. /// <param name="a">A string to compare.</param>
  879. /// <param name="b">Another string to compare.</param>
  880. /// <returns>True if the two strings are equal.</returns>
  881. public static bool operator ==(in NativeText a, in FixedString4096Bytes b)
  882. {
  883. unsafe {
  884. var aref = UnsafeUtilityExtensions.AsRef(a);
  885. int alen = aref.Length;
  886. int blen = b.utf8LengthInBytes;
  887. byte* aptr = (byte*) aref.GetUnsafePtr();
  888. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  889. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  890. }
  891. }
  892. /// <summary>
  893. /// Returns true if two strings are unequal.
  894. /// </summary>
  895. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  896. /// <param name="a">A string to compare.</param>
  897. /// <param name="b">Another string to compare.</param>
  898. /// <returns>True if the two strings are unequal.</returns>
  899. public static bool operator !=(in NativeText a, in FixedString4096Bytes b)
  900. {
  901. return !(a == b);
  902. }
  903. /// <summary>
  904. /// Returns true if this string and another are equal.
  905. /// </summary>
  906. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  907. /// <param name="other">Another string to compare with.</param>
  908. /// <returns>True if the two strings are equal.</returns>
  909. public bool Equals(FixedString4096Bytes other)
  910. {
  911. return this == other;
  912. }
  913. /// <summary>
  914. /// Returns a managed string copy of this string.
  915. /// </summary>
  916. /// <returns>A managed string copy of this string.</returns>
  917. [ExcludeFromBurstCompatTesting("Returns managed string")]
  918. public override String ToString()
  919. {
  920. if (m_Data == null)
  921. return "";
  922. CheckRead();
  923. return this.ConvertToString();
  924. }
  925. /// <summary>
  926. /// Returns a hash code of this string.
  927. /// </summary>
  928. /// <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>
  929. /// <returns>A hash code of this string.</returns>
  930. public override int GetHashCode()
  931. {
  932. return this.ComputeHashCode();
  933. }
  934. /// <summary>
  935. /// Returns true if this string and another object are equal.
  936. /// </summary>
  937. /// <remarks>For the object to be equal, it must itself be a managed string, NativeText, or FixedString*N*Bytes.
  938. ///
  939. /// Two strings are equal if they have equal length and all their characters match.</remarks>
  940. /// <param name="other">Another string to compare with.</param>
  941. /// <returns>True if this string and the object are equal.</returns>
  942. [ExcludeFromBurstCompatTesting("Takes managed object")]
  943. public override bool Equals(object other)
  944. {
  945. if(ReferenceEquals(null, other)) return false;
  946. if(other is String aString) return Equals(aString);
  947. if(other is NativeText aNativeText) return Equals(aNativeText);
  948. if(other is ReadOnly aReadOnly) return Equals(aReadOnly);
  949. if(other is FixedString32Bytes a32) return Equals(a32);
  950. if(other is FixedString64Bytes a64) return Equals(a64);
  951. if(other is FixedString128Bytes a128) return Equals(a128);
  952. if(other is FixedString512Bytes a512) return Equals(a512);
  953. if(other is FixedString4096Bytes a4096) return Equals(a4096);
  954. return false;
  955. }
  956. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  957. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  958. internal static void CheckNull(void* dataPtr)
  959. {
  960. if (dataPtr == null)
  961. {
  962. throw new InvalidOperationException("NativeText has yet to be created or has been destroyed!");
  963. }
  964. }
  965. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  966. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  967. readonly void CheckRead()
  968. {
  969. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  970. AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
  971. #endif
  972. }
  973. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  974. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  975. void CheckWrite()
  976. {
  977. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  978. AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
  979. #endif
  980. }
  981. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  982. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  983. readonly void CheckWriteAndBumpSecondaryVersion()
  984. {
  985. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  986. AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety);
  987. #endif
  988. }
  989. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  990. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  991. void CheckIndexInRange(int index)
  992. {
  993. if (index < 0)
  994. throw new IndexOutOfRangeException($"Index {index} must be positive.");
  995. if (index >= Length)
  996. throw new IndexOutOfRangeException($"Index {index} is out of range in NativeText of {Length} length.");
  997. }
  998. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  999. void ThrowCopyError(CopyError error, String source)
  1000. {
  1001. throw new ArgumentException($"NativeText: {error} while copying \"{source}\"");
  1002. }
  1003. /// <summary>
  1004. /// A read-only alias for the value of a NativeText. Does not have its own allocated storage.
  1005. /// </summary>
  1006. [NativeContainer]
  1007. [NativeContainerIsReadOnly]
  1008. public unsafe struct ReadOnly
  1009. : INativeList<byte>
  1010. , IUTF8Bytes
  1011. , IComparable<String>
  1012. , IEquatable<String>
  1013. , IComparable<NativeText>
  1014. , IEquatable<NativeText>
  1015. , IComparable<FixedString32Bytes>
  1016. , IEquatable<FixedString32Bytes>
  1017. , IComparable<FixedString64Bytes>
  1018. , IEquatable<FixedString64Bytes>
  1019. , IComparable<FixedString128Bytes>
  1020. , IEquatable<FixedString128Bytes>
  1021. , IComparable<FixedString512Bytes>
  1022. , IEquatable<FixedString512Bytes>
  1023. , IComparable<FixedString4096Bytes>
  1024. , IEquatable<FixedString4096Bytes>
  1025. {
  1026. [NativeDisableUnsafePtrRestriction]
  1027. internal UnsafeText* m_Data;
  1028. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  1029. internal AtomicSafetyHandle m_Safety;
  1030. internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<NativeText.ReadOnly>();
  1031. internal ReadOnly(UnsafeText* text, AtomicSafetyHandle safety)
  1032. {
  1033. m_Data = text;
  1034. m_Safety = safety;
  1035. CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText.ReadOnly");
  1036. }
  1037. #else
  1038. internal ReadOnly(UnsafeText* text)
  1039. {
  1040. m_Data = text;
  1041. }
  1042. #endif
  1043. /// <summary>
  1044. /// The current capacity in bytes of this string.
  1045. /// </summary>
  1046. /// <remarks>
  1047. /// The null-terminator byte is not included in the capacity, so the string's character buffer is `Capacity + 1` in size.
  1048. /// </remarks>
  1049. /// <value>The current capacity in bytes of the string.</value>
  1050. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  1051. public int Capacity
  1052. {
  1053. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1054. readonly get
  1055. {
  1056. CheckNull(m_Data);
  1057. CheckRead();
  1058. return m_Data->Capacity;
  1059. }
  1060. set
  1061. {
  1062. ErrorWrite();
  1063. }
  1064. }
  1065. /// <summary>
  1066. /// Whether this string has no characters.
  1067. /// </summary>
  1068. /// <value>True if this string has no characters or if the string has not been constructed.</value>
  1069. public bool IsEmpty
  1070. {
  1071. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1072. readonly get
  1073. {
  1074. if (m_Data == null)
  1075. {
  1076. return true;
  1077. }
  1078. CheckRead();
  1079. return m_Data->IsEmpty;
  1080. }
  1081. set
  1082. {
  1083. ErrorWrite();
  1084. }
  1085. }
  1086. /// <summary>
  1087. /// The current length in bytes of this string.
  1088. /// </summary>
  1089. /// <remarks>
  1090. /// The length does not include the null terminator byte.
  1091. /// </remarks>
  1092. /// <value>The current length in bytes of the UTF-8 encoded string.</value>
  1093. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  1094. public int Length
  1095. {
  1096. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1097. readonly get
  1098. {
  1099. CheckNull(m_Data);
  1100. CheckRead();
  1101. return m_Data->Length;
  1102. }
  1103. set
  1104. {
  1105. ErrorWrite();
  1106. }
  1107. }
  1108. /// <summary>
  1109. /// The byte at an index.
  1110. /// </summary>
  1111. /// <param name="index">A zero-based byte index.</param>
  1112. /// <value>The byte at the index.</value>
  1113. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1114. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.</exception>
  1115. public byte this[int index]
  1116. {
  1117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1118. readonly get
  1119. {
  1120. CheckNull(m_Data);
  1121. CheckRead();
  1122. return m_Data->ElementAt(index);
  1123. }
  1124. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1125. set
  1126. {
  1127. ErrorWrite();
  1128. }
  1129. }
  1130. /// <summary>
  1131. /// Sets the length to 0. For a NativeText.Readonly this function does nothing, unless safety checks are enabled (in which case it throws).
  1132. /// </summary>
  1133. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined.</exception>
  1134. public void Clear()
  1135. {
  1136. ErrorWrite();
  1137. }
  1138. /// <summary>
  1139. /// Returns a reference to the byte (not character) at an index. Unsupported by NativeText.ReadOnly.
  1140. /// </summary>
  1141. /// <remarks>
  1142. /// This function is a no-op when ENABLE_UNITY_COLLECTIONS_CHECKS is not defined, throws otherwise.
  1143. /// </remarks>
  1144. /// <param name="index">A byte index.</param>
  1145. /// <returns>A reference to the byte at the index.</returns>
  1146. /// <exception cref="NotSupportedException">Thrown when called. This operation is not supported.</exception>
  1147. public ref byte ElementAt(int index)
  1148. {
  1149. throw new NotSupportedException("Trying to retrieve non-readonly ref to NativeText.ReadOnly data. This is not permitted.");
  1150. }
  1151. /// <summary>
  1152. /// Returns a pointer to this string's character buffer.
  1153. /// </summary>
  1154. /// <remarks>
  1155. /// The pointer is made invalid by operations that reallocate the character buffer, such as setting <see cref="Capacity"/>.
  1156. /// </remarks>
  1157. /// <returns>A pointer to this string's character buffer.</returns>
  1158. public byte* GetUnsafePtr()
  1159. {
  1160. CheckNull(m_Data);
  1161. CheckRead();
  1162. return m_Data->GetUnsafePtr();
  1163. }
  1164. /// <summary>
  1165. /// Attempt to set the length in bytes of this string. For NativeText.ReadOnly this function is a no-op and always returns false.
  1166. /// </summary>
  1167. /// <param name="newLength">The new length in bytes of the string.</param>
  1168. /// <param name="clearOptions">Whether any bytes added should be zeroed out.</param>
  1169. /// <returns>Always false.</returns>
  1170. /// <exception cref="NotSupportedException">Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined.</exception>
  1171. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
  1172. {
  1173. ErrorWrite();
  1174. return false;
  1175. }
  1176. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  1177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1178. internal static void CheckNull(void* dataPtr)
  1179. {
  1180. if (dataPtr == null)
  1181. {
  1182. throw new InvalidOperationException("NativeText.ReadOnly has yet to be created or has been destroyed!");
  1183. }
  1184. }
  1185. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  1186. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1187. readonly void CheckRead()
  1188. {
  1189. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  1190. // Ensure we are allowed to read
  1191. AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
  1192. #endif
  1193. }
  1194. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  1195. void ErrorWrite()
  1196. {
  1197. throw new NotSupportedException("Trying to write to a NativeText.ReadOnly. Write operations are not permitted and are ignored.");
  1198. }
  1199. /// <summary>
  1200. /// Returns the lexicographical sort order of this string relative to another.
  1201. /// </summary>
  1202. /// <param name="other">Another string to compare with.</param>
  1203. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1204. ///
  1205. /// 0 denotes both strings have the same sort position.<br/>
  1206. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1207. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1208. /// </returns>
  1209. [ExcludeFromBurstCompatTesting("Takes managed string")]
  1210. public int CompareTo(String other)
  1211. {
  1212. CheckNull(m_Data);
  1213. CheckRead();
  1214. return m_Data->ToString().CompareTo(other);
  1215. }
  1216. /// <summary>
  1217. /// Returns true if this string and another are equal.
  1218. /// </summary>
  1219. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1220. /// <param name="other">Another string to compare with.</param>
  1221. /// <returns>True if the two strings are equal.</returns>
  1222. [ExcludeFromBurstCompatTesting("Takes managed string")]
  1223. public bool Equals(String other)
  1224. {
  1225. CheckNull(m_Data);
  1226. CheckRead();
  1227. return m_Data->ToString().Equals(other);
  1228. }
  1229. /// <summary>
  1230. /// Returns the lexicographical sort order of this string relative to another.
  1231. /// </summary>
  1232. /// <param name="other">Another string to compare with.</param>
  1233. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1234. ///
  1235. /// 0 denotes both strings have the same sort position.<br/>
  1236. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1237. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1238. /// </returns>
  1239. public int CompareTo(ReadOnly other)
  1240. {
  1241. CheckNull(m_Data);
  1242. CheckRead();
  1243. return FixedStringMethods.CompareTo(ref *m_Data, *other.m_Data);
  1244. }
  1245. /// <summary>
  1246. /// Returns true if this string and another are equal.
  1247. /// </summary>
  1248. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1249. /// <param name="other">Another string to compare with.</param>
  1250. /// <returns>True if the two strings are equal.</returns>
  1251. public bool Equals(ReadOnly other)
  1252. {
  1253. CheckNull(m_Data);
  1254. CheckRead();
  1255. return FixedStringMethods.Equals(ref *m_Data, *other.m_Data);
  1256. }
  1257. /// <summary>
  1258. /// Returns the lexicographical sort order of this string relative to another.
  1259. /// </summary>
  1260. /// <param name="other">Another string to compare with.</param>
  1261. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1262. ///
  1263. /// 0 denotes both strings have the same sort position.<br/>
  1264. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1265. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1266. /// </returns>
  1267. public int CompareTo(NativeText other)
  1268. {
  1269. CheckNull(m_Data);
  1270. CheckRead();
  1271. return FixedStringMethods.CompareTo(ref this, *other.m_Data);
  1272. }
  1273. /// <summary>
  1274. /// Returns true if this string and another are equal.
  1275. /// </summary>
  1276. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1277. /// <param name="other">Another string to compare with.</param>
  1278. /// <returns>True if the two strings are equal.</returns>
  1279. public bool Equals(NativeText other)
  1280. {
  1281. CheckNull(m_Data);
  1282. CheckRead();
  1283. return FixedStringMethods.Equals(ref this, *other.m_Data);
  1284. }
  1285. /// <summary>
  1286. /// Returns the lexicographical sort order of this string relative to another.
  1287. /// </summary>
  1288. /// <param name="other">Another string to compare with.</param>
  1289. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1290. ///
  1291. /// 0 denotes both strings have the same sort position.<br/>
  1292. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1293. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1294. /// </returns>
  1295. public int CompareTo(FixedString32Bytes other)
  1296. {
  1297. return FixedStringMethods.CompareTo(ref this, other);
  1298. }
  1299. /// <summary>
  1300. /// Returns true if two strings are equal.
  1301. /// </summary>
  1302. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1303. /// <param name="a">A string to compare.</param>
  1304. /// <param name="b">Another string to compare.</param>
  1305. /// <returns>True if the two strings are equal.</returns>
  1306. public static bool operator ==(in ReadOnly a, in FixedString32Bytes b)
  1307. {
  1308. CheckNull(a.m_Data);
  1309. a.CheckRead();
  1310. unsafe {
  1311. var aref = *a.m_Data;
  1312. int alen = aref.Length;
  1313. int blen = b.utf8LengthInBytes;
  1314. byte* aptr = (byte*) aref.GetUnsafePtr();
  1315. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  1316. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  1317. }
  1318. }
  1319. /// <summary>
  1320. /// Returns true if two strings are unequal.
  1321. /// </summary>
  1322. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1323. /// <param name="a">A string to compare.</param>
  1324. /// <param name="b">Another string to compare.</param>
  1325. /// <returns>True if the two strings are unequal.</returns>
  1326. public static bool operator !=(in ReadOnly a, in FixedString32Bytes b)
  1327. {
  1328. return !(a == b);
  1329. }
  1330. /// <summary>
  1331. /// Returns true if this string and another are equal.
  1332. /// </summary>
  1333. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1334. /// <param name="other">Another string to compare with.</param>
  1335. /// <returns>True if the two strings are equal.</returns>
  1336. public bool Equals(FixedString32Bytes other)
  1337. {
  1338. return this == other;
  1339. }
  1340. /// <summary>
  1341. /// Returns the lexicographical sort order of this string relative to another.
  1342. /// </summary>
  1343. /// <param name="other">Another string to compare with.</param>
  1344. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1345. ///
  1346. /// 0 denotes both strings have the same sort position.<br/>
  1347. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1348. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1349. /// </returns>
  1350. public int CompareTo(FixedString64Bytes other)
  1351. {
  1352. return FixedStringMethods.CompareTo(ref this, other);
  1353. }
  1354. /// <summary>
  1355. /// Returns true if two strings are equal.
  1356. /// </summary>
  1357. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1358. /// <param name="a">A string to compare.</param>
  1359. /// <param name="b">Another string to compare.</param>
  1360. /// <returns>True if the two strings are equal.</returns>
  1361. public static bool operator ==(in ReadOnly a, in FixedString64Bytes b)
  1362. {
  1363. CheckNull(a.m_Data);
  1364. a.CheckRead();
  1365. unsafe {
  1366. var aref = *a.m_Data;
  1367. int alen = aref.Length;
  1368. int blen = b.utf8LengthInBytes;
  1369. byte* aptr = (byte*) aref.GetUnsafePtr();
  1370. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  1371. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  1372. }
  1373. }
  1374. /// <summary>
  1375. /// Returns true if two strings are unequal.
  1376. /// </summary>
  1377. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1378. /// <param name="a">A string to compare.</param>
  1379. /// <param name="b">Another string to compare.</param>
  1380. /// <returns>True if the two strings are unequal.</returns>
  1381. public static bool operator !=(in ReadOnly a, in FixedString64Bytes b)
  1382. {
  1383. return !(a == b);
  1384. }
  1385. /// <summary>
  1386. /// Returns true if this string and another are equal.
  1387. /// </summary>
  1388. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1389. /// <param name="other">Another string to compare with.</param>
  1390. /// <returns>True if the two strings are equal.</returns>
  1391. public bool Equals(FixedString64Bytes other)
  1392. {
  1393. return this == other;
  1394. }
  1395. /// <summary>
  1396. /// Returns the lexicographical sort order of this string relative to another.
  1397. /// </summary>
  1398. /// <param name="other">Another string to compare with.</param>
  1399. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1400. ///
  1401. /// 0 denotes both strings have the same sort position.<br/>
  1402. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1403. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1404. /// </returns>
  1405. public int CompareTo(FixedString128Bytes other)
  1406. {
  1407. return FixedStringMethods.CompareTo(ref this, other);
  1408. }
  1409. /// <summary>
  1410. /// Returns true if two strings are equal.
  1411. /// </summary>
  1412. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1413. /// <param name="a">A string to compare.</param>
  1414. /// <param name="b">Another string to compare.</param>
  1415. /// <returns>True if the two strings are equal.</returns>
  1416. public static bool operator ==(in ReadOnly a, in FixedString128Bytes b)
  1417. {
  1418. CheckNull(a.m_Data);
  1419. a.CheckRead();
  1420. unsafe {
  1421. var aref = *a.m_Data;
  1422. int alen = aref.Length;
  1423. int blen = b.utf8LengthInBytes;
  1424. byte* aptr = (byte*) aref.GetUnsafePtr();
  1425. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  1426. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  1427. }
  1428. }
  1429. /// <summary>
  1430. /// Returns true if two strings are unequal.
  1431. /// </summary>
  1432. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1433. /// <param name="a">A string to compare.</param>
  1434. /// <param name="b">Another string to compare.</param>
  1435. /// <returns>True if the two strings are unequal.</returns>
  1436. public static bool operator !=(in ReadOnly a, in FixedString128Bytes b)
  1437. {
  1438. return !(a == b);
  1439. }
  1440. /// <summary>
  1441. /// Returns true if this string and another are equal.
  1442. /// </summary>
  1443. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1444. /// <param name="other">Another string to compare with.</param>
  1445. /// <returns>True if the two strings are equal.</returns>
  1446. public bool Equals(FixedString128Bytes other)
  1447. {
  1448. return this == other;
  1449. }
  1450. /// <summary>
  1451. /// Returns the lexicographical sort order of this string relative to another.
  1452. /// </summary>
  1453. /// <param name="other">Another string to compare with.</param>
  1454. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1455. ///
  1456. /// 0 denotes both strings have the same sort position.<br/>
  1457. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1458. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1459. /// </returns>
  1460. public int CompareTo(FixedString512Bytes other)
  1461. {
  1462. return FixedStringMethods.CompareTo(ref this, other);
  1463. }
  1464. /// <summary>
  1465. /// Returns true if two strings are equal.
  1466. /// </summary>
  1467. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1468. /// <param name="a">A string to compare.</param>
  1469. /// <param name="b">Another string to compare.</param>
  1470. /// <returns>True if the two strings are equal.</returns>
  1471. public static bool operator ==(in ReadOnly a, in FixedString512Bytes b)
  1472. {
  1473. CheckNull(a.m_Data);
  1474. a.CheckRead();
  1475. unsafe {
  1476. var aref = *a.m_Data;
  1477. int alen = aref.Length;
  1478. int blen = b.utf8LengthInBytes;
  1479. byte* aptr = (byte*) aref.GetUnsafePtr();
  1480. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  1481. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  1482. }
  1483. }
  1484. /// <summary>
  1485. /// Returns true if two strings are unequal.
  1486. /// </summary>
  1487. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1488. /// <param name="a">A string to compare.</param>
  1489. /// <param name="b">Another string to compare.</param>
  1490. /// <returns>True if the two strings are unequal.</returns>
  1491. public static bool operator !=(in ReadOnly a, in FixedString512Bytes b)
  1492. {
  1493. return !(a == b);
  1494. }
  1495. /// <summary>
  1496. /// Returns true if this string and another are equal.
  1497. /// </summary>
  1498. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1499. /// <param name="other">Another string to compare with.</param>
  1500. /// <returns>True if the two strings are equal.</returns>
  1501. public bool Equals(FixedString512Bytes other)
  1502. {
  1503. return this == other;
  1504. }
  1505. /// <summary>
  1506. /// Returns the lexicographical sort order of this string relative to another.
  1507. /// </summary>
  1508. /// <param name="other">Another string to compare with.</param>
  1509. /// <returns>A number denoting the lexicographical sort order of this string relative to the other string:
  1510. ///
  1511. /// 0 denotes both strings have the same sort position.<br/>
  1512. /// -1 denotes that this string should be sorted to precede the other.<br/>
  1513. /// +1 denotes that this string should be sorted to follow the other.<br/>
  1514. /// </returns>
  1515. public int CompareTo(FixedString4096Bytes other)
  1516. {
  1517. return FixedStringMethods.CompareTo(ref this, other);
  1518. }
  1519. /// <summary>
  1520. /// Returns true if two strings are equal.
  1521. /// </summary>
  1522. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1523. /// <param name="a">A string to compare.</param>
  1524. /// <param name="b">Another string to compare.</param>
  1525. /// <returns>True if the two strings are equal.</returns>
  1526. public static bool operator ==(in ReadOnly a, in FixedString4096Bytes b)
  1527. {
  1528. CheckNull(a.m_Data);
  1529. a.CheckRead();
  1530. unsafe {
  1531. var aref = *a.m_Data;
  1532. int alen = aref.Length;
  1533. int blen = b.utf8LengthInBytes;
  1534. byte* aptr = (byte*) aref.GetUnsafePtr();
  1535. byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes);
  1536. return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen);
  1537. }
  1538. }
  1539. /// <summary>
  1540. /// Returns true if two strings are unequal.
  1541. /// </summary>
  1542. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1543. /// <param name="a">A string to compare.</param>
  1544. /// <param name="b">Another string to compare.</param>
  1545. /// <returns>True if the two strings are unequal.</returns>
  1546. public static bool operator !=(in ReadOnly a, in FixedString4096Bytes b)
  1547. {
  1548. return !(a == b);
  1549. }
  1550. /// <summary>
  1551. /// Returns true if this string and another are equal.
  1552. /// </summary>
  1553. /// <remarks>Two strings are equal if they have equal length and all their characters match.</remarks>
  1554. /// <param name="other">Another string to compare with.</param>
  1555. /// <returns>True if the two strings are equal.</returns>
  1556. public bool Equals(FixedString4096Bytes other)
  1557. {
  1558. return this == other;
  1559. }
  1560. /// <summary>
  1561. /// Returns a managed string copy of this string.
  1562. /// </summary>
  1563. /// <returns>A managed string copy of this string.</returns>
  1564. [ExcludeFromBurstCompatTesting("Returns managed string")]
  1565. public override String ToString()
  1566. {
  1567. if (m_Data == null)
  1568. return "";
  1569. CheckRead();
  1570. return this.ConvertToString();
  1571. }
  1572. /// <summary>
  1573. /// Returns a hash code of this string.
  1574. /// </summary>
  1575. /// <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>
  1576. /// <returns>A hash code of this string.</returns>
  1577. public override int GetHashCode()
  1578. {
  1579. CheckRead();
  1580. return this.ComputeHashCode();
  1581. }
  1582. /// <summary>
  1583. /// Returns true if this string and another object are equal.
  1584. /// </summary>
  1585. /// <remarks>For the object to be equal, it must itself be a managed string, NativeText, or FixedString*N*Bytes.
  1586. ///
  1587. /// Two strings are equal if they have equal length and all their characters match.</remarks>
  1588. /// <param name="other">Another string to compare with.</param>
  1589. /// <returns>True if this string and the object are equal.</returns>
  1590. [ExcludeFromBurstCompatTesting("Takes managed object")]
  1591. public override bool Equals(object other)
  1592. {
  1593. if(ReferenceEquals(null, other)) return false;
  1594. if(other is String aString) return Equals(aString);
  1595. if(other is NativeText aNativeText) return Equals(aNativeText);
  1596. if(other is ReadOnly aReadOnly) return Equals(aReadOnly);
  1597. if(other is FixedString32Bytes a32) return Equals(a32);
  1598. if(other is FixedString64Bytes a64) return Equals(a64);
  1599. if(other is FixedString128Bytes a128) return Equals(a128);
  1600. if(other is FixedString512Bytes a512) return Equals(a512);
  1601. if(other is FixedString4096Bytes a4096) return Equals(a4096);
  1602. return false;
  1603. }
  1604. /// <summary>
  1605. /// A copy of this string as a managed string.
  1606. /// </summary>
  1607. /// <remarks>
  1608. /// For internal use only. Use <see cref="ToString"/> instead.
  1609. /// </remarks>
  1610. /// <value>A copy of this string as a managed string.</value>
  1611. [CreateProperty]
  1612. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  1613. [ExcludeFromBurstCompatTesting("Returns managed string")]
  1614. public string Value => ToString();
  1615. /// <summary>
  1616. /// Returns an enumerator for iterating over the characters of the NativeText.
  1617. /// </summary>
  1618. /// <returns>An enumerator for iterating over the characters of the NativeText.</returns>
  1619. public Enumerator GetEnumerator()
  1620. {
  1621. return new Enumerator(this);
  1622. }
  1623. }
  1624. /// <summary>
  1625. /// Returns a readonly version of this NativeText instance.
  1626. /// </summary>
  1627. /// <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>
  1628. /// <returns>ReadOnly instance for this.</returns>
  1629. public ReadOnly AsReadOnly()
  1630. {
  1631. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  1632. var ash = m_Safety;
  1633. return new ReadOnly(m_Data, ash);
  1634. #else
  1635. return new ReadOnly(m_Data);
  1636. #endif
  1637. }
  1638. }
  1639. [NativeContainer]
  1640. [GenerateTestsForBurstCompatibility]
  1641. internal unsafe struct NativeTextDispose
  1642. {
  1643. [NativeDisableUnsafePtrRestriction]
  1644. public UnsafeText* m_TextData;
  1645. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  1646. public AtomicSafetyHandle m_Safety;
  1647. #endif
  1648. public void Dispose()
  1649. {
  1650. UnsafeText.Free(m_TextData);
  1651. }
  1652. }
  1653. [BurstCompile]
  1654. internal unsafe struct NativeTextDisposeJob : IJob
  1655. {
  1656. public NativeTextDispose Data;
  1657. public void Execute()
  1658. {
  1659. Data.Dispose();
  1660. }
  1661. }
  1662. }