• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

neon-sunset / U8String / 5933756664

22 Aug 2023 02:28AM UTC coverage: 23.927% (+3.6%) from 20.279%
5933756664

push

github

neon-sunset
ci: remove dead code and fix the build

152 of 828 branches covered (18.36%)

Branch coverage included in aggregate %.

500 of 1897 relevant lines covered (26.36%)

43459.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/U8String.Splitting.cs
1
using System.Collections;
2
using System.Runtime.InteropServices;
3
using System.Text;
4
using U8Primitives.Abstractions;
5
using U8Primitives.InteropServices;
6

7
#pragma warning disable RCS1085, RCS1085FadeOut, IDE0032 // Use auto-implemented property. Why: readable fields.
8
namespace U8Primitives;
9

10
public readonly partial struct U8String
11
{
12
    public U8SplitPair SplitFirst(byte separator)
13
    {
14
        if (!U8Info.IsAsciiByte(separator))
×
15
        {
16
            // TODO: EH UX
17
            ThrowHelpers.ArgumentOutOfRange();
×
18
        }
19

20
        var source = this;
×
21
        if (!source.IsEmpty)
×
22
        {
23
            var span = source.UnsafeSpan;
×
24
            var index = span.IndexOf(separator);
×
25
            if (index >= 0)
×
26
            {
27
                return new(source, index, 1);
×
28
            }
29

30
            return U8SplitPair.NotFound(source);
×
31
        }
32

33
        return default;
×
34
    }
35

36
    public U8SplitPair SplitFirst(char separator) => char.IsAscii(separator)
×
37
        ? SplitFirst((byte)separator)
×
38
        : SplitFirstUnchecked(U8Scalar.Create(separator, checkAscii: false).AsSpan());
×
39

40
    public U8SplitPair SplitFirst(Rune separator) => separator.IsAscii
×
41
        ? SplitFirst((byte)separator.Value)
×
42
        : SplitFirstUnchecked(U8Scalar.Create(separator, checkAscii: false).AsSpan());
×
43

44
    public U8SplitPair SplitFirst(U8String separator)
45
    {
46
        var source = this;
×
47
        if (!source.IsEmpty)
×
48
        {
49
            if (!separator.IsEmpty)
×
50
            {
51
                var span = source.UnsafeSpan;
×
52
                var index = span.IndexOf(separator.UnsafeSpan);
×
53
                if (index >= 0)
×
54
                {
55
                    return new(source, index, separator.Length);
×
56
                }
57
            }
58

59
            return U8SplitPair.NotFound(source);
×
60
        }
61

62
        return default;
×
63
    }
64

65
    // It would be *really nice* to aggressively inline this method
66
    // but the way validation is currently implemented does not significantly
67
    // benefit from splitting on UTF-8 literals while possibly risking
68
    // running out of inlining budget significantly regressing performance everywhere else.
69
    public U8SplitPair SplitFirst(ReadOnlySpan<byte> separator)
70
    {
71
        var source = this;
×
72
        if (!source.IsEmpty)
×
73
        {
74
            if (!separator.IsEmpty)
×
75
            {
76
                var span = source.UnsafeSpan;
×
77
                var index = span.IndexOf(separator);
×
78
                if (index >= 0)
×
79
                {
80
                    // Same as with Slice(int, int), this might dereference past the end of the string.
81
                    // TODO: Do something about it if it's ever an issue.
82
                    if (U8Info.IsContinuationByte(source.UnsafeRefAdd(index)) ||
×
83
                        U8Info.IsContinuationByte(source.UnsafeRefAdd(index + separator.Length)))
×
84
                    {
85
                        ThrowHelpers.InvalidSplit();
×
86
                    }
87

88
                    return new(source, index, separator.Length);
×
89
                }
90
            }
91

92
            return U8SplitPair.NotFound(source);
×
93
        }
94

95
        return default;
×
96
    }
97

98
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
99
    public U8SplitPair SplitFirstUnchecked(ReadOnlySpan<byte> separator)
100
    {
101
        var source = this;
×
102
        if (!source.IsEmpty)
×
103
        {
104
            if (!separator.IsEmpty)
×
105
            {
106
                var span = source.UnsafeSpan;
×
107
                var index = span.IndexOf(separator);
×
108
                if (index >= 0)
×
109
                {
110
                    return new(source, index, separator.Length);
×
111
                }
112
            }
113

114
            return U8SplitPair.NotFound(source);
×
115
        }
116

117
        return default;
×
118
    }
119

120
    public U8SplitPair SplitLast(byte separator)
121
    {
122
        if (!U8Info.IsAsciiByte(separator))
×
123
        {
124
            // TODO: EH UX
125
            ThrowHelpers.ArgumentOutOfRange();
×
126
        }
127

128
        var source = this;
×
129
        if (!source.IsEmpty)
×
130
        {
131
            var span = source.UnsafeSpan;
×
132
            var index = span.LastIndexOf(separator);
×
133
            if (index >= 0)
×
134
            {
135
                return new(source, index, 1);
×
136
            }
137

138
            return U8SplitPair.NotFound(source);
×
139
        }
140

141
        return default;
×
142
    }
143

144
    public U8SplitPair SplitLast(char separator) => char.IsAscii(separator)
×
145
        ? SplitLast((byte)separator)
×
146
        : SplitLastUnchecked(U8Scalar.Create(separator, checkAscii: false).AsSpan());
×
147

148
    public U8SplitPair SplitLast(Rune separator) => separator.IsAscii
×
149
        ? SplitLast((byte)separator.Value)
×
150
        : SplitLastUnchecked(U8Scalar.Create(separator, checkAscii: false).AsSpan());
×
151

152
    public U8SplitPair SplitLast(U8String separator)
153
    {
154
        var source = this;
×
155
        if (!source.IsEmpty)
×
156
        {
157
            if (!separator.IsEmpty)
×
158
            {
159
                var span = source.UnsafeSpan;
×
160
                var index = span.LastIndexOf(separator.UnsafeSpan);
×
161
                if (index >= 0)
×
162
                {
163
                    return new(source, index, separator.Length);
×
164
                }
165
            }
166

167
            return U8SplitPair.NotFound(source);
×
168
        }
169

170
        return default;
×
171
    }
172

173
    public U8SplitPair SplitLast(ReadOnlySpan<byte> separator)
174
    {
175
        var source = this;
×
176
        if (!source.IsEmpty)
×
177
        {
178
            if (!separator.IsEmpty)
×
179
            {
180
                var span = source.UnsafeSpan;
×
181
                var index = span.LastIndexOf(separator);
×
182
                if (index >= 0)
×
183
                {
184
                    if (U8Info.IsContinuationByte(source.UnsafeRefAdd(index)) ||
×
185
                        U8Info.IsContinuationByte(source.UnsafeRefAdd(index + separator.Length)))
×
186
                    {
187
                        ThrowHelpers.InvalidSplit();
×
188
                    }
189

190
                    return new(source, index, separator.Length);
×
191
                }
192
            }
193

194
            return U8SplitPair.NotFound(source);
×
195
        }
196

197
        return default;
×
198
    }
199

200
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
201
    public U8SplitPair SplitLastUnchecked(ReadOnlySpan<byte> separator)
202
    {
203
        var source = this;
×
204
        if (!source.IsEmpty)
×
205
        {
206
            if (!separator.IsEmpty)
×
207
            {
208
                var span = source.UnsafeSpan;
×
209
                var index = span.LastIndexOf(separator);
×
210
                if (index >= 0)
×
211
                {
212
                    return new(source, index, separator.Length);
×
213
                }
214
            }
215

216
            return U8SplitPair.NotFound(source);
×
217
        }
218

219
        return default;
×
220
    }
221

222
    public U8Split<byte> Split(byte separator)
223
    {
224
        if (!U8Info.IsAsciiByte(separator))
×
225
        {
226
            ThrowHelpers.ArgumentOutOfRange();
×
227
        }
228

229
        return new(this, separator);
×
230
    }
231

232
    public U8Split<char> Split(char separator)
233
    {
234
        if (char.IsSurrogate(separator))
×
235
        {
236
            ThrowHelpers.ArgumentOutOfRange();
×
237
        }
238

239
        return new(this, separator);
×
240
    }
241

242
    public U8Split<Rune> Split(Rune separator) => new(this, separator);
×
243

244
    public U8Split Split(U8String separator)
245
    {
246
        return new(this, separator);
×
247
    }
248

249
    public U8RefSplit Split(ReadOnlySpan<byte> separator)
250
    {
251
        if (!IsValid(separator))
×
252
        {
253
            ThrowHelpers.InvalidSplit();
×
254
        }
255

256
        return new(this, separator);
×
257
    }
258

259
    public ConfiguredU8Split<byte> Split(byte separator, U8SplitOptions options)
260
    {
261
        if (!U8Info.IsAsciiByte(separator))
×
262
        {
263
            ThrowHelpers.ArgumentOutOfRange();
×
264
        }
265

266
        return new(this, separator, options);
×
267
    }
268

269
    public ConfiguredU8Split<char> Split(char separator, U8SplitOptions options)
270
    {
271
        if (char.IsSurrogate(separator))
×
272
        {
273
            ThrowHelpers.ArgumentOutOfRange();
×
274
        }
275

276
        return new(this, separator, options);
×
277
    }
278

279
    public ConfiguredU8Split<Rune> Split(Rune separator, U8SplitOptions options) => new(this, separator, options);
×
280

281
    public ConfiguredU8Split Split(U8String separator, U8SplitOptions options)
282
    {
283
        return new(this, separator, options);
×
284
    }
285
}
286

