[StringExtension] - Optimize FixExtraWhiteSpaces.

This commit is contained in:
Ivandro Ismael 2016-11-10 17:43:08 +00:00
parent a091491c35
commit 742df3b649

View File

@ -144,11 +144,41 @@ namespace Nikse.SubtitleEdit.Core
{ {
if (string.IsNullOrEmpty(s)) if (string.IsNullOrEmpty(s))
return s; return s;
int len = s.Length;
int k = -1;
for (int i = len - 1; i >= 0; i--)
{
char ch = s[i];
if (k < 2)
{
if (ch == 0x20)
{
k = i + 1;
}
}
else if (ch != 0x20)
{
// Two or more white-spaces found!
if (k - (i + 1) > 1)
{
// Keep only one white-space.
s = s.Remove(i + 1, k - (i + 2));
}
while (s.Contains(" ")) // No white-space after/before line break.
s = s.Replace(" ", " "); if ((ch == '\n' || ch == '\r') && i + 1 < s.Length && s[i + 1] == 0x20)
s = s.Replace(" " + Environment.NewLine, Environment.NewLine); {
return s.Replace(Environment.NewLine + " ", Environment.NewLine); s = s.Remove(i + 1, 1);
}
// Reset remove length.
k = -1;
}
if (ch == 0x20 && i + 1 < s.Length && (s[i + 1] == '\n' || s[i + 1] == '\r'))
{
s = s.Remove(i, 1);
}
}
return s;
} }
public static bool ContainsLetter(this string s) public static bool ContainsLetter(this string s)