IUtf8String.cs
3.07 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
namespace UniJSON
{
public interface IUtf8String: IEnumerable<Byte>
{
int ByteLength { get; }
}
/// <summary>
/// Immutable short utf8 string
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public struct Utf8String4 : IEquatable<Utf8String4>, IUtf8String
{
[FieldOffset(0)]
uint _value;
[FieldOffset(0)]
byte _byte0;
[FieldOffset(1)]
byte _byte1;
[FieldOffset(2)]
byte _byte2;
[FieldOffset(3)]
byte _byte3;
public int ByteLength
{
get
{
if (_byte0 == 0) return 0;
if (_byte1 == 0) return 1;
if (_byte2 == 0) return 2;
if (_byte3 == 0) return 3;
return 4;
}
}
static Utf8String4 Create(uint value)
{
return new Utf8String4
{
_value = value
};
}
public static Utf8String4 Create(IEnumerable<byte> bytes)
{
var u = new Utf8String4();
var it = bytes.GetEnumerator();
if (!it.MoveNext()) return u;
u._byte0 = it.Current;
if (!it.MoveNext()) return u;
u._byte1 = it.Current;
if (!it.MoveNext()) return u;
u._byte2 = it.Current;
if (!it.MoveNext()) return u;
u._byte3 = it.Current;
if (!it.MoveNext())
{
throw new ArgumentOutOfRangeException();
}
return u;
}
public static Utf8String4 Create(string src)
{
return Create(Utf8String.Encoding.GetBytes(src));
}
public bool Equals(Utf8String4 other)
{
return _value == other._value;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj is Utf8String4)
{
return Equals((Utf8String4)obj);
}
{
var s = obj as string;
if (s != null)
{
return ToString() == s;
}
}
return false;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public override string ToString()
{
return Utf8String.Encoding.GetString(this.ToArray());
}
public IEnumerator<byte> GetEnumerator()
{
if (_byte0 == 0) yield break;
yield return _byte0;
if (_byte1 == 0) yield break;
yield return _byte1;
if (_byte2 == 0) yield break;
yield return _byte2;
if (_byte3 == 0) yield break;
yield return _byte3;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}