22 #define BEG(no) (regs->beg[(no)])
23 #define END(no) (regs->end[(no)])
32 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
34 #undef rb_str_new_cstr
35 #undef rb_tainted_str_new_cstr
36 #undef rb_usascii_str_new_cstr
37 #undef rb_external_str_new_cstr
38 #undef rb_locale_str_new_cstr
43 #undef rb_tainted_str_new2
44 #undef rb_usascii_str_new2
45 #undef rb_str_dup_frozen
46 #undef rb_str_buf_new_cstr
47 #undef rb_str_buf_new2
48 #undef rb_str_buf_cat2
56 #define RUBY_MAX_CHAR_LEN 16
57 #define STR_TMPLOCK FL_USER7
58 #define STR_NOEMBED FL_USER1
59 #define STR_SHARED FL_USER2
60 #define STR_ASSOC FL_USER3
61 #define STR_SHARED_P(s) FL_ALL((s), STR_NOEMBED|ELTS_SHARED)
62 #define STR_ASSOC_P(s) FL_ALL((s), STR_NOEMBED|STR_ASSOC)
63 #define STR_NOCAPA (STR_NOEMBED|ELTS_SHARED|STR_ASSOC)
64 #define STR_NOCAPA_P(s) (FL_TEST((s),STR_NOEMBED) && FL_ANY((s),ELTS_SHARED|STR_ASSOC))
65 #define STR_UNSET_NOCAPA(s) do {\
66 if (FL_TEST((s),STR_NOEMBED)) FL_UNSET((s),(ELTS_SHARED|STR_ASSOC));\
70 #define STR_SET_NOEMBED(str) do {\
71 FL_SET((str), STR_NOEMBED);\
72 STR_SET_EMBED_LEN((str), 0);\
74 #define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED)
75 #define STR_EMBED_P(str) (!FL_TEST((str), STR_NOEMBED))
76 #define STR_SET_EMBED_LEN(str, n) do { \
78 RBASIC(str)->flags &= ~RSTRING_EMBED_LEN_MASK;\
79 RBASIC(str)->flags |= (tmp_n) << RSTRING_EMBED_LEN_SHIFT;\
82 #define STR_SET_LEN(str, n) do { \
83 if (STR_EMBED_P(str)) {\
84 STR_SET_EMBED_LEN((str), (n));\
87 RSTRING(str)->as.heap.len = (n);\
91 #define STR_DEC_LEN(str) do {\
92 if (STR_EMBED_P(str)) {\
93 long n = RSTRING_LEN(str);\
95 STR_SET_EMBED_LEN((str), n);\
98 RSTRING(str)->as.heap.len--;\
102 #define RESIZE_CAPA(str,capacity) do {\
103 if (STR_EMBED_P(str)) {\
104 if ((capacity) > RSTRING_EMBED_LEN_MAX) {\
105 char *tmp = ALLOC_N(char, (capacity)+1);\
106 memcpy(tmp, RSTRING_PTR(str), RSTRING_LEN(str));\
107 RSTRING(str)->as.heap.ptr = tmp;\
108 RSTRING(str)->as.heap.len = RSTRING_LEN(str);\
109 STR_SET_NOEMBED(str);\
110 RSTRING(str)->as.heap.aux.capa = (capacity);\
114 REALLOC_N(RSTRING(str)->as.heap.ptr, char, (capacity)+1);\
115 if (!STR_NOCAPA_P(str))\
116 RSTRING(str)->as.heap.aux.capa = (capacity);\
120 #define is_ascii_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
121 #define is_broken_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN)
123 #define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str))
145 static inline const char *
148 #if SIZEOF_VALUE == 8
149 # define NONASCII_MASK 0x8080808080808080ULL
150 #elif SIZEOF_VALUE == 4
151 # define NONASCII_MASK 0x80808080UL
154 if ((
int)
sizeof(
VALUE) * 2 < e - p) {
157 s = (
const VALUE*)(~lowbits & ((
VALUE)p + lowbits));
158 while (p < (
const char *)s) {
165 if (*s & NONASCII_MASK) {
185 const char *e = p + len;
366 return RSTRING(str)->as.heap.len;
369 return RSTRING(str)->as.heap.aux.capa;
378 str->as.heap.ptr = 0;
379 str->as.heap.len = 0;
380 str->as.heap.aux.capa = 0;
409 RSTRING(str)->as.heap.aux.capa = len;
456 #define rb_str_new2 rb_str_new_cstr
467 #define rb_usascii_str_new2 rb_usascii_str_new_cstr
488 #define rb_tainted_str_new2 rb_tainted_str_new_cstr
499 const unsigned char *start, *sp;
500 unsigned char *dest, *
dp;
501 size_t converted_output = 0;
505 if (from == to)
return str;
520 RBASIC(econv_wrapper)->klass = 0;
527 while ((dest = (
unsigned char*)
RSTRING_PTR(newstr)),
528 (dp = dest + converted_output),
532 size_t converted_input = sp - start;
533 size_t rest = len - converted_input;
534 converted_output = dp - dest;
536 if (converted_input && converted_output &&
537 rest < (
LONG_MAX / converted_output)) {
538 rest = (rest * converted_output) / converted_input;
543 olen += rest < 2 ? 2 : rest;
650 RSTRING(str2)->as.heap.aux.shared = str;
686 #define rb_str_new3 rb_str_new_shared
701 RSTRING(str2)->as.heap.aux.shared = shared;
705 RSTRING(str)->as.heap.aux.shared = str2;
723 if ((ofs > 0) || (klass !=
RBASIC(str)->klass) ||
727 RSTRING(str)->as.heap.ptr += ofs;
728 RSTRING(str)->as.heap.len -= ofs;
741 str = str_new4(klass, orig);
743 RSTRING(str)->as.heap.aux.shared = assoc;
746 str = str_new4(klass, orig);
753 #define rb_str_new4 rb_str_new_frozen
763 #define rb_str_new5 rb_str_new_with_class
766 str_new_empty(
VALUE str)
774 #define STR_BUF_MIN_SIZE 128
785 RSTRING(str)->as.heap.aux.capa = capa;
787 RSTRING(str)->as.heap.ptr[0] =
'\0';
805 #define rb_str_buf_new2 rb_str_buf_new_cstr
841 return RSTRING(str)->as.heap.aux.capa;
861 if (str == str2)
return;
880 RSTRING(str)->as.heap.aux.shared =
RSTRING(str2)->as.heap.aux.shared;
922 RSTRING(str)->as.heap.len = len;
926 RSTRING(str)->as.heap.aux.shared = shared;
973 if (argc > 0 &&
rb_scan_args(argc, argv,
"01", &orig) == 1)
1018 for (c=0; p<e; c++) {
1068 for (c=0; p<e; c++) {
1086 #ifdef NONASCII_MASK
1087 #define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
1102 count_utf8_lead_bytes_with_word(
const VALUE *s)
1109 d &= NONASCII_MASK >> 7;
1114 #if SIZEOF_VALUE == 8
1133 #ifdef NONASCII_MASK
1138 if ((
int)
sizeof(
VALUE) * 2 < e - p) {
1141 s = (
const VALUE*)(~lowbits & ((
VALUE)p + lowbits));
1143 while (p < (
const char *)s) {
1144 if (is_utf8_lead_byte(*p)) len++;
1148 len += count_utf8_lead_bytes_with_word(s);
1151 p = (
const char *)s;
1154 if (is_utf8_lead_byte(*p)) len++;
1287 while (n <= len/2) {
1288 memcpy(ptr2 + n, ptr2, n);
1291 memcpy(ptr2 + n, ptr2, len-n);
1351 long capa = len + expand;
1353 if (len > capa) len = capa;
1354 ptr =
ALLOC_N(
char, capa + 1);
1361 RSTRING(str)->as.heap.ptr = ptr;
1362 RSTRING(str)->as.heap.len = len;
1363 RSTRING(str)->as.heap.aux.capa = capa;
1366 #define str_make_independent(str) str_make_independent_expand((str), 0L)
1385 else if (expand > 0) {
1387 long capa = len + expand;
1390 RSTRING(str)->as.heap.aux.capa = capa;
1416 RSTRING(str)->as.heap.ptr = 0;
1417 RSTRING(str)->as.heap.len = 0;
1435 assoc =
RSTRING(assoc)->as.heap.aux.shared;
1457 return RSTRING(str)->as.heap.aux.shared;
1496 if (!s || memchr(s, 0, len)) {
1542 const char *p2, *e2;
1545 while (p < e && 0 < nth) {
1572 while (p < e && nth--) {
1604 const char *pp =
str_nth(p, e, nth, enc, singlebyte);
1605 if (!pp)
return e -
p;
1616 #ifdef NONASCII_MASK
1618 str_utf8_nth(
const char *
p,
const char *e,
long *nthp)
1621 if ((
int)SIZEOF_VALUE * 2 < e - p && (
int)SIZEOF_VALUE * 2 < nth) {
1624 s = (
const VALUE*)(~lowbits & ((
VALUE)p + lowbits));
1626 while (p < (
const char *)s) {
1627 if (is_utf8_lead_byte(*p)) nth--;
1631 nth -= count_utf8_lead_bytes_with_word(s);
1633 }
while (s < t && (
int)
sizeof(
VALUE) <= nth);
1637 if (is_utf8_lead_byte(*p)) {
1638 if (nth == 0)
break;
1648 str_utf8_offset(
const char *p,
const char *e,
long nth)
1650 const char *pp = str_utf8_nth(p, e, &nth);
1697 if (len < 0)
return 0;
1702 if (beg > blen)
return 0;
1705 if (beg < 0)
return 0;
1707 if (beg + len > blen)
1709 if (len < 0)
return 0;
1714 if (len > -beg) len = -beg;
1728 if (beg < 0)
return 0;
1730 if (len == 0)
goto end;
1740 #ifdef NONASCII_MASK
1743 p = str_utf8_nth(s, e, &beg);
1744 if (beg > 0)
return 0;
1745 len = str_utf8_offset(p, e, len);
1751 p = s + beg * char_sz;
1755 else if (len * char_sz > e - p)
1760 else if ((p =
str_nth_len(s, e, &beg, enc)) == e) {
1761 if (beg > 0)
return 0;
1779 if (!p)
return Qnil;
1784 RSTRING(str2)->as.heap.len = len;
1807 #define rb_str_dup_frozen rb_str_new_frozen
1839 rb_bug(
"probable buffer overflow: %ld for %ld", len, capa);
1862 RSTRING(str)->as.ary[len] =
'\0';
1869 char *ptr =
RSTRING(str)->as.heap.ptr;
1871 if (slen > len) slen = len;
1872 if (slen > 0)
MEMCPY(
RSTRING(str)->as.ary, ptr,
char, slen);
1873 RSTRING(str)->as.ary[len] =
'\0';
1875 if (independent)
xfree(ptr);
1878 else if (!independent) {
1881 else if (slen < len || slen - len > 1024) {
1885 RSTRING(str)->as.heap.aux.capa = len;
1887 RSTRING(str)->as.heap.len = len;
1888 RSTRING(str)->as.heap.ptr[len] =
'\0';
1896 long capa, total, off = -1;
1902 if (len == 0)
return 0;
1911 capa =
RSTRING(str)->as.heap.aux.capa;
1917 if (capa <= total) {
1918 while (total > capa) {
1920 capa = (total + 4095) / 4096;
1923 capa = (capa + 1) * 2;
1937 #define str_buf_cat2(str, ptr) str_buf_cat((str), (ptr), strlen(ptr))
1942 if (len == 0)
return str;
1964 p =
RSTRING(str)->as.heap.ptr;
1965 memcpy(p +
RSTRING(str)->as.heap.len, ptr, len);
1966 len =
RSTRING(str)->as.heap.len += len;
1982 int ptr_encindex,
int ptr_cr,
int *ptr_cr_ret)
1990 if (str_encindex == ptr_encindex) {
2020 *ptr_cr_ret = ptr_cr;
2022 if (str_encindex != ptr_encindex &&
2032 res_encindex = str_encindex;
2037 res_encindex = str_encindex;
2041 res_encindex = ptr_encindex;
2046 res_encindex = str_encindex;
2053 res_encindex = str_encindex;
2086 unsigned int c = (
unsigned char)*ptr;
2129 RSTRING(str)->as.heap.len = len;
2177 buf[0] = (char)code;
2273 #define lesser(a,b) (((a)>(b))?(b):(a))
2285 if (idx1 == idx2)
return TRUE;
2304 const char *ptr1, *ptr2;
2307 if (str1 == str2)
return 0;
2310 if (ptr1 == ptr2 || (retval =
memcmp(ptr1, ptr2,
lesser(len1, len2))) == 0) {
2319 if (len1 > len2)
return 1;
2322 if (retval > 0)
return 1;
2331 const char *ptr1, *ptr2;
2337 if (
memcmp(ptr1, ptr2, len) == 0)
2353 if (str1 == str2)
return Qtrue;
2373 if (str1 == str2)
return Qtrue;
2440 char *p1, *p1end, *p2, *p2end;
2451 while (p1 < p1end && p2 < p2end) {
2453 unsigned int c1 =
TOUPPER(*p1 & 0xff);
2454 unsigned int c2 =
TOUPPER(*p2 & 0xff);
2456 return INT2FIX(c1 < c2 ? -1 : 1);
2463 while (p1 < p1end && p2 < p2end) {
2467 if (0 <= c1 && 0 <= c2) {
2471 return INT2FIX(c1 < c2 ? -1 : 1);
2477 len = l1 < l2 ? l1 : l2;
2480 return INT2FIX(r < 0 ? -1 : 1);
2482 return INT2FIX(l1 < l2 ? -1 : 1);
2509 if (offset < 0)
return -1;
2511 if (len - offset < slen)
return -1;
2518 if (slen == 0)
return offset;
2521 slen = RSTRING_LEN(sub);
2522 len = RSTRING_LEN(str) - offset;
2526 if (pos < 0)
return pos;
2528 if (t == s + pos)
break;
2529 if ((len -= t - s) <= 0)
return -1;
2533 return pos + offset;
2561 if (
rb_scan_args(argc, argv,
"11", &sub, &initpos) == 2) {
2607 if (pos == -1)
return Qnil;
2615 char *s, *sbeg, *e, *t;
2626 if (len < slen)
return -1;
2627 if (len - pos < slen) {
2637 s =
str_nth(sbeg, e, pos, enc, singlebyte);
2639 if (
memcmp(s, t, slen) == 0) {
2642 if (pos == 0)
break;
2676 if (
rb_scan_args(argc, argv,
"11", &sub, &vpos) == 2) {
2687 if (pos > len) pos = len;
2704 if (pos >= 0)
return LONG2NUM(pos);
2721 if (pos >= 0)
return LONG2NUM(pos);
2822 for (i = len-1; 0 <= i && (
unsigned char)p[i] == 0xff; i--)
2826 ++((
unsigned char*)p)[
i];
2834 memset(p+l, 0xff, len-l);
2840 for (len2 = len-1; 0 < len2; len2--) {
2845 memset(p+len2+1, 0xff, len-(len2+1));
2856 for (i = len-1; 0 <= i && (
unsigned char)p[i] == 0; i--)
2860 --((
unsigned char*)p)[
i];
2868 memset(p+l, 0, len-l);
2874 for (len2 = len-1; 0 < len2; len2--) {
2879 memset(p+len2+1, 0, len-(len2+1));
2910 MEMCPY(save, p,
char, len);
2917 MEMCPY(p, save,
char, len);
2920 MEMCPY(save, p,
char, len);
2925 MEMCPY(p, save,
char, len);
2930 MEMCPY(p, save,
char, len);
2940 MEMCPY(carry, p,
char, len);
2944 MEMCPY(carry, p,
char, len);
2980 char *sbeg, *s, *e, *last_alnum = 0;
2984 long carry_pos = 0, carry_len = 1;
3016 carry_pos = s - sbeg;
3032 MEMCPY(carry, s,
char, l);
3035 carry_pos = s - sbeg;
3040 memmove(s + carry_len, s, RSTRING_LEN(str) - carry_pos);
3102 VALUE end, exclusive;
3103 VALUE current, after_end;
3110 excl =
RTEST(exclusive);
3120 if (c > e || (excl && c == e))
return beg;
3123 if (!excl && c == e)
break;
3125 if (excl && c == e)
break;
3154 if (excl && bi == ei)
break;
3175 if (n > 0 || (excl && n == 0))
return beg;
3184 if (
NIL_P(next))
break;
3345 if (len > olen) len = olen;
3353 memmove(ptr, oldptr + len, nlen);
3358 ptr =
RSTRING(str)->as.heap.ptr += len;
3359 RSTRING(str)->as.heap.len = nlen;
3424 if (slen < len || slen < beg + len) {
3453 long start, end, len;
3499 switch (
TYPE(indx)) {
3629 for (i=0; i<
argc; i++) {
3634 if (!
NIL_P(result)) {
3646 switch (
TYPE(pat)) {
3716 if (iter || !
NIL_P(hash)) {
3764 memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
3766 memcpy(p + beg0, rp, rlen);
3834 long offset, blen, slen, len,
last;
3860 if (bang)
return Qnil;
3881 if (iter || !
NIL_P(hash)) {
3917 offset = end0 + len;
3956 return str_gsub(argc, argv, str, 1);
4006 return str_gsub(argc, argv, str, 0);
4025 if (str == str2)
return str;
4122 if (beg > n || len < 0)
return Qnil;
4125 if (beg < 0)
return Qnil;
4140 RSTRING(str2)->as.heap.len = len;
4174 switch (
TYPE(indx)) {
4270 if (clen > 1 || (*s & 0x80)) single = 0;
4280 if (clen > 1 || (*s & 0x80)) single = 0;
4357 if (i == -1)
return Qfalse;
4388 if (argc == 0) base = 10;
4452 #define CHAR_ESC_LEN 13
4467 else if (c < 0x10000) {
4503 const char *
p, *pend, *prev;
4521 const unsigned char *q = (
const unsigned char *)p;
4522 if (q[0] == 0xFE && q[1] == 0xFF)
4524 else if (q[0] == 0xFF && q[1] == 0xFE)
4529 else if (enc == utf32) {
4530 const unsigned char *q = (
const unsigned char *)p;
4531 if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF)
4533 else if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF)
4544 if (p > prev)
str_buf_cat(result, prev, p - prev);
4547 n = (int)(pend - p);
4558 if ((asciicompat || unicode_p) &&
4559 (c ==
'"'|| c ==
'\\' ||
4564 (cc ==
'$' || cc ==
'@' || cc ==
'{'))))) {
4565 if (p - n > prev)
str_buf_cat(result, prev, p - n - prev);
4567 if (asciicompat || enc == resenc) {
4573 case '\n': cc =
'n';
break;
4574 case '\r': cc =
'r';
break;
4575 case '\t': cc =
't';
break;
4576 case '\f': cc =
'f';
break;
4577 case '\013': cc =
'v';
break;
4578 case '\010': cc =
'b';
break;
4579 case '\007': cc =
'a';
break;
4580 case 033: cc =
'e';
break;
4581 default: cc = 0;
break;
4584 if (p - n > prev)
str_buf_cat(result, prev, p - n - prev);
4596 if (p - n > prev)
str_buf_cat(result, prev, p - n - prev);
4602 if (p > prev)
str_buf_cat(result, prev, p - prev);
4609 #define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
4626 const char *
p, *pend;
4634 unsigned char c = *p++;
4636 case '"':
case '\\':
4637 case '\n':
case '\r':
4638 case '\t':
case '\f':
4639 case '\013':
case '\010':
case '\007':
case '\033':
4656 while (cc >>= 4) len++;
4673 p =
RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
4678 unsigned char c = *p++;
4680 if (c ==
'"' || c ==
'\\') {
4684 else if (c ==
'#') {
4685 if (
IS_EVSTR(p, pend)) *q++ =
'\\';
4688 else if (c ==
'\n') {
4692 else if (c ==
'\r') {
4696 else if (c ==
'\t') {
4700 else if (c ==
'\f') {
4704 else if (c ==
'\013') {
4708 else if (c ==
'\010') {
4712 else if (c ==
'\007') {
4716 else if (c ==
'\033') {
4742 snprintf(q, qend-q,
".force_encoding(\"%s\")", enc->
name);
4785 unsigned int c = *(
unsigned char*)s;
4788 *s =
'A' + (c -
'a');
4800 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
4802 *s =
'A' + (c -
'a');
4819 if (modify)
return str;
4867 unsigned int c = *(
unsigned char*)s;
4870 *s =
'a' + (c -
'A');
4883 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
4885 *s =
'a' + (c -
'A');
4902 if (modify)
return str;
4972 if (modify)
return str;
5036 if (modify)
return str;
5077 if (t->
p == t->
pend)
return -1;
5085 if (t->
p < t->
pend) {
5089 if (t->
now < 0x80 && c < 0x80) {
5091 "invalid range \"%c-%c\" in string transliteration",
5128 const unsigned int errc = -1;
5129 unsigned int trans[256];
5131 struct tr trsrc, trrepl;
5133 unsigned int c, c0,
last = 0;
5134 int modify = 0,
i, l;
5140 #define CHECK_IF_ASCII(c) \
5141 (void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
5142 (cr = ENC_CODERANGE_VALID) : 0)
5163 trsrc.
p + l < trsrc.
pend) {
5169 trsrc.
gen = trrepl.
gen = 0;
5170 trsrc.
now = trrepl.
now = 0;
5171 trsrc.
max = trrepl.
max = 0;
5174 for (
i=0;
i<256;
i++) {
5177 while ((c =
trnext(&trsrc, enc)) != errc) {
5186 while ((c =
trnext(&trrepl, enc)) != errc)
5189 for (
i=0;
i<256;
i++) {
5190 if (trans[
i] != errc) {
5198 for (
i=0;
i<256;
i++) {
5201 while ((c =
trnext(&trsrc, enc)) != errc) {
5202 r =
trnext(&trrepl, enc);
5203 if (r == errc) r = trrepl.
now;
5222 unsigned int save = -1;
5238 if (cflag) c =
last;
5241 else if (cflag) c = errc;
5247 if (c != (
unsigned int)-1) {
5259 if (enc != e1) may_modify = 1;
5261 while (t - buf + tlen >= max) {
5268 if (may_modify &&
memcmp(s, t, tlen) != 0) {
5285 c = (
unsigned char)*s;
5286 if (trans[c] != errc) {
5317 if (cflag) c =
last;
5320 else if (cflag) c = errc;
5324 c = cflag ? last : errc;
5332 if (enc != e1) may_modify = 1;
5334 while (t - buf + tlen >= max) {
5342 if (may_modify &&
memcmp(s, t, tlen) != 0) {
5382 return tr_trans(str, src, repl, 0);
5429 #define TR_TABLE_SIZE 257
5434 const unsigned int errc = -1;
5438 VALUE table = 0, ptable = 0;
5439 int i, l, cflag = 0;
5449 for (i=0; i<256; i++) {
5452 stable[256] = cflag;
5454 else if (stable[256] && !cflag) {
5457 for (i=0; i<256; i++) {
5461 while ((c =
trnext(&tr, enc)) != errc) {
5463 buf[c & 0xff] = !cflag;
5468 if (!table && (first || *tablep || stable[256])) {
5485 for (i=0; i<256; i++) {
5486 stable[
i] = stable[
i] && buf[
i];
5488 if (!table && !cflag) {
5498 return table[
c] != 0;
5530 VALUE del = 0, nodel = 0;
5532 int i, ascompat, cr;
5536 for (i=0; i<
argc; i++) {
5553 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5566 if (
tr_find(c, squeez, del, nodel)) {
5581 if (modify)
return str;
5622 VALUE del = 0, nodel = 0;
5632 for (i=0; i<
argc; i++) {
5652 unsigned int c = *(
unsigned char*)s++;
5653 if (c != save || (argc > 0 && !squeez[c])) {
5662 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5663 if (c != save || (argc > 0 && !squeez[c])) {
5671 if (c != save || (argc > 0 && !
tr_find(c, squeez, del, nodel))) {
5687 if (modify)
return str;
5727 return tr_trans(str, src, repl, 1);
5785 VALUE del = 0, nodel = 0;
5791 for (i=0; i<
argc; i++) {
5805 if (*(
unsigned char*)s++ == c) n++;
5820 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5829 if (
tr_find(c, table, del, nodel)) {
5840 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
5841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5842 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5843 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5844 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5845 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5846 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5847 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5848 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5849 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5850 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5851 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5852 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5853 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5854 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5855 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5858 #define ascii_isspace(c) isspacetable[(unsigned char)(c)]
5913 enum {awk, string, regexp} split_type;
5914 long beg, end,
i = 0;
5918 if (
rb_scan_args(argc, argv,
"02", &spat, &limit) == 2) {
5920 if (lim <= 0) limit =
Qnil;
5921 else if (lim == 1) {
5942 split_type = string;
5946 split_type = regexp;
5963 split_type = regexp;
5969 if (split_type == awk) {
5978 while (ptr < eptr) {
5979 c = (
unsigned char)*ptr++;
5987 if (!
NIL_P(limit) && lim <=
i)
break;
6002 while (ptr < eptr) {
6014 if (!
NIL_P(limit) && lim <= i)
break;
6029 else if (split_type ==
string) {
6043 while (ptr < eptr &&
6044 (end =
rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
6047 if (t != ptr + end) {
6053 if (!
NIL_P(limit) && lim <= ++i)
break;
6067 if (start == end &&
BEG(0) ==
END(0)) {
6072 else if (last_null == 1) {
6080 if (ptr+start == ptr+len)
6090 beg = start =
END(0);
6094 for (idx=1; idx < regs->
num_regs; idx++) {
6095 if (
BEG(idx) == -1)
continue;
6096 if (
BEG(idx) ==
END(idx))
6097 tmp = str_new_empty(str);
6102 if (!
NIL_P(limit) && lim <= ++i)
break;
6107 tmp = str_new_empty(str);
6112 if (
NIL_P(limit) && lim == 0) {
6138 unsigned int newline;
6139 const char *
p, *pend, *s, *ptr;
6156 rb_warn(
"given block not used");
6159 rb_warning(
"passing a block to String#lines is deprecated");
6184 len = RSTRING_LEN(str);
6191 p = memchr(p,
'\n', pend - p);
6211 rslen = RSTRING_LEN(rs);
6223 if (rslen == 0 && c == newline) {
6236 const char *pp = p + (rslen ? rslen : n);
6338 rb_warn(
"given block not used");
6341 rb_warning(
"passing a block to String#bytes is deprecated");
6410 const char *end_ptr = ptr + len;
6411 for (len = 0; ptr < end_ptr; ++len) {
6431 rb_warn(
"given block not used");
6434 rb_warning(
"passing a block to String#chars is deprecated");
6453 for (i = 0; i < len; i += n) {
6463 for (i = 0; i < len; i += n) {
6524 const char *ptr, *end;
6534 rb_warn(
"given block not used");
6537 rb_warning(
"passing a block to String#codepoints is deprecated");
6615 const char *
p, *p2, *beg, *end;
6619 if (beg > end)
return 0;
6699 if (len == 0)
return Qnil;
6751 while (len>0 && p[len-1] ==
'\n') {
6753 if (len>0 && p[len-1] ==
'\r')
6763 if (rslen > len)
return Qnil;
6765 if (rslen == 1 && newline ==
'\n')
6773 if (p[len-1] == newline &&
6906 while (s < t && ((c = *(t-1)) ==
'\0' ||
ascii_isspace(c))) t--;
6921 RSTRING_PTR(str)[len] =
'\0';
7013 for (i=1; i < regs->
num_regs; i++) {
7059 long last = -1, prev = 0;
7148 extern char *
crypt(
const char *,
const char *);
7150 const char *s, *saltp;
7153 char salt_8bit_clean[3];
7164 if (!
ISASCII((
unsigned char)saltp[0]) || !
ISASCII((
unsigned char)saltp[1])) {
7165 salt_8bit_clean[0] = saltp[0] & 0x7f;
7166 salt_8bit_clean[1] = saltp[1] & 0x7f;
7167 salt_8bit_clean[2] =
'\0';
7168 saltp = salt_8bit_clean;
7171 res =
crypt(s, saltp);
7246 char *ptr, *
p, *pend;
7249 unsigned long sum0 = 0;
7268 sum0 += (
unsigned char)*p;
7279 if (bits < (
int)
sizeof(
long)*
CHAR_BIT) {
7280 sum0 &= (((
unsigned long)1)<<bits)-1;
7304 long width, len, flen = 1, fclen = 1;
7307 const char *
f =
" ";
7308 long n,
size, llen, rlen, llen2 = 0, rlen2 = 0;
7310 int singlebyte = 1, cr;
7322 if (flen == 0 || fclen == 0) {
7327 if (width < 0 || len >= width)
return rb_str_dup(str);
7329 llen = (jflag ==
'l') ? 0 : ((jflag ==
'r') ? n : n/2);
7333 llen2 =
str_offset(f, f + flen, llen % fclen, enc, singlebyte);
7334 rlen2 =
str_offset(f, f + flen, rlen % fclen, enc, singlebyte);
7337 if ((len = llen / fclen + rlen / fclen) >=
LONG_MAX / flen ||
7338 (len *= flen) >=
LONG_MAX - llen2 - rlen2 ||
7339 (len += llen2 + rlen2) >=
LONG_MAX - size) {
7346 memset(p, *f, llen);
7350 while (llen >= fclen) {
7356 memcpy(p, f, llen2);
7363 memset(p, *f, rlen);
7367 while (rlen >= fclen) {
7373 memcpy(p, f, rlen2);
7487 return rb_ary_new3(3, str, str_new_empty(str), str_new_empty(str));
7491 if (pos == 0 &&
RSTRING_LEN(sep) == 0)
goto failed;
7537 return rb_ary_new3(3, str_new_empty(str), str_new_empty(str), str);
7565 for (i=0; i<
argc; i++) {
7590 for (i=0; i<
argc; i++) {
7597 s = e - RSTRING_LEN(tmp);
7703 static const char ellipsis[] =
"...";
7704 const long ellipsislen =
sizeof(ellipsis) - 1;
7707 const char *
const p =
RSTRING_PTR(str), *e = p + blen;
7708 VALUE estr, ret = 0;
7712 (e =
rb_enc_nth(p, e, len, enc)) - p == blen) {
7715 else if (len <= ellipsislen ||
7782 if (sym1 == sym2)
return Qtrue;
7878 memcpy(dest + 1, ptr, len);
7945 enum {SYM_PROC_CACHE_SIZE = 67};
7950 if (!sym_proc_cache) {
7957 index = (
id % SYM_PROC_CACHE_SIZE) << 1;
7960 if (aryp[index] == sym) {
7961 return aryp[index + 1];
7966 aryp[index + 1] = proc;
8148 switch (
TYPE(name)) {
8185 #define rb_intern(str) rb_intern_const(str)