RichTextParser.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using Unity.Burst;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace LitMotion
  5. {
  6. [BurstCompile]
  7. internal static class RichTextParser
  8. {
  9. [BurstCompile]
  10. public static void GetSymbols(ref FixedString32Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol32Bytes> symbols, out int charCount)
  11. {
  12. symbols = new UnsafeList<RichTextSymbol32Bytes>(32, allocator);
  13. charCount = 0;
  14. var buffer = new NativeText(32, Allocator.Temp);
  15. var enumerator = source.GetEnumerator();
  16. var currentSymbolType = RichTextSymbolType.Text;
  17. var prevRune = default(Unicode.Rune);
  18. while (enumerator.MoveNext())
  19. {
  20. var current = enumerator.Current;
  21. if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
  22. {
  23. if (buffer.Length > 0)
  24. {
  25. var text = new FixedString32Bytes();
  26. text.CopyFrom(buffer);
  27. symbols.Add(new RichTextSymbol32Bytes(currentSymbolType, text));
  28. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  29. buffer.Clear();
  30. }
  31. buffer.Append(current);
  32. currentSymbolType = RichTextSymbolType.TagStart;
  33. }
  34. else if (current.value == '/' && prevRune.value == '<')
  35. {
  36. buffer.Append(current);
  37. currentSymbolType = RichTextSymbolType.TagEnd;
  38. }
  39. else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
  40. {
  41. buffer.Append(current);
  42. if (buffer.Length > 0)
  43. {
  44. var text = new FixedString32Bytes();
  45. text.CopyFrom(buffer);
  46. symbols.Add(new RichTextSymbol32Bytes(currentSymbolType, text));
  47. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  48. buffer.Clear();
  49. }
  50. currentSymbolType = RichTextSymbolType.Text;
  51. }
  52. else
  53. {
  54. buffer.Append(current);
  55. }
  56. prevRune = current;
  57. }
  58. if (buffer.Length > 0)
  59. {
  60. var text = new FixedString32Bytes();
  61. text.CopyFrom(buffer);
  62. symbols.Add(new RichTextSymbol32Bytes(currentSymbolType, text));
  63. charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  64. }
  65. buffer.Dispose();
  66. }
  67. [BurstCompile]
  68. public static void GetSymbols(ref FixedString64Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol64Bytes> symbols, out int charCount)
  69. {
  70. symbols = new UnsafeList<RichTextSymbol64Bytes>(32, allocator);
  71. charCount = 0;
  72. var buffer = new NativeText(64, Allocator.Temp);
  73. var enumerator = source.GetEnumerator();
  74. var currentSymbolType = RichTextSymbolType.Text;
  75. var prevRune = default(Unicode.Rune);
  76. while (enumerator.MoveNext())
  77. {
  78. var current = enumerator.Current;
  79. if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
  80. {
  81. if (buffer.Length > 0)
  82. {
  83. var text = new FixedString64Bytes();
  84. text.CopyFrom(buffer);
  85. symbols.Add(new RichTextSymbol64Bytes(currentSymbolType, text));
  86. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  87. buffer.Clear();
  88. }
  89. buffer.Append(current);
  90. currentSymbolType = RichTextSymbolType.TagStart;
  91. }
  92. else if (current.value == '/' && prevRune.value == '<')
  93. {
  94. buffer.Append(current);
  95. currentSymbolType = RichTextSymbolType.TagEnd;
  96. }
  97. else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
  98. {
  99. buffer.Append(current);
  100. if (buffer.Length > 0)
  101. {
  102. var text = new FixedString64Bytes();
  103. text.CopyFrom(buffer);
  104. symbols.Add(new RichTextSymbol64Bytes(currentSymbolType, text));
  105. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  106. buffer.Clear();
  107. }
  108. currentSymbolType = RichTextSymbolType.Text;
  109. }
  110. else
  111. {
  112. buffer.Append(current);
  113. }
  114. prevRune = current;
  115. }
  116. if (buffer.Length > 0)
  117. {
  118. var text = new FixedString64Bytes();
  119. text.CopyFrom(buffer);
  120. symbols.Add(new RichTextSymbol64Bytes(currentSymbolType, text));
  121. charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  122. }
  123. buffer.Dispose();
  124. }
  125. [BurstCompile]
  126. public static void GetSymbols(ref FixedString128Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol128Bytes> symbols, out int charCount)
  127. {
  128. symbols = new UnsafeList<RichTextSymbol128Bytes>(32, allocator);
  129. charCount = 0;
  130. var buffer = new NativeText(128, Allocator.Temp);
  131. var enumerator = source.GetEnumerator();
  132. var currentSymbolType = RichTextSymbolType.Text;
  133. var prevRune = default(Unicode.Rune);
  134. while (enumerator.MoveNext())
  135. {
  136. var current = enumerator.Current;
  137. if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
  138. {
  139. if (buffer.Length > 0)
  140. {
  141. var text = new FixedString128Bytes();
  142. text.CopyFrom(buffer);
  143. symbols.Add(new RichTextSymbol128Bytes(currentSymbolType, text));
  144. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  145. buffer.Clear();
  146. }
  147. buffer.Append(current);
  148. currentSymbolType = RichTextSymbolType.TagStart;
  149. }
  150. else if (current.value == '/' && prevRune.value == '<')
  151. {
  152. buffer.Append(current);
  153. currentSymbolType = RichTextSymbolType.TagEnd;
  154. }
  155. else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
  156. {
  157. buffer.Append(current);
  158. if (buffer.Length > 0)
  159. {
  160. var text = new FixedString128Bytes();
  161. text.CopyFrom(buffer);
  162. symbols.Add(new RichTextSymbol128Bytes(currentSymbolType, text));
  163. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  164. buffer.Clear();
  165. }
  166. currentSymbolType = RichTextSymbolType.Text;
  167. }
  168. else
  169. {
  170. buffer.Append(current);
  171. }
  172. prevRune = current;
  173. }
  174. if (buffer.Length > 0)
  175. {
  176. var text = new FixedString128Bytes();
  177. text.CopyFrom(buffer);
  178. symbols.Add(new RichTextSymbol128Bytes(currentSymbolType, text));
  179. charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  180. }
  181. buffer.Dispose();
  182. }
  183. [BurstCompile]
  184. public static void GetSymbols(ref FixedString512Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol512Bytes> symbols, out int charCount)
  185. {
  186. symbols = new UnsafeList<RichTextSymbol512Bytes>(32, allocator);
  187. charCount = 0;
  188. var buffer = new NativeText(512, Allocator.Temp);
  189. var enumerator = source.GetEnumerator();
  190. var currentSymbolType = RichTextSymbolType.Text;
  191. var prevRune = default(Unicode.Rune);
  192. while (enumerator.MoveNext())
  193. {
  194. var current = enumerator.Current;
  195. if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
  196. {
  197. if (buffer.Length > 0)
  198. {
  199. var text = new FixedString512Bytes();
  200. text.CopyFrom(buffer);
  201. symbols.Add(new RichTextSymbol512Bytes(currentSymbolType, text));
  202. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  203. buffer.Clear();
  204. }
  205. buffer.Append(current);
  206. currentSymbolType = RichTextSymbolType.TagStart;
  207. }
  208. else if (current.value == '/' && prevRune.value == '<')
  209. {
  210. buffer.Append(current);
  211. currentSymbolType = RichTextSymbolType.TagEnd;
  212. }
  213. else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
  214. {
  215. buffer.Append(current);
  216. if (buffer.Length > 0)
  217. {
  218. var text = new FixedString512Bytes();
  219. text.CopyFrom(buffer);
  220. symbols.Add(new RichTextSymbol512Bytes(currentSymbolType, text));
  221. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  222. buffer.Clear();
  223. }
  224. currentSymbolType = RichTextSymbolType.Text;
  225. }
  226. else
  227. {
  228. buffer.Append(current);
  229. }
  230. prevRune = current;
  231. }
  232. if (buffer.Length > 0)
  233. {
  234. var text = new FixedString512Bytes();
  235. text.CopyFrom(buffer);
  236. symbols.Add(new RichTextSymbol512Bytes(currentSymbolType, text));
  237. charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  238. }
  239. buffer.Dispose();
  240. }
  241. [BurstCompile]
  242. public static void GetSymbols(ref FixedString4096Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol4096Bytes> symbols, out int charCount)
  243. {
  244. symbols = new UnsafeList<RichTextSymbol4096Bytes>(32, allocator);
  245. charCount = 0;
  246. var buffer = new NativeText(4096, Allocator.Temp);
  247. var enumerator = source.GetEnumerator();
  248. var currentSymbolType = RichTextSymbolType.Text;
  249. var prevRune = default(Unicode.Rune);
  250. while (enumerator.MoveNext())
  251. {
  252. var current = enumerator.Current;
  253. if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
  254. {
  255. if (buffer.Length > 0)
  256. {
  257. var text = new FixedString4096Bytes();
  258. text.CopyFrom(buffer);
  259. symbols.Add(new RichTextSymbol4096Bytes(currentSymbolType, text));
  260. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  261. buffer.Clear();
  262. }
  263. buffer.Append(current);
  264. currentSymbolType = RichTextSymbolType.TagStart;
  265. }
  266. else if (current.value == '/' && prevRune.value == '<')
  267. {
  268. buffer.Append(current);
  269. currentSymbolType = RichTextSymbolType.TagEnd;
  270. }
  271. else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
  272. {
  273. buffer.Append(current);
  274. if (buffer.Length > 0)
  275. {
  276. var text = new FixedString4096Bytes();
  277. text.CopyFrom(buffer);
  278. symbols.Add(new RichTextSymbol4096Bytes(currentSymbolType, text));
  279. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  280. buffer.Clear();
  281. }
  282. currentSymbolType = RichTextSymbolType.Text;
  283. }
  284. else
  285. {
  286. buffer.Append(current);
  287. }
  288. prevRune = current;
  289. }
  290. if (buffer.Length > 0)
  291. {
  292. var text = new FixedString4096Bytes();
  293. text.CopyFrom(buffer);
  294. symbols.Add(new RichTextSymbol4096Bytes(currentSymbolType, text));
  295. charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  296. }
  297. buffer.Dispose();
  298. }
  299. }
  300. }