287
public readonly record struct U8SplitPair
288
{
289
    readonly U8String _value;
290
    readonly int _offset;
291
    readonly int _stride;
292

293
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
294
    internal U8SplitPair(U8String value, int offset, int stride)
295
    {
296
        _value = value;
×
297
        _offset = offset;
×
298
        _stride = stride;
×
299
    }
×
300

301
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
302
    public static U8SplitPair NotFound(U8String value)
303
    {
304
        return new(value, value.Length, 0);
×
305
    }
306

307
    public U8String Segment
308
    {
309
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
310
        get => U8Marshal.Slice(_value, 0, _offset);
×
311
    }
312

313
    public U8String Remainder
314
    {
315
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
316
        get => U8Marshal.Slice(_value, _offset + _stride);
×
317
    }
318

319
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
320
    public void Deconstruct(out U8String segment, out U8String remainder)
321
    {
322
        segment = Segment;
×
323
        remainder = Remainder;
×
324
    }
×
325

326
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
327
    public static implicit operator (U8String, U8String)(U8SplitPair value)
328
    {
329
        return (value.Segment, value.Remainder);
×
330
    }
331
}
332

333
public struct U8Split : ICollection<U8String>, IU8Enumerable<U8Split.Enumerator>
334
{
335
    readonly U8String _value;
336
    readonly U8String _separator;
337
    int _count;
338

339
    public U8Split(U8String value, U8String separator)
340
    {
341
        _value = value;
×
342
        _separator = separator;
×
343
        _count = value.IsEmpty ? 0 : -1;
×
344
    }
×
345

346
    public int Count
347
    {
348
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
349
        get
350
        {
351
            var count = _count;
×
352
            if (count >= 0)
×
353
            {
354
                return count;
×
355
            }
356

357
            // Matches the behavior of string.Split('\n').Length for "hello\n"
358
            // TODO: Should we break consistency and not count the very last segment if it is empty?
359
            return _count = Count(_value.UnsafeSpan, _separator) + 1;
×
360

361
            static int Count(ReadOnlySpan<byte> value, ReadOnlySpan<byte> separator)
362
            {
363
                return U8Searching.Count(value, separator);
×
364
            }
365
        }
366
    }
367

368
    public readonly bool Contains(U8String item)
369
    {
370
        return U8Searching.SplitContains(_value, _separator, item);
×
371
    }
372

373
    public void CopyTo(U8String[] array, int index)
374
    {
375
        this.CopyTo<U8Split, Enumerator, U8String>(array.AsSpan()[index..]);
×
376
    }
×
377

378
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
379
    public readonly void Deconstruct(out U8String first, out U8String second)
380
    {
381
        this.Deconstruct<U8Split, Enumerator, U8String>(out first, out second);
×
382
    }
×
383

384
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
385
    public readonly void Deconstruct(out U8String first, out U8String second, out U8String third)
386
    {
387
        this.Deconstruct<U8Split, Enumerator, U8String>(out first, out second, out third);
×
388
    }
×
389

390
    public U8String[] ToArray() => this.ToArray<U8Split, Enumerator, U8String>();
×
391
    public List<U8String> ToList() => this.ToList<U8Split, Enumerator, U8String>();
×
392

393
    /// <summary>
394
    /// Returns a <see cref="Enumerator"/> over the provided string.
395
    /// </summary>
396
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
397
    public readonly Enumerator GetEnumerator() => new(_value, _separator);
×
398

399
    readonly IEnumerator<U8String> IEnumerable<U8String>.GetEnumerator() => GetEnumerator();
×
400
    readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
×
401
    readonly bool ICollection<U8String>.IsReadOnly => true;
×
402

403
    public struct Enumerator : IU8Enumerator
404
    {
405
        readonly byte[]? _value;
406
        readonly U8String _separator;
407
        U8Range _current;
408
        U8Range _remaining;
409

410
        internal Enumerator(U8String value, U8String separator)
411
        {
412
            _value = value._value;
×
413
            _separator = separator;
×
414
            _remaining = value._inner;
×
415
        }
×
416

417
        public readonly U8String Current => new(_value, _current.Offset, _current.Length);
×
418

419
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
420
        public bool MoveNext()
421
        {
422
            var remaining = _remaining;
×
423
            if (remaining.Length > 0)
×
424
            {
425
                var value = _value!.SliceUnsafe(remaining.Offset, remaining.Length);
×
426
                var separator = _separator;
×
427
                var index = value.IndexOf(separator);
×
428
                if (index >= 0)
×
429
                {
430
                    _current = new(remaining.Offset, index);
×
431
                    _remaining = new(
×
432
                        remaining.Offset + index + separator.Length,
×
433
                        remaining.Length - index - separator.Length);
×
434
                }
435
                else
436
                {
437
                    _current = remaining;
×
438
                    _remaining = default;
×
439
                }
440

441
                return true;
×
442
            }
443

444
            return false;
×
445
        }
446

447
        readonly object IEnumerator.Current => Current;
×
448
        readonly void IEnumerator.Reset() => throw new NotSupportedException();
×
449
        readonly void IDisposable.Dispose() { }
×
450
    }
451

452
    readonly void ICollection<U8String>.Add(U8String item) => throw new NotSupportedException();
×
453
    readonly void ICollection<U8String>.Clear() => throw new NotSupportedException();
×
454
    readonly bool ICollection<U8String>.Remove(U8String item) => throw new NotSupportedException();
×
455
}
456

