C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
StringSplitter.cs
Go to the documentation of this file.
1using System;
2
4{
9 public class StringSplitter
10 {
15 public StringSplitter(int bufferSize)
16 {
17 buffer = new string[bufferSize];
18 }
19
23 public string[] buffer;
24
28 public string[] Results => buffer;
29
39 public int Split(string value, char separator)
40 {
41 int resultIndex = 0;
42 int startIndex = 0;
43
44 // Find the mid-parts
45 for (int i = 0; i < value.Length; i++)
46 {
47 if (value[i] == separator)
48 {
49 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
50 resultIndex++;
51 startIndex = i + 1;
52 }
53 }
54
55 // Find the last part
56 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
57 resultIndex++;
58
59 return resultIndex;
60 }
61
71 public int SafeSplit(string value, char separator)
72 {
73 int resultIndex = 0;
74 int startIndex = 0;
75
76 // Find the mid-parts
77 for (int i = 0; i < value.Length; i++)
78 {
79 if (value[i] == separator)
80 {
81 // Check if the array needs to be resized
82 if (buffer.Length == resultIndex)
83 {
84 Array.Resize(ref buffer, buffer.Length * 2);
85 }
86
87 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
88 resultIndex++;
89 startIndex = i + 1;
90 }
91 }
92
93 // Check if the array needs to be resized
94 if (buffer.Length == resultIndex)
95 {
96 Array.Resize(ref buffer, buffer.Length * 2);
97 }
98
99 // Find the last part
100 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
101
102 return resultIndex + 1;
103 }
104
112 public int Split(string value, string separator)
113 {
114 int resultIndex = 0;
115 int startIndex = 0;
116
117 // Calculate the max index so we don't return false results during the index comparison
118 // which is set to true at start, so if only one character is left and matches will remain true
119 int maxIndex = value.Length - separator.Length;
120 for (int i = 0; i < maxIndex; i++)
121 {
122 bool matchFound = true;
123 for (int n = 0; n < separator.Length && (n + i) < value.Length; n++)
124 {
125 if (value[i + n] != separator[n])
126 {
127 matchFound = false;
128 break;
129 }
130 }
131
132 if (matchFound)
133 {
134 buffer[resultIndex] = value.Substring(startIndex, i + separator.Length - startIndex - separator.Length);
135 resultIndex++;
136 startIndex = i + separator.Length;
137 i += separator.Length - 1;
138 }
139 }
140
141 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
142 return resultIndex + 1;
143 }
144
151 public int SafeSplit(string value, string separator)
152 {
153 int resultIndex = 0;
154 int startIndex = 0;
155 // Calculate the max index so we don't return false results during the index comparison
156 // which is set to true at start, so if only one character is left and matches will remain true
157 int maxIndex = value.Length - separator.Length;
158 for (int i = 0; i < maxIndex; i++)
159 {
160 // Match the whole string
161 bool matchFound = true;
162 for (int n = 0; n < separator.Length && (n + i) < value.Length; n++)
163 {
164 if (value[i + n] != separator[n])
165 {
166 matchFound = false;
167 break;
168 }
169 }
170
171 if (matchFound)
172 {
173 // Check if the array needs to be resized
174 if (buffer.Length == resultIndex)
175 {
176 Array.Resize(ref buffer, buffer.Length * 2);
177 }
178
179 buffer[resultIndex] = value.Substring(startIndex, i + separator.Length - startIndex - separator.Length);
180 resultIndex++;
181 startIndex = i + separator.Length;
182 i += separator.Length - 1;
183 }
184 }
185
186 // Check if the array needs to be resized
187 if (buffer.Length == resultIndex)
188 {
189 Array.Resize(ref buffer, buffer.Length * 2);
190 }
191
192 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
193 return resultIndex + 1;
194 }
195
205 public int Split(string value, char[] separators)
206 {
207 int resultIndex = 0;
208 int startIndex = 0;
209
210 // Find the mid-parts
211 for (int i = 0; i < value.Length; i++)
212 {
213 // Match the whole string
214 bool matchFound = false;
215 for (int n = 0; n < separators.Length; n++)
216 {
217 if (value[i] == separators[n])
218 {
219 matchFound = true;
220 break;
221 }
222 }
223
224 if (matchFound)
225 {
226 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
227 resultIndex++;
228 startIndex = i + 1;
229 }
230 }
231
232 // Find the last part
233 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
234 resultIndex++;
235
236 return resultIndex;
237 }
238
248 public int SafeSplit(string value, char[] separators)
249 {
250 int resultIndex = 0;
251 int startIndex = 0;
252
253 // Find the mid-parts
254 for (int i = 0; i < value.Length; i++)
255 {
256 // Match the whole string
257 bool matchFound = false;
258 for (int n = 0; n < separators.Length; n++)
259 {
260 if (value[i] == separators[n])
261 {
262 matchFound = true;
263 break;
264 }
265 }
266
267 if (matchFound)
268 {
269 // Check if the array needs to be resized
270 if (buffer.Length == resultIndex)
271 {
272 Array.Resize(ref buffer, buffer.Length * 2);
273 }
274
275 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
276 resultIndex++;
277 startIndex = i + 1;
278 }
279 }
280
281 // Check if the array needs to be resized
282 if (buffer.Length == resultIndex)
283 {
284 Array.Resize(ref buffer, buffer.Length * 2);
285 }
286
287 // Find the last part
288 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
289 resultIndex++;
290
291 return resultIndex;
292 }
293
304 public int Split(string value, char separator, StringSplitOptions options)
305 {
306 int resultIndex = 0;
307 int startIndex = 0;
308
309 // Find the mid-parts
310 for (int i = 0; i < value.Length; i++)
311 {
312 if (value[i] == separator)
313 {
314 if (options == StringSplitOptions.None || i - startIndex > 0)
315 {
316 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
317 resultIndex++;
318 }
319
320 startIndex = i + 1;
321 }
322 }
323
324 // Find the last part
325 if (options == StringSplitOptions.None || value.Length - startIndex > 0)
326 {
327 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
328 resultIndex++;
329 }
330
331 return resultIndex;
332 }
333
344 public int SafeSplit(string value, char separator, StringSplitOptions options)
345 {
346 int resultIndex = 0;
347 int startIndex = 0;
348
349 // Find the mid-parts
350 for (int i = 0; i < value.Length; i++)
351 {
352 if (value[i] == separator)
353 {
354 if (options == StringSplitOptions.None || i - startIndex > 0)
355 {
356 // Check if the array needs to be resized
357 if (buffer.Length == resultIndex)
358 {
359 Array.Resize(ref buffer, buffer.Length * 2);
360 }
361 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
362 resultIndex++;
363 }
364
365 startIndex = i + 1;
366 }
367 }
368
369 // Find the last part
370 if (options == StringSplitOptions.None || value.Length - startIndex > 0)
371 {
372 // Check if the array needs to be resized
373 if (buffer.Length == resultIndex)
374 {
375 Array.Resize(ref buffer, buffer.Length * 2);
376 }
377
378 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
379 resultIndex++;
380 }
381
382 return resultIndex;
383 }
384
393 public int Split(string value, string separator, StringSplitOptions options)
394 {
395 int resultIndex = 0;
396 int startIndex = 0;
397
398 // Calculate the max index so we don't return false results during the index comparison
399 // which is set to true at start, so if only one character is left and matches will remain true
400 int maxIndex = value.Length - separator.Length;
401 for (int i = 0; i < maxIndex; i++)
402 {
403 bool matchFound = true;
404 for (int n = 0; n < separator.Length && (n + i) < value.Length; n++)
405 {
406 if (value[i + n] != separator[n])
407 {
408 matchFound = false;
409 break;
410 }
411 }
412
413 if (matchFound)
414 {
415 if (options == StringSplitOptions.None || i + separator.Length - startIndex - separator.Length > 0)
416 {
417 buffer[resultIndex] = value.Substring(startIndex, i + separator.Length - startIndex - separator.Length);
418 resultIndex++;
419 }
420
421 startIndex = i + separator.Length;
422 i += separator.Length - 1;
423 }
424 }
425
426 if (options == StringSplitOptions.None || value.Length - startIndex > 0)
427 {
428 // Find the last part
429 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
430 resultIndex++;
431 }
432
433 return resultIndex;
434 }
435
443 public int SafeSplit(string value, string separator, StringSplitOptions options)
444 {
445 int resultIndex = 0;
446 int startIndex = 0;
447 // Calculate the max index so we don't return false results during the index comparison
448 // which is set to true at start, so if only one character is left and matches will remain true
449 int maxIndex = value.Length - separator.Length;
450 for (int i = 0; i < maxIndex; i++)
451 {
452 // Match the whole string
453 bool matchFound = true;
454 for (int n = 0; n < separator.Length && (n + i) < value.Length; n++)
455 {
456 if (value[i + n] != separator[n])
457 {
458 matchFound = false;
459 break;
460 }
461 }
462
463 if (matchFound)
464 {
465 if (options == StringSplitOptions.None || i + separator.Length - startIndex - separator.Length > 0)
466 {
467 // Check if the array needs to be resized
468 if (buffer.Length == resultIndex)
469 {
470 Array.Resize(ref buffer, buffer.Length * 2);
471 }
472 buffer[resultIndex] = value.Substring(startIndex, i + separator.Length - startIndex - separator.Length);
473 resultIndex++;
474 }
475
476 startIndex = i + separator.Length;
477 i += separator.Length - 1;
478 }
479 }
480
481 if (options == StringSplitOptions.None || value.Length - startIndex > 0)
482 {
483 // Check if the array needs to be resized
484 if (buffer.Length == resultIndex)
485 {
486 Array.Resize(ref buffer, buffer.Length * 2);
487 }
488
489 // Find the last part
490 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
491 resultIndex++;
492 }
493
494 return resultIndex;
495 }
496
507 public int Split(string value, char[] separators, StringSplitOptions options)
508 {
509 int resultIndex = 0;
510 int startIndex = 0;
511
512 // Find the mid-parts
513 for (int i = 0; i < value.Length; i++)
514 {
515 // Match the whole string
516 bool matchFound = false;
517 for (int n = 0; n < separators.Length; n++)
518 {
519 if (value[i] == separators[n])
520 {
521 matchFound = true;
522 break;
523 }
524 }
525
526 if (matchFound)
527 {
528 if (options == StringSplitOptions.None || i - startIndex > 0)
529 {
530 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
531 resultIndex++;
532 }
533
534 startIndex = i + 1;
535 }
536 }
537
538 // Find the last part
539 if (options == StringSplitOptions.None || value.Length - startIndex > 0)
540 {
541 // Find the last part
542 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
543 resultIndex++;
544 }
545
546 return resultIndex;
547 }
548
559 public int SafeSplit(string value, char[] separators, StringSplitOptions options)
560 {
561 int resultIndex = 0;
562 int startIndex = 0;
563
564 // Find the mid-parts
565 for (int i = 0; i < value.Length; i++)
566 {
567 // Match the whole string
568 bool matchFound = false;
569 for (int n = 0; n < separators.Length; n++)
570 {
571 if (value[i] == separators[n])
572 {
573 matchFound = true;
574 break;
575 }
576 }
577
578 if (matchFound)
579 {
580 if (options == StringSplitOptions.None || i - startIndex > 0)
581 {
582 // Check if the array needs to be resized
583 if (buffer.Length == resultIndex)
584 {
585 Array.Resize(ref buffer, buffer.Length * 2);
586 }
587
588 buffer[resultIndex] = value.Substring(startIndex, i - startIndex);
589 resultIndex++;
590 }
591
592 startIndex = i + 1;
593 }
594 }
595
596 // Find the last part
597 if (options == StringSplitOptions.None || value.Length - startIndex > 0)
598 {
599 // Check if the array needs to be resized
600 if (buffer.Length == resultIndex)
601 {
602 Array.Resize(ref buffer, buffer.Length * 2);
603 }
604
605 // Find the last part
606 buffer[resultIndex] = value.Substring(startIndex, value.Length - startIndex);
607 resultIndex++;
608 }
609
610 return resultIndex;
611 }
612 }
613}
A high performance string splitter https://github.com/meikeric/String.Split/blob/master/Split....
int SafeSplit(string value, char separator)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
int SafeSplit(string value, char separator, StringSplitOptions options)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
string[] buffer
The string buffer container.
int Split(string value, string separator, StringSplitOptions options)
Returns a string array that contains the substrings in this instance that are delimited by a specifie...
StringSplitter(int bufferSize)
Create a new StringSplitter object with the given buffer size.
int SafeSplit(string value, string separator)
Split the string.
int SafeSplit(string value, char[] separators, StringSplitOptions options)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
int SafeSplit(string value, string separator, StringSplitOptions options)
Split the string.
int Split(string value, char separator, StringSplitOptions options)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
int Split(string value, char separator)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
int Split(string value, char[] separators)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
int SafeSplit(string value, char[] separators)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
int Split(string value, string separator)
Returns a string array that contains the substrings in this instance that are delimited by a specifie...
int Split(string value, char[] separators, StringSplitOptions options)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
string[] Results
Get the results of the last split call.