forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringBuilder.java
579 lines (489 loc) · 22.9 KB
/
StringBuilder.java
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang;
import jdk.internal.HotSpotIntrinsicCandidate;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* A mutable sequence of characters. This class provides an API compatible
* with {@code StringBuffer}, but with no guarantee of synchronization.
* This class is designed for use as a drop-in replacement for
* {@code StringBuffer} in places where the string buffer was being
* used by a single thread (as is generally the case). Where possible,
* it is recommended that this class be used in preference to
* {@code StringBuffer} as it will be faster under most implementations.
*
* <p>The principal operations on a {@code StringBuilder} are the
* {@code append} and {@code insert} methods, which are
* overloaded so as to accept data of any type. Each effectively
* converts a given datum to a string and then appends or inserts the
* characters of that string to the string builder. The
* {@code append} method always adds these characters at the end
* of the builder; the {@code insert} method adds the characters at
* a specified point.
* <p>
* For example, if {@code z} refers to a string builder object
* whose current contents are "{@code start}", then
* the method call {@code z.append("le")} would cause the string
* builder to contain "{@code startle}", whereas
* {@code z.insert(4, "le")} would alter the string builder to
* contain "{@code starlet}".
* <p>
* In general, if sb refers to an instance of a {@code StringBuilder},
* then {@code sb.append(x)} has the same effect as
* {@code sb.insert(sb.length(), x)}.
* <p>
* Every string builder has a capacity. As long as the length of the
* character sequence contained in the string builder does not exceed
* the capacity, it is not necessary to allocate a new internal
* buffer. If the internal buffer overflows, it is automatically made larger.
*
* <p>Instances of {@code StringBuilder} are not safe for
* use by multiple threads. If such synchronization is required then it is
* recommended that {@link java.lang.StringBuffer} be used.
*
* <p>Unless otherwise noted, passing a {@code null} argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
*
* @author Michael McCloskey
* @apiNote {@code StringBuilder} implements {@code Comparable} but does not override
* {@link Object#equals equals}. Thus, the natural ordering of {@code StringBuilder}
* is inconsistent with equals. Care should be exercised if {@code StringBuilder}
* objects are used as keys in a {@code SortedMap} or elements in a {@code SortedSet}.
* See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
* {@link java.util.SortedSet SortedSet} for more information.
* @see java.lang.StringBuffer
* @see java.lang.String
* @since 1.5
*/
// 非线程安全的字符序列,适合单线程下操作大量字符,内部实现为字节数组
public final class StringBuilder extends AbstractStringBuilder implements Serializable, Comparable<StringBuilder>, CharSequence {
/** use serialVersionUID for interoperability */
static final long serialVersionUID = 4383685877147921099L;
/*▼ 构造方法 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Constructs a string builder with no characters in it and an
* initial capacity of 16 characters.
*/
@HotSpotIntrinsicCandidate
public StringBuilder() {
super(16);
}
/**
* Constructs a string builder with no characters in it and an
* initial capacity specified by the {@code capacity} argument.
*
* @param capacity the initial capacity.
*
* @throws NegativeArraySizeException if the {@code capacity} argument is less than {@code 0}.
*/
@HotSpotIntrinsicCandidate
public StringBuilder(int capacity) {
super(capacity);
}
/**
* Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* {@code 16} plus the length of the string argument.
*
* @param str the initial contents of the buffer.
*/
@HotSpotIntrinsicCandidate
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
/**
* Constructs a string builder that contains the same characters
* as the specified {@code CharSequence}. The initial capacity of
* the string builder is {@code 16} plus the length of the
* {@code CharSequence} argument.
*
* @param seq the sequence to copy.
*/
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
/*▲ 构造方法 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 添加 ████████████████████████████████████████████████████████████████████████████████┓ */
// 向StringBuilder末尾添加一个字符序列
@Override
public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
// 向StringBuilder末尾添加一个字符序列,该子序列取自字符序列s的[start, end)范围
@Override
public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
// 向StringBuilder末尾添加一个字符串str
@Override
@HotSpotIntrinsicCandidate
public StringBuilder append(String str) {
super.append(str);
return this;
}
// 向StringBuilder末尾添加一个StringBuffer
/**
* Appends the specified {@code StringBuffer} to this sequence.
* <p>
* The characters of the {@code StringBuffer} argument are appended,
* in order, to this sequence, increasing the
* length of this sequence by the length of the argument.
* If {@code sb} is {@code null}, then the four characters
* {@code "null"} are appended to this sequence.
* <p>
* Let <i>n</i> be the length of this character sequence just prior to
* execution of the {@code append} method. Then the character at index
* <i>k</i> in the new character sequence is equal to the character at
* index <i>k</i> in the old character sequence, if <i>k</i> is less than
* <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
* in the argument {@code sb}.
*
* @param sb the {@code StringBuffer} to append.
*
* @return a reference to this object.
*/
public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
// 向StringBuilder末尾添加一个字符序列
@Override
public StringBuilder append(char[] str) {
super.append(str);
return this;
}
// 向StringBuilder末尾添加一个子序列,该子序列取自字符数组s的[offset, offset+len)范围
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder append(char[] str, int offset, int len) {
super.append(str, offset, len);
return this;
}
// 向StringBuilder末尾添加一个Object值的字符串序列
@Override
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
// 向StringBuilder末尾添加一个boolean值的字符串序列
@Override
public StringBuilder append(boolean b) {
super.append(b);
return this;
}
// 向StringBuilder末尾添加一个char值的字符串序列
@Override
@HotSpotIntrinsicCandidate
public StringBuilder append(char c) {
super.append(c);
return this;
}
// 向StringBuilder末尾添加一个int值的字符串序列
@Override
@HotSpotIntrinsicCandidate
public StringBuilder append(int i) {
super.append(i);
return this;
}
// 向StringBuilder末尾添加一个long值的字符串序列
@Override
public StringBuilder append(long l) {
super.append(l);
return this;
}
// 向StringBuilder末尾添加一个float值的字符串序列
@Override
public StringBuilder append(float f) {
super.append(f);
return this;
}
// 向StringBuilder末尾添加一个double值的字符串序列
@Override
public StringBuilder append(double d) {
super.append(d);
return this;
}
// 向StringBuilder末尾添加一个由Unicode码点值表示的char的字符串序列
/**
* @since 1.5
*/
@Override
public StringBuilder appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
}
/*▲ 添加 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 删除 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
// 删除[start, end)范围内的char
@Override
public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
// 删除索引为index的char
@Override
public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}
/*▲ 删除 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 插入 ████████████████████████████████████████████████████████████████████████████████┓ */
// 向StringBuilder的dstOffset索引处插入一个子序列s
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int dstOffset, CharSequence s) {
super.insert(dstOffset, s);
return this;
}
// 向StringBuilder的dstOffset索引处插入一个子序列,该子序列取自字符序列s的[start, end)范围
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end) {
super.insert(dstOffset, s, start, end);
return this;
}
// 向StringBuilder的offset索引处插入一个子符序列str
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, char[] str) {
super.insert(offset, str);
return this;
}
// 向StringBuilder的index索引处插入一个子序列,该子序列取自字符序列str的[offset, offset+len)范围
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int index, char[] str, int offset, int len) {
super.insert(index, str, offset, len);
return this;
}
// 向StringBuilder的offset索引处插入一个字符串str
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, String str) {
super.insert(offset, str);
return this;
}
// 向StringBuilder的offset索引处插入一个Object值的字符串序列
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, Object obj) {
super.insert(offset, obj);
return this;
}
// 向StringBuilder的offset索引处插入一个boolean值的字符串序列
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, boolean b) {
super.insert(offset, b);
return this;
}
// 向StringBuilder的offset索引处插入一个char值的字符串序列
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, char c) {
super.insert(offset, c);
return this;
}
// 向StringBuilder的offset索引处插入一个int值的字符串序列
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, int i) {
super.insert(offset, i);
return this;
}
// 向StringBuilder的offset索引处插入一个long值的字符串序列
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, long l) {
super.insert(offset, l);
return this;
}
// 向StringBuilder的offset索引处插入一个float值的字符串序列
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, float f) {
super.insert(offset, f);
return this;
}
// 向StringBuilder的offset索引处插入一个double值的字符串序列
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, double d) {
super.insert(offset, d);
return this;
}
/*▲ 插入 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 替换 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
// 用str替换StringBuilder在[start, end)范围内的字符序列
@Override
public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}
/*▲ 替换 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 求子串 ████████████████████████████████████████████████████████████████████████████████┓ */
/*▲ 求子串 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 查找子串位置 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回子串str在当前主串StringBuilder中第一次出现的位置
@Override
public int indexOf(String str) {
return super.indexOf(str);
}
// 返回子串str在当前主串StringBuilder中第一次出现的位置(从主串fromIndex处向后搜索)
@Override
public int indexOf(String str, int fromIndex) {
return super.indexOf(str, fromIndex);
}
// 返回子串str在当前主串StringBuilder中最后一次出现的位置
@Override
public int lastIndexOf(String str) {
return super.lastIndexOf(str);
}
// 返回子串str在当前主串StringBuilder中最后一次出现的位置(从主串fromIndex处向前搜索)
@Override
public int lastIndexOf(String str, int fromIndex) {
return super.lastIndexOf(str, fromIndex);
}
/*▲ 查找子串位置 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 逆置 ████████████████████████████████████████████████████████████████████████████████┓ */
// 逆置StringBuilder
@Override
public StringBuilder reverse() {
super.reverse();
return this;
}
/*▲ 逆置 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 比较 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Compares two {@code StringBuilder} instances lexicographically. This method
* follows the same rules for lexicographical comparison as defined in the
* {@linkplain java.lang.CharSequence#compare(java.lang.CharSequence,
* java.lang.CharSequence) CharSequence.compare(this, another)} method.
*
* <p>
* For finer-grained, locale-sensitive String comparison, refer to
* {@link java.text.Collator}.
*
* @param another the {@code StringBuilder} to be compared with
*
* @return the value {@code 0} if this {@code StringBuilder} contains the same
* character sequence as that of the argument {@code StringBuilder}; a negative integer
* if this {@code StringBuilder} is lexicographically less than the
* {@code StringBuilder} argument; or a positive integer if this {@code StringBuilder}
* is lexicographically greater than the {@code StringBuilder} argument.
*
* @since 11
*/
// 比较两个StringBuilder的内容
@Override
public int compareTo(StringBuilder another) {
return super.compareTo(another);
}
/*▲ 比较 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 序列化 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Save the state of the {@code StringBuilder} instance to a stream
* (that is, serialize it).
*
* @serialData the number of characters currently stored in the string
* builder ({@code int}), followed by the characters in the
* string builder ({@code char[]}). The length of the
* {@code char} array may be greater than the number of
* characters currently stored in the string builder, in which
* case extra characters are ignored.
*/
private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
s.writeInt(count);
char[] val = new char[capacity()];
if(isLatin1()) {
StringLatin1.getChars(value, 0, count, val, 0);
} else {
StringUTF16.getChars(value, 0, count, val, 0);
}
s.writeObject(val);
}
/**
* readObject is called to restore the state of the StringBuffer from a stream.
*/
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
char[] val = (char[]) s.readObject();
initBytes(val, 0, val.length);
}
/*▲ 序列化 ████████████████████████████████████████████████████████████████████████████████┛ */
@Override
@HotSpotIntrinsicCandidate
public String toString() {
// Create a copy, don't share the array
return isLatin1() ? StringLatin1.newString(value, 0, count) : StringUTF16.newString(value, 0, count);
}
}