Merge pull request #272 from SubtitleEdit/is_integer

Remove double integer conversions
This commit is contained in:
Nikolaj Olsson 2014-09-21 08:50:27 +02:00
commit 5ddb843474
9 changed files with 48 additions and 62 deletions

View File

@ -141,25 +141,21 @@ namespace Nikse.SubtitleEdit.Controls
if (times.Length == 4)
{
int hours = 0;
if (Utilities.IsInteger(times[0]))
hours = int.Parse(times[0]);
int hours;
int.TryParse(times[0], out hours);
int minutes = 0;
if (Utilities.IsInteger(times[1]))
minutes = int.Parse(times[1]);
int minutes;
int.TryParse(times[1], out minutes);
int seconds = 0;
if (Utilities.IsInteger(times[2]))
seconds = int.Parse(times[2]);
int seconds;
int.TryParse(times[2], out seconds);
int milliSeconds = 0;
if (Utilities.IsInteger(times[3]))
milliSeconds = int.Parse(times[3].PadRight(3, '0'));
int milliSeconds;
int.TryParse(times[3].PadRight(3, '0'), out milliSeconds);
var tc = new TimeCode(hours, minutes, seconds, milliSeconds);
if (times[0].StartsWith('-') && tc.TotalMilliseconds > 0)
tc.TotalMilliseconds = tc.TotalMilliseconds * -1;
if (hours < 0 && tc.TotalMilliseconds > 0)
tc.TotalMilliseconds *= -1;
return tc;
}
}
@ -172,21 +168,20 @@ namespace Nikse.SubtitleEdit.Controls
if (times.Length == 4)
{
int hours = 0;
if (Utilities.IsInteger(times[0]))
hours = int.Parse(times[0]);
int hours;
int.TryParse(times[0], out hours);
int minutes = 0;
if (Utilities.IsInteger(times[1]))
minutes = int.Parse(times[1]);
int minutes;
int.TryParse(times[1], out minutes);
int seconds = 0;
if (Utilities.IsInteger(times[2]))
seconds = int.Parse(times[2]);
int seconds;
int.TryParse(times[2], out seconds);
int milliSeconds = 0;
if (Utilities.IsInteger(times[3]))
milliSeconds = Logic.SubtitleFormats.SubtitleFormat.FramesToMillisecondsMax999(int.Parse(times[3]));
int milliSeconds;
if (int.TryParse(times[3], out milliSeconds))
{
milliSeconds = Logic.SubtitleFormats.SubtitleFormat.FramesToMillisecondsMax999(milliSeconds);
}
return new TimeCode(hours, minutes, seconds, milliSeconds);
}

View File

@ -1,4 +1,5 @@
using Nikse.SubtitleEdit.Logic;
using System.Text.RegularExpressions;
using Nikse.SubtitleEdit.Logic;
using Nikse.SubtitleEdit.Logic.SubtitleFormats;
using Nikse.SubtitleEdit.Logic.VobSub;
using System;
@ -747,8 +748,7 @@ $DROP=[DROPVALUE]" + Environment.NewLine + Environment.NewLine +
return;
xAndY = xAndY.ToLower();
string[] parts = xAndY.Split('x');
if (parts.Length == 2 && Utilities.IsInteger(parts[0]) && Utilities.IsInteger(parts[1]))
if (Regex.IsMatch(xAndY, @"\d+x\d+", RegexOptions.IgnoreCase))
{
for (int i = 0; i < comboBoxResolution.Items.Count; i++)
{

View File

@ -80,11 +80,11 @@ namespace Nikse.SubtitleEdit.Forms
var arr = name.Replace("-to-", "_").Split('_');
if (arr.Length == 3)
{
if (Utilities.IsInteger(arr[1]) && Utilities.IsInteger(arr[2]))
int startTime, endTime;
if (int.TryParse(arr[1], out startTime) && int.TryParse(arr[2], out endTime))
{
p.StartTime.TotalMilliseconds = Convert.ToInt32(arr[1]);
p.EndTime.TotalMilliseconds = Convert.ToInt32(arr[2]);
p.StartTime.TotalMilliseconds = startTime;
p.EndTime.TotalMilliseconds = endTime;
}
}
}

View File

@ -1062,8 +1062,9 @@ namespace Nikse.SubtitleEdit.Forms
if (pac.Name.Replace(" ", string.Empty).Equals(toFormat, StringComparison.OrdinalIgnoreCase) || toFormat.Equals("pac", StringComparison.OrdinalIgnoreCase) || toFormat.Equals(".pac", StringComparison.OrdinalIgnoreCase))
{
pac.BatchMode = true;
if (!string.IsNullOrEmpty(pacCodePage) && Utilities.IsInteger(pacCodePage))
pac.CodePage = Convert.ToInt32(pacCodePage);
int codePage;
if (!string.IsNullOrEmpty(pacCodePage) && int.TryParse(pacCodePage, out codePage))
pac.CodePage = codePage;
targetFormatFound = true;
outputFileName = FormatOutputFileNameForBatchConvert(fileName, pac.Extension, outputFolder, overwrite);
Console.Write("{0}: {1} -> {2}...", count, Path.GetFileName(fileName), outputFileName);

View File

@ -7,6 +7,8 @@ namespace Nikse.SubtitleEdit.Forms
{
public sealed partial class StartNumberingFrom : Form
{
private int _startFromNumber;
public StartNumberingFrom()
{
InitializeComponent();
@ -82,8 +84,7 @@ namespace Nikse.SubtitleEdit.Forms
private void ButtonOkClick(object sender, EventArgs e)
{
string s = textBox1.Text;
if (Utilities.IsInteger(s))
if (int.TryParse(textBox1.Text, out _startFromNumber))
{
DialogResult = DialogResult.OK;
}
@ -99,12 +100,8 @@ namespace Nikse.SubtitleEdit.Forms
{
get
{
int number;
if (int.TryParse(textBox1.Text, out number))
return number;
return 1;
return _startFromNumber;
}
}
}
}

View File

@ -95,18 +95,18 @@ namespace Nikse.SubtitleEdit.Forms
Font font;
try
{
double fontSize = 20;
if (Utilities.IsInteger(textBoxFontSize.Text.Replace("px", string.Empty)))
var fontSize = 20.0f;
int fontSizeInt;
if (int.TryParse(textBoxFontSize.Text.Replace("px", string.Empty), out fontSizeInt))
{
fontSize = Convert.ToInt32(textBoxFontSize.Text.Replace("px", string.Empty));
fontSize = fontSizeInt;
}
else if (textBoxFontSize.Text.EndsWith('%'))
{
int num;
if (int.TryParse(textBoxFontSize.Text.TrimEnd('%'), out num))
fontSize = fontSize * num / 100.0;
if (int.TryParse(textBoxFontSize.Text.TrimEnd('%'), out fontSizeInt))
fontSize *= fontSizeInt / 100.0f;
}
font = new Font(comboBoxFontName.Text, (float)fontSize);
font = new Font(comboBoxFontName.Text, fontSize);
}
catch
{

View File

@ -121,9 +121,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
switch (_expecting)
{
case ExpectingLine.Number:
if (Utilities.IsInteger(line))
int number;
if (int.TryParse(line, out number))
{
_paragraph.Number = int.Parse(line);
_paragraph.Number = number;
_expecting = ExpectingLine.TimeCodes;
}
else if (!string.IsNullOrWhiteSpace(line))

View File

@ -308,10 +308,10 @@ namespace Nikse.SubtitleEdit.Logic
matches = regexTimeCodes2.Matches(line);
if (matches.Count == 2)
{
string[] start = matches[0].ToString().Split(new[] { '.', ',', ';', ':' }, StringSplitOptions.RemoveEmptyEntries);
if (Utilities.IsInteger(start[0]))
var start = matches[0].Value.Split(new[] { '.', ',', ';', ':' }, StringSplitOptions.RemoveEmptyEntries);
int i;
if (int.TryParse(start[0], out i))
{
int i = int.Parse(start[0]);
if (count == -1 && i < 2)
count = i;
if (count != i)

View File

@ -200,14 +200,6 @@ namespace Nikse.SubtitleEdit.Logic
return false;
}
public static bool IsLong(string s)
{
long i;
if (long.TryParse(s, out i))
return true;
return false;
}
public static SubtitleFormat GetSubtitleFormatByFriendlyName(string friendlyName)
{
foreach (SubtitleFormat format in SubtitleFormat.AllSubtitleFormats)