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

neon-sunset / U8String / 5909906392

19 Aug 2023 06:18AM UTC coverage: 22.422% (+0.7%) from 21.682%
5909906392

push

github

neon-sunset
fix+docs: fix tests build and update TODO

120 of 772 branches covered (15.54%)

Branch coverage included in aggregate %.

50 of 50 new or added lines in 1 file covered. (100.0%)

428 of 1672 relevant lines covered (25.6%)

30558.84 hits per line

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

0.0
/src/U8String.Comparison.cs
1
using System.Collections.Immutable;
2
using System.IO.Hashing;
3
using System.Runtime.InteropServices;
4

5
namespace U8Primitives;
6

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

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

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

31
        return 1;
×
32
    }
33

34
    /// <summary>
35
    /// Indicates whether the current <see cref="U8String"/> instance is equal to another
36
    /// object of <see cref="U8String"/> or <see cref="byte"/> array.
37
    /// </summary>
38
    public override bool Equals(object? obj)
39
    {
40
        return obj switch
×
41
        {
×
42
            U8String other => Equals(other),
×
43
            byte[] other => Equals(other),
×
44
            _ => false,
×
45
        };
×
46
    }
47

48
    public bool Equals(U8String? other)
49
    {
50
        return other.HasValue && Equals(other.Value);
×
51
    }
52

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

67
            return deref.UnsafeSpan.SequenceEqual(other.UnsafeSpan);
×
68
        }
69

70
        return false;
×
71
    }
72

73
    public bool Equals(byte[]? other)
74
    {
75
        return other != null && AsSpan().SequenceEqual(other);
×
76
    }
77

78
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
79
    public bool Equals(Span<byte> other)
80
    {
81
        return AsSpan().SequenceEqual(other);
×
82
    }
83

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

90
    public bool Equals(U8String other, U8Comparison comparisonType)
91
    {
92
        return comparisonType switch
×
93
        {
×
94
            U8Comparison.Ordinal => U8Comparer.Ordinal.Equals(this, other),
×
95
            U8Comparison.AsciiIgnoreCase => U8Comparer.AsciiIgnoreCase.Equals(this, other),
×
96
            _ => ThrowHelpers.ArgumentOutOfRange<bool>(nameof(comparisonType)),
×
97
        };
×
98
    }
99

100
    public bool SourceEquals(U8String other)
101
    {
102
        return ReferenceEquals(_value, other._value);
×
103
    }
104

105
    public bool SourceEquals(ImmutableArray<byte> other)
106
    {
107
        var arr = ImmutableCollectionsMarshal.AsArray(other);
×
108

109
        return ReferenceEquals(_value, arr);
×
110
    }
111

112
    public bool SourceEquals(byte[] other)
113
    {
114
        return ReferenceEquals(_value, other);
×
115
    }
116

117
    /// <summary>
118
    /// Returns a hash code for this instance.
119
    /// </summary>
120
    /// <remarks>
121
    /// The hash code is calculated using the xxHash32 algorithm.
122
    /// </remarks>
123
    public override int GetHashCode()
124
    {
125
        // TODO: Consider non-default seed?
126
        var hash = XxHash3.HashToUInt64(this, U8Constants.DefaultHashSeed);
×
127

128
        return ((int)hash) ^ (int)(hash >> 32);
×
129
    }
130
}
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