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

neon-sunset / U8String / 6005208900

28 Aug 2023 09:36PM UTC coverage: 18.096% (-0.2%) from 18.326%
6005208900

push

github

neon-sunset
feat: Extend NativeU8String and restructure solution to account for increased line count, add roadmap draft

134 of 1050 branches covered (0.0%)

Branch coverage included in aggregate %.

1058 of 1058 new or added lines in 25 files covered. (100.0%)

478 of 2332 relevant lines covered (20.5%)

35305.41 hits per line

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

0.0
/src/NativeU8String.cs
1
using System.ComponentModel;
2
using System.Runtime.InteropServices;
3
using System.Text;
4

5
namespace U8Primitives; // .InteropServices?
6

7
// The best is yet to come :^)
8
// Q: store a bit in _length to indicate if it's null-terminated?
9
// Is it viable to do some tagged pointer shenanigans?
10
// Q: Consider some clever WeakReference handler with dealloc callback? What kind of object should I use for root?
11
// Q: UnmanagedU8String? How does CString look like?
12
// Q: NativeU8String<TAlloc>?
13
// Q: Always null-terminated?
14
// TODO: Double-check to ensure zero-extension on all casts and math (after all, _length must never be negative)
15
// TODO: Sanity of handling free(_ptr) seems to necessitate introducing U8Slice or U8Span after all...this is because
16
// _ptr must remain pointing to a start of the original allocation. Seriously consider Rust-like approach where
17
// 1) the main type is a string slice and 2) it can be implicitly converted to and implements the majority of the API surface,
18
// so worst case it is possible to just e.g. U8String[..].Split(...) or U8String.AsSpan/Slice().Split(...).
19
// Alternatively, this can be handled through generic extension methods but that would make it incompatible with ref structs.
20
// Another option: refactor abstractions into trait-like interfaces and have IUncheckedSliceable<TSelf>, ISpanConvertible<TElement>, etc.
21
#pragma warning disable IDE0032, RCS1085 // Use auto-implemented property. Why: readable layout
22
public unsafe readonly partial struct NativeU8String : IDisposable
23
{
24
    public static NativeU8String Empty => default;
×
25

26
    readonly byte* _ptr;
27
    readonly nint _length;
28

29
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
30
    internal NativeU8String(byte* ptr, nint length)
31
    {
32
        _ptr = ptr;
×
33
        _length = length;
×
34
    }
×
35

36
    public nint Length => _length;
×
37

38
    internal Span<byte> UnsafeSpan
39
    {
40
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
41
        get => MemoryMarshal.CreateSpan(ref _ptr[0], (int)_length);
×
42
    }
43

44
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
45
    public ReadOnlySpan<byte> AsSpan()
46
    {
47
        return MemoryMarshal.CreateReadOnlySpan(ref _ptr[0], (int)_length);
×
48
    }
49

50
    [EditorBrowsable(EditorBrowsableState.Never)]
51
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
52
    public ref readonly byte GetPinnableReference()
53
    {
54
        return ref _ptr[0];
×
55
    }
56

57
    public override string ToString()
58
    {
59
        return Encoding.UTF8.GetString(this);
×
60
    }
61

62
    public void Dispose()
63
    {
64
        if (_ptr != null) NativeMemory.Free(_ptr);
×
65
    }
×
66

67
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
68
    public static implicit operator ReadOnlySpan<byte>(NativeU8String str) => str.AsSpan();
×
69
}
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