-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
111 lines (95 loc) · 3.18 KB
/
Program.cs
File metadata and controls
111 lines (95 loc) · 3.18 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpStrings {
class DumpStrings {
public DumpStrings (string path) {
mText = " " + File.ReadAllText (path).Replace ("\r\n", "\n") + " ";
}
/// <summary>This runs the string parsing algorithm and dumps the output to the console</summary>
public void Dump () {
mIdx = 2;
while (mIdx < mText.Length) {
PrintNextStr ();
mIdx++;
}
}
void PrintWS (int c) {
var def = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Cyan;
while (c-- > 0) Console.Write ('_');
Console.ForegroundColor = def;
}
void PrintNextStr () {
ToNextQuote (true); if (mIdx >= mText.Length) return;
var start = ++mIdx;
ToNextQuote (false); if (mIdx >= mText.Length) return;
Console.Write (++count + ", ");
var str = mText.Substring (start, mIdx - start);
var cnt = str.TakeWhile (c => c == ' ').Count ();
PrintWS (cnt);
var trim = str.Trim (); cnt += trim.Length;
Console.Write (trim);
PrintWS (str.Length - cnt);
Console.WriteLine ();
}
void ToNextQuote (bool open) {
while (mIdx < mText.Length) {
if (IsValidQuote (open)) return;
if (open) SkipComment ();
mIdx++;
}
}
bool IsValidQuote (bool open) {
if (mText[mIdx] != '"') return false;
char c = '"', d = '"', e = '\"';
if (open) return mText[mIdx - 1] != '\'' && mText[mIdx - 1] != '\\';
// Close quote "bhial"*/
int i = mIdx - 1;
bool escaped = false;/*asdfdsa
asdfsad
"asdfsda"
*/
while (i < mText.Length && mText[i] == '\\') {
escaped = !escaped;
i--;
}
return !escaped;
}
void SkipComment () {
string str = mText.Substring (mIdx - 1, 2);
if (str == MultiComment) {
GetCommentEnd (Newline);
} else if (str == SLCommentStart) {
GetCommentEnd (SLCommentEnd);
} else return;
}
void GetCommentEnd (string delim) {
while (mIdx < mText.Length && mText.Substring (mIdx++ - delim.Length, delim.Length) != delim) ;
}
const string Newline = "\n";
const string MultiComment = "//";
const string SLCommentStart = "/*";
const string SLCommentEnd = "*/";
int mIdx;
string mText;
long count = 0;
}
class Program {
static void Main (string[] args) {
if (args.Length < 0) Console.WriteLine ("Enter a valid filename");
var path = args[0];
if (string.IsNullOrEmpty (path) || !File.Exists (path)) {
Log (ErrMsg, path);
return;
}
var parser = new DumpStrings (path);
parser.Dump ();
}
static void Log (string msg, params object[] args) => Console.WriteLine (string.Format (msg, args));
static readonly string ErrMsg = "The file \"{0}\" is not valid or does not exist";
}
}