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

neon-sunset / U8String / 5922049812

21 Aug 2023 04:39AM UTC coverage: 20.136% (-1.6%) from 21.736%
5922049812

push

github

neon-sunset
feat: work in progress - comparers/globalization, planning, splitters and native string prototype scaffolding

122 of 856 branches covered (14.25%)

Branch coverage included in aggregate %.

188 of 188 new or added lines in 9 files covered. (100.0%)

439 of 1930 relevant lines covered (22.75%)

26474.14 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.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 U8Comparer.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
    /// <summary>
124
    /// Returns a hash code for this instance.
125
    /// </summary>
126
    /// <remarks>
127
    /// The hash code is calculated using the xxHash32 algorithm.
128
    /// </remarks>
129
    public override int GetHashCode()
130
    {
131
        var hash = XxHash3.HashToUInt64(this, U8Constants.DefaultHashSeed);
×
132

133
        return ((int)hash) ^ (int)(hash >> 32);
×
134
    }
135
}
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