457
// TODO: Optimize even more. This design is far from the northstar of perfect codegen
458
// but it still somehow manages to outperform Rust split iterators
459
public struct U8Split<TSeparator> :
460
    ICollection<U8String>,
461
    IU8Enumerable<U8Split<TSeparator>.Enumerator>
462
        where TSeparator : unmanaged
463
{
464
    readonly U8String _value;
465
    readonly TSeparator _separator;
466
    int _count;
467

468
    internal U8Split(U8String value, TSeparator separator)
469
    {
470
        _value = value;
×
471
        _separator = separator;
×
472
        _count = value.IsEmpty ? 0 : -1;
×
473
    }
×
474

475
    public int Count
476
    {
477
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
478
        get
479
        {
480
            var count = _count;
×
481
            if (count >= 0)
×
482
            {
483
                return count;
×
484
            }
485

486
            // Matches the behavior of string.Split('\n').Length for "hello\n"
487
            // TODO: Should we break consistency and not count the very last segment if it is empty?
488
            return _count = Count(_value, _separator) + 1;
×
489

490
            static int Count(U8String value, TSeparator separator)
491
            {
492
                return U8Searching.Count(value, separator);
×
493
            }
494
        }
495
    }
496

497
    public readonly bool Contains(U8String item)
498
    {
499
        return U8Searching.SplitContains(_value, _separator, item);
×
500
    }
501

502
    public void CopyTo(U8String[] array, int index)
503
    {
504
        this.CopyTo<U8Split<TSeparator>, Enumerator, U8String>(array.AsSpan()[index..]);
×
505
    }
×
506

507
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
508
    public readonly void Deconstruct(out U8String first, out U8String second)
509
    {
510
        this.Deconstruct<U8Split<TSeparator>, Enumerator, U8String>(out first, out second);
×
511
    }
×
512

513
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
514
    public readonly void Deconstruct(out U8String first, out U8String second, out U8String third)
515
    {
516
        this.Deconstruct<U8Split<TSeparator>, Enumerator, U8String>(out first, out second, out third);
×
517
    }
×
518

519
    public U8String[] ToArray() => this.ToArray<U8Split<TSeparator>, Enumerator, U8String>();
×
520
    public List<U8String> ToList() => this.ToList<U8Split<TSeparator>, Enumerator, U8String>();
×
521

522
    /// <summary>
523
    /// Returns a <see cref="Enumerator"/> over the provided string.
524
    /// </summary>
525
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
526
    public readonly Enumerator GetEnumerator() => new(_value, _separator);
×
527

528
    readonly IEnumerator<U8String> IEnumerable<U8String>.GetEnumerator() => GetEnumerator();
×
529
    readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
×
530
    readonly bool ICollection<U8String>.IsReadOnly => true;
×
531

532
    public struct Enumerator : IU8Enumerator
533
    {
534
        readonly byte[]? _value;
535
        readonly TSeparator _separator;
536
        U8Range _current;
537
        U8Range _remaining;
538

539
        internal Enumerator(U8String value, TSeparator separator)
540
        {
541
            _value = value._value;
×
542
            _separator = separator;
×
543
            _remaining = value._inner;
×
544
        }
×
545

546
        public readonly U8String Current => new(_value, _current.Offset, _current.Length);
×
547

548
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
549
        public bool MoveNext()
550
        {
551
            var remaining = _remaining;
×
552
            if (remaining.Length > 0)
×
553
            {
554
                var scalar = U8Scalar.Create(_separator);
×
555
                var value = _value!.SliceUnsafe(remaining.Offset, remaining.Length);
×
556
                var index = U8Searching.IndexOf(value, scalar);
×
557
                if (index >= 0)
×
558
                {
559
                    _current = new(remaining.Offset, index);
×
560
                    _remaining = new(
×
561
                        remaining.Offset + index + scalar.Size,
×
562
                        remaining.Length - index - scalar.Size);
×
563
                }
564
                else
565
                {
566
                    _current = remaining;
×
567
                    _remaining = default;
×
568
                }
569

570
                return true;
×
571
            }
572

573
            return false;
×
574
        }
575

576
        readonly object IEnumerator.Current => Current;
×
577
        readonly void IEnumerator.Reset() => throw new NotSupportedException();
×
578
        readonly void IDisposable.Dispose() { }
×
579
    }
580

581
    readonly void ICollection<U8String>.Add(U8String item) => throw new NotSupportedException();
×
582
    readonly void ICollection<U8String>.Clear() => throw new NotSupportedException();
×
583
    readonly bool ICollection<U8String>.Remove(U8String item) => throw new NotSupportedException();
×
584
}
585

