• 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/Implementations/Standard/U8String.Comparison.cs
1
using System.Collections.Immutable;
2
using System.Globalization;
3
using System.IO.Hashing;
4
using System.Runtime.InteropServices;
5

6
namespace U8Primitives;
7

8
public readonly partial struct U8String
9
{
10
    /// <summary>
11
    /// Compares two <see cref="U8String"/> instances using lexicographical semantics and returns
12
    /// an integer that indicates whether the first instance precedes, follows, or occurs in the same
13
    /// position in the sort order as the second instance.
14
    /// </summary>
15
    public int CompareTo(U8String other)
16
    {
17
        return U8Comparison.Ordinal.Compare(this, other);
×
18
    }
19

20
    public int CompareTo(U8String? other)
21
    {
22
        return other.HasValue ? CompareTo(other.Value) : 1;
×
23
    }
24

25
    public int CompareTo(byte[]? other)
26
    {
27
        if (other != null)
×
28
        {
29
            return CompareTo(new U8String(other, 0, other.Length));
×
30
        }
31

32
        return 1;
×
33
    }
34

35
    public int CompareTo<T>(U8String other, T comparer)
36
        where T : IComparer<U8String>
37
    {
38
        return comparer.Compare(this, other);
×
39
    }
40

41
    /// <summary>
42
    /// Indicates whether the current <see cref="U8String"/> instance is equal to another
43
    /// object of <see cref="U8String"/> or <see cref="byte"/> array.
44
    /// </summary>
45
    public override bool Equals(object? obj)
46
    {
47
        return obj switch
×
48
        {
×
49
            U8String other => Equals(other),
×
50
            byte[] other => Equals(other),
×
51
            _ => false,
×
52
        };
×
53
    }
54

55
    public bool Equals(U8String? other)
56
    {
57
        return other.HasValue && Equals(other.Value);
×
58
    }
59

60
    // It seems we really must aggressively inline this. Otherwise, it will always be kept as
61
    // not inlined, having to pay the full price of comparisons on every call, unlike string.
62
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
63
    public bool Equals(U8String other)
64
    {
65
        var deref = this;
×
66
        if (deref.Length == other.Length)
×
67
        {
68
            if (deref.Offset == other.Offset &&
×
69
                ReferenceEquals(deref._value, other._value))
×
70
            {
71
                return true;
×
72
            }
73

74
            return deref.UnsafeSpan.SequenceEqual(other.UnsafeSpan);
×
75
        }
76

77
        return false;
×
78
    }
79

80
    public bool Equals(byte[]? other)
81
    {
82
        return other != null && AsSpan().SequenceEqual(other);
×
83
    }
84

85
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
86
    public bool Equals(Span<byte> other)
87
    {
88
        return AsSpan().SequenceEqual(other);
×
89
    }
90

91
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
92
    public bool Equals(ReadOnlySpan<byte> other)
93
    {
94
        return AsSpan().SequenceEqual(other);
×
95
    }
96

97
    public bool Equals<T>(U8String other, T comparer)
98
        where T : IEqualityComparer<U8String>
99
    {
100
        return comparer.Equals(this, other);
×
101
    }
102

103
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
104
    public bool SourceEquals(U8String other)
105
    {
106
        return ReferenceEquals(_value, other._value);
×
107
    }
108

109
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
110
    public bool SourceEquals(ImmutableArray<byte> other)
111
    {
112
        var arr = ImmutableCollectionsMarshal.AsArray(other);
×
113

114
        return ReferenceEquals(_value, arr);
×
115
    }
116

117
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
118
    public bool SourceEquals(byte[] other)
119
    {
120
        return ReferenceEquals(_value, other);
×
121
    }
122

123
    /// <inheritdoc cref="GetHashCode(ReadOnlySpan{byte})"/>
124
    public override int GetHashCode()
125
    {
126
        return GetHashCode(AsSpan());
×
127
    }
128

129
    /// <summary>
130
    /// Returns a hash code for this instance.
131
    /// </summary>
132
    /// <param name="value">UTF-8 bytes to calculate the hash code for.</param>
133
    /// <remarks>
134
    /// The hash code is calculated using the xxHash3 algorithm.
135
    /// </remarks>
136
    public static int GetHashCode(ReadOnlySpan<byte> value)
137
    {
138
        var hash = XxHash3.HashToUInt64(value, U8Constants.DefaultHashSeed);
×
139

140
        return ((int)hash) ^ (int)(hash >> 32);
×
141
    }
142
}
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