forked from ArthurHub/HTML-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubString.cs
More file actions
187 lines (165 loc) · 6.34 KB
/
Copy pathSubString.cs
File metadata and controls
187 lines (165 loc) · 6.34 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
182
183
184
185
186
187
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
namespace TheArtOfDev.HtmlRenderer.Core.Utils
{
/// <summary>
/// Represents sub-string of a full string starting at specific location with a specific length.
/// </summary>
internal sealed class SubString
{
#region Fields and Consts
/// <summary>
/// the full string that this sub-string is part of
/// </summary>
private readonly string _fullString;
/// <summary>
/// the start index of the sub-string
/// </summary>
private readonly int _startIdx;
/// <summary>
/// the length of the sub-string starting at <see cref="_startIdx"/>
/// </summary>
private readonly int _length;
#endregion
/// <summary>
/// Init sub-string that is the full string.
/// </summary>
/// <param name="fullString">the full string that this sub-string is part of</param>
public SubString(string fullString)
{
ArgChecker.AssertArgNotNull(fullString, "fullString");
_fullString = fullString;
_startIdx = 0;
_length = fullString.Length;
}
/// <summary>
/// Init.
/// </summary>
/// <param name="fullString">the full string that this sub-string is part of</param>
/// <param name="startIdx">the start index of the sub-string</param>
/// <param name="length">the length of the sub-string starting at <paramref name="startIdx"/></param>
/// <exception cref="ArgumentNullException"><paramref name="fullString"/> is null</exception>
public SubString(string fullString, int startIdx, int length)
{
ArgChecker.AssertArgNotNull(fullString, "fullString");
if (startIdx < 0 || startIdx >= fullString.Length)
throw new ArgumentOutOfRangeException("startIdx", "Must within fullString boundries");
if (length < 0 || startIdx + length > fullString.Length)
throw new ArgumentOutOfRangeException("length", "Must within fullString boundries");
_fullString = fullString;
_startIdx = startIdx;
_length = length;
}
/// <summary>
/// the full string that this sub-string is part of
/// </summary>
public string FullString
{
get { return _fullString; }
}
/// <summary>
/// the start index of the sub-string
/// </summary>
public int StartIdx
{
get { return _startIdx; }
}
/// <summary>
/// the length of the sub-string starting at <see cref="_startIdx"/>
/// </summary>
public int Length
{
get { return _length; }
}
/// <summary>
/// Get string char at specific index.
/// </summary>
/// <param name="idx">the idx to get the char at</param>
/// <returns>char at index</returns>
public char this[int idx]
{
get
{
if (idx < 0 || idx > _length)
throw new ArgumentOutOfRangeException("idx", "must be within the string range");
return _fullString[_startIdx + idx];
}
}
/// <summary>
/// Is the sub-string is empty string.
/// </summary>
/// <returns>true - empty string, false - otherwise</returns>
public bool IsEmpty()
{
return _length < 1;
}
/// <summary>
/// Is the sub-string is empty string or contains only whitespaces.
/// </summary>
/// <returns>true - empty or whitespace string, false - otherwise</returns>
public bool IsEmptyOrWhitespace()
{
for (int i = 0; i < _length; i++)
{
if (!char.IsWhiteSpace(_fullString, _startIdx + i))
return false;
}
return true;
}
/// <summary>
/// Is the sub-string contains only whitespaces (at least one).
/// </summary>
/// <returns>true - empty or whitespace string, false - otherwise</returns>
public bool IsWhitespace()
{
if (_length < 1)
return false;
for (int i = 0; i < _length; i++)
{
if (!char.IsWhiteSpace(_fullString, _startIdx + i))
return false;
}
return true;
}
/// <summary>
/// Get a string of the sub-string.<br/>
/// This will create a new string object!
/// </summary>
/// <returns>new string that is the sub-string represented by this instance</returns>
public string CutSubstring()
{
return _length > 0 ? _fullString.Substring(_startIdx, _length) : string.Empty;
}
/// <summary>
/// Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
/// </summary>
/// <param name="startIdx">The zero-based starting character position of a substring in this instance.</param>
/// <param name="length">The number of characters in the substring. </param>
/// <returns>A String equivalent to the substring of length length that begins at startIndex in this instance, or
/// Empty if startIndex is equal to the length of this instance and length is zero. </returns>
public string Substring(int startIdx, int length)
{
if (startIdx < 0 || startIdx > _length)
throw new ArgumentOutOfRangeException("startIdx");
if (length > _length)
throw new ArgumentOutOfRangeException("length");
if (startIdx + length > _length)
throw new ArgumentOutOfRangeException("length");
return _fullString.Substring(_startIdx + startIdx, length);
}
public override string ToString()
{
return string.Format("Sub-string: {0}", _length > 0 ? _fullString.Substring(_startIdx, _length) : string.Empty);
}
}
}