586
public readonly struct ConfiguredU8Split :
587
    IU8Enumerable<ConfiguredU8Split.Enumerator>
588
{
589
    readonly U8String _value;
590
    readonly U8String _separator;
591
    readonly U8SplitOptions _options;
592

593
    public ConfiguredU8Split(U8String value, U8String separator, U8SplitOptions options)
594
    {
595
        _value = value;
×
596
        _separator = separator;
×
597
        _options = options;
×
598
    }
×
599

600
    public readonly Enumerator GetEnumerator() => new(_value, _separator, _options);
×
601

602
    readonly IEnumerator<U8String> IEnumerable<U8String>.GetEnumerator() => GetEnumerator();
×
603
    readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
×
604

605
    public struct Enumerator : IU8Enumerator
606
    {
607
        readonly byte[]? _value;
608
        readonly U8String _separator;
609
        readonly U8SplitOptions _options;
610
        U8Range _current;
611
        U8Range _remaining;
612

613
        public Enumerator(U8String value, U8String separator, U8SplitOptions options)
614
        {
615
            _value = value._value;
×
616
            _separator = separator;
×
617
            _options = options;
×
618
            _remaining = value._inner;
×
619
        }
×
620

621
        public readonly U8String Current => new(_value, _current.Offset, _current.Length);
×
622

623
        // TODO: Not most efficient but it works for now
624
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
625
        public bool MoveNext()
626
        {
627
        Next:
628
            var remaining = _remaining;
×
629
            if (remaining.Length > 0)
×
630
            {
631
                var value = _value!.SliceUnsafe(remaining.Offset, remaining.Length);
×
632
                var separator = _separator;
×
633
                var index = value.IndexOf(separator);
×
634
                if (index >= 0)
×
635
                {
636
                    _current = (_options & U8SplitOptions.Trim) != U8SplitOptions.Trim
×
637
                        ? new(remaining.Offset, index)
×
638
                        : TrimEntry(_value!, new(remaining.Offset, index));
×
639
                    _remaining = new(
×
640
                        remaining.Offset + index + separator.Length,
×
641
                        remaining.Length - index - separator.Length);
×
642
                }
643
                else
644
                {
645
                    _current = (_options & U8SplitOptions.Trim) != U8SplitOptions.Trim
×
646
                        ? remaining
×
647
                        : TrimEntry(_value!, remaining);
×
648
                    _remaining = default;
×
649
                }
650

651
                if ((_options & U8SplitOptions.RemoveEmpty) is U8SplitOptions.RemoveEmpty
×
652
                    && _current.Length is 0)
×
653
                {
654
                    goto Next;
655
                }
656

657
                return true;
×
658
            }
659

660
            return false;
×
661
        }
662

663
        private static U8Range TrimEntry(byte[] value, U8Range range)
664
        {
665
            // This could have been done better but works for now.
666
            return new U8String(value, range).Trim()._inner;
×
667
        }
668

669
        readonly object IEnumerator.Current => Current;
×
670
        readonly void IEnumerator.Reset() => throw new NotSupportedException();
×
671
        readonly void IDisposable.Dispose() { }
×
672
    }
673
}
674

