[MacSub] - Fix typos + more optimization.

This commit is contained in:
Ivandro Ismael 2016-10-25 19:00:53 +01:00
parent 1f6cb847e6
commit 8a62a082b9

View File

@ -68,8 +68,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
switch (expecting)
{
case Expecting.StartFrame:
// 10 = length of int.MaxValues (2147483647); +1 = if contain '/'
if (line.Length <= 10 + 1 && (CharUtils.IsDigit(line[0]) || line[0] == '/'))
if (ContainsOnlyNumber(line))
{
p.StartFrame = int.Parse(line.TrimStart(trimChar));
expecting = Expecting.Text;
@ -92,11 +91,18 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
break;
case Expecting.EndFrame:
if (ContainsOnlyNumber(line))
{
p.EndFrame = int.Parse(line.TrimStart(trimChar));
subtitle.Paragraphs.Add(p);
// Prepare for next reading.
p = new Paragraph();
expecting = Expecting.StartFrame;
}
else
{
_errorCount++;
}
break;
}
}
@ -122,5 +128,20 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
return sb.ToString();
}
public static bool ContainsOnlyNumber(string inp)
{
int len = inp.Length;
// 10 = length of int.MaxValue (2147483647); +1 if starts with '/'
if (len == 0 || len > 11 || inp[0] != '/')
return false;
int halfLen = len / 2;
for (int i = 1; i <= halfLen; i++) // /10.0 (Do not parse double)
{
if (!(CharUtils.IsDigit(inp[i]) && CharUtils.IsDigit(inp[len - i])))
return false;
}
return true;
}
}
}