123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- using Unity.Burst;
- using Unity.Collections;
- using Unity.Collections.LowLevel.Unsafe;
- namespace LitMotion
- {
- [BurstCompile]
- internal static class RichTextParser
- {
- [BurstCompile]
- public static void GetSymbols(ref FixedString32Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol32Bytes> symbols, out int charCount)
- {
- symbols = new UnsafeList<RichTextSymbol32Bytes>(32, allocator);
- charCount = 0;
- var buffer = new NativeText(32, Allocator.Temp);
- var enumerator = source.GetEnumerator();
- var currentSymbolType = RichTextSymbolType.Text;
- var prevRune = default(Unicode.Rune);
- while (enumerator.MoveNext())
- {
- var current = enumerator.Current;
- if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
- {
- if (buffer.Length > 0)
- {
- var text = new FixedString32Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol32Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagStart;
- }
- else if (current.value == '/' && prevRune.value == '<')
- {
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagEnd;
- }
- else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
- {
- buffer.Append(current);
- if (buffer.Length > 0)
- {
- var text = new FixedString32Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol32Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- currentSymbolType = RichTextSymbolType.Text;
- }
- else
- {
- buffer.Append(current);
- }
- prevRune = current;
- }
- if (buffer.Length > 0)
- {
- var text = new FixedString32Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol32Bytes(currentSymbolType, text));
- charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- }
- buffer.Dispose();
- }
- [BurstCompile]
- public static void GetSymbols(ref FixedString64Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol64Bytes> symbols, out int charCount)
- {
- symbols = new UnsafeList<RichTextSymbol64Bytes>(32, allocator);
- charCount = 0;
- var buffer = new NativeText(64, Allocator.Temp);
- var enumerator = source.GetEnumerator();
- var currentSymbolType = RichTextSymbolType.Text;
- var prevRune = default(Unicode.Rune);
- while (enumerator.MoveNext())
- {
- var current = enumerator.Current;
- if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
- {
- if (buffer.Length > 0)
- {
- var text = new FixedString64Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol64Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagStart;
- }
- else if (current.value == '/' && prevRune.value == '<')
- {
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagEnd;
- }
- else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
- {
- buffer.Append(current);
- if (buffer.Length > 0)
- {
- var text = new FixedString64Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol64Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- currentSymbolType = RichTextSymbolType.Text;
- }
- else
- {
- buffer.Append(current);
- }
- prevRune = current;
- }
- if (buffer.Length > 0)
- {
- var text = new FixedString64Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol64Bytes(currentSymbolType, text));
- charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- }
- buffer.Dispose();
- }
- [BurstCompile]
- public static void GetSymbols(ref FixedString128Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol128Bytes> symbols, out int charCount)
- {
- symbols = new UnsafeList<RichTextSymbol128Bytes>(32, allocator);
- charCount = 0;
- var buffer = new NativeText(128, Allocator.Temp);
- var enumerator = source.GetEnumerator();
- var currentSymbolType = RichTextSymbolType.Text;
- var prevRune = default(Unicode.Rune);
- while (enumerator.MoveNext())
- {
- var current = enumerator.Current;
- if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
- {
- if (buffer.Length > 0)
- {
- var text = new FixedString128Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol128Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagStart;
- }
- else if (current.value == '/' && prevRune.value == '<')
- {
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagEnd;
- }
- else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
- {
- buffer.Append(current);
- if (buffer.Length > 0)
- {
- var text = new FixedString128Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol128Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- currentSymbolType = RichTextSymbolType.Text;
- }
- else
- {
- buffer.Append(current);
- }
- prevRune = current;
- }
- if (buffer.Length > 0)
- {
- var text = new FixedString128Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol128Bytes(currentSymbolType, text));
- charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- }
- buffer.Dispose();
- }
- [BurstCompile]
- public static void GetSymbols(ref FixedString512Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol512Bytes> symbols, out int charCount)
- {
- symbols = new UnsafeList<RichTextSymbol512Bytes>(32, allocator);
- charCount = 0;
- var buffer = new NativeText(512, Allocator.Temp);
- var enumerator = source.GetEnumerator();
- var currentSymbolType = RichTextSymbolType.Text;
- var prevRune = default(Unicode.Rune);
- while (enumerator.MoveNext())
- {
- var current = enumerator.Current;
- if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
- {
- if (buffer.Length > 0)
- {
- var text = new FixedString512Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol512Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagStart;
- }
- else if (current.value == '/' && prevRune.value == '<')
- {
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagEnd;
- }
- else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
- {
- buffer.Append(current);
- if (buffer.Length > 0)
- {
- var text = new FixedString512Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol512Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- currentSymbolType = RichTextSymbolType.Text;
- }
- else
- {
- buffer.Append(current);
- }
- prevRune = current;
- }
- if (buffer.Length > 0)
- {
- var text = new FixedString512Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol512Bytes(currentSymbolType, text));
- charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- }
- buffer.Dispose();
- }
- [BurstCompile]
- public static void GetSymbols(ref FixedString4096Bytes source, Allocator allocator, out UnsafeList<RichTextSymbol4096Bytes> symbols, out int charCount)
- {
- symbols = new UnsafeList<RichTextSymbol4096Bytes>(32, allocator);
- charCount = 0;
- var buffer = new NativeText(4096, Allocator.Temp);
- var enumerator = source.GetEnumerator();
- var currentSymbolType = RichTextSymbolType.Text;
- var prevRune = default(Unicode.Rune);
- while (enumerator.MoveNext())
- {
- var current = enumerator.Current;
- if (current.value == '<' && currentSymbolType is not (RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd))
- {
- if (buffer.Length > 0)
- {
- var text = new FixedString4096Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol4096Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagStart;
- }
- else if (current.value == '/' && prevRune.value == '<')
- {
- buffer.Append(current);
- currentSymbolType = RichTextSymbolType.TagEnd;
- }
- else if (current.value == '>' && currentSymbolType is RichTextSymbolType.TagStart or RichTextSymbolType.TagEnd)
- {
- buffer.Append(current);
- if (buffer.Length > 0)
- {
- var text = new FixedString4096Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol4096Bytes(currentSymbolType, text));
- if (currentSymbolType == RichTextSymbolType.Text) charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- buffer.Clear();
- }
- currentSymbolType = RichTextSymbolType.Text;
- }
- else
- {
- buffer.Append(current);
- }
- prevRune = current;
- }
- if (buffer.Length > 0)
- {
- var text = new FixedString4096Bytes();
- text.CopyFrom(buffer);
- symbols.Add(new RichTextSymbol4096Bytes(currentSymbolType, text));
- charCount += FixedStringHelper.GetUtf8CharCount(ref text);
- }
- buffer.Dispose();
- }
- }
- }
|