22 #define BEG(no) (regs->beg[(no)])
23 #define END(no) (regs->end[(no)])
32 #define STRING_ENUMERATORS_WANTARRAY 0
34 #undef rb_str_new_cstr
35 #undef rb_tainted_str_new_cstr
36 #undef rb_usascii_str_new_cstr
37 #undef rb_enc_str_new_cstr
38 #undef rb_external_str_new_cstr
39 #undef rb_locale_str_new_cstr
40 #undef rb_str_dup_frozen
41 #undef rb_str_buf_new_cstr
42 #undef rb_str_buf_cat2
50 #define RUBY_MAX_CHAR_LEN 16
51 #define STR_TMPLOCK FL_USER7
52 #define STR_UNSET_NOCAPA(s) do {\
53 if (FL_TEST((s),STR_NOEMBED)) FL_UNSET((s),(ELTS_SHARED|STR_ASSOC));\
56 #define STR_SET_NOEMBED(str) do {\
57 FL_SET((str), STR_NOEMBED);\
58 STR_SET_EMBED_LEN((str), 0);\
60 #define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED)
61 #define STR_SET_EMBED_LEN(str, n) do { \
63 RBASIC(str)->flags &= ~RSTRING_EMBED_LEN_MASK;\
64 RBASIC(str)->flags |= (tmp_n) << RSTRING_EMBED_LEN_SHIFT;\
67 #define STR_SET_LEN(str, n) do { \
68 if (STR_EMBED_P(str)) {\
69 STR_SET_EMBED_LEN((str), (n));\
72 RSTRING(str)->as.heap.len = (n);\
76 #define STR_DEC_LEN(str) do {\
77 if (STR_EMBED_P(str)) {\
78 long n = RSTRING_LEN(str);\
80 STR_SET_EMBED_LEN((str), n);\
83 RSTRING(str)->as.heap.len--;\
87 #define TERM_LEN(str) rb_enc_mbminlen(rb_enc_get(str))
88 #define TERM_FILL(ptr, termlen) do {\
89 char *const term_fill_ptr = (ptr);\
90 const int term_fill_len = (termlen);\
91 *term_fill_ptr = '\0';\
92 if (UNLIKELY(term_fill_len > 1))\
93 memset(term_fill_ptr, 0, term_fill_len);\
96 #define RESIZE_CAPA(str,capacity) do {\
97 const int termlen = TERM_LEN(str);\
98 if (STR_EMBED_P(str)) {\
99 if ((capacity) > RSTRING_EMBED_LEN_MAX) {\
100 char *const tmp = ALLOC_N(char, (capacity)+termlen);\
101 const long tlen = RSTRING_LEN(str);\
102 memcpy(tmp, RSTRING_PTR(str), tlen);\
103 RSTRING(str)->as.heap.ptr = tmp;\
104 RSTRING(str)->as.heap.len = tlen;\
105 STR_SET_NOEMBED(str);\
106 RSTRING(str)->as.heap.aux.capa = (capacity);\
110 REALLOC_N(RSTRING(str)->as.heap.ptr, char, (capacity)+termlen);\
111 if (!STR_NOCAPA_P(str))\
112 RSTRING(str)->as.heap.aux.capa = (capacity);\
116 #define STR_SET_SHARED(str, shared_str) do { \
117 RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
118 FL_SET((str), ELTS_SHARED); \
121 #define STR_HEAP_PTR(str) (RSTRING(str)->as.heap.ptr)
122 #define STR_HEAP_SIZE(str) (RSTRING(str)->as.heap.aux.capa + TERM_LEN(str))
124 #define STR_ENC_GET(str) get_encoding(str)
131 const unsigned char *q;
134 case ENCINDEX_UTF_16:
137 if (q[0] == 0xFE && q[1] == 0xFF) {
140 if (q[0] == 0xFF && q[1] == 0xFE) {
144 case ENCINDEX_UTF_32:
147 if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF) {
150 if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF) {
196 *key = *value = *fstr = str;
253 static inline const char *
256 #if SIZEOF_VALUE == 8
257 # define NONASCII_MASK 0x8080808080808080ULL
258 #elif SIZEOF_VALUE == 4
259 # define NONASCII_MASK 0x80808080UL
262 if ((
int)
sizeof(
VALUE) * 2 < e - p) {
265 s = (
const VALUE*)(~lowbits & ((
VALUE)p + lowbits));
266 while (p < (
const char *)s) {
273 if (*s & NONASCII_MASK) {
293 const char *e = p + len;
474 return RSTRING(str)->as.heap.len;
477 return RSTRING(str)->as.heap.aux.capa;
512 RSTRING(str)->as.heap.aux.capa = len;
530 return str_new0(klass, ptr, len, 1);
615 const unsigned char *start, *sp;
616 unsigned char *dest, *
dp;
617 size_t converted_output = 0;
621 if (from == to)
return str;
644 while ((dest = (
unsigned char*)
RSTRING_PTR(newstr)),
645 (dp = dest + converted_output),
649 size_t converted_input = sp - start;
650 size_t rest = len - converted_input;
651 converted_output = dp - dest;
653 if (converted_input && converted_output &&
654 rest < (
LONG_MAX / converted_output)) {
655 rest = (rest * converted_output) / converted_input;
660 olen += rest < 2 ? 2 : rest;
843 if ((ofs > 0) || (klass !=
RBASIC(str)->klass) ||
847 RSTRING(str)->as.heap.ptr += ofs;
848 RSTRING(str)->as.heap.len -= ofs;
888 #define STR_BUF_MIN_SIZE 128
899 RSTRING(str)->as.heap.aux.capa = capa;
901 RSTRING(str)->as.heap.ptr[0] =
'\0';
976 if (str == str2)
return;
1038 RSTRING(str)->as.heap.len = len;
1089 if (argc > 0 &&
rb_scan_args(argc, argv,
"01", &orig) == 1)
1134 for (c=0; p<e; c++) {
1184 for (c=0; p<e; c++) {
1202 #ifdef NONASCII_MASK
1203 #define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
1218 count_utf8_lead_bytes_with_word(
const VALUE *s)
1225 d &= NONASCII_MASK >> 7;
1230 #if SIZEOF_VALUE == 8
1249 #ifdef NONASCII_MASK
1254 if ((
int)
sizeof(
VALUE) * 2 < e - p) {
1257 s = (
const VALUE*)(~lowbits & ((
VALUE)p + lowbits));
1259 while (p < (
const char *)s) {
1260 if (is_utf8_lead_byte(*p)) len++;
1264 len += count_utf8_lead_bytes_with_word(s);
1267 p = (
const char *)s;
1270 if (is_utf8_lead_byte(*p)) len++;
1403 while (n <= len/2) {
1404 memcpy(ptr2 + n, ptr2, n);
1407 memcpy(ptr2 + n, ptr2, len-n);
1466 long capa = len + expand;
1468 if (len > capa) len = capa;
1469 ptr =
ALLOC_N(
char, capa + termlen);
1476 RSTRING(str)->as.heap.ptr = ptr;
1477 RSTRING(str)->as.heap.len = len;
1478 RSTRING(str)->as.heap.aux.capa = capa;
1481 #define str_make_independent(str) str_make_independent_expand((str), 0L)
1500 else if (expand > 0) {
1502 long capa = len + expand;
1507 RSTRING(str)->as.heap.aux.capa = capa;
1533 RSTRING(str)->as.heap.ptr = 0;
1534 RSTRING(str)->as.heap.len = 0;
1552 assoc =
RSTRING(assoc)->as.heap.aux.shared;
1574 return RSTRING(str)->as.heap.aux.shared;
1609 for (; n > 0; --n) {
1618 const char *e = s + len;
1631 if (capa < len + termlen) {
1658 if (!s || memchr(s, 0, len)) {
1713 const char *p2, *e2;
1716 while (p < e && 0 < nth) {
1743 while (p < e && nth--) {
1775 const char *pp =
str_nth(p, e, nth, enc, singlebyte);
1776 if (!pp)
return e - p;
1787 #ifdef NONASCII_MASK
1789 str_utf8_nth(
const char *p,
const char *e,
long *nthp)
1792 if ((
int)SIZEOF_VALUE * 2 < e - p && (
int)SIZEOF_VALUE * 2 < nth) {
1795 s = (
const VALUE*)(~lowbits & ((
VALUE)p + lowbits));
1797 while (p < (
const char *)s) {
1798 if (is_utf8_lead_byte(*p)) nth--;
1802 nth -= count_utf8_lead_bytes_with_word(s);
1804 }
while (s < t && (
int)
sizeof(
VALUE) <= nth);
1808 if (is_utf8_lead_byte(*p)) {
1809 if (nth == 0)
break;
1819 str_utf8_offset(
const char *p,
const char *e,
long nth)
1821 const char *pp = str_utf8_nth(p, e, &nth);
1868 if (len < 0)
return 0;
1873 if (beg > blen)
return 0;
1876 if (beg < 0)
return 0;
1878 if (beg + len > blen)
1880 if (len < 0)
return 0;
1885 if (len > -beg) len = -beg;
1899 if (beg < 0)
return 0;
1901 if (len == 0)
goto end;
1911 #ifdef NONASCII_MASK
1914 p = str_utf8_nth(s, e, &beg);
1915 if (beg > 0)
return 0;
1916 len = str_utf8_offset(p, e, len);
1922 p = s + beg * char_sz;
1926 else if (len * char_sz > e - p)
1931 else if ((p =
str_nth_len(s, e, &beg, enc)) == e) {
1932 if (beg > 0)
return 0;
1950 if (!p)
return Qnil;
1955 RSTRING(str2)->as.heap.len = len;
1978 #define rb_str_dup_frozen rb_str_new_frozen
2018 rb_bug(
"probable buffer overflow: %ld for %ld", len, capa);
2041 if (len == slen)
return str;
2053 if (slen > len) slen = len;
2054 if (slen > 0)
MEMCPY(
RSTRING(str)->as.ary, ptr,
char, slen);
2060 else if (!independent) {
2061 if (len == slen)
return str;
2064 else if ((capa =
RSTRING(str)->as.heap.aux.capa) < len ||
2065 (capa - len) > (len < 1024 ? len : 1024)) {
2067 RSTRING(str)->as.heap.aux.capa = len;
2069 else if (len == slen)
return str;
2070 RSTRING(str)->as.heap.len = len;
2079 long capa, total, off = -1;
2086 if (len == 0)
return 0;
2095 capa =
RSTRING(str)->as.heap.aux.capa;
2101 if (capa <= total) {
2102 while (total > capa) {
2103 if (capa + termlen >=
LONG_MAX / 2) {
2104 capa = (total + 4095) / 4096 * 4096;
2107 capa = (capa + termlen) * 2;
2121 #define str_buf_cat2(str, ptr) str_buf_cat((str), (ptr), strlen(ptr))
2126 if (len == 0)
return str;
2148 p =
RSTRING(str)->as.heap.ptr;
2149 memcpy(p +
RSTRING(str)->as.heap.len, ptr, len);
2150 len =
RSTRING(str)->as.heap.len += len;
2166 int ptr_encindex,
int ptr_cr,
int *ptr_cr_ret)
2174 if (str_encindex == ptr_encindex) {
2204 *ptr_cr_ret = ptr_cr;
2206 if (str_encindex != ptr_encindex &&
2216 res_encindex = str_encindex;
2221 res_encindex = str_encindex;
2225 res_encindex = ptr_encindex;
2230 res_encindex = str_encindex;
2237 res_encindex = str_encindex;
2270 unsigned int c = (
unsigned char)*ptr;
2306 long len1 =
RSTRING(str)->as.heap.len, len = len1 + len2;
2314 RSTRING(str)->as.heap.len = len;
2362 buf[0] = (char)code;
2458 #define lesser(a,b) (((a)>(b))?(b):(a))
2470 if (idx1 == idx2)
return TRUE;
2489 const char *ptr1, *ptr2;
2492 if (str1 == str2)
return 0;
2495 if (ptr1 == ptr2 || (retval =
memcmp(ptr1, ptr2,
lesser(len1, len2))) == 0) {
2504 if (len1 > len2)
return 1;
2507 if (retval > 0)
return 1;
2516 const char *ptr1, *ptr2;
2522 if (
memcmp(ptr1, ptr2, len) == 0)
2545 if (str1 == str2)
return Qtrue;
2565 if (str1 == str2)
return Qtrue;
2632 char *p1, *p1end, *p2, *p2end;
2643 while (p1 < p1end && p2 < p2end) {
2645 unsigned int c1 =
TOUPPER(*p1 & 0xff);
2646 unsigned int c2 =
TOUPPER(*p2 & 0xff);
2648 return INT2FIX(c1 < c2 ? -1 : 1);
2655 while (p1 < p1end && p2 < p2end) {
2659 if (0 <= c1 && 0 <= c2) {
2663 return INT2FIX(c1 < c2 ? -1 : 1);
2669 len = l1 < l2 ? l1 : l2;
2672 return INT2FIX(r < 0 ? -1 : 1);
2674 return INT2FIX(l1 < l2 ? -1 : 1);
2689 long pos, len, slen;
2700 if (offset < 0)
return -1;
2702 if (len - offset < slen)
return -1;
2707 offset =
str_offset(s, e, offset, enc, single_byte);
2710 if (slen == 0)
return offset;
2713 slen = RSTRING_LEN(sub);
2714 len = RSTRING_LEN(str) - offset;
2718 if (pos < 0)
return pos;
2720 if (t == s + pos)
break;
2722 if (len <= 0)
return -1;
2726 return pos + offset;
2754 if (
rb_scan_args(argc, argv,
"11", &sub, &initpos) == 2) {
2800 if (pos == -1)
return Qnil;
2808 char *hit, *adjusted;
2810 long slen, searchlen;
2814 if (slen == 0)
return pos;
2819 searchlen = s - sbeg + 1;
2822 hit = memrchr(sbeg, c, searchlen);
2825 if (hit != adjusted) {
2826 searchlen = adjusted - sbeg;
2829 if (
memcmp(hit, t, slen) == 0)
2831 searchlen = adjusted - sbeg;
2832 }
while (searchlen > 0);
2849 if (
memcmp(s, t, slen) == 0) {
2852 if (pos == 0)
break;
2876 if (len < slen)
return -1;
2877 if (len - pos < slen) pos = len - slen;
2878 if (len == 0)
return pos;
2920 if (
rb_scan_args(argc, argv,
"11", &sub, &vpos) == 2) {
2931 if (pos > len) pos = len;
2948 if (pos >= 0)
return LONG2NUM(pos);
2965 if (pos >= 0)
return LONG2NUM(pos);
3084 for (i = len-1; 0 <= i && (
unsigned char)p[i] == 0xff; i--)
3088 ++((
unsigned char*)p)[i];
3096 memset(p+l, 0xff, len-l);
3102 for (len2 = len-1; 0 < len2; len2--) {
3107 memset(p+len2+1, 0xff, len-(len2+1));
3137 for (i = len-1; 0 <= i && (
unsigned char)p[i] == 0; i--)
3141 --((
unsigned char*)p)[i];
3149 memset(p+l, 0, len-l);
3155 for (len2 = len-1; 0 < len2; len2--) {
3160 memset(p+len2+1, 0, len-(len2+1));
3191 MEMCPY(save, p,
char, len);
3198 MEMCPY(p, save,
char, len);
3201 MEMCPY(save, p,
char, len);
3206 MEMCPY(p, save,
char, len);
3211 MEMCPY(p, save,
char, len);
3221 MEMCPY(carry, p,
char, len);
3225 MEMCPY(carry, p,
char, len);
3261 char *sbeg, *s, *e, *last_alnum = 0;
3265 long carry_pos = 0, carry_len = 1;
3299 carry_pos = s - sbeg;
3328 MEMCPY(carry, s,
char, l);
3331 carry_pos = s - sbeg;
3336 memmove(s + carry_len, s, RSTRING_LEN(str) - carry_pos);
3398 VALUE end, exclusive;
3399 VALUE current, after_end;
3406 excl =
RTEST(exclusive);
3416 if (c > e || (excl && c == e))
return beg;
3419 if (!excl && c == e)
break;
3421 if (excl && c == e)
break;
3450 if (excl && bi == ei)
break;
3471 if (n > 0 || (excl && n == 0))
return beg;
3480 if (
NIL_P(next))
break;
3641 if (len > olen) len = olen;
3649 memmove(ptr, oldptr + len, nlen);
3654 ptr =
RSTRING(str)->as.heap.ptr += len;
3655 RSTRING(str)->as.heap.len = nlen;
3720 if (slen < len || slen < beg + len) {
3749 long start, end, len;
3795 switch (
TYPE(indx)) {
3925 for (i=0; i<
argc; i++) {
3930 if (!
NIL_P(result)) {
3942 switch (
TYPE(pat)) {
4010 if (iter || !
NIL_P(hash)) {
4057 memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
4059 memcpy(p + beg0, rp, rlen);
4126 long offset, blen, slen, len,
last;
4152 if (bang)
return Qnil;
4173 if (iter || !
NIL_P(hash)) {
4192 len = beg0 - offset;
4209 offset = end0 + len;
4248 return str_gsub(argc, argv, str, 1);
4298 return str_gsub(argc, argv, str, 0);
4317 if (str == str2)
return str;
4414 if (beg > n || len < 0)
return Qnil;
4417 if (beg < 0)
return Qnil;
4432 RSTRING(str2)->as.heap.len = len;
4466 switch (
TYPE(indx)) {
4562 if (clen > 1 || (*s & 0x80)) single = 0;
4572 if (clen > 1 || (*s & 0x80)) single = 0;
4649 if (i == -1)
return Qfalse;
4680 if (argc == 0) base = 10;
4744 #define CHAR_ESC_LEN 13
4759 else if (c < 0x10000) {
4796 const char *p, *pend, *prev;
4811 if (actenc != enc) {
4821 if (p > prev)
str_buf_cat(result, prev, p - prev);
4824 n = (int)(pend - p);
4835 if ((asciicompat || unicode_p) &&
4836 (c ==
'"'|| c ==
'\\' ||
4841 (cc ==
'$' || cc ==
'@' || cc ==
'{'))))) {
4842 if (p - n > prev)
str_buf_cat(result, prev, p - n - prev);
4844 if (asciicompat || enc == resenc) {
4850 case '\n': cc =
'n';
break;
4851 case '\r': cc =
'r';
break;
4852 case '\t': cc =
't';
break;
4853 case '\f': cc =
'f';
break;
4854 case '\013': cc =
'v';
break;
4855 case '\010': cc =
'b';
break;
4856 case '\007': cc =
'a';
break;
4857 case 033: cc =
'e';
break;
4858 default: cc = 0;
break;
4861 if (p - n > prev)
str_buf_cat(result, prev, p - n - prev);
4873 if (p - n > prev)
str_buf_cat(result, prev, p - n - prev);
4879 if (p > prev)
str_buf_cat(result, prev, p - prev);
4886 #define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
4903 const char *p, *pend;
4911 unsigned char c = *p++;
4913 case '"':
case '\\':
4914 case '\n':
case '\r':
4915 case '\t':
case '\f':
4916 case '\013':
case '\010':
case '\007':
case '\033':
4929 if (u8 && c > 0x7F) {
4933 while (cc >>= 4) len++;
4950 p =
RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
4955 unsigned char c = *p++;
4957 if (c ==
'"' || c ==
'\\') {
4961 else if (c ==
'#') {
4962 if (
IS_EVSTR(p, pend)) *q++ =
'\\';
4965 else if (c ==
'\n') {
4969 else if (c ==
'\r') {
4973 else if (c ==
'\t') {
4977 else if (c ==
'\f') {
4981 else if (c ==
'\013') {
4985 else if (c ==
'\010') {
4989 else if (c ==
'\007') {
4993 else if (c ==
'\033') {
5019 snprintf(q, qend-q,
".force_encoding(\"%s\")", enc->
name);
5062 unsigned int c = *(
unsigned char*)s;
5065 *s =
'A' + (c -
'a');
5077 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5079 *s =
'A' + (c -
'a');
5096 if (modify)
return str;
5144 unsigned int c = *(
unsigned char*)s;
5147 *s =
'a' + (c -
'A');
5160 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5162 *s =
'a' + (c -
'A');
5179 if (modify)
return str;
5249 if (modify)
return str;
5313 if (modify)
return str;
5354 if (t->
p == t->
pend)
return -1;
5362 if (t->
p < t->
pend) {
5366 if (t->
now < 0x80 && c < 0x80) {
5368 "invalid range \"%c-%c\" in string transliteration",
5405 const unsigned int errc = -1;
5406 unsigned int trans[256];
5408 struct tr trsrc, trrepl;
5410 unsigned int c, c0,
last = 0;
5411 int modify = 0, i, l;
5417 #define CHECK_IF_ASCII(c) \
5418 (void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
5419 (cr = ENC_CODERANGE_VALID) : 0)
5440 trsrc.
p + l < trsrc.
pend) {
5446 trsrc.
gen = trrepl.
gen = 0;
5447 trsrc.
now = trrepl.
now = 0;
5448 trsrc.
max = trrepl.
max = 0;
5451 for (i=0; i<256; i++) {
5454 while ((c =
trnext(&trsrc, enc)) != errc) {
5463 while ((c =
trnext(&trrepl, enc)) != errc)
5466 for (i=0; i<256; i++) {
5467 if (trans[i] != errc) {
5475 for (i=0; i<256; i++) {
5478 while ((c =
trnext(&trsrc, enc)) != errc) {
5479 r =
trnext(&trrepl, enc);
5480 if (r == errc) r = trrepl.
now;
5499 unsigned int save = -1;
5515 if (cflag) c =
last;
5518 else if (cflag) c = errc;
5524 if (c != (
unsigned int)-1) {
5536 if (enc != e1) may_modify = 1;
5538 while (t - buf + tlen >= max) {
5545 if (may_modify &&
memcmp(s, t, tlen) != 0) {
5562 c = (
unsigned char)*s;
5563 if (trans[c] != errc) {
5594 if (cflag) c =
last;
5597 else if (cflag) c = errc;
5601 c = cflag ? last : errc;
5609 if (enc != e1) may_modify = 1;
5611 while (t - buf + tlen >= max) {
5619 if (may_modify &&
memcmp(s, t, tlen) != 0) {
5659 return tr_trans(str, src, repl, 0);
5706 #define TR_TABLE_SIZE 257
5711 const unsigned int errc = -1;
5715 VALUE table = 0, ptable = 0;
5716 int i, l, cflag = 0;
5726 for (i=0; i<256; i++) {
5729 stable[256] = cflag;
5731 else if (stable[256] && !cflag) {
5734 for (i=0; i<256; i++) {
5738 while ((c =
trnext(&tr, enc)) != errc) {
5740 buf[c & 0xff] = !cflag;
5745 if (!table && (first || *tablep || stable[256])) {
5762 for (i=0; i<256; i++) {
5763 stable[i] = stable[i] && buf[i];
5765 if (!table && !cflag) {
5775 return table[
c] != 0;
5807 VALUE del = 0, nodel = 0;
5809 int i, ascompat, cr;
5813 for (i=0; i<
argc; i++) {
5830 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5843 if (
tr_find(c, squeez, del, nodel)) {
5858 if (modify)
return str;
5899 VALUE del = 0, nodel = 0;
5909 for (i=0; i<
argc; i++) {
5929 unsigned int c = *(
unsigned char*)s++;
5930 if (c != save || (argc > 0 && !squeez[c])) {
5939 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
5940 if (c != save || (argc > 0 && !squeez[c])) {
5948 if (c != save || (argc > 0 && !
tr_find(c, squeez, del, nodel))) {
5964 if (modify)
return str;
6004 return tr_trans(str, src, repl, 1);
6062 VALUE del = 0, nodel = 0, tstr;
6086 if (*(
unsigned char*)s++ == c) n++;
6093 for (i=1; i<
argc; i++) {
6108 if (ascompat && (c = *(
unsigned char*)s) < 0x80) {
6117 if (
tr_find(c, table, del, nodel)) {
6128 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
6129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6130 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6138 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
6146 #define ascii_isspace(c) isspacetable[(unsigned char)(c)]
6201 enum {awk, string, regexp} split_type;
6202 long beg, end, i = 0;
6206 if (
rb_scan_args(argc, argv,
"02", &spat, &limit) == 2) {
6208 if (lim <= 0) limit =
Qnil;
6209 else if (lim == 1) {
6230 split_type = string;
6234 split_type = regexp;
6251 split_type = regexp;
6257 if (split_type == awk) {
6266 while (ptr < eptr) {
6267 c = (
unsigned char)*ptr++;
6275 if (!
NIL_P(limit) && lim <= i)
break;
6282 if (!
NIL_P(limit)) ++i;
6290 while (ptr < eptr) {
6302 if (!
NIL_P(limit) && lim <= i)
break;
6309 if (!
NIL_P(limit)) ++i;
6317 else if (split_type ==
string) {
6331 while (ptr < eptr &&
6332 (end =
rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
6335 if (t != ptr + end) {
6341 if (!
NIL_P(limit) && lim <= ++i)
break;
6355 if (start == end &&
BEG(0) ==
END(0)) {
6360 else if (last_null == 1) {
6368 if (ptr+start == ptr+len)
6378 beg = start =
END(0);
6382 for (idx=1; idx < regs->
num_regs; idx++) {
6383 if (
BEG(idx) == -1)
continue;
6384 if (
BEG(idx) ==
END(idx))
6390 if (!
NIL_P(limit) && lim <= ++i)
break;
6400 if (
NIL_P(limit) && lim == 0) {
6425 VALUE line, rs, orig = str;
6426 const char *ptr, *pend, *subptr, *subend, *rsptr, *hit, *adjusted;
6427 long pos, len, rslen;
6428 int paragraph_mode = 0;
6439 #if STRING_ENUMERATORS_WANTARRAY
6440 rb_warn(
"given block not used");
6443 rb_warning(
"passing a block to String#lines is deprecated");
6494 while (subptr < pend) {
6495 pos =
rb_memsearch(rsptr, rslen, subptr, pend - subptr, enc);
6499 if (hit != adjusted) {
6503 subend = hit + rslen;
6504 if (paragraph_mode) {
6520 if (subptr != pend) {
6608 #if STRING_ENUMERATORS_WANTARRAY
6609 rb_warn(
"given block not used");
6612 rb_warning(
"passing a block to String#bytes is deprecated");
6697 #if STRING_ENUMERATORS_WANTARRAY
6698 rb_warn(
"given block not used");
6701 rb_warning(
"passing a block to String#chars is deprecated");
6716 for (i = 0; i < len; i += n) {
6726 for (i = 0; i < len; i += n) {
6787 const char *ptr, *end;
6801 #if STRING_ENUMERATORS_WANTARRAY
6802 rb_warn(
"given block not used");
6805 rb_warning(
"passing a block to String#codepoints is deprecated");
6879 const char *p, *p2, *beg, *end;
6883 if (beg > end)
return 0;
6963 if (len == 0)
return Qnil;
7015 while (len>0 && p[len-1] ==
'\n') {
7017 if (len>0 && p[len-1] ==
'\r')
7027 if (rslen > len)
return Qnil;
7029 if (rslen == 1 && newline ==
'\n')
7037 if (p[len-1] == newline &&
7170 while (s < t && ((c = *(t-1)) ==
'\0' ||
ascii_isspace(c))) t--;
7185 RSTRING_PTR(str)[len] =
'\0';
7277 for (i=1; i < regs->
num_regs; i++) {
7323 long last = -1, prev = 0;
7412 extern char *
crypt(
const char *,
const char *);
7414 const char *s, *saltp;
7417 char salt_8bit_clean[3];
7428 if (!
ISASCII((
unsigned char)saltp[0]) || !
ISASCII((
unsigned char)saltp[1])) {
7429 salt_8bit_clean[0] = saltp[0] & 0x7f;
7430 salt_8bit_clean[1] = saltp[1] & 0x7f;
7431 salt_8bit_clean[2] =
'\0';
7432 saltp = salt_8bit_clean;
7435 res =
crypt(s, saltp);
7510 char *ptr, *p, *pend;
7513 unsigned long sum0 = 0;
7532 sum0 += (
unsigned char)*p;
7543 if (bits < (
int)
sizeof(
long)*
CHAR_BIT) {
7544 sum0 &= (((
unsigned long)1)<<bits)-1;
7568 long width, len, flen = 1, fclen = 1;
7571 const char *
f =
" ";
7572 long n,
size, llen, rlen, llen2 = 0, rlen2 = 0;
7574 int singlebyte = 1, cr;
7586 if (flen == 0 || fclen == 0) {
7591 if (width < 0 || len >= width)
return rb_str_dup(str);
7593 llen = (jflag ==
'l') ? 0 : ((jflag ==
'r') ? n : n/2);
7597 llen2 =
str_offset(f, f + flen, llen % fclen, enc, singlebyte);
7598 rlen2 =
str_offset(f, f + flen, rlen % fclen, enc, singlebyte);
7601 if ((len = llen / fclen + rlen / fclen) >=
LONG_MAX / flen ||
7602 (len *= flen) >=
LONG_MAX - llen2 - rlen2 ||
7603 (len += llen2 + rlen2) >=
LONG_MAX - size) {
7610 memset(p, *f, llen);
7614 while (llen >= fclen) {
7620 memcpy(p, f, llen2);
7627 memset(p, *f, rlen);
7631 while (rlen >= fclen) {
7637 memcpy(p, f, rlen2);
7755 if (pos == 0 &&
RSTRING_LEN(sep) == 0)
goto failed;
7833 for (i=0; i<
argc; i++) {
7834 VALUE tmp = argv[i];
7858 for (i=0; i<
argc; i++) {
7859 VALUE tmp = argv[i];
7865 s = e - RSTRING_LEN(tmp);
7971 static const char ellipsis[] =
"...";
7972 const long ellipsislen =
sizeof(ellipsis) - 1;
7975 const char *
const p =
RSTRING_PTR(str), *e = p + blen;
7976 VALUE estr, ret = 0;
7980 (e =
rb_enc_nth(p, e, len, enc)) - p == blen) {
7983 else if (len <= ellipsislen ||
8056 #define DEFAULT_REPLACE_CHAR(str) do { \
8057 static const char replace[sizeof(str)-1] = str; \
8058 rep = replace; replen = (int)sizeof(replace); \
8074 else if (!
NIL_P(repl)) {
8113 if (e - p < clen) clen = e - p;
8120 for (; clen > 1; clen--) {
8189 else if (encidx == ENCINDEX_UTF_16BE) {
8192 else if (encidx == ENCINDEX_UTF_16LE) {
8195 else if (encidx == ENCINDEX_UTF_32BE) {
8198 else if (encidx == ENCINDEX_UTF_32LE) {
8219 if (e - p < clen) clen = e - p;
8220 if (clen <= mbminlen * 2) {
8225 for (; clen > mbminlen; clen-=mbminlen) {
8362 if (sym1 == sym2)
return Qtrue;
8460 memcpy(dest + 1, ptr, len);
8527 enum {SYM_PROC_CACHE_SIZE = 67};
8532 if (!sym_proc_cache) {
8539 index = (
id % SYM_PROC_CACHE_SIZE) << 1;
8542 if (aryp[index] == sym) {
8543 return aryp[index + 1];
8548 aryp[index + 1] = proc;
8765 #define rb_intern(str) rb_intern_const(str)