-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathustring.c
3039 lines (2752 loc) · 81.9 KB
/
ustring.c
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ustring.c - ICU based Unicode string support.
*
* $Id: ustring.c,v 1.20 2006/01/23 14:26:45 meadow Exp $
*
* Copyright (c) 2006 Nikolai Lugovoi
*
* This code is based on original ruby String class source (string.c):
*
* * string.c -
* *
* * Copyright (C) 1993-2003 Yukihiro Matsumoto
* * Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* * Copyright (C) 2000 Information-technology Promotion Agency, Japan
* *
**/
#include "icu_common.h"
VALUE icu_ustr_replace(VALUE str, VALUE str2);
VALUE ustr_gsub(int argc, VALUE * argv, VALUE str, int bang, int once);
extern VALUE icu_from_rstr(int argc, VALUE * argv, VALUE str);
VALUE rb_cURegexp;
VALUE rb_cUString;
VALUE rb_cUMatch;
VALUE rb_cUResourceBundle;
VALUE rb_cULocale;
VALUE rb_cUCalendar;
VALUE rb_cUConverter;
VALUE rb_cUCollator;
#include "uregex.h"
/* to be used in <=>, casecmp */
static UCollator * s_UCA_collator, * s_case_UCA_collator;
static void
free_ustr(str)
ICUString *str;
{
if (str->ptr)
free(str->ptr);
str->ptr = 0;
free(str);
}
inline void icu_check_frozen(int check_busy, VALUE str)
{
rb_check_frozen(str);
if(check_busy && USTRING(str)->busy > 0 ) rb_raise(rb_eRuntimeError, "String is busy. Can't modify");
}
#define START_BUF_LEN 16
/**
* Allocate ICUString struct with given +capa+ capacity,
* if mode == 1 and UChar != 0 - copy len UChars from src,
* else set pointer to src.
*/
#define ICU_COPY 1
#define ICU_SET 0
VALUE icu_ustr_alloc_and_wrap(UChar * src, long len, long capa, int mode)
{
ICUString *n_str = ALLOC_N(ICUString, 1);
size_t alloc_capa;
if( mode == ICU_COPY ) {
alloc_capa = START_BUF_LEN > capa ? START_BUF_LEN : capa;
if(alloc_capa<=len) alloc_capa = len + 1;
n_str->ptr = ALLOC_N(UChar, alloc_capa);
n_str->capa = alloc_capa;
n_str->len = len;
if( src ) {
u_memcpy(n_str->ptr, src, len);
n_str->ptr[len] = 0;
}
} else {
n_str->ptr = src;
n_str->len = len;
n_str->capa = capa;
}
if(n_str->capa <= n_str->len) rb_raise(rb_eRuntimeError, "Capacity is not large then len, sentinel can't be set!");
n_str->busy = 0;
n_str->ptr[n_str->len] = 0;
return Data_Wrap_Struct(rb_cUString, 0, free_ustr, n_str);
}
VALUE
icu_ustr_alloc(klass)
VALUE klass;
{
return icu_ustr_alloc_and_wrap(NULL, 0, 0, ICU_COPY);
}
void ustr_capa_resize(ICUString * str, long new_capa)
{
if (new_capa != str->capa) {
if (str->capa < new_capa || (str->capa - new_capa > 1024)) {
if(new_capa < START_BUF_LEN) new_capa = START_BUF_LEN;
REALLOC_N(str->ptr, UChar, new_capa);
str->capa = new_capa;
}
}
}
/* delete +del_len+ units from string and insert replacement */
void ustr_splice_units(ICUString * str, long start, long del_len, const UChar * replacement, long repl_len)
{
long new_len;
UChar * temp = 0 ;
if( str->busy ) {
rb_warn("Attempt to modify busy string. Ignored");
return;
}
if( repl_len < 0) return;
if( del_len == 0 && repl_len == 0) return;
new_len = str->len - del_len + repl_len;
if (replacement == str->ptr ) {
temp = ALLOC_N(UChar, repl_len);
u_memcpy(temp, replacement, repl_len);
replacement = temp;
}
if ( repl_len >= del_len) ustr_capa_resize(str, new_len+1);
/* move tail */
if(str->len - (start+del_len) > 0) {
u_memmove(str->ptr + start+repl_len, str->ptr + start+del_len, str->len-(start+del_len) );
}
/* copy string */
if( repl_len > 0) u_memcpy(str->ptr+start, replacement, repl_len);
if ( repl_len < del_len) ustr_capa_resize(str, new_len+1);
str->len = new_len;
str->ptr[new_len] = 0;
if(temp) {
free(temp);
}
}
static inline void
ustr_mod_check(VALUE s, UChar *p, long len)
{
if (ICU_PTR(s) != p || ICU_LEN(s) != len){
rb_raise(rb_eRuntimeError, "string modified");
}
}
VALUE
ustr_new(klass, ptr, len)
VALUE klass;
UChar *ptr;
long len;
{
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
return icu_ustr_alloc_and_wrap(ptr, len, len+1, ICU_COPY);
}
VALUE
icu_ustr_new(ptr, len)
const UChar *ptr;
long len;
{
return ustr_new(rb_cUString, ptr, len);
}
VALUE
icu_ustr_new_set(ptr, len, capa)
UChar *ptr;
long len;
long capa;
{
return icu_ustr_alloc_and_wrap(ptr, len, capa, ICU_SET);
}
VALUE
icu_ustr_new2(ptr)
const UChar *ptr;
{
if (!ptr) {
rb_raise(rb_eArgError, "NULL pointer given");
}
return icu_ustr_new(ptr, u_strlen(ptr));
}
inline VALUE
icu_ustr_new_capa(UChar * ptr, long len, long capa)
{
return icu_ustr_alloc_and_wrap(ptr, len, capa, ICU_COPY);
}
/* ------------ */
/**
* call-seq:
* UString.new(str="".u) => new_str
*
* Returns a new string object containing a copy of <i>str</i>.
*/
VALUE
icu_ustr_init(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE orig;
if (rb_scan_args(argc, argv, "01", &orig) == 1)
{
icu_ustr_replace(str, orig);
}
return str;
}
/**
* call-seq:
* str.length => integer
*
* Returns the length of <i>str</i>.
*/
VALUE
icu_ustr_length(str)
VALUE str;
{
return LONG2NUM(ICU_LEN(str));
}
/**
* call-seq:
* str.empty? => true or false
*
* Returns <code>true</code> if <i>str</i> has a length of zero.
*
* "hello".u.empty? #=> false
* "".u.empty? #=> true
*/
VALUE
icu_ustr_empty(str)
VALUE str;
{
return 0 == ICU_LEN(str) ? Qtrue : Qfalse;
}
VALUE
icu_ustr_resize(str, len)
VALUE str;
long len;
{
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
ustr_capa_resize(USTRING(str), len);
ICU_LEN(str) = len;
ICU_PTR(str)[len] = 0; /* sentinel */
return str;
}
/**
* call-seq:
* str.replace(other_str) => str
*
* Replaces the contents and taintedness of <i>str</i> with the corresponding
* values in <i>other_str</i>.
*
* s = "hello".u #=> "hello"
* s.replace "world".u #=> "world"
*/
VALUE
icu_ustr_replace(str, str2)
VALUE str,
str2;
{
if (str == str2)
return str;
icu_check_frozen(1, str);
Check_Class(str2, rb_cUString);
ustr_splice_units(USTRING(str), 0, ICU_LEN(str), ICU_PTR(str2), ICU_LEN(str2));
OBJ_INFECT(str, str2);
return str;
}
/**
* call-seq:
* string.clear -> string
*
* Makes string empty.
*
* a = "abcde".u
* a.clear #=> ""
*/
VALUE
icu_ustr_clear(str)
VALUE str;
{
icu_check_frozen(1, str);
icu_ustr_resize(str, 0);
return str;
}
int icu_collator_cmp (UCollator * collator, VALUE str1, VALUE str2)
{
int ret = 0, result ;
result = ucol_strcoll(collator, ICU_PTR(str1), ICU_LEN(str1), ICU_PTR(str2), ICU_LEN(str2));
switch(result){
case UCOL_EQUAL: ret = 0;break;
case UCOL_GREATER: ret = 1;break;
case UCOL_LESS: ret = -1;break;
}
return ret;
}
int
icu_ustr_cmp(str1, str2)
VALUE str1,
str2;
{
return icu_collator_cmp(s_UCA_collator, str1, str2);
}
/**
* call-seq:
* str == obj => true or false
*
* Equality---If <i>obj</i> is not a <code>UString</code>, returns
* <code>false</code>. Otherwise, returns <code>true</code> if
* strings are of the same length and content
*
*/
VALUE
icu_ustr_equal(str1, str2)
VALUE str1,
str2;
{
if (str1 == str2)
return Qtrue;
if (CLASS_OF(str2) != rb_cUString) {
return Qfalse;
}
if (ICU_LEN(str1) == ICU_LEN(str2) &&
u_strncmp(ICU_PTR(str1), ICU_PTR(str2), ICU_LEN(str1) ) == 0) {
return Qtrue;
}
return Qfalse;
}
/**
* call-seq:
* str <=> other_str => -1, 0, +1
*
* Comparison---Returns -1 if <i>other_str</i> is less than, 0 if
* <i>other_str</i> is equal to, and +1 if <i>other_str</i> is greater than
* <i>str</i>.
*
* <code><=></code> is the basis for the methods <code><</code>,
* <code><=</code>, <code>></code>, <code>>=</code>, and <code>between?</code>,
* included from module <code>Comparable</code>. The method
* <code>String#==</code> does not use <code>Comparable#==</code>.
*
* This method uses UCA rules, see also #strcoll for locale-specific string collation.
*
* "abcdef".u <=> "abcde".u #=> 1
* "abcdef".u <=> "abcdef".u #=> 0
* "abcdef".u <=> "abcdefg".u #=> -1
* "abcdef".u <=> "ABCDEF".u #=> -1
*/
VALUE
icu_ustr_cmp_m(str1, str2)
VALUE str1,
str2;
{
long result;
if (CLASS_OF(str2) != rb_cUString) {
return Qnil;
} else {
result = icu_ustr_cmp(str1, str2);
}
return LONG2NUM(result);
}
/**
* call-seq:
* str.casecmp(other_str) => -1, 0, +1
*
* Case-insensitive version of <code>UString#<=></code> .
* This method uses UCA collator with secondary strength, see #strcoll
*
*
* "abcdef".u.casecmp("abcde".u) #=> 1
* "aBcDeF".u.casecmp("abcdef".u) #=> 0
* "abcdef".u.casecmp("abcdefg".u) #=> -1
* "abcdef".u.casecmp("ABCDEF".u) #=> 0
*/
VALUE
icu_ustr_casecmp(str1, str2)
VALUE str1,
str2;
{
Check_Class(str2, rb_cUString);
return INT2FIX(icu_collator_cmp(s_case_UCA_collator, str1, str2));
}
/**
* call-seq:
* str + other_str => new_str
*
* Concatenation---Returns a new <code>UString</code> containing
* <i>other_str</i> concatenated to <i>str</i>.
*
* "Hello from ".u + "main".u #=> "Hello from main"
*/
VALUE
icu_ustr_plus(str1, str2)
VALUE str1,
str2;
{
VALUE str3;
Check_Class(str2, rb_cUString);
str3 = icu_ustr_new_capa(ICU_PTR(str1), ICU_LEN(str1), ICU_LEN(str1) + ICU_LEN(str2));
ustr_splice_units(USTRING(str3), ICU_LEN(str3), 0, ICU_PTR(str2), ICU_LEN(str2));
if (OBJ_TAINTED(str1) || OBJ_TAINTED(str2))
OBJ_TAINT(str3);
return str3;
}
/**
* call-seq:
* str * integer => new_str
*
* Copy---Returns a new <code>UString</code> containing <i>integer</i> copies of
* the receiver.
*
* "Ho! ".u * 3 #=> "Ho! Ho! Ho! ".u
*/
VALUE
icu_ustr_times(str, times)
VALUE str,
times;
{
VALUE str2;
long i,
len;
Check_Type(times, T_FIXNUM);
len = NUM2LONG(times);
if (len < 0) {
rb_raise(rb_eArgError, "negative argument");
}
if (len && LONG_MAX / len < ICU_LEN(str)) {
rb_raise(rb_eArgError, "argument too big");
}
str2 = icu_ustr_new_capa(0, 0, len *= ICU_LEN(str));
for (i = 0; i < len; i += ICU_LEN(str)) {
ustr_splice_units(USTRING(str2), i, 0, ICU_PTR(str), ICU_LEN(str));
}
ICU_PTR(str2)[ICU_LEN(str2)] = 0;
OBJ_INFECT(str2, str);
return str2;
}
/**
* call-seq:
* str << other_str => str
* str.concat(other_str) => str
*
* Append---Concatenates the given string object to <i>str</i>.
*
* a = "hello ".u
* a << "world".u #=> "hello world"
*/
VALUE
icu_ustr_concat(str1, str2)
VALUE str1,
str2;
{
icu_check_frozen(1, str1);
Check_Class(str2, rb_cUString);
if (ICU_LEN(str2) > 0) {
ustr_splice_units(USTRING(str1), ICU_LEN(str1), 0, ICU_PTR(str2), ICU_LEN(str2));
OBJ_INFECT(str1, str2);
}
return str1;
}
int
icu_ustr_hash(str)
VALUE str;
{
register long len = ICU_LEN(str) * (sizeof(UChar));
register char *p = (char*)ICU_PTR(str);
register int key = 0;
while (len--) {
key += *p++;
key += (key << 10);
key ^= (key >> 6);
}
key += (key << 3);
key ^= (key >> 11);
key += (key << 15);
return key;
}
/**
* call-seq:
* str.hash => fixnum
*
* Return a hash based on the string's length and content.
*/
VALUE
icu_ustr_hash_m(str)
VALUE str;
{
int key = icu_ustr_hash(str);
return INT2FIX(key);
}
VALUE
icu_ustr_dup(str)
VALUE str;
{
VALUE dup = icu_ustr_new(ICU_PTR(str), ICU_LEN(str));
return dup;
}
/**
* call-seq:
* str.upcase!(locale = "") => str or nil
*
* Upcases the contents of <i>str</i>, returning <code>nil</code> if no changes
* were made. This method is locale-sensitive.
*/
VALUE
icu_ustr_upcase_bang(argc, argv, str)
int argc;
VALUE * argv;
VALUE str;
{
UErrorCode error = 0;
UChar *buf = 0;
long len ;
VALUE loc;
char * locale = NULL;
icu_check_frozen(1, str);
buf = ALLOC_N(UChar, ICU_LEN(str) + 1);
if (rb_scan_args(argc, argv, "01", &loc) == 1) {
if( loc != Qnil) {
Check_Type(loc, T_STRING);
locale = RSTRING_PTR(loc);
}
}
len = u_strToUpper(buf, ICU_LEN(str), ICU_PTR(str), ICU_LEN(str), locale, &error);
if (U_BUFFER_OVERFLOW_ERROR == error) {
REALLOC_N(buf, UChar, len + 1);
error = 0;
len =
u_strToUpper(buf, len, ICU_PTR(str), ICU_LEN(str), locale, &error);
}
if (0 == u_strncmp(buf, ICU_PTR(str), len))
return Qnil;
free(ICU_PTR(str));
ICU_PTR(str) = buf;
ICU_LEN(str) = len;
return str;
}
/**
* call-seq:
* str.upcase(locale = "") => new_str
*
* Returns a copy of <i>str</i> with all lowercase letters replaced with their
* uppercase counterparts. The operation is locale sensitive.
*
* "hEllO".u.upcase #=> "HELLO"
*/
VALUE
icu_ustr_upcase(argc, argv, str)
int argc;
VALUE * argv;
VALUE str;
{
str = icu_ustr_dup(str);
icu_ustr_upcase_bang(argc, argv, str);
return str;
}
/**
* call-seq:
* str.downcase!(locale = "") => str or nil
*
* Downcases the contents of <i>str</i>, returning <code>nil</code> if no
* changes were made.
*/
VALUE
icu_ustr_downcase_bang(argc, argv, str)
int argc;
VALUE * argv;
VALUE str;
{
UErrorCode error = 0;
UChar *buf;
long len ;
VALUE loc;
char * locale = NULL;
buf = ALLOC_N(UChar, ICU_LEN(str) + 1);
icu_check_frozen(1, str);
if (rb_scan_args(argc, argv, "01", &loc) == 1) {
if( loc != Qnil) {
Check_Type(loc, T_STRING);
locale = RSTRING_PTR(loc);
}
}
len =
u_strToLower(buf, ICU_LEN(str), ICU_PTR(str), ICU_LEN(str), locale,
&error);
if (U_BUFFER_OVERFLOW_ERROR == error) {
REALLOC_N(buf, UChar, len + 1);
error = 0;
len =
u_strToLower(buf, len , ICU_PTR(str), ICU_LEN(str), locale,
&error);
}
if (0 == u_strncmp(buf, ICU_PTR(str), len))
return Qnil;
free(ICU_PTR(str));
ICU_PTR(str) = buf;
ICU_LEN(str) = len;
return str;
}
/**
* call-seq:
* str.downcase(locale = "") => new_str
*
* Returns a copy of <i>str</i> with all uppercase letters replaced with their
* lowercase counterparts. The operation is locale sensitive.
*
* "hEllO".u.downcase #=> "hello"
*/
VALUE
icu_ustr_downcase(argc, argv, str)
int argc;
VALUE * argv;
VALUE str;
{
str = icu_ustr_dup(str);
icu_ustr_downcase_bang(argc, argv, str);
return str;
}
/**
* call-seq:
* str.foldcase
*
* Case-fold the characters in a string.
* Case-folding is locale-independent and not context-sensitive.
*
*/
VALUE
icu_ustr_foldcase(str)
VALUE str;
{
UErrorCode error = 0;
UChar *buf;
long len, capa ;
capa = ICU_LEN(str) + 1;
buf = ALLOC_N(UChar, capa);
len = u_strFoldCase(buf, capa-1, ICU_PTR(str), ICU_LEN(str), U_FOLD_CASE_DEFAULT, &error);
if (U_BUFFER_OVERFLOW_ERROR == error) {
capa = len + 1;
REALLOC_N(buf, UChar, len + 1);
error = 0;
len = u_strFoldCase(buf, capa, ICU_PTR(str), ICU_LEN(str), U_FOLD_CASE_DEFAULT, &error);
}
return icu_ustr_new_set(buf, len, capa) ;
}
static long
icu_ustr_index(str, sub, offset)
VALUE str,
sub;
long offset;
{
long pos;
UChar *found;
if (offset < 0) {
offset += ICU_LEN(str);
if (offset < 0)
return -1;
}
if (ICU_LEN(str) - offset < ICU_LEN(sub))
return -1;
if (ICU_LEN(sub) == 0)
return offset;
found =
u_strFindFirst(ICU_PTR(str) + offset, ICU_LEN(str) - offset,
ICU_PTR(sub), ICU_LEN(sub));
if (NULL == found)
return -1;
pos = found - (ICU_PTR(str) + offset);
return pos + offset;
}
/**
* call-seq:
* str.index(substring [, offset]) => fixnum or nil
* str.index(regexp [, offset]) => fixnum or nil
*
* Returns the index of the first occurrence of the given <i>substring</i>,
* or pattern (<i>regexp</i>) in <i>str</i>. Returns
* <code>nil</code> if not found. If the second parameter is present, it
* specifies the position in the string to begin the search.
*
* "hello".u.index('e'.u) #=> 1
* "hello".u.index('lo'.u) #=> 3
* "hello".u.index('a'.u) #=> nil
* "hello".u.index(/[aeiou]/.U, -3) #=> 4
*/
VALUE
icu_ustr_index_m(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE sub;
VALUE initpos;
long pos ;
int processed = 0;
if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
pos = NUM2LONG(initpos);
} else {
pos = 0;
}
if (pos < 0) {
pos += ICU_LEN(str);
}
if( CLASS_OF(sub) == rb_cUString) {
pos = icu_ustr_index(str, sub, pos);
processed = 1;
}
if( CLASS_OF(sub) == rb_cURegexp) {
pos = icu_reg_search(sub, str, pos, 0);
processed = 1;
}
if(! processed ) {
rb_raise(rb_eTypeError, "Wrong Type, expected UString or URegexp, got %s", rb_class2name(CLASS_OF(sub)));
}
if (pos == -1)
return Qnil;
return LONG2NUM(pos);
}
static long
icu_ustr_rindex(str, sub, pos)
VALUE str,
sub;
long pos;
{
long len = ICU_LEN(sub);
UChar *found;
/*
* substring longer than string
*/
if (ICU_LEN(str) < len)
return -1;
if (ICU_LEN(str) - pos < len) {
pos = ICU_LEN(str) - len;
}
found = u_strFindLast(ICU_PTR(str), pos, ICU_PTR(sub), ICU_LEN(sub));
if (NULL == found)
return -1;
pos = found - (ICU_PTR(str));
return pos;
}
/**
* call-seq:
* str.rindex(substring [, fixnum]) => fixnum or nil
* str.rindex(regexp [, fixnum]) => fixnum or nil
*
* Returns the index of the last occurrence of the given <i>substring</i>,
* or pattern (<i>regexp</i>) in <i>str</i>. Returns <code>nil</code> if not
* found. If the second parameter is present, it specifies the position in the
* string to end the search---characters beyond this point will not be considered.
*
* "hello".u.rindex('e') #=> 1
* "hello".u.rindex('l') #=> 3
* "hello".u.rindex('a') #=> nil
* "hello".u.rindex(/[aeiou]/.U, -2) #=> 1
*/
VALUE
icu_ustr_rindex_m(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE sub;
VALUE position;
long pos;
if (rb_scan_args(argc, argv, "11", &sub, &position) == 2) {
pos = NUM2LONG(position);
if (pos < 0) {
pos += ICU_LEN(str);
if (pos < 0) {
return Qnil;
}
}
if (pos > ICU_LEN(str))
pos = ICU_LEN(str);
} else {
pos = ICU_LEN(str);
}
switch (TYPE(sub)) {
case T_DATA:
if (CLASS_OF(sub) == rb_cUString) {
pos = icu_ustr_rindex(str, sub, pos);
if (pos >= 0)
return LONG2NUM(pos);
break;
}
if (CLASS_OF(sub) == rb_cURegexp) {
pos = icu_reg_search(sub, str, pos, 1);
if (pos >= 0)
return LONG2NUM(pos);
break;
}
default:
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sub));
}
return Qnil;
}
/**
* call-seq:
* str.lstrip! => self or nil
*
* Removes leading whitespace from <i>str</i>, returning <code>nil</code> if no
* change was made. See also <code>UString#rstrip!</code> and
* <code>UString#strip!</code>, in all these methods whitespace is an
* Unicode char that has White_Space property.
*
* " hello ".u.lstrip #=> "hello "
* "hello".u.lstrip! #=> nil
*/
VALUE
icu_ustr_lstrip_bang(str)
VALUE str;
{
UChar *s;
int32_t i,
n,
c;
icu_check_frozen(1, str);
s = ICU_PTR(str);
n = ICU_LEN(str);
if (!s || n == 0)
return Qnil;
/*
* remove spaces at head
*/
i = 0;
U16_GET(s, 0, i, n, c); /* care about surrogates */
while (i < n && u_isUWhiteSpace(c)) {
U16_NEXT(s, i, n, c); /* care surr */
}
if (i > 0) {
if(! u_isUWhiteSpace(c)) --i;
ICU_LEN(str) = n - i;
u_memmove(ICU_PTR(str), s + i, ICU_LEN(str));
ICU_PTR(str)[ICU_LEN(str)] = 0;
return str;
}
return Qnil;
}
/**
* call-seq:
* str.lstrip => new_str
*
* Returns a copy of <i>str</i> with leading whitespace removed. See also
* <code>UString#rstrip</code> and <code>UString#strip</code>.
*
* " hello ".u.lstrip #=> "hello "
* "hello".u.lstrip #=> "hello"
*/
VALUE
icu_ustr_lstrip(str)
VALUE str;
{
str = icu_ustr_dup(str);
icu_ustr_lstrip_bang(str);
return str;
}
/**
* call-seq:
* str.rstrip! => self or nil
*
* Removes trailing whitespace from <i>str</i>, returning <code>nil</code> if
* no change was made. See also <code>UString#lstrip!</code> and
* <code>UString#strip!</code>.
*
* " hello ".u.rstrip #=> " hello"
* "hello".u.rstrip! #=> nil
*/
VALUE
icu_ustr_rstrip_bang(str)
VALUE str;
{
UChar *s;
int32_t i,
n,
c;
icu_check_frozen(1, str);
s = ICU_PTR(str);
n = ICU_LEN(str);
if (!s || n == 0)
return Qnil;
i = n - 1;
U16_GET(s, 0, n - 1, n, c); /* care surrogates */
i = n;
/*
* remove trailing spaces
*/
while (i > 0 && u_isUWhiteSpace(c)) {
U16_PREV(s, 0, i, c); /* care surrogates */
}
if (i < n) {
if(! u_isUWhiteSpace(c)) ++i;
ICU_LEN(str) = i;
ICU_PTR(str)[i] = 0;
return str;
}
return Qnil;
}
/**
* call-seq:
* str.rstrip => new_str
*
* Returns a copy of <i>str</i> with trailing whitespace removed. See also
* <code>UString#lstrip</code> and <code>UString#strip</code>.
*
* " hello ".u.rstrip #=> " hello"
* "hello".u.rstrip #=> "hello"
*/
VALUE
icu_ustr_rstrip(str)
VALUE str;
{
str = icu_ustr_dup(str);
icu_ustr_rstrip_bang(str);
return str;
}
/**
* call-seq:
* str.strip! => str or nil
*
* Removes leading and trailing whitespace from <i>str</i>. Returns
* <code>nil</code> if <i>str</i> was not altered.
*/