forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringOperationUtil.cs
250 lines (233 loc) · 7.16 KB
/
StringOperationUtil.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
241
242
243
244
245
246
247
248
249
250
using System.Threading;
using System.Text;
// Info: OptimizedStringOperation
// Concatenating many strings allocates a lot of temporary memory in the managed heap. It is recommend to use System.Text.StringBuffer instead.
// Sometimes this is hard to fix, especially if you already have written a lot of code. This solution should help you to work around that problem easily.
// source: https://bitbucket.org/Unity-Technologies/enterprise-support
namespace StringOperationUtil
{
/// <summary>
/// Using this,you can optimize string concat operation easily.
/// To use this , you should put this on the top of code.
/// ------
/// using StrOpe = StringOperationUtil.OptimizedStringOperation;
/// ------
///
/// - before code
/// string str = "aaa" + 20 + "bbbb";
///
/// - after code
/// string str = StrOpe.i + "aaa" + 20 + "bbbb";
///
/// "StrOpe.i" is for MainThread , do not call from other theads.
/// If "StrOpe.i" is called from Mainthread , reuse same object.
///
/// You can also use "StrOpe.small" / "StrOpe.medium" / "StrOpe.large" instead of "StrOpe.i".
/// These are creating instance.
/// </summary>
public class OptimizedStringOperation
{
private static OptimizedStringOperation instance = null;
#if !UNITY_WEBGL
private static Thread singletonThread = null;
#endif
private StringBuilder sb = null;
static OptimizedStringOperation()
{
instance = new OptimizedStringOperation(1024);
}
private OptimizedStringOperation(int capacity)
{
sb = new StringBuilder(capacity);
}
public static OptimizedStringOperation Create(int capacity)
{
return new OptimizedStringOperation(capacity);
}
public static OptimizedStringOperation small
{
get
{
return Create(64);
}
}
public static OptimizedStringOperation medium
{
get
{
return Create(256);
}
}
public static OptimizedStringOperation large
{
get
{
return Create(1024);
}
}
public static OptimizedStringOperation i
{
get
{
#if !UNITY_WEBGL
// Bind instance to thread.
if (singletonThread == null )
{
singletonThread = Thread.CurrentThread;
}
// check thread...
if (singletonThread != Thread.CurrentThread)
{
#if DEBUG || UNITY_EDITOR
UnityEngine.Debug.LogError("Execute from another thread.");
#endif
return small;
}
#endif
instance.sb.Length = 0;
return instance;
}
}
public int Capacity
{
set { this.sb.Capacity = value; }
get { return sb.Capacity; }
}
public int Length
{
set { this.sb.Length = value; }
get { return this.sb.Length; }
}
public OptimizedStringOperation Remove(int startIndex, int length)
{
sb.Remove(startIndex, length);
return this;
}
public OptimizedStringOperation Replace(string oldValue, string newValue)
{
sb.Replace(oldValue, newValue);
return this;
}
public override string ToString()
{
return sb.ToString();
}
public void Clear()
{
// StringBuilder.Clear() doesn't support .Net 3.5...
// "Capasity = 0" doesn't work....
sb = new StringBuilder(0);
}
public OptimizedStringOperation ToLower()
{
int length = sb.Length;
for (int i = 0; i < length; ++i)
{
if (char.IsUpper(sb[i]))
{
sb.Replace(sb[i], char.ToLower(sb[i]), i, 1);
}
}
return this;
}
public OptimizedStringOperation ToUpper()
{
int length = sb.Length;
for (int i = 0; i < length; ++i)
{
if (char.IsLower(sb[i]))
{
sb.Replace(sb[i], char.ToUpper(sb[i]), i, 1);
}
}
return this;
}
public OptimizedStringOperation Trim()
{
return TrimEnd().TrimStart();
}
public OptimizedStringOperation TrimStart()
{
int length = sb.Length;
for (int i = 0; i < length; ++i)
{
if (!char.IsWhiteSpace(sb[i]))
{
if (i > 0)
{
sb.Remove(0, i);
}
break;
}
}
return this;
}
public OptimizedStringOperation TrimEnd()
{
int length = sb.Length;
for (int i = length - 1; i >= 0; --i)
{
if (!char.IsWhiteSpace(sb[i]))
{
if (i < length - 1)
{
sb.Remove(i, length - i);
}
break;
}
}
return this;
}
public static implicit operator string(OptimizedStringOperation t)
{
return t.ToString();
}
#region ADD_OPERATOR
public static OptimizedStringOperation operator +(OptimizedStringOperation t, bool v)
{
t.sb.Append(v);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, int v)
{
t.sb.Append(v);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, short v)
{
t.sb.Append(v);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, byte v)
{
t.sb.Append(v);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, float v)
{
t.sb.Append(v);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, char c)
{
t.sb.Append(c);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, char[] c)
{
t.sb.Append(c);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, string str)
{
t.sb.Append(str);
return t;
}
public static OptimizedStringOperation operator +(OptimizedStringOperation t, StringBuilder sb)
{
t.sb.Append(sb);
return t;
}
#endregion ADD_OPERATOR
}
}