mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 03:02:35 +01:00
Optimize TypeWriterEffect logic and refactor code
Mainly focused on streamlining the logic in the TypeWriterEffect class. Implemented StringBuilder for improved performance and readability, removing unneeded code and variables in the process. Additionally, transferred the GetTextWithClosedFontTag method from TypeWriterEffect to KaraokeCharTransform, improving encapsulation and code coherence.
This commit is contained in:
parent
b070bf52b1
commit
e05a705b92
@ -33,5 +33,8 @@ namespace Nikse.SubtitleEdit.Core.Common.TextEffect
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
private readonly string _fontClose = "</font>";
|
||||
private string GetTextWithClosedFontTag(in string text, int index) => text.Substring(0, index + 1) + _fontClose + text.Substring(index + 1);
|
||||
}
|
||||
}
|
@ -5,24 +5,24 @@ namespace Nikse.SubtitleEdit.Core.Common.TextEffect
|
||||
{
|
||||
public class TypeWriterEffect : TextEffectBase
|
||||
{
|
||||
private readonly StringBuilder _sb = new StringBuilder();
|
||||
private readonly StringBuilder _sbClosing = new StringBuilder();
|
||||
|
||||
public override string[] Transform(string text)
|
||||
{
|
||||
// todo: fix this!
|
||||
var closingTags = new List<string>();
|
||||
var closingTags = new List<string>(text.Length / 3 + 1);
|
||||
var list = new List<string>();
|
||||
var sb = new StringBuilder();
|
||||
var sbClosing = new StringBuilder();
|
||||
|
||||
|
||||
for (var i = 0; i < text.Length; i++)
|
||||
{
|
||||
var ch = text[i];
|
||||
if (ch == '<' || ch == '{')
|
||||
if (HtmlUtil.IsStartTagSymbol(ch))
|
||||
{
|
||||
var closingIdx = text.IndexOf(HtmlUtil.GetClosingPair(ch), i + 1);
|
||||
// invalid tag (no closing)
|
||||
if (closingIdx < 0)
|
||||
{
|
||||
sb.Append(ch); // < or {
|
||||
_sb.Append(ch); // < or {
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -40,33 +40,30 @@ namespace Nikse.SubtitleEdit.Core.Common.TextEffect
|
||||
}
|
||||
}
|
||||
|
||||
sb.Append(tag);
|
||||
_sb.Append(tag);
|
||||
i = closingIdx;
|
||||
}
|
||||
}
|
||||
else // anything else
|
||||
{
|
||||
sb.Append(ch);
|
||||
_sb.Append(ch);
|
||||
|
||||
// always have char like white space to be followed by a visible character
|
||||
if (IsVisibleChar(ch))
|
||||
{
|
||||
sbClosing.Clear();
|
||||
_sbClosing.Clear();
|
||||
// write closing for all open tag
|
||||
for (var j = closingTags.Count - 1; j >= 0; j--)
|
||||
{
|
||||
sbClosing.Append(closingTags[j]);
|
||||
_sbClosing.Append(closingTags[j]);
|
||||
}
|
||||
|
||||
list.Add(sb + sbClosing.ToString());
|
||||
list.Add(_sb + _sbClosing.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
private readonly string _fontClose = "</font>";
|
||||
protected string GetTextWithClosedFontTag(in string text, int index) => text.Substring(0, index + 1) + _fontClose + text.Substring(index + 1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user