Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Utf8String.cs « Text « Internal « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b44102c582c5aef69790e3d5afbfe239759ba62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;

namespace Internal.Text
{
    public struct Utf8String : IEquatable<Utf8String>, IComparable<Utf8String>
    {
        private byte[] _value;

        public Utf8String(byte[] underlyingArray)
        {
            _value = underlyingArray;
        }

        public Utf8String(string s)
        {
            _value = Encoding.UTF8.GetBytes(s);
        }

        // TODO: This should return ReadOnlySpan<byte> instead once available
        public byte[] UnderlyingArray => _value;
        public int Length => _value.Length;

        // For now, define implicit conversions between string and Utf8String to aid the transition
        // These conversions will be removed eventually
        public static implicit operator Utf8String(string s)
        {
            return new Utf8String(s);
        }

        public override string ToString()
        {
            return Encoding.UTF8.GetString(_value);
        }

        public override bool Equals(object obj)
        {
            return (obj is Utf8String) && Equals((Utf8String)obj);
        }

        public unsafe override int GetHashCode()
        {
            int length = _value.Length;
            uint hash = (uint)length;
            fixed (byte* ap = _value)
            {
                byte* a = ap;

                while (length >= 4)
                {
                    hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *(uint*)a;
                    a += 4; length -= 4;
                }
                if (length >= 2)
                {
                    hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *(ushort*)a;
                    a += 2; length -= 2;
                }
                if (length > 0)
                {
                    hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *a;
                }
                hash += BitOperations.RotateLeft(hash, 7);
                hash += BitOperations.RotateLeft(hash, 15);
                return (int)hash;
            }
        }

        public bool Equals(Utf8String other)
        {
            int length = _value.Length;
            if (length != other.Length)
                return false;

            if (_value == other._value)
                return true;

            unsafe
            {
                fixed (byte* ap = _value) fixed (byte* bp = other._value)
                {
                    byte* a = ap;
                    byte* b = bp;

                    while (length >= 4)
                    {
                        if (*(int*)a != *(int*)b) return false;
                        a += 4; b += 4; length -= 4;
                    }
                    if (length >= 2)
                    {
                        if (*(short*)a != *(short*)b) return false;
                        a += 2; b += 2; length -= 2;
                    }
                    if (length > 0)
                    {
                        if (*a != *b) return false;
                    }
                    return true;
                }
            }
        }

        private static int Compare(Utf8String strA, Utf8String strB)
        {
            int length = Math.Min(strA.Length, strB.Length);

            unsafe
            {
                fixed (byte* ap = strA._value)
                fixed (byte* bp = strB._value)
                {
                    byte* a = ap;
                    byte* b = bp;

                    while (length > 0)
                    {
                        if (*a != *b)
                            return *a - *b;
                        a += 1;
                        b += 1;
                        length -= 1;
                    }

                    // At this point, we have compared all the characters in at least one string.
                    // The longer string will be larger.
                    // We could optimize and compare lengths before iterating strings, but we want
                    // Foo and Foo1 to be sorted adjacent to eachother.
                    return strA.Length - strB.Length;
                }
            }
        }

        public int CompareTo(Utf8String other)
        {
            return Compare(this, other);
        }
    }
}