-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathClassTabCheck.cs
120 lines (111 loc) · 4.77 KB
/
ClassTabCheck.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace GitForce
{
/// <summary>
/// Contains code to check files for TABs and EOL spaces.
/// Implements the optional functionality enabled by a checkbox in the Settings->Files
/// </summary>
static class ClassTabCheck
{
/// <summary>
/// If user checked a preference to scan for TABS, and the file extension matches,
/// check the file(s) for TABs and EOL's spaces
/// </summary>
public static void CheckForTabs(List<string> files)
{
// Only check if the user setting enables the functionality
if (!Properties.Settings.Default.WarnOnTabs)
return;
// Contains the final list of files that have TABs or EOL spaces
List<string> xfiles;
// Wrap the file checks with a performance diagnostics so we can track how long it takes to parse all files
Stopwatch timer = new Stopwatch();
timer.Start();
{
// Create a Regex expression corresponding to each type of file extension to match
string values = Properties.Settings.Default.WarnOnTabsExt;
string[] extList = values.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
List<Regex> regexes = new List<Regex>();
foreach (string sFileMask in extList)
{
string expression = sFileMask.Trim().Replace(".", "[.]").Replace("*", ".*").Replace("?", ".") + "$";
regexes.Add(new Regex(expression));
}
xfiles = CheckTabsInFiles(files, regexes);
}
timer.Stop();
App.PrintLogMessage("TabCheck: elapsed: " + timer.ElapsedMilliseconds + " ms", MessageType.Debug);
// Print all files that have TABs or EOL spaces
if (xfiles.Count > 0)
{
// Although it is a warning, internally we use a message type "Error" so the output will print in
// red color and grab the attention
App.PrintStatusMessage("WARNING: The following files contain TABs or EOL spaces:", MessageType.Error);
foreach (string xfile in xfiles)
App.PrintStatusMessage(xfile, MessageType.Error);
}
}
/// <summary>
/// Check a list of files, filtered by a list of Regex expressions, for TABs or EOL spaces
/// Returns a subset of files that contain TABs or EOL spaces
/// </summary>
private static List<string> CheckTabsInFiles(List<string> files, List<Regex> regexes)
{
List<string> xfiles = new List<string>();
// Filter which files to check by using a regular expression of each file name
foreach (string file in files)
{
foreach (Regex regex in regexes)
{
// This file is to be checked using this particular regular expression
if (regex.IsMatch(file))
{
App.PrintLogMessage("TabCheck: " + file, MessageType.Debug);
if (CheckTabsInFile(file))
xfiles.Add(file);
}
}
}
return xfiles;
}
/// <summary>
/// Check if a file contains TAB characters or EOL spaces
/// Assumes the file is a readable text file
/// </summary>
private static bool CheckTabsInFile(string file)
{
string[] lines;
try
{
// Read the target file to check and separate into individual lines
// If anything's wrong, this call will throw exceptions
lines = File.ReadAllLines(file);
}
catch (Exception ex)
{
App.PrintStatusMessage(ex.Message, MessageType.Error);
return false;
}
// This site compares several methods that could be used to quickly scan strings:
// http://cc.davelozinski.com/c-sharp/fastest-way-to-check-if-a-string-occurs-within-a-string
// Surprisingly, the fastest method seems to be the most basic indexed approach
foreach (string line in lines)
{
bool eolspace = false;
for (int i = 0; i < line.Length; i++)
{
if (line[i] == '\t')
return true;
eolspace = line[i] == ' ';
}
if (eolspace)
return true;
}
return false;
}
}
}