FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/source/NetCoreServer/Buffer.cs at master · aftercore/NetCoreServer · GitHub
aftercore
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
source
/
NetCoreServer
/
Buffer.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
247 lines (221 loc) · 7.74 KB
Breadcrumbs
NetCoreServer
/
source
/
NetCoreServer
/
Buffer.cs
Copy path
File metadata and controls
247 lines (221 loc) · 7.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using
System
;
using
System
.
Diagnostics
;
using
System
.
Text
;
namespace
NetCoreServer
{
/// <summary>
/// Dynamic byte buffer
/// </summary>
public
class
Buffer
{
private
byte
[
]
_data
;
private
long
_size
;
private
long
_offset
;
/// <summary>
/// Is the buffer empty?
/// </summary>
public
bool
IsEmpty
=>
(
_data
==
null
)
||
(
_size
==
0
)
;
/// <summary>
/// Bytes memory buffer
/// </summary>
public
byte
[
]
Data
=>
_data
;
/// <summary>
/// Bytes memory buffer capacity
/// </summary>
public
long
Capacity
=>
_data
.
Length
;
/// <summary>
/// Bytes memory buffer size
/// </summary>
public
long
Size
=>
_size
;
/// <summary>
/// Bytes memory buffer offset
/// </summary>
public
long
Offset
=>
_offset
;
/// <summary>
/// Buffer indexer operator
/// </summary>
public
byte
this
[
long
index
]
=>
_data
[
index
]
;
/// <summary>
/// Initialize a new expandable buffer with zero capacity
/// </summary>
public
Buffer
(
)
{
_data
=
new
byte
[
0
]
;
_size
=
0
;
_offset
=
0
;
}
/// <summary>
/// Initialize a new expandable buffer with the given capacity
/// </summary>
public
Buffer
(
long
capacity
)
{
_data
=
new
byte
[
capacity
]
;
_size
=
0
;
_offset
=
0
;
}
/// <summary>
/// Initialize a new expandable buffer with the given data
/// </summary>
public
Buffer
(
byte
[
]
data
)
{
_data
=
data
;
_size
=
data
.
Length
;
_offset
=
0
;
}
#region Memory buffer methods
/// <summary>
/// Get a span of bytes from the current buffer
/// </summary>
public
Span
<
byte
>
AsSpan
(
)
{
return
new
Span
<
byte
>
(
_data
,
(
int
)
_offset
,
(
int
)
_size
)
;
}
/// <summary>
/// Get a string from the current buffer
/// </summary>
public
override
string
ToString
(
)
{
return
ExtractString
(
0
,
_size
)
;
}
/// <summary>
/// Clear the current buffer and its offset
/// </summary>
public
void
Clear
(
)
{
_size
=
0
;
_offset
=
0
;
}
/// <summary>
/// Extract the string from buffer of the given offset and size
/// </summary>
public
string
ExtractString
(
long
offset
,
long
size
)
{
Debug
.
Assert
(
(
(
offset
+
size
)
<=
Size
)
,
"Invalid offset & size!"
)
;
if
(
(
offset
+
size
)
>
Size
)
throw
new
ArgumentException
(
"Invalid offset & size!"
,
nameof
(
offset
)
)
;
return
Encoding
.
UTF8
.
GetString
(
_data
,
(
int
)
offset
,
(
int
)
size
)
;
}
/// <summary>
/// Remove the buffer of the given offset and size
/// </summary>
public
void
Remove
(
long
offset
,
long
size
)
{
Debug
.
Assert
(
(
(
offset
+
size
)
<=
Size
)
,
"Invalid offset & size!"
)
;
if
(
(
offset
+
size
)
>
Size
)
throw
new
ArgumentException
(
"Invalid offset & size!"
,
nameof
(
offset
)
)
;
Array
.
Copy
(
_data
,
offset
+
size
,
_data
,
offset
,
_size
-
size
-
offset
)
;
_size
-=
size
;
if
(
_offset
>=
(
offset
+
size
)
)
_offset
-=
size
;
else
if
(
_offset
>=
offset
)
{
_offset
-=
_offset
-
offset
;
if
(
_offset
>
Size
)
_offset
=
Size
;
}
}
/// <summary>
/// Reserve the buffer of the given capacity
/// </summary>
public
void
Reserve
(
long
capacity
)
{
Debug
.
Assert
(
(
capacity
>=
0
)
,
"Invalid reserve capacity!"
)
;
if
(
capacity
<
0
)
throw
new
ArgumentException
(
"Invalid reserve capacity!"
,
nameof
(
capacity
)
)
;
if
(
capacity
>
Capacity
)
{
byte
[
]
data
=
new
byte
[
Math
.
Max
(
capacity
,
2
*
Capacity
)
]
;
Array
.
Copy
(
_data
,
0
,
data
,
0
,
_size
)
;
_data
=
data
;
}
}
/// <summary>
/// Resize the current buffer
/// </summary>
public
void
Resize
(
long
size
)
{
Reserve
(
size
)
;
_size
=
size
;
if
(
_offset
>
_size
)
_offset
=
_size
;
}
/// <summary>
/// Shift the current buffer offset
/// </summary>
public
void
Shift
(
long
offset
)
{
_offset
+=
offset
;
}
/// <summary>
/// Unshift the current buffer offset
/// </summary>
public
void
Unshift
(
long
offset
)
{
_offset
-=
offset
;
}
#endregion
#region Buffer I/O methods
/// <summary>
/// Append the single byte
/// </summary>
/// <param name="value">Byte value to append</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
byte
value
)
{
Reserve
(
_size
+
1
)
;
_data
[
_size
]
=
value
;
_size
+=
1
;
return
1
;
}
/// <summary>
/// Append the given buffer
/// </summary>
/// <param name="buffer">Buffer to append</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
byte
[
]
buffer
)
{
Reserve
(
_size
+
buffer
.
Length
)
;
Array
.
Copy
(
buffer
,
0
,
_data
,
_size
,
buffer
.
Length
)
;
_size
+=
buffer
.
Length
;
return
buffer
.
Length
;
}
/// <summary>
/// Append the given buffer fragment
/// </summary>
/// <param name="buffer">Buffer to append</param>
/// <param name="offset">Buffer offset</param>
/// <param name="size">Buffer size</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
Reserve
(
_size
+
size
)
;
Array
.
Copy
(
buffer
,
offset
,
_data
,
_size
,
size
)
;
_size
+=
size
;
return
size
;
}
/// <summary>
/// Append the given span of bytes
/// </summary>
/// <param name="buffer">Buffer to append as a span of bytes</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
ReadOnlySpan
<
byte
>
buffer
)
{
Reserve
(
_size
+
buffer
.
Length
)
;
buffer
.
CopyTo
(
new
Span
<
byte
>
(
_data
,
(
int
)
_size
,
buffer
.
Length
)
)
;
_size
+=
buffer
.
Length
;
return
buffer
.
Length
;
}
/// <summary>
/// Append the given buffer
/// </summary>
/// <param name="buffer">Buffer to append</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
Buffer
buffer
)
=>
Append
(
buffer
.
AsSpan
(
)
)
;
/// <summary>
/// Append the given text in UTF-8 encoding
/// </summary>
/// <param name="text">Text to append</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
string
text
)
{
int
length
=
Encoding
.
UTF8
.
GetMaxByteCount
(
text
.
Length
)
;
Reserve
(
_size
+
length
)
;
long
result
=
Encoding
.
UTF8
.
GetBytes
(
text
,
0
,
text
.
Length
,
_data
,
(
int
)
_size
)
;
_size
+=
result
;
return
result
;
}
/// <summary>
/// Append the given text in UTF-8 encoding
/// </summary>
/// <param name="text">Text to append as a span of characters</param>
/// <returns>Count of append bytes</returns>
public
long
Append
(
ReadOnlySpan
<
char
>
text
)
{
int
length
=
Encoding
.
UTF8
.
GetMaxByteCount
(
text
.
Length
)
;
Reserve
(
_size
+
length
)
;
long
result
=
Encoding
.
UTF8
.
GetBytes
(
text
,
new
Span
<
byte
>
(
_data
,
(
int
)
_size
,
length
)
)
;
_size
+=
result
;
return
result
;
}
#endregion
}
}
Back
|
FazBrowse Home
|
New Git URL