-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCString.cs
More file actions
111 lines (96 loc) · 2.12 KB
/
CString.cs
File metadata and controls
111 lines (96 loc) · 2.12 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.Text;
namespace StringUtils;
public class CString
{
private readonly StringBuilder _sb = new();
private CString(ReadOnlySpan<Char> value)
{
var lineIndex = -1;
var minDepth = 100000;
foreach (var line in value.EnumerateLines())
{
lineIndex++;
if (lineIndex == 0)
{
if (!line.IsWhiteSpace())
{
_sb.Append(line);
}
}
else // find the minimum indent of lines 2+
{
var depth = GetIndentDepth(line);
if (minDepth > depth)
minDepth = depth;
}
}
// If there is only 1 line, we are finished
if (lineIndex == 0)
{
return;
}
// Remove leading spaces/tabs of 2nd and subsequent lines
lineIndex = -1;
foreach (var line in value.EnumerateLines())
{
lineIndex++;
if (lineIndex == 0) //ignore first line
continue;
if (_sb.Length > 0)
_sb.AppendLine();
_sb.Append(line[minDepth..]);
}
/*
// An String-based version of the parsing. Not 100% correct ATM. For example, it adds a newline at end
var lines = value.Split(new [] { "\r\n" }, StringSplitOptions.None);
if (lines.Length == 1)
{
_value = value;
return;
}
// look for the indent size from the 2nd line on
var minIndent = 1000;
for (int i = 1; i < lines.Length; i++)
{
var lineIndent = GetIndentDepth(lines[i]);
if (lineIndent < minIndent)
minIndent = lineIndent;
}
//var sb = new StringBuilder();
// First line
if (!String.IsNullOrWhiteSpace(lines[0]))
{
sb.AppendLine(lines[0]);
}
// Second and subsequent lines
for (int i = 1; i < lines.Length; i++)
{
if (String.IsNullOrWhiteSpace(lines[i]))
{
sb.AppendLine();
}
else
{
//var indent = FindIndent(lines[i]);
sb.AppendLine(lines[i].Substring(minIndent));
}
}
_value = sb.ToString();
*/
}
private int GetIndentDepth(ReadOnlySpan<char> line)
{
for (var i = 0; i < line.Length; i++)
{
if (line[i] == '\t' || line[i] == ' ')
continue;
return i;
}
return 0;
}
public static implicit operator CString(string x) => new CString(x);
public override string ToString()
{
return _sb.ToString();
}
}