Browse Source

Optimised string allocations when writing text with Writer.cs

master
Christopher 9 years ago
parent
commit
87f995dd3e
  1. 106
      Assets/Fungus/UI/Scripts/Writer.cs

106
Assets/Fungus/UI/Scripts/Writer.cs

@ -4,6 +4,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System; using System;
using System.Reflection; using System.Reflection;
using System.Text;
namespace Fungus namespace Fungus
{ {
@ -89,6 +90,15 @@ namespace Fungus
protected List<IWriterListener> writerListeners = new List<IWriterListener>(); protected List<IWriterListener> writerListeners = new List<IWriterListener>();
protected StringBuilder openString = new StringBuilder(256);
protected StringBuilder closeString = new StringBuilder(256);
protected StringBuilder leftString = new StringBuilder(1024);
protected StringBuilder rightString = new StringBuilder(1024);
protected StringBuilder outputString = new StringBuilder(1024);
protected string hiddenColorOpen = "";
protected string hiddenColorClose = "";
public string text public string text
{ {
get get
@ -173,6 +183,11 @@ namespace Fungus
protected virtual void Start() protected virtual void Start()
{ {
// Cache the hidden color string
Color32 c = hiddenTextColor;
hiddenColorOpen = String.Format("<color=#{0:X2}{1:X2}{2:X2}{3:X2}>", c.r, c.g, c.b, c.a);
hiddenColorClose = "</color>";
if (forceRichText) if (forceRichText)
{ {
if (textUI != null) if (textUI != null)
@ -211,58 +226,58 @@ namespace Fungus
return false; return false;
} }
protected virtual string OpenMarkup() protected virtual void UpdateOpenMarkup()
{ {
string tagText = ""; openString.Length = 0;
if (SupportsRichText()) if (SupportsRichText())
{ {
if (sizeActive) if (sizeActive)
{ {
tagText += "<size=" + sizeValue + ">"; openString.Append("<size=");
openString.Append(sizeValue);
openString.Append(">");
} }
if (colorActive) if (colorActive)
{ {
tagText += "<color=" + colorText + ">"; openString.Append("<color=");
openString.Append(colorText);
openString.Append(">");
} }
if (boldActive) if (boldActive)
{ {
tagText += "<b>"; openString.Append("<b>");
} }
if (italicActive) if (italicActive)
{ {
tagText += "<i>"; openString.Append("<i>");
} }
} }
return tagText;
} }
protected virtual string CloseMarkup() protected virtual void UpdateCloseMarkup()
{ {
string closeText = ""; closeString.Length = 0;
if (SupportsRichText()) if (SupportsRichText())
{ {
if (italicActive) if (italicActive)
{ {
closeText += "</i>"; closeString.Append("</i>");
} }
if (boldActive) if (boldActive)
{ {
closeText += "</b>"; closeString.Append("</b>");
} }
if (colorActive) if (colorActive)
{ {
closeText += "</color>"; closeString.Append("</color>");
} }
if (sizeActive) if (sizeActive)
{ {
closeText += "</size>"; closeString.Append("</size>");
} }
} }
return closeText;
} }
public virtual void SetTextColor(Color textColor) public virtual void SetTextColor(Color textColor)
@ -474,6 +489,7 @@ namespace Fungus
exitFlag = true; exitFlag = true;
break; break;
case TextTagParser.TokenType.Message: case TextTagParser.TokenType.Message:
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
{ {
@ -613,12 +629,12 @@ namespace Fungus
} }
string startText = text; string startText = text;
string openText = OpenMarkup(); UpdateOpenMarkup();
string closeText = CloseMarkup(); UpdateCloseMarkup();
float timeAccumulator = Time.deltaTime; float timeAccumulator = Time.deltaTime;
for (int i = 0; i < param.Length; ++i) for (int i = 0; i <= param.Length; ++i)
{ {
// Exit immediately if the exit flag has been set // Exit immediately if the exit flag has been set
if (exitFlag) if (exitFlag)
@ -626,11 +642,9 @@ namespace Fungus
break; break;
} }
string left = ""; PartitionString(writeWholeWords, param, i);
string right = ""; ConcatenateString(startText);
text = outputString.ToString();
PartitionString(writeWholeWords, param, i, out left, out right);
text = ConcatenateString(startText, openText, closeText, left, right);
NotifyGlyph(); NotifyGlyph();
@ -641,9 +655,9 @@ namespace Fungus
} }
// Punctuation pause // Punctuation pause
if (left.Length > 0 && if (leftString.Length > 0 &&
right.Length > 0 && rightString.Length > 0 &&
IsPunctuation(left.Substring(left.Length - 1)[0])) IsPunctuation(leftString.ToString(leftString.Length - 1, 1)[0]))
{ {
yield return StartCoroutine(DoWait(currentPunctuationPause)); yield return StartCoroutine(DoWait(currentPunctuationPause));
} }
@ -663,10 +677,13 @@ namespace Fungus
} }
} }
protected void PartitionString(bool wholeWords, string inputString, int i, out string left, out string right) protected void PartitionString(bool wholeWords, string inputString, int i)
{ {
left = ""; leftString.Length = 0;
right = ""; leftString.Append(inputString);
rightString.Length = 0;
rightString.Append(inputString);
if (wholeWords) if (wholeWords)
{ {
@ -676,34 +693,37 @@ namespace Fungus
if (Char.IsWhiteSpace(inputString[j]) || if (Char.IsWhiteSpace(inputString[j]) ||
j == inputString.Length - 1) j == inputString.Length - 1)
{ {
left = inputString.Substring(0, j + 1); leftString.Remove(j, inputString.Length - j);
right = inputString.Substring(j + 1, inputString.Length - j - 1); rightString.Remove(0, j);
break; break;
} }
} }
} }
else else
{ {
left = inputString.Substring(0, i + 1); leftString.Remove(i, inputString.Length - i);
right = inputString.Substring(i + 1); rightString.Remove(0, i);
} }
} }
protected string ConcatenateString(string startText, string openText, string closeText, string leftText, string rightText) protected void ConcatenateString(string startText)
{ {
string tempText = startText + openText + leftText + closeText; outputString.Length = 0;
Color32 c = hiddenTextColor; // string tempText = startText + openText + leftText + closeText;
string hiddenColor = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", c.r, c.g, c.b, c.a); outputString.Append(startText);
outputString.Append(openString);
outputString.Append(leftString);
outputString.Append(closeString);
// Make right hand side text hidden // Make right hand side text hidden
if (SupportsRichText() && if (SupportsRichText() &&
rightText.Length > 0) rightString.Length > 0)
{ {
tempText += "<color=" + hiddenColor + ">" + rightText + "</color>"; outputString.Append(hiddenColorOpen);
outputString.Append(rightString);
outputString.Append(hiddenColorClose);
} }
return tempText;
} }
public virtual string GetTagHelp() public virtual string GetTagHelp()
@ -791,7 +811,7 @@ namespace Fungus
if (go != null) if (go != null)
{ {
iTween.ShakePosition(go, axis, time); iTween.ShakePosition(go, axis, time);
} }
} }

Loading…
Cancel
Save