675
public readonly struct ConfiguredU8Split<TSeparator> :
676
    IU8Enumerable<ConfiguredU8Split<TSeparator>.Enumerator>
677
        where TSeparator : unmanaged
678
{
679
    readonly U8String _value;
680
    readonly TSeparator _separator;
681
    readonly U8SplitOptions _options;
682

683
    internal ConfiguredU8Split(U8String value, TSeparator separator, U8SplitOptions options)
684
    {
685
        _value = value;
×
686
        _separator = separator;
×
687
        _options = options;
×
688
    }
×
689

690
    public readonly Enumerator GetEnumerator() => new(_value, _separator, _options);
×
691

692
    readonly IEnumerator<U8String> IEnumerable<U8String>.GetEnumerator() => GetEnumerator();
×
693
    readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
×
694

695
    public struct Enumerator : IU8Enumerator
696
    {
697
        readonly byte[]? _value;
698
        readonly TSeparator _separator;
699
        readonly U8SplitOptions _options;
700
        U8Range _current;
701
        U8Range _remaining;
702

703
        internal Enumerator(U8String value, TSeparator separator, U8SplitOptions options)
704
        {
705
            _value = value._value;
×
706
            _separator = separator;
×
707
            _options = options;
×
708
            _remaining = value._inner;
×
709
        }
×
710

711
        public readonly U8String Current => new(_value, _current.Offset, _current.Length);
×
712

713
        // TODO: Not most efficient but it works for now
714
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
715
        public bool MoveNext()
716
        {
717
        Next:
718
            var remaining = _remaining;
×
719
            if (remaining.Length > 0)
×
720
            {
721
                var scalar = U8Scalar.Create(_separator);
×
722
                var value = _value!.SliceUnsafe(remaining.Offset, remaining.Length);
×
723
                var index = U8Searching.IndexOf(value, scalar);
×
724
                if (index >= 0)
×
725
                {
726
                    _current = (_options & U8SplitOptions.Trim) != U8SplitOptions.Trim
×
727
                        ? new(remaining.Offset, index)
×
728
                        : TrimEntry(_value!, new(remaining.Offset, index));
×
729
                    _remaining = new(
×
730
                        remaining.Offset + index + scalar.Size,
×
731
                        remaining.Length - index - scalar.Size);
×
732
                }
733
                else
734
                {
735
                    _current = (_options & U8SplitOptions.Trim) != U8SplitOptions.Trim
×
736
                        ? remaining
×
737
                        : TrimEntry(_value!, remaining);
×
738
                    _remaining = default;
×
739
                }
740

741
                if ((_options & U8SplitOptions.RemoveEmpty) is U8SplitOptions.RemoveEmpty
×
742
                    && _current.Length is 0)
×
743
                {
744
                    goto Next;
745
                }
746

747
                return true;
×
748
            }
749

750
            return false;
×
751
        }
752

753
        private static U8Range TrimEntry(byte[] value, U8Range range)
754
        {
755
            // This could have been done better but works for now.
756
            return new U8String(value, range).Trim()._inner;
×
757
        }
758

759
        readonly object IEnumerator.Current => Current;
×
760
        readonly void IEnumerator.Reset() => throw new NotSupportedException();
×
761
        readonly void IDisposable.Dispose() { }
×
762
    }
763
}
764

765
// Unfortunately, because ref structs cannot be used as generic type arguments,
766
// we have to resort to duplicating the implementation and exposing methods manually.
767
// What's worse, ConfiguredU8RefSplit has to be duplicated as well. Things we do for performance...
768
// TODO: Implement this
769
// TODO 2: Eventually, remove duplicate code once ref structs can be used as generics.
770
public readonly ref struct U8RefSplit
771
{
772
    readonly U8String _value;
773
    readonly ReadOnlySpan<byte> _separator;
774

775
    public U8RefSplit(U8String value, ReadOnlySpan<byte> separator)
776
    {
777
        _value = value;
×
778
        _separator = separator;
×
779
    }
×
780

781
    public bool Contains(U8String item)
782
    {
783
        return U8Searching.SplitContains(_value, _separator, item);
×
784
    }
785

786
    public void CopyTo(U8String[] array, int index)
787
    {
788
        var span = array.AsSpan()[index..];
×
789
        var split = this;
×
790
        if (split._value.Length > 0)
×
791
        {
792
            var count = U8Searching.Count(
×
793
                split._value.UnsafeSpan,
×
794
                split._separator) + 1;
×
795
            span = span[..count];
×
796

797
            var i = 0;
×
798
            ref var dst = ref span.AsRef();
×
799
            foreach (var item in split)
×
800
            {
801
                dst.Add(i++) = item;
×
802
            }
803
        }
804
    }
×
805

806
    public U8String[] ToArray()
807
    {
808
        var split = this;
×
809
        if (split._value.Length > 0)
×
810
        {
811
            var count = U8Searching.Count(
×
812
                split._value.UnsafeSpan,
×
813
                split._separator) + 1;
×
814

815
            var result = new U8String[count];
×
816
            var span = result.AsSpan();
×
817

818
            var i = 0;
×
819
            ref var dst = ref span.AsRef();
×
820
            foreach (var item in split)
×
821
            {
822
                dst.Add(i++) = item;
×
823
            }
824

825
            return result;
×
826
        }
827

828
        return Array.Empty<U8String>();
×
829
    }
830

831
    public List<U8String> ToList()
832
    {
833
        var split = this;
×
834
        if (split._value.Length > 0)
×
835
        {
836
            var count = U8Searching.Count(
×
837
                split._value.UnsafeSpan,
×
838
                split._separator) + 1;
×
839

840
            var result = new List<U8String>(count);
×
841
            CollectionsMarshal.SetCount(result, count);
×
842
            var span = CollectionsMarshal.AsSpan(result);
×
843

844
            var i = 0;
×
845
            ref var dst = ref span.AsRef();
×
846
            foreach (var item in split)
×
847
            {
848
                dst.Add(i++) = item;
×
849
            }
850

851
            return result;
×
852
        }
853

854
        return new List<U8String>();
×
855
    }
856

857
    public readonly Enumerator GetEnumerator() => new(_value, _separator);
×
858

859
    public ref struct Enumerator
860
    {
861
        readonly byte[]? _value;
862
        readonly ReadOnlySpan<byte> _separator;
863
        U8Range _current;
864
        U8Range _remaining;
865

866
        internal Enumerator(U8String value, ReadOnlySpan<byte> separator)
867
        {
868
            _value = value._value;
×
869
            _separator = separator;
×
870
            _remaining = value._inner;
×
871
        }
×
872

873
        public readonly U8String Current
874
        {
875
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
876
            get => new(_value, _current.Offset, _current.Length);
×
877
        }
878

879
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
880
        public bool MoveNext()
881
        {
882
            var remaining = _remaining;
×
883
            if (remaining.Length > 0)
×
884
            {
885
                var value = _value!.SliceUnsafe(remaining.Offset, remaining.Length);
×
886
                var index = value.IndexOf(_separator);
×
887
                if (index >= 0)
×
888
                {
889
                    _current = new(remaining.Offset, index);
×
890
                    _remaining = new(
×
891
                        remaining.Offset + index + _separator.Length,
×
892
                        remaining.Length - index - _separator.Length);
×
893
                }
894
                else
895
                {
896
                    _current = remaining;
×
897
                    _remaining = default;
×
898
                }
899

900
                return true;
×
901
            }
902

903
            return false;
×
904
        }
905
    }
906
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc