RichTextParser.tt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <#@ template language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. <#
  8. var sizes = new int[]
  9. {
  10. 32, 64, 128, 512, 4096
  11. };
  12. #>
  13. using Unity.Burst;
  14. using Unity.Collections;
  15. using Unity.Collections.LowLevel.Unsafe;
  16. namespace LitMotion
  17. {
  18. [BurstCompile]
  19. internal static class RichTextParser
  20. {
  21. <# foreach(var size in sizes) { #>
  22. [BurstCompile]
  23. public static void GetSymbols(ref FixedString<#=size#>Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol<#=size#>Bytes> symbols, out int charCount)
  24. {
  25. symbols = new UnsafeList<RichTextSymbol<#=size#>Bytes>(32, allocator);
  26. charCount = 0;
  27. var buffer = new NativeText(<#=size#>, Allocator.Temp);
  28. var enumerator = source.GetEnumerator();
  29. var currentSymbolType = RichTextSymbolType.Text;
  30. var prevRune = default(Unicode.Rune);
  31. while (enumerator.MoveNext())
  32. {
  33. var current = enumerator.Current;
  34. if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
  35. {
  36. if (buffer.Length > 0)
  37. {
  38. var text = new FixedString<#=size#>Bytes();
  39. text.CopyFrom(buffer);
  40. symbols.Add(new RichTextSymbol<#=size#>Bytes(currentSymbolType, text));
  41. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  42. buffer.Clear();
  43. }
  44. buffer.Append(current);
  45. currentSymbolType = RichTextSymbolType.TagStart;
  46. }
  47. else if (current.value == '/' && prevRune.value == '<')
  48. {
  49. buffer.Append(current);
  50. currentSymbolType = RichTextSymbolType.TagEnd;
  51. }
  52. else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
  53. {
  54. buffer.Append(current);
  55. if (buffer.Length > 0)
  56. {
  57. var text = new FixedString<#=size#>Bytes();
  58. text.CopyFrom(buffer);
  59. symbols.Add(new RichTextSymbol<#=size#>Bytes(currentSymbolType, text));
  60. if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  61. buffer.Clear();
  62. }
  63. currentSymbolType = RichTextSymbolType.Text;
  64. }
  65. else
  66. {
  67. buffer.Append(current);
  68. }
  69. prevRune = current;
  70. }
  71. if (buffer.Length > 0)
  72. {
  73. var text = new FixedString<#=size#>Bytes();
  74. text.CopyFrom(buffer);
  75. symbols.Add(new RichTextSymbol<#=size#>Bytes(currentSymbolType, text));
  76. charCount += FixedStringHelper.GetUtf8CharCount(ref text);
  77. }
  78. buffer.Dispose();
  79. }
  80. <# } #>
  81. }
  82. }