-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringBuilderLOH.cs
107 lines (90 loc) · 3.89 KB
/
StringBuilderLOH.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
// https://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs
// ... skipped ...
#define APPEND_HELPER_FIX
namespace System.Text {
using System;
using System.Text;
using System.Runtime;
using System.Runtime.Serialization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Threading;
using System.Globalization;
using System.Diagnostics.Contracts;
// This class represents a mutable string. It is convenient for situations in
// which it is desirable to modify a string, perhaps by removing, replacing, or
// inserting characters, without creating a new String subsequent to
// each modification.
//
// The methods contained within this class do not return a new StringBuilder
// object unless specified otherwise. This class may be used in conjunction with the String
// class to carry out modifications upon strings.
//
// When passing null into a constructor in VJ and VC, the null
// should be explicitly type cast.
// For Example:
// StringBuilder sb1 = new StringBuilder((StringBuilder)null);
// StringBuilder sb2 = new StringBuilder((String)null);
// Console.WriteLine(sb1);
// Console.WriteLine(sb2);
//
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]
public sealed class StringBuilder : ISerializable {
// ... skipped ...
// A StringBuilder is internally represented as a linked list of blocks each of which holds
// a chunk of the string. It turns out string as a whole can also be represented as just a chunk,
// so that is what we do.
// We want to keep chunk arrays out of large object heap (< 85K bytes ~ 40K chars) to be sure.
// Making the maximum chunk size big means less allocation code called, but also more waste
// in unused characters and slower inserts / replaces (since you do need to slide characters over
// within a buffer).
internal const int MaxChunkSize = 8000;
// ... skipped ...
#if APPEND_HELPER_FIX
// We put this fixed in its own helper to avoid the cost zero initing valueChars in the
// case we don't actually use it.
[System.Security.SecuritySafeCritical] // auto-generated
private void AppendHelper(string value) {
unsafe {
fixed (char* valueChars = value)
{
if (value.Length <= MaxChunkSize)
{
Append(valueChars, value.Length);
}
else
{
//
// do it in multiple loops
//
int numOfChunks = value.Length / MaxChunkSize;
int remainder = value.Length % MaxChunkSize;
for (int i = 0; i < numOfChunks; ++i)
{
Append(valueChars + (i * MaxChunkSize), MaxChunkSize);
}
if (remainder > 0)
{
Append(valueChars + value.Length - remainder, remainder);
}
}
}
}
}
#else
// We put this fixed in its own helper to avoid the cost zero initing valueChars in the
// case we don't actually use it.
[System.Security.SecuritySafeCritical] // auto-generated
private void AppendHelper(string value) {
unsafe {
fixed (char* valueChars = value)
Append(valueChars, value.Length);
}
}
#endif
// ... skipped ...
}
}