-
Notifications
You must be signed in to change notification settings - Fork 962
/
AbstractStringBuilder.m
873 lines (797 loc) · 31.2 KB
/
AbstractStringBuilder.m
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "java/lang/AbstractStringBuilder.h"
#include "J2ObjC_source.h"
#include "java/io/InvalidObjectException.h"
#include "java/lang/ArrayIndexOutOfBoundsException.h"
#include "java/lang/Character.h"
#include "java/lang/Integer.h"
#include "java/lang/Long.h"
#include "java/lang/NegativeArraySizeException.h"
#include "java/lang/NullPointerException.h"
#include "java/lang/OutOfMemoryError.h"
#include "java/lang/StringIndexOutOfBoundsException.h"
#include "java/util/Arrays.h"
#include "java/util/stream/IntStream.h"
#include "java_lang_Integer.h"
#include "java_lang_Long.h"
#include "libcore/util/EmptyArray.h"
#include "sun/misc/FloatingDecimal.h"
// Full name as expected by generated metadata.
#define JavaLangAbstractStringBuilder_INITIAL_CAPACITY 16
// Shorter alias for convenience.
#define INITIAL_CAPACITY JavaLangAbstractStringBuilder_INITIAL_CAPACITY
static void JreStringBuilder_move(JreStringBuilder *self, jint size, jint index);
static void OutOfMemory() {
@throw [[[JavaLangOutOfMemoryError alloc] init] autorelease];
}
static inline void NewBuffer(JreStringBuilder *sb, jint size) {
sb->buffer_ = (jchar *)malloc(size * sizeof(jchar));
if (__builtin_expect(sb->buffer_ == NULL, 0)) {
OutOfMemory();
}
sb->bufferSize_ = size;
}
static JavaLangStringIndexOutOfBoundsException *IndexAndLength(JreStringBuilder *sb, jint index) {
@throw [[[JavaLangStringIndexOutOfBoundsException alloc]
initWithInt:sb->count_ withInt:index] autorelease];
}
static JavaLangStringIndexOutOfBoundsException *StartEndAndLength(
JreStringBuilder *sb, jint start, jint end) {
@throw [[[JavaLangStringIndexOutOfBoundsException alloc]
initWithInt:sb->count_ withInt:start withInt:end - start] autorelease];
}
@implementation JavaLangAbstractStringBuilder
- (IOSCharArray *)getValue {
return [IOSCharArray arrayWithChars:delegate_.buffer_ count:delegate_.count_];
}
- (void)setWithCharArray:(IOSCharArray *)val
withInt:(jint)len {
if (val == nil) {
val = LibcoreUtilEmptyArray_get_CHAR();
}
if (((IOSCharArray *) nil_chk(val))->size_ < len) {
@throw [[[JavaIoInvalidObjectException alloc] initWithNSString:@"count out of range"]
autorelease];
}
if (len > delegate_.bufferSize_) {
free(delegate_.buffer_);
NewBuffer(&delegate_, len);
}
memcpy(delegate_.buffer_, val->buffer_, len * sizeof(jchar));
delegate_.count_ = len;
}
void JreStringBuilder_initWithCapacity(JreStringBuilder *sb, jint capacity) {
NewBuffer(sb, capacity);
sb->count_ = 0;
}
- (instancetype)init {
JavaLangAbstractStringBuilder_init(self);
return self;
}
- (instancetype)initWithInt:(jint)capacity {
JavaLangAbstractStringBuilder_initWithInt_(self, capacity);
return self;
}
- (instancetype)initWithNSString:(NSString *)string {
JavaLangAbstractStringBuilder_initWithNSString_(self, string);
return self;
}
void JavaLangAbstractStringBuilder_init(JavaLangAbstractStringBuilder *self) {
NewBuffer(&self->delegate_, INITIAL_CAPACITY);
}
void JavaLangAbstractStringBuilder_initWithInt_(
JavaLangAbstractStringBuilder *self, jint capacity) {
if (capacity < 0) {
@throw [[[JavaLangNegativeArraySizeException alloc] initWithNSString:
JavaLangInteger_toStringWithInt_(capacity)] autorelease];
}
NewBuffer(&self->delegate_, capacity);
}
void JavaLangAbstractStringBuilder_initWithNSString_(
JavaLangAbstractStringBuilder *self, NSString *string) {
nil_chk(string);
self->delegate_.count_ = (jint)[string length];
NewBuffer(&self->delegate_, self->delegate_.count_ + INITIAL_CAPACITY);
[string getCharacters:self->delegate_.buffer_ range:NSMakeRange(0, self->delegate_.count_)];
}
static void EnlargeBuffer(JreStringBuilder *sb, jint min) {
jint newSize = MAX(((sb->bufferSize_ >> 1) + sb->bufferSize_) + 2, min);
sb->buffer_ = (jchar *)realloc(sb->buffer_, newSize * sizeof(jchar));
if (__builtin_expect(sb->buffer_ == NULL, 0)) {
OutOfMemory();
}
sb->bufferSize_ = newSize;
}
static void EnsureCapacity(JreStringBuilder *sb, jint size) {
if (size > sb->bufferSize_) {
EnlargeBuffer(sb, size);
}
}
void JreStringBuilder_appendNull(JreStringBuilder *sb) {
jint newCount = sb->count_ + 4;
EnsureCapacity(sb, newCount);
jchar *buf = sb->buffer_ + sb->count_;
*(buf++) = 'n';
*(buf++) = 'u';
*(buf++) = 'l';
*(buf++) = 'l';
sb->count_ += 4;
}
void JreStringBuilder_appendBuffer(JreStringBuilder *sb, const unichar *buffer, int length) {
int newCount = sb->count_ + length;
EnsureCapacity(sb, newCount);
memcpy(sb->buffer_ + sb->count_, buffer, length * sizeof(jchar));
sb->count_ = newCount;
}
void JreStringBuilder_appendCharArray(JreStringBuilder *sb, IOSCharArray *chars) {
nil_chk(chars);
JreStringBuilder_appendBuffer(sb, chars->buffer_, chars->size_);
}
void JreStringBuilder_appendCharArraySubset(
JreStringBuilder *sb, IOSCharArray *chars, jint offset, jint length) {
nil_chk(chars);
JavaUtilArrays_checkOffsetAndCountWithInt_withInt_withInt_(chars->size_, offset, length);
JreStringBuilder_appendBuffer(sb, chars->buffer_ + offset, length);
}
void JreStringBuilder_appendChar(JreStringBuilder *sb, jchar ch) {
EnsureCapacity(sb, sb->count_ + 1);
sb->buffer_[sb->count_++] = ch;
}
void JreStringBuilder_appendString(JreStringBuilder *sb, NSString *string) {
if (string == nil) {
JreStringBuilder_appendNull(sb);
return;
}
jint length = (jint)CFStringGetLength((CFStringRef)string);
jint newCount = sb->count_ + length;
EnsureCapacity(sb, newCount);
CFStringGetCharacters((CFStringRef)string, CFRangeMake(0, length), sb->buffer_ + sb->count_);
sb->count_ = newCount;
}
void JreStringBuilder_appendCharSequence(
JreStringBuilder *sb, id<JavaLangCharSequence> s, jint start, jint end) {
if (s == nil) {
s = @"null";
}
if ((start | end) < 0 || start > end || end > [s length]) {
@throw [[[JavaLangIndexOutOfBoundsException alloc] init] autorelease];
}
jint length = end - start;
jint newCount = sb->count_ + length;
EnsureCapacity(sb, newCount);
if ([s isKindOfClass:[NSString class]]) {
[(NSString *)s getCharacters:sb->buffer_ + sb->count_ range:NSMakeRange(start, end - start)];
} else if ([s isKindOfClass:[JavaLangAbstractStringBuilder class]]) {
JavaLangAbstractStringBuilder *other = (JavaLangAbstractStringBuilder *)s;
memcpy(sb->buffer_ + sb->count_, other->delegate_.buffer_ + start, length * sizeof(jchar));
} else {
jint j = sb->count_;
for (jint i = start; i < end; i++) {
sb->buffer_[j++] = [s charAtWithInt:i];
}
}
sb->count_ = newCount;
}
void JreStringBuilder_appendInt(JreStringBuilder *sb, jint i) {
if (i == JavaLangInteger_MIN_VALUE) {
JreStringBuilder_appendString(sb, @"-2147483648");
return;
}
jint appendedLength = (i < 0) ? JavaLangInteger_stringSizeWithInt_(-i) + 1
: JavaLangInteger_stringSizeWithInt_(i);
jint newCount = sb->count_ + appendedLength;
EnsureCapacity(sb, newCount);
JavaLangInteger_getCharsRaw(i, newCount, sb->buffer_);
sb->count_ = newCount;
}
void JreStringBuilder_appendLong(JreStringBuilder *sb, jlong l) {
if (l == JavaLangLong_MIN_VALUE) {
JreStringBuilder_appendString(sb, @"-9223372036854775808");
return;
}
jint appendedLength = (l < 0) ? JavaLangLong_stringSizeWithLong_(-l) + 1
: JavaLangLong_stringSizeWithLong_(l);
jint newCount = sb->count_ + appendedLength;
EnsureCapacity(sb, newCount);
JavaLangLong_getCharsRaw(l, newCount, sb->buffer_);
sb->count_ = newCount;
}
void JreStringBuilder_appendDouble(JreStringBuilder *sb, jdouble d) {
SunMiscFloatingDecimal_appendToWithDouble_withId_(d, (id)sb);
}
void JreStringBuilder_appendFloat(JreStringBuilder *sb, jfloat f) {
SunMiscFloatingDecimal_appendToWithFloat_withId_(f, (id)sb);
}
- (jint)capacity {
return delegate_.bufferSize_;
}
- (jchar)charAtWithInt:(jint)index {
if (index < 0 || index >= delegate_.count_) {
@throw IndexAndLength(&delegate_, index);
}
return delegate_.buffer_[index];
}
void JreStringBuilder_delete(JreStringBuilder *sb, jint start, jint end) {
if (start >= 0) {
if (end > sb->count_) {
end = sb->count_;
}
if (end == start) {
return;
}
if (end > start) {
jint length = sb->count_ - end;
if (length >= 0) {
memmove(sb->buffer_ + start, sb->buffer_ + end, length * sizeof(jchar));
}
sb->count_ -= end - start;
return;
}
}
@throw StartEndAndLength(sb, start, end);
}
void JreStringBuilder_deleteCharAt(JreStringBuilder *sb, jint index) {
if (index < 0 || index >= sb->count_) {
@throw IndexAndLength(sb, index);
}
jint length = sb->count_ - index - 1;
if (length > 0) {
memmove(sb->buffer_ + index, sb->buffer_ + index + 1, length * sizeof(jchar));
}
sb->count_--;
}
- (void)ensureCapacityWithInt:(jint)min {
if (min > delegate_.bufferSize_) {
jint ourMin = delegate_.bufferSize_ * 2 + 2;
EnlargeBuffer(&delegate_, MAX(ourMin, min));
}
}
- (void)getCharsWithInt:(jint)start
withInt:(jint)end
withCharArray:(IOSCharArray *)dst
withInt:(jint)dstStart {
if (start > delegate_.count_ || end > delegate_.count_ || start > end) {
@throw StartEndAndLength(&delegate_, start, end);
}
jint length = end - start;
IOSArray_checkRange(delegate_.bufferSize_, start, length);
nil_chk(dst);
IOSArray_checkRange(dst->size_, dstStart, length);
memcpy(dst->buffer_ + dstStart, delegate_.buffer_ + start, length * sizeof(jchar));
}
void JreStringBuilder_insertCharArray(JreStringBuilder *sb, jint index, IOSCharArray *chars) {
if (index < 0 || index > sb->count_) {
@throw IndexAndLength(sb, index);
}
nil_chk(chars);
if (chars->size_ != 0) {
JreStringBuilder_move(sb, chars->size_, index);
memcpy(sb->buffer_ + index, chars->buffer_, chars->size_ * sizeof(jchar));
sb->count_ += chars->size_;
}
}
void JreStringBuilder_insertCharArraySubset(
JreStringBuilder *sb, jint index, IOSCharArray *chars, jint start, jint length) {
nil_chk(chars);
if (index >= 0 && index <= sb->count_) {
if (start >= 0 && length >= 0 && length <= chars->size_ - start) {
if (length != 0) {
JreStringBuilder_move(sb, length, index);
memcpy(sb->buffer_ + index, chars->buffer_ + start, length * sizeof(jchar));
sb->count_ += length;
}
return;
}
}
@throw [[[JavaLangStringIndexOutOfBoundsException alloc] initWithNSString:
[NSString stringWithFormat:@"this.length=%d; index=%d; chars.length=%d; start=%d; length=%d",
sb->count_, index, chars->size_, start, length]] autorelease];
}
void JreStringBuilder_insertChar(JreStringBuilder *sb, jint index, jchar ch) {
if (index < 0 || index > sb->count_) {
@throw [[[JavaLangArrayIndexOutOfBoundsException alloc]
initWithInt:sb->count_ withInt:index] autorelease];
}
JreStringBuilder_move(sb, 1, index);
sb->buffer_[index] = ch;
sb->count_++;
}
void JreStringBuilder_insertString(JreStringBuilder *sb, jint index, NSString *string) {
if (index >= 0 && index <= sb->count_) {
if (string == nil) {
string = @"null";
}
jint min = (jint)[string length];
if (min != 0) {
JreStringBuilder_move(sb, min, index);
[string getCharacters:sb->buffer_ + index range:NSMakeRange(0, min)];
sb->count_ += min;
}
} else {
@throw IndexAndLength(sb, index);
}
}
void JreStringBuilder_insertCharSequence(
JreStringBuilder *sb, jint index, id<JavaLangCharSequence> s, jint start,
jint end) {
if (s == nil) {
s = @"null";
}
if ((index | start | end) < 0 || index > sb->count_ || start > end
|| end > [s length]) {
@throw [[[JavaLangIndexOutOfBoundsException alloc] init] autorelease];
}
JreStringBuilder_insertString(sb, index, [[s subSequenceFrom:start to:end] description]);
}
- (jint)length {
return delegate_.count_;
}
void JreStringBuilder_move(JreStringBuilder *sb, jint size, jint index) {
jint newCount;
if (sb->bufferSize_ - sb->count_ >= size) {
memmove(sb->buffer_ + index + size, sb->buffer_ + index,
(sb->count_ - index) * sizeof(jchar));
return;
}
newCount = MAX(sb->count_ + size, sb->bufferSize_ * 2 + 2);
jchar *oldBuffer = sb->buffer_;
NewBuffer(sb, newCount);
memcpy(sb->buffer_, oldBuffer, index * sizeof(jchar));
memcpy(sb->buffer_ + index + size, oldBuffer + index, (sb->count_ - index) * sizeof(jchar));
free(oldBuffer);
}
void JreStringBuilder_replace(JreStringBuilder *sb, jint start, jint end, NSString *string) {
if (start >= 0) {
if (end > sb->count_) {
end = sb->count_;
}
if (end > start) {
nil_chk(string);
jint stringLength = (jint)[string length];
jint diff = end - start - stringLength;
if (diff > 0) {
memmove(sb->buffer_ + start + stringLength, sb->buffer_ + end,
(sb->count_ - end) * sizeof(jchar));
} else if (diff < 0) {
JreStringBuilder_move(sb, -diff, end);
}
[string getCharacters:sb->buffer_ + start range:NSMakeRange(0, stringLength)];
sb->count_ -= diff;
return;
}
if (start == end) {
if (string == nil) {
@throw [[[JavaLangNullPointerException alloc]
initWithNSString:@"string == null"] autorelease];
}
JreStringBuilder_insertString(sb, start, string);
return;
}
}
@throw StartEndAndLength(sb, start, end);
}
void JreStringBuilder_reverse(JreStringBuilder *sb) {
if (sb->count_ < 2) {
return;
}
jchar *buf = sb->buffer_;
jint end = sb->count_ - 1;
jchar frontHigh = buf[0];
jchar endLow = buf[end];
jboolean allowFrontSur = true, allowEndSur = true;
for (jint i = 0, mid = sb->count_ / 2; i < mid; i++, --end) {
jchar frontLow = buf[i + 1];
jchar endHigh = buf[end - 1];
jboolean surAtFront = allowFrontSur && frontLow >= (jint)0xdc00 && frontLow <= (jint)0xdfff
&& frontHigh >= (jint)0xd800 && frontHigh <= (jint)0xdbff;
if (surAtFront && (sb->count_ < 3)) {
return;
}
jboolean surAtEnd = allowEndSur && endHigh >= (jint)0xd800 && endHigh <= (jint)0xdbff
&& endLow >= (jint)0xdc00 && endLow <= (jint)0xdfff;
allowFrontSur = allowEndSur = true;
if (surAtFront == surAtEnd) {
if (surAtFront) {
buf[end] = frontLow;
buf[end - 1] = frontHigh;
buf[i] = endHigh;
buf[i + 1] = endLow;
frontHigh = buf[i + 2];
endLow = buf[end - 2];
i++;
end--;
} else {
buf[end] = frontHigh;
buf[i] = endLow;
frontHigh = frontLow;
endLow = endHigh;
}
} else {
if (surAtFront) {
buf[end] = frontLow;
buf[i] = endLow;
endLow = endHigh;
allowFrontSur = false;
} else {
buf[end] = frontHigh;
buf[i] = endHigh;
frontHigh = frontLow;
allowEndSur = false;
}
}
}
if ((sb->count_ & 1) == 1 && (!allowFrontSur || !allowEndSur)) {
buf[end] = allowFrontSur ? endLow : frontHigh;
}
}
- (void)setCharAtWithInt:(jint)index
withChar:(jchar)ch {
if (index < 0 || index >= delegate_.count_) {
@throw IndexAndLength(&delegate_, index);
}
delegate_.buffer_[index] = ch;
}
- (void)setLengthWithInt:(jint)length {
if (length < 0) {
@throw [[[JavaLangStringIndexOutOfBoundsException alloc]
initWithNSString:[NSString stringWithFormat:@"length < 0: %d", length]] autorelease];
}
EnsureCapacity(&delegate_, length);
if (delegate_.count_ < length) {
memset(delegate_.buffer_ + delegate_.count_, 0, (length - delegate_.count_) * sizeof(jchar));
}
delegate_.count_ = length;
}
- (NSString *)substringWithInt:(jint)start {
if (start >= 0 && start <= delegate_.count_) {
if (start == delegate_.count_) {
return @"";
}
return [NSString stringWithCharacters:delegate_.buffer_ + start
length:delegate_.count_ - start];
}
@throw IndexAndLength(&delegate_, start);
}
- (NSString *)substringWithInt:(jint)start
withInt:(jint)end {
if (start >= 0 && start <= end && end <= delegate_.count_) {
if (start == end) {
return @"";
}
return [NSString stringWithCharacters:delegate_.buffer_ + start length:end - start];
}
@throw StartEndAndLength(&delegate_, start, end);
}
- (NSString *)description {
return JreStringBuilder_toString(&delegate_);
}
NSString *JreStringBuilder_toString(JreStringBuilder *sb) {
if (sb->count_ == 0) {
return @"";
}
return [NSString stringWithCharacters:sb->buffer_ length:sb->count_];
}
NSString *JreStringBuilder_toStringAndDealloc(JreStringBuilder *sb) {
if (sb->count_ == 0) {
free(sb->buffer_);
return @"";
}
NSString *result;
jint wasted = sb->bufferSize_ - sb->count_;
// Same test as used by the original AbstractString.java to determine whether
// to use a shared buffer.
if (wasted >= 256 || (wasted >= INITIAL_CAPACITY && wasted >= (sb->count_ >> 1))) {
result = (NSString *)CFStringCreateWithCharacters(NULL, sb->buffer_, sb->count_);
free(sb->buffer_);
} else {
// Don't free the buffer because we're passing it off to the CFString constructor.
result = (NSString *)CFStringCreateWithCharactersNoCopy(
NULL, sb->buffer_, sb->count_, kCFAllocatorMalloc);
}
return [result autorelease];
}
- (id<JavaLangCharSequence>)subSequenceFrom:(jint)start
to:(jint)end {
return [self substringWithInt:start withInt:end];
}
- (jint)indexOfWithNSString:(NSString *)string {
return [self indexOfWithNSString:string withInt:0];
}
- (jint)indexOfWithNSString:(NSString *)subString
withInt:(jint)start {
nil_chk(subString);
if (start < 0) {
start = 0;
}
jint subCount = (jint)[subString length];
if (subCount > 0) {
if (subCount + start > delegate_.count_) {
return -1;
}
jchar firstChar = [subString characterAtIndex:0];
while (true) {
jint i = start;
jboolean found = false;
for (; i < delegate_.count_; i++) {
if (delegate_.buffer_[i] == firstChar) {
found = true;
break;
}
}
if (!found || subCount + i > delegate_.count_) {
return -1;
}
jint o1 = i, o2 = 0;
while (++o2 < subCount && delegate_.buffer_[++o1] == [subString characterAtIndex:o2]) {
}
if (o2 == subCount) {
return i;
}
start = i + 1;
}
}
return (start < delegate_.count_ || start == 0) ? start : delegate_.count_;
}
- (jint)lastIndexOfWithNSString:(NSString *)string {
return [self lastIndexOfWithNSString:string withInt:delegate_.count_];
}
- (jint)lastIndexOfWithNSString:(NSString *)subString
withInt:(jint)start {
nil_chk(subString);
jint subCount = (jint)[subString length];
if (subCount <= delegate_.count_ && start >= 0) {
if (subCount > 0) {
if (start > delegate_.count_ - subCount) {
start = delegate_.count_ - subCount;
}
jchar firstChar = [subString characterAtIndex:0];
while (true) {
jint i = start;
jboolean found = false;
for (; i >= 0; --i) {
if (delegate_.buffer_[i] == firstChar) {
found = true;
break;
}
}
if (!found) {
return -1;
}
jint o1 = i, o2 = 0;
while (++o2 < subCount && delegate_.buffer_[++o1] == [subString characterAtIndex:o2]) {
}
if (o2 == subCount) {
return i;
}
start = i - 1;
}
}
return start < delegate_.count_ ? start : delegate_.count_;
}
return -1;
}
- (void)trimToSize {
if (delegate_.count_ < delegate_.bufferSize_) {
jchar *oldBuffer = delegate_.buffer_;
NewBuffer(&delegate_, delegate_.count_);
memcpy(delegate_.buffer_, oldBuffer, delegate_.count_ * sizeof(jchar));
free(oldBuffer);
}
}
// Defined in Character.java
jint JavaLangCharacter_codePointAtRaw(const jchar *seq, jint index, jint limit);
jint JavaLangCharacter_codePointBeforeRaw(const jchar *seq, jint index, jint start);
jint JavaLangCharacter_codePointCountRaw(const jchar *seq, jint offset, jint count);
jint JavaLangCharacter_offsetByCodePointsRaw(
const jchar *seq, jint start, jint count, jint index, jint codePointOffset);
- (jint)codePointAtWithInt:(jint)index {
if (index < 0 || index >= delegate_.count_) {
@throw IndexAndLength(&delegate_, index);
}
return JavaLangCharacter_codePointAtRaw(delegate_.buffer_, index, delegate_.count_);
}
- (jint)codePointBeforeWithInt:(jint)index {
if (index < 1 || index > delegate_.count_) {
@throw IndexAndLength(&delegate_, index);
}
return JavaLangCharacter_codePointBeforeRaw(delegate_.buffer_, index, 0);
}
- (jint)codePointCountWithInt:(jint)start
withInt:(jint)end {
if (start < 0 || end > delegate_.count_ || start > end) {
@throw StartEndAndLength(&delegate_, start, end);
}
return JavaLangCharacter_codePointCountRaw(delegate_.buffer_, start, end - start);
}
- (jint)offsetByCodePointsWithInt:(jint)index
withInt:(jint)codePointOffset {
return JavaLangCharacter_offsetByCodePointsRaw(
delegate_.buffer_, 0, delegate_.count_, index, codePointOffset);
}
- (JavaLangAbstractStringBuilder *)appendWithChar:(jchar)c{
JreStringBuilder_appendChar(&self->delegate_, c);
return self;
}
- (JavaLangAbstractStringBuilder *)appendWithCharArray:(IOSCharArray *)str
withInt:(jint)offset
withInt:(jint)len{
JreStringBuilder_appendCharArraySubset(&self->delegate_, str, offset, len);
return self;
}
- (JavaLangAbstractStringBuilder *)appendWithNSString:(NSString *)str {
JreStringBuilder_appendString(&self->delegate_, str);
return self;
}
// Default methods in java.lang.CharSequence.
- (id<JavaUtilStreamIntStream>)chars {
return JavaLangCharSequence_chars(self);
}
- (id<JavaUtilStreamIntStream>)codePoints {
return JavaLangCharSequence_codePoints(self);
}
- (id<JavaLangAppendable>)appendWithJavaLangCharSequence:(id<JavaLangCharSequence>)cs {
// can't call an abstract method
[self doesNotRecognizeSelector:_cmd];
return 0;
}
- (id<JavaLangAppendable>)appendWithJavaLangCharSequence:(id<JavaLangCharSequence>)cs
withInt:(jint)start
withInt:(jint)end {
// can't call an abstract method
[self doesNotRecognizeSelector:_cmd];
return 0;
}
- (void)dealloc {
free(delegate_.buffer_);
[super dealloc];
}
// Suppress undeclared-selector warnings to avoid creating method bodies
// for all the abstract methods which are implemented in subclasses.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
+ (const J2ObjcClassInfo *)__metadata {
static J2ObjcMethodInfo methods[] = {
{ NULL, NULL, 0x0, -1, -1, -1, -1, -1, -1 },
{ NULL, NULL, 0x0, -1, 0, -1, -1, -1, -1 },
{ NULL, "I", 0x1, -1, -1, -1, -1, -1, -1 },
{ NULL, "I", 0x1, -1, -1, -1, -1, -1, -1 },
{ NULL, "V", 0x1, 1, 0, -1, -1, -1, -1 },
{ NULL, "V", 0x0, 2, 0, -1, -1, -1, -1 },
{ NULL, "V", 0x1, -1, -1, -1, -1, -1, -1 },
{ NULL, "V", 0x1, 3, 0, -1, -1, -1, -1 },
{ NULL, "C", 0x1, 4, 0, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 5, 0, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 6, 0, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 7, 8, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 9, 8, -1, -1, -1, -1 },
{ NULL, "V", 0x1, 10, 11, -1, -1, -1, -1 },
{ NULL, "V", 0x1, 12, 13, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 15, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 16, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 17, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x0, 14, 18, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 19, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 20, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 21, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 22, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 23, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 24, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 0, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 25, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 26, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 14, 27, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 28, 8, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 29, 0, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 30, 0, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 31, 32, -1, -1, -1, -1 },
{ NULL, "LNSString;", 0x1, 33, 0, -1, -1, -1, -1 },
{ NULL, "LJavaLangCharSequence;", 0x1, 34, 8, -1, -1, -1, -1 },
{ NULL, "LNSString;", 0x1, 33, 8, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 36, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 37, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 38, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 39, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 40, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 41, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 42, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 13, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 8, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 43, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 44, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, 35, 45, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 46, 16, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 46, 47, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 48, 16, -1, -1, -1, -1 },
{ NULL, "I", 0x1, 48, 47, -1, -1, -1, -1 },
{ NULL, "LJavaLangAbstractStringBuilder;", 0x1, -1, -1, -1, -1, -1, -1 },
{ NULL, "LNSString;", 0x401, 49, -1, -1, -1, -1, -1 },
{ NULL, "[C", 0x10, -1, -1, -1, -1, -1, -1 },
};
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-multiple-method-names"
methods[0].selector = @selector(init);
methods[1].selector = @selector(initWithInt:);
methods[2].selector = @selector(length);
methods[3].selector = @selector(capacity);
methods[4].selector = @selector(ensureCapacityWithInt:);
methods[5].selector = @selector(expandCapacityWithInt:);
methods[6].selector = @selector(trimToSize);
methods[7].selector = @selector(setLengthWithInt:);
methods[8].selector = @selector(charAtWithInt:);
methods[9].selector = @selector(codePointAtWithInt:);
methods[10].selector = @selector(codePointBeforeWithInt:);
methods[11].selector = @selector(codePointCountWithInt:withInt:);
methods[12].selector = @selector(offsetByCodePointsWithInt:withInt:);
methods[13].selector = @selector(getCharsWithInt:withInt:withCharArray:withInt:);
methods[14].selector = @selector(setCharAtWithInt:withChar:);
methods[15].selector = @selector(appendWithId:);
methods[16].selector = @selector(appendWithNSString:);
methods[17].selector = @selector(appendWithJavaLangStringBuffer:);
methods[18].selector = @selector(appendWithJavaLangAbstractStringBuilder:);
methods[19].selector = @selector(appendWithJavaLangCharSequence:);
methods[20].selector = @selector(appendWithJavaLangCharSequence:withInt:withInt:);
methods[21].selector = @selector(appendWithCharArray:);
methods[22].selector = @selector(appendWithCharArray:withInt:withInt:);
methods[23].selector = @selector(appendWithBoolean:);
methods[24].selector = @selector(appendWithChar:);
methods[25].selector = @selector(appendWithInt:);
methods[26].selector = @selector(appendWithLong:);
methods[27].selector = @selector(appendWithFloat:);
methods[28].selector = @selector(appendWithDouble:);
methods[29].selector = @selector(delete__WithInt:withInt:);
methods[30].selector = @selector(appendCodePointWithInt:);
methods[31].selector = @selector(deleteCharAtWithInt:);
methods[32].selector = @selector(replaceWithInt:withInt:withNSString:);
methods[33].selector = @selector(substringWithInt:);
methods[34].selector = @selector(subSequenceFrom:to:);
methods[35].selector = @selector(substringWithInt:withInt:);
methods[36].selector = @selector(insertWithInt:withCharArray:withInt:withInt:);
methods[37].selector = @selector(insertWithInt:withId:);
methods[38].selector = @selector(insertWithInt:withNSString:);
methods[39].selector = @selector(insertWithInt:withCharArray:);
methods[40].selector = @selector(insertWithInt:withJavaLangCharSequence:);
methods[41].selector = @selector(insertWithInt:withJavaLangCharSequence:withInt:withInt:);
methods[42].selector = @selector(insertWithInt:withBoolean:);
methods[43].selector = @selector(insertWithInt:withChar:);
methods[44].selector = @selector(insertWithInt:withInt:);
methods[45].selector = @selector(insertWithInt:withLong:);
methods[46].selector = @selector(insertWithInt:withFloat:);
methods[47].selector = @selector(insertWithInt:withDouble:);
methods[48].selector = @selector(indexOfWithNSString:);
methods[49].selector = @selector(indexOfWithNSString:withInt:);
methods[50].selector = @selector(lastIndexOfWithNSString:);
methods[51].selector = @selector(lastIndexOfWithNSString:withInt:);
methods[52].selector = @selector(reverse);
methods[53].selector = @selector(description);
methods[54].selector = @selector(getValue);
#pragma clang diagnostic pop
static const J2ObjcFieldInfo fields[] = {
{ "value_", "[C", .constantValue.asLong = 0, 0x0, -1, -1, -1, -1 },
{ "count_", "I", .constantValue.asLong = 0, 0x0, -1, -1, -1, -1 },
};
static const void *ptrTable[] = {
"I", "ensureCapacity", "expandCapacity", "setLength", "charAt", "codePointAt",
"codePointBefore", "codePointCount", "II", "offsetByCodePoints", "getChars", "II[CI",
"setCharAt", "IC", "append", "LNSObject;", "LNSString;", "LJavaLangStringBuffer;",
"LJavaLangAbstractStringBuilder;", "LJavaLangCharSequence;", "LJavaLangCharSequence;II", "[C",
"[CII", "Z", "C", "J", "F", "D", "delete", "appendCodePoint", "deleteCharAt", "replace",
"IILNSString;", "substring", "subSequence", "insert", "I[CII", "ILNSObject;", "ILNSString;",
"I[C", "ILJavaLangCharSequence;", "ILJavaLangCharSequence;II", "IZ", "IJ", "IF", "ID",
"indexOf", "LNSString;I", "lastIndexOf", "toString" };
static const J2ObjcClassInfo _JavaLangAbstractStringBuilder = {
"AbstractStringBuilder", "java.lang", ptrTable, methods, fields, 7, 0x400, 55, 2, -1, -1, -1,
-1, -1 };
return &_JavaLangAbstractStringBuilder;
}
#pragma clang diagnostic pop
@end