-
Notifications
You must be signed in to change notification settings - Fork 2
/
MutableString.cs
240 lines (207 loc) · 7.94 KB
/
MutableString.cs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
namespace Performance
{
public struct MutableString
{
private static int _globalCount = 0;
public MutableString(string s)
{
_string = s;
_capacity = _string.Length;
_lock = new object();
}
public MutableString(int capacity)
{
char[] buffer = new char[capacity];
// set a monotonically increasing value in the newly created buffer to ensure it doesn't get interned
buffer[0] = (char) (_globalCount & 0xffff);
buffer[1] = (char) ((_globalCount >> 16) & 0xffff);
_globalCount++;
_string = new string(buffer);
_capacity = _string.Length;
_lock = new object();
}
private readonly object _lock;
public string String => _string;
// the underlying string object
private string _string { get; }
public int Capacity => _capacity;
private int _capacity;
public int Length => _string.Length;
// implicitly cast from the system string type
public static implicit operator MutableString(string s)
{
return new MutableString(s);
}
// implicitly cast to the system string type
public static implicit operator string(MutableString s)
{
return s.ToString();
}
// SetSubString overwrites a part of the character buffer with a new sequence
// of characters. If optionally updates the length to truncate the string
// at the end of the new sequence
public void SetSubString(int destPos, string src, bool updateLength = false)
{
if (src.Length > Capacity - destPos) throw new ArgumentOutOfRangeException();
unsafe
{
fixed (char* pDest = _string)
fixed (char* pSrc = src)
{
// the chars are 2 bytes wide
Buffer.MemoryCopy(pSrc, &pDest[destPos], Capacity * 2, src.Length * 2);
}
}
if (updateLength) SetLength(destPos + src.Length);
}
// SetString overwrites the character buffer with new characters
// and set the buffer length which has the effect of replacing
// the existing string with the new one
public void SetString(string src)
{
if (src.Length > Length) throw new ArgumentOutOfRangeException();
SetLength(src.Length);
SetSubString(0, src);
}
// Set the string using the contents of a StackBuffer
public void SetStackBuffer(int destPos, StackBuffer buffer)
{
var newLength = destPos + buffer.Count;
if (destPos + buffer.Count > Capacity) throw new ArgumentOutOfRangeException();
SetLength(newLength);
for (var i = destPos; i < newLength; i++) this[i] = buffer[i];
}
// Sets the length of the character buffer
// in the underlying native object
private void SetLength(int newLength)
{
if (newLength > _capacity) throw new ArgumentOutOfRangeException();
unsafe
{
// acquire a mutual exclusion lock
lock (_lock)
{
/*
https://github.com/dotnet/runtime/blob/master/src/coreclr/src/vm/object.h
StringLength is stored immediately before the char buffer
quoted from object.h
* StringObject
*
* Special String implementation for performance.
*
* m_StringLength - Length of string in number of WCHARs
* m_FirstChar - The string buffer
*
class StringObject : public Object
{
DWORD m_StringLength;
WCHAR m_FirstChar;
....
}
*/
fixed (char* charBuffer = _string)
{
var pLength = (int*) charBuffer;
pLength -= 1;
*pLength = newLength;
}
} // release the lock
}
}
public override string ToString()
{
return _string;
}
public bool Contains(string s)
{
return _string.Contains(s);
}
public static bool operator ==(MutableString a, MutableString b)
{
return EqualsHelper(a, b);
}
public static bool operator !=(MutableString a, MutableString b)
{
return !EqualsHelper(a, b);
}
public override bool Equals(Object obj)
{
//Check for null and compare run-time types.
if ((obj == null) || this.GetType() != obj.GetType())
{
return false;
}
else
{
MutableString mutableString = (MutableString) obj;
return EqualsHelper(this, mutableString);
}
}
public override int GetHashCode()
{
return _string.GetHashCode();
}
// this helper function is taken from the C# reference source
// https://referencesource.microsoft.com/#mscorlib/system/string.cs,11648d2d83718c5e,references
private static bool EqualsHelper(MutableString strA, MutableString strB)
{
unsafe
{
var length = strA.Length;
fixed (char* ap = strA.String)
fixed (char* bp = strB.String)
{
var a = ap;
var b = bp;
// unroll the loop
while (length >= 10)
{
if (*(int*) a != *(int*) b) return false;
if (*(int*) (a + 2) != *(int*) (b + 2)) return false;
if (*(int*) (a + 4) != *(int*) (b + 4)) return false;
if (*(int*) (a + 6) != *(int*) (b + 6)) return false;
if (*(int*) (a + 8) != *(int*) (b + 8)) return false;
a += 10;
b += 10;
length -= 10;
}
// This depends on the fact that the String objects are
// always zero terminated and that the terminating zero is not included
// in the length. For odd string sizes, the last compare will include
// the zero terminator.
while (length > 0)
{
if (*(int*) a != *(int*) b) break;
a += 2;
b += 2;
length -= 2;
}
return length <= 0;
}
}
}
public char this[int index]
{
// use the getter from the underlying string object
get => _string[index];
set
{
if (index < 0 || index >= Capacity) throw new IndexOutOfRangeException();
// for the setter we need access to the native char buffer
unsafe
{
// acquire a mutual exclusion lock
lock (_lock)
{
// pin the string's char buffer so we can access it
fixed (char* valueChars = _string)
{
valueChars[index] = value;
}
} // release the lock
}
}
}
}
}