Skip to content

Commit f68b7ad

Browse files
committed
Add missing null checks and braces for consistency
This commit includes additional null checks and properly adds braces for single-line conditionals across multiple files to improve readability and prevent potential errors. These changes enhance code maintainability by ensuring consistency in style and behavior, reducing the likelihood of future issues.
1 parent a3f9e2e commit f68b7ad

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

source/AmericanToBritish/DLL/AmericanToBritishConverter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public bool ExtendsBuiltInWordList
2323
public string Convert(string text)
2424
{
2525
if (string.IsNullOrWhiteSpace(text))
26+
{
2627
return text;
28+
}
29+
2730
for (int index = 0; index < _regexList.Count; index++)
2831
{
2932
var regex = _regexList[index];
@@ -119,7 +122,10 @@ private string RevertFontColorFix(string s)
119122
{
120123
var tagEndIndex = s.IndexOf('>', tagIndex + 5);
121124
if (tagEndIndex < 0)
125+
{
122126
break;
127+
}
128+
123129
var tag = s.Substring(tagIndex, tagEndIndex - tagIndex);
124130
var colourIndex = tag.IndexOf("colour", StringComparison.OrdinalIgnoreCase);
125131
while (colourIndex >= 0)

source/AmericanToBritish/DLL/Logic/SubRip.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ public void LoadSubtitle(Subtitle subtitle, IList<string> lines, string fileName
6262
var line = _currentLine.Trim();
6363

6464
if (!TryParseLine(subtitle, line))
65+
{
6566
_errorCount++;
67+
}
6668
}
6769
if (_expecting == LineType.Text)
70+
{
6871
subtitle.Paragraphs.Add(_paragraph);
72+
}
6973
else if (_expecting != LineType.Number)
74+
{
7075
_errorCount++;
76+
}
7177

7278
subtitle.FileName = fileName;
7379
}
@@ -182,7 +188,10 @@ private bool TryParseLine(Subtitle subtitle, string line)
182188
private void AddCurrentLineToParagraphText()
183189
{
184190
if (_paragraph.Text.Length > 0)
191+
{
185192
_paragraph.Text += Environment.NewLine;
193+
}
194+
186195
_paragraph.Text += _currentLine;
187196
}
188197

source/AmericanToBritish/DLL/Logic/Utilities.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public static bool IsInteger(string s)
6666
public static string RemoveHtmlTags(string s, bool alsoSsa)
6767
{
6868
if (string.IsNullOrEmpty(s))
69+
{
6970
return null;
71+
}
7072

7173
int idx;
7274
if (alsoSsa)
@@ -77,13 +79,19 @@ public static string RemoveHtmlTags(string s, bool alsoSsa)
7779
{
7880
var endIdx = s.IndexOf('}', idx + 2);
7981
if (endIdx < idx)
82+
{
8083
break;
84+
}
85+
8186
s = s.Remove(idx, endIdx - idx + 1);
8287
idx = s.IndexOf(SSATAg, idx, StringComparison.Ordinal);
8388
}
8489
}
8590
if (!s.Contains("<"))
91+
{
8692
return s;
93+
}
94+
8795
s = Regex.Replace(s, "</?[ibu]>", string.Empty, RegexOptions.IgnoreCase);
8896

8997
s = Regex.Replace(s, "</?font>", string.Empty, RegexOptions.IgnoreCase);
@@ -92,7 +100,10 @@ public static string RemoveHtmlTags(string s, bool alsoSsa)
92100
{
93101
var endIdx = s.IndexOf('>', idx + 5);
94102
if (endIdx < idx)
103+
{
95104
break;
105+
}
106+
96107
s = s.Remove(idx, endIdx - idx + 1);
97108
idx = s.IndexOf("<font", idx, StringComparison.OrdinalIgnoreCase);
98109
}
@@ -102,7 +113,10 @@ public static string RemoveHtmlTags(string s, bool alsoSsa)
102113
public static int GetNumberOfLines(string text)
103114
{
104115
if (text == null)
116+
{
105117
return 0;
118+
}
119+
106120
var lines = 1;
107121
var idx = text.IndexOf('\n');
108122
while (idx >= 0)
@@ -124,7 +138,9 @@ public static string GetWordListFileName()
124138
// the caller of the method, not the location of the loaded assembly.
125139
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); // Note: Subtitle edit loads assembly from raw bytes
126140
if (path.StartsWith("file:\\", StringComparison.Ordinal))
141+
{
127142
path = path.Remove(0, 6);
143+
}
128144

129145
path = Path.Combine(path, "Plugins"); // try to find portable SE Plugins folder
130146
if (!Directory.Exists(path))

source/AmericanToBritish/DLL/ManageWordsForm.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public WordPair(string americanWord, string britishWord, XElement documentNode)
4343
if (list.Count > 0 || !_isOk)
4444
{
4545
if (list.Count == 1)
46+
{
4647
list.First.Value.BackColor = HighlightColor;
48+
}
49+
4750
BackColor = HighlightColor;
4851
}
4952
_pairNode = list.AddLast(this);
@@ -80,9 +83,14 @@ public override void Remove()
8083
var list = _pairNode.List;
8184
list.Remove(_pairNode);
8285
if (list.Count == 0)
86+
{
8387
PairsByUs.Remove(_usWord);
88+
}
8489
else if (list.Count == 1 && list.First.Value._isOk)
90+
{
8591
list.First.Value.BackColor = _defaultColor;
92+
}
93+
8694
_node.Remove();
8795
base.Remove();
8896
}
@@ -96,7 +104,9 @@ public int Compare(object o1, object o2)
96104
{
97105
cmp = string.Compare(wp1.Text, wp2.Text, StringComparison.InvariantCultureIgnoreCase);
98106
if (cmp == 0)
107+
{
99108
cmp = wp1._sortIndex.CompareTo(wp2._sortIndex);
109+
}
100110
}
101111
return cmp;
102112
}
@@ -153,7 +163,10 @@ private void InitializeListView(XDocument xdoc)
153163
private void buttonOK_Click(object sender, EventArgs e)
154164
{
155165
if (_path != null && _xdoc != null)
166+
{
156167
_xdoc.Save(_path);
168+
}
169+
157170
DialogResult = DialogResult.OK;
158171
}
159172

@@ -216,7 +229,9 @@ private void toolStripMenuItemRemoveSelected_Click(object sender, EventArgs e)
216229
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
217230
{
218231
if (_path == null)
232+
{
219233
e.Cancel = true;
234+
}
220235
}
221236

222237
private void ManageWordsForm_FormClosed(object sender, FormClosedEventArgs e)
@@ -251,9 +266,14 @@ private void textBoxBritishAmerican_KeyDown(object sender, KeyEventArgs e)
251266
{
252267
var caret = tb.SelectionStart;
253268
if (caret > 0)
269+
{
254270
tb.Select(0, caret);
271+
}
255272
else
273+
{
256274
tb.SelectAll();
275+
}
276+
257277
tb.Copy();
258278
tb.Select(caret, 0);
259279
e.SuppressKeyPress = true;
@@ -266,9 +286,14 @@ private void textBoxBritishAmerican_KeyDown(object sender, KeyEventArgs e)
266286
{
267287
var caret = tb.SelectionStart;
268288
if (caret > 0)
289+
{
269290
tb.Select(0, caret);
291+
}
270292
else
293+
{
271294
tb.SelectAll();
295+
}
296+
272297
tb.Cut();
273298
tb.Select(0, 0);
274299
e.SuppressKeyPress = true;
@@ -282,7 +307,10 @@ private void listView1_KeyDown(object sender, KeyEventArgs e)
282307
{
283308
toolStripMenuItemRemoveSelected_Click(null, null);
284309
if (!string.IsNullOrWhiteSpace(textBoxAmerican.Text))
310+
{
285311
textBoxBritish.Select();
312+
}
313+
286314
e.SuppressKeyPress = true;
287315
}
288316
else if (e.KeyData == Keys.Right)
@@ -309,7 +337,10 @@ private void SetFocusToHighlighted(int direction)
309337
do
310338
{
311339
if ((index = (index + direction + items.Count) % items.Count) == start)
340+
{
312341
return;
342+
}
343+
313344
item = items[index] as WordPair;
314345
}
315346
while (item.IsHighlighted);
@@ -320,7 +351,9 @@ private void SetFocusToHighlighted(int direction)
320351
while (!(item = items[index] as WordPair).IsHighlighted)
321352
{
322353
if ((index = (index + direction + items.Count) % items.Count) == start)
354+
{
323355
return;
356+
}
324357
}
325358
listView1.SelectedItems.Clear();
326359
item.EnsureVisible();

source/AmericanToBritish/DLL/Plugin.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ string IPlugin.DoAction(Form parentForm, string subtitle, double frameRate, stri
4444
}
4545

4646
if (!string.IsNullOrEmpty(listViewLineSeparatorString))
47+
{
4748
Configuration.ListViewLineSeparatorString = listViewLineSeparatorString;
49+
}
4850

4951
var srt = new SubRip();
5052
var sub = new Subtitle(srt);
@@ -59,7 +61,9 @@ string IPlugin.DoAction(Form parentForm, string subtitle, double frameRate, stri
5961
using (var form = new PluginForm(sub, (this as IPlugin).Name, (this as IPlugin).Description))
6062
{
6163
if (form.ShowDialog(parentForm) == DialogResult.OK)
64+
{
6265
return form.FixedSubtitle;
66+
}
6367
}
6468
return string.Empty;
6569
}

source/AmericanToBritish/DLL/PluginForm.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ private void listViewFixes_KeyDown(object sender, KeyEventArgs e)
247247
do
248248
{
249249
if (++index >= listViewFixes.Items.Count)
250+
{
250251
index = 0;
252+
}
253+
251254
if ((listViewFixes.Items[index].Tag as Conversion).EditedText != null)
252255
{
253256
e.SuppressKeyPress = true;
@@ -270,7 +273,10 @@ private void listViewFixes_KeyDown(object sender, KeyEventArgs e)
270273
do
271274
{
272275
if (--index < 0)
276+
{
273277
index = listViewFixes.Items.Count - 1;
278+
}
279+
274280
if ((listViewFixes.Items[index].Tag as Conversion).EditedText != null)
275281
{
276282
e.SuppressKeyPress = true;
@@ -314,7 +320,10 @@ private void textBoxEditAfter_KeyDown(object sender, KeyEventArgs e)
314320
while (eol < tb.TextLength && tb.Text[eol] != '\n')
315321
eol++;
316322
if (eol < tb.TextLength)
323+
{
317324
eol++;
325+
}
326+
318327
tb.Select(sol, eol - sol);
319328
tb.Copy();
320329
tb.Select(sol, 0);
@@ -333,7 +342,10 @@ private void textBoxEditAfter_KeyDown(object sender, KeyEventArgs e)
333342
while (eol < tb.TextLength && tb.Text[eol] != '\n')
334343
eol++;
335344
if (eol < tb.TextLength)
345+
{
336346
eol++;
347+
}
348+
337349
tb.Select(sol, eol - sol);
338350
tb.Cut();
339351
if (sol >= tb.TextLength)
@@ -349,17 +361,25 @@ private void textBoxEditAfter_KeyDown(object sender, KeyEventArgs e)
349361
private void toolStripMenuItemManageLocalwords_Click(object sender, EventArgs e)
350362
{
351363
if (!File.Exists(_localFileName))
364+
{
352365
return;
366+
}
367+
353368
using (var manageWords = new ManageWordsForm())
354369
{
355370
manageWords.Initialize(_localFileName);
356371
manageWords.ShowDialog(this);
357372
}
358373

359374
if (radioButtonLocalList.Checked)
375+
{
360376
radioButtonLocalList_Click(null, null);
377+
}
378+
361379
if (radioButtonBothLists.Checked)
380+
{
362381
radioButtonBothLists_Click(null, null);
382+
}
363383
}
364384

365385
private void toolStripMenuItemViewBuiltInWords_Click(object sender, EventArgs e)
@@ -407,14 +427,20 @@ private void contextMenuStripFixes_Opening(object sender, System.ComponentModel.
407427
private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e)
408428
{
409429
if (!SubtitleLoaded())
430+
{
410431
return;
432+
}
433+
411434
DoSelection(true);
412435
}
413436

414437
private void toolStripMenuItemInvert_Click(object sender, EventArgs e)
415438
{
416439
if (!SubtitleLoaded())
440+
{
417441
return;
442+
}
443+
418444
DoSelection(false);
419445
}
420446

0 commit comments

Comments
 (0)