-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextUtils.cs
More file actions
181 lines (144 loc) · 5.09 KB
/
Copy pathTextUtils.cs
File metadata and controls
181 lines (144 loc) · 5.09 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
namespace NewProject
{
public static class TextUtils
{
public static bool IsNullOrWhiteSpace(string s)
{
if (s == null) return true;
if (s.Length == 0) return true;
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
return false;
}
return true;
}
public static bool EqualsManual(string a, string b)
{
if (a == null || b == null) return false;
if (a.Length != b.Length) return false;
for (int i = 0; i < a.Length; i++)
if (a[i] != b[i]) return false;
return true;
}
public static int FindChar(string text, char target, int startIndex)
{
if (text == null) return -1;
if (startIndex < 0) startIndex = 0;
for (int i = startIndex; i < text.Length; i++)
if (text[i] == target) return i;
return -1;
}
public static int FindString(string text, string pattern, int startIndex)
{
if (text == null || pattern == null) return -1;
if (pattern.Length == 0) return startIndex;
if (startIndex < 0) startIndex = 0;
if (pattern.Length > text.Length) return -1;
for (int i = startIndex; i <= text.Length - pattern.Length; i++)
{
bool match = true;
for (int j = 0; j < pattern.Length; j++)
{
if (text[i + j] != pattern[j])
{
match = false;
break;
}
}
if (match) return i;
}
return -1;
}
public static bool StartsWithManual(string text, string prefix)
{
if (text == null || prefix == null) return false;
if (prefix.Length > text.Length) return false;
for (int i = 0; i < prefix.Length; i++)
if (text[i] != prefix[i]) return false;
return true;
}
public static bool ContainsManual(string text, string value)
{
return FindString(text, value, 0) != -1;
}
public static string SubstringManual(string text, int start, int length)
{
if (text == null) return "";
if (start < 0) return "";
if (length < 0) return "";
if (start + length > text.Length) return "";
char[] result = new char[length];
for (int i = 0; i < length; i++)
result[i] = text[start + i];
return new string(result);
}
public static string SubstringFrom(string text, int start)
{
if (text == null) return "";
if (start < 0 || start > text.Length) return "";
return SubstringManual(text, start, text.Length - start);
}
public static string TrimSpacesAndNulls(string text)
{
if (text == null) return "";
int start = 0;
int end = text.Length - 1;
while (start <= end && (text[start] == ' ' || text[start] == '\0'))
start++;
while (end >= start && (text[end] == ' ' || text[end] == '\0'))
end--;
if (end < start) return "";
return SubstringManual(text, start, end - start + 1);
}
public static string TrimEndSpacesAndNulls(string text)
{
if (text == null) return "";
int end = text.Length - 1;
while (end >= 0 && (text[end] == ' ' || text[end] == '\0'))
end--;
if (end < 0) return "";
return SubstringManual(text, 0, end + 1);
}
public static string ReadUntil(string text, ref int i, char stopChar)
{
if (text == null) return "";
int start = i;
while (i < text.Length)
{
char c = text[i];
if (c == stopChar || c == ' ')
break;
i++;
}
int len = i - start;
if (len <= 0) return "";
return SubstringManual(text, start, len);
}
public static int ReadIntUntil(string text, ref int i, char stopChar)
{
if (text == null) return -1;
int value = 0;
bool hasDigit = false;
while (i < text.Length)
{
char c = text[i];
if (c == stopChar || c == ' ')
break;
if (c < '0' || c > '9') return -1;
hasDigit = true;
value = value * 10 + (c - '0');
i++;
}
return hasDigit ? value : -1;
}
public static void SkipSpaces(string text, ref int i)
{
if (text == null) return;
while (i < text.Length && text[i] == ' ')
i++;
}
}
}