Merge pull request #2075 from ivandrofly/string_extensions

[StringExtension] - Optimize FixExtraWhiteSpaces.
This commit is contained in:
Nikolaj Olsson 2016-11-16 17:23:11 +01:00 committed by GitHub
commit 42b176d77c

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)