Skip to content

Commit 0bb1a13

Browse files
committed
Fix bug that could not handle a text which ends with close tag
1 parent f877505 commit 0bb1a13

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Assets/RichTextHelper/RichTextHelper.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ public static int RichTextLength(this string text)
4040
var length = 0;
4141
while (m.IsConsumable())
4242
{
43-
m.Consume();
44-
length += 1;
43+
if (m.Consume())
44+
{
45+
length += 1;
46+
}
4547
}
4648
return length;
4749
}
@@ -97,7 +99,9 @@ public bool IsConsumable()
9799
return consumedLength < originalText.Length;
98100
}
99101

100-
public void Consume()
102+
// Return if real character is consumed
103+
// If line's last part is closing tag, this function return false when consumed last closing tag.
104+
public bool Consume()
101105
{
102106
Debug.Assert(IsConsumable());
103107
var peekedOriginChar = PeekNextOriginChar();
@@ -111,11 +115,20 @@ public void Consume()
111115
{
112116
ConsumeStartTag();
113117
}
114-
Consume();
118+
if (IsConsumable())
119+
{
120+
Consume();
121+
return true;
122+
}
123+
else
124+
{
125+
return false;
126+
}
115127
}
116128
else
117129
{
118130
ConsumeRawChar();
131+
return true;
119132
}
120133
}
121134

Assets/test/RichTextHelperTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ public void NestedTest()
5252
Assert.AreEqual(sampleTagText.RichTextSubString(3), "<color=#000>a<i>b<b>c</b></i></color>");
5353
Assert.AreEqual(sampleTagText.RichTextSubString(4), "<color=#000>a<i>b<b>c</b>d</i></color>");
5454
}
55+
56+
[Test]
57+
public void CloseTagIsLast()
58+
{
59+
var sampleTagText = "<color=#000>a<i>b<b>c</b>d</i></color>";
60+
Assert.AreEqual(sampleTagText.RichTextSubString(5), sampleTagText);
61+
Assert.AreEqual(sampleTagText.RichTextSubString(1), "<color=#000>a</color>");
62+
Assert.AreEqual(sampleTagText.RichTextSubString(2), "<color=#000>a<i>b</i></color>");
63+
Assert.AreEqual(sampleTagText.RichTextSubString(3), "<color=#000>a<i>b<b>c</b></i></color>");
64+
Assert.AreEqual(sampleTagText.RichTextSubString(4), "<color=#000>a<i>b<b>c</b>d</i></color>");
65+
}
5566
}

0 commit comments

Comments
 (0)