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

NativeText.gen.cs 74KB

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