| 1 |
#include "internal.h" |
| 2 |
#include "internal/string.h" |
| 3 |
#include "ruby/encoding.h" |
| 4 |
|
| 5 |
static VALUE |
| 6 |
bug_str_cstr_term(VALUE str) |
| 7 |
{ |
| 8 |
long len; |
| 9 |
char *s; |
| 10 |
int c; |
| 11 |
rb_encoding *enc; |
| 12 |
|
| 13 |
len = RSTRING_LEN(str); |
| 14 |
s = StringValueCStr(str); |
| 15 |
rb_gc(); |
| 16 |
enc = rb_enc_get(str); |
| 17 |
c = rb_enc_codepoint(&s[len], &s[len+rb_enc_mbminlen(enc)], enc); |
| 18 |
return INT2NUM(c); |
| 19 |
} |
| 20 |
|
| 21 |
static VALUE |
| 22 |
bug_str_cstr_unterm(VALUE str, VALUE c) |
| 23 |
{ |
| 24 |
long len; |
| 25 |
|
| 26 |
rb_str_modify(str); |
| 27 |
len = RSTRING_LEN(str); |
| 28 |
RSTRING_PTR(str)[len] = NUM2CHR(c); |
| 29 |
return str; |
| 30 |
} |
| 31 |
|
| 32 |
static VALUE |
| 33 |
bug_str_cstr_term_char(VALUE str) |
| 34 |
{ |
| 35 |
long len; |
| 36 |
char *s; |
| 37 |
int c; |
| 38 |
rb_encoding *enc = rb_enc_get(str); |
| 39 |
|
| 40 |
RSTRING_GETMEM(str, s, len); |
| 41 |
s += len; |
| 42 |
len = rb_enc_mbminlen(enc); |
| 43 |
c = rb_enc_precise_mbclen(s, s + len, enc); |
| 44 |
if (!MBCLEN_CHARFOUND_P(c)) { |
| 45 |
c = (unsigned char)*s; |
| 46 |
} |
| 47 |
else { |
| 48 |
c = rb_enc_mbc_to_codepoint(s, s + len, enc); |
| 49 |
if (!c) return Qnil; |
| 50 |
} |
| 51 |
return rb_enc_uint_chr((unsigned int)c, enc); |
| 52 |
} |
| 53 |
|
| 54 |
static VALUE |
| 55 |
bug_str_unterminated_substring(VALUE str, VALUE vbeg, VALUE vlen) |
| 56 |
{ |
| 57 |
long beg = NUM2LONG(vbeg); |
| 58 |
long len = NUM2LONG(vlen); |
| 59 |
rb_str_modify(str); |
| 60 |
if (len < 0) rb_raise(rb_eArgError, "negative length: %ld", len); |
| 61 |
if (RSTRING_LEN(str) < beg) rb_raise(rb_eIndexError, "beg: %ld", beg); |
| 62 |
if (RSTRING_LEN(str) < beg + len) rb_raise(rb_eIndexError, "end: %ld", beg + len); |
| 63 |
str = rb_str_new_shared(str); |
| 64 |
RSTRING(str)->len = len; |
| 65 |
if (STR_EMBED_P(str)) { |
| 66 |
memmove(RSTRING(str)->as.embed.ary, RSTRING(str)->as.embed.ary + beg, len); |
| 67 |
} |
| 68 |
else { |
| 69 |
RSTRING(str)->as.heap.ptr += beg; |
| 70 |
} |
| 71 |
return str; |
| 72 |
} |
| 73 |
|
| 74 |
static VALUE |
| 75 |
bug_str_s_cstr_term(VALUE self, VALUE str) |
| 76 |
{ |
| 77 |
Check_Type(str, T_STRING); |
| 78 |
return bug_str_cstr_term(str); |
| 79 |
} |
| 80 |
|
| 81 |
static VALUE |
| 82 |
bug_str_s_cstr_unterm(VALUE self, VALUE str, VALUE c) |
| 83 |
{ |
| 84 |
Check_Type(str, T_STRING); |
| 85 |
return bug_str_cstr_unterm(str, c); |
| 86 |
} |
| 87 |
|
| 88 |
static VALUE |
| 89 |
bug_str_s_cstr_term_char(VALUE self, VALUE str) |
| 90 |
{ |
| 91 |
Check_Type(str, T_STRING); |
| 92 |
return bug_str_cstr_term_char(str); |
| 93 |
} |
| 94 |
|
| 95 |
#define TERM_LEN(str) rb_enc_mbminlen(rb_enc_get(str)) |
| 96 |
#define TERM_FILL(ptr, termlen) do {\ |
| 97 |
char *const term_fill_ptr = (ptr);\ |
| 98 |
const int term_fill_len = (termlen);\ |
| 99 |
*term_fill_ptr = '\0';\ |
| 100 |
if (UNLIKELY(term_fill_len > 1))\ |
| 101 |
memset(term_fill_ptr, 0, term_fill_len);\ |
| 102 |
} while (0) |
| 103 |
|
| 104 |
static VALUE |
| 105 |
bug_str_s_cstr_noembed(VALUE self, VALUE str) |
| 106 |
{ |
| 107 |
VALUE str2 = rb_str_new(NULL, 0); |
| 108 |
long capacity = RSTRING_LEN(str) + TERM_LEN(str); |
| 109 |
char *buf = ALLOC_N(char, capacity); |
| 110 |
Check_Type(str, T_STRING); |
| 111 |
FL_SET((str2), STR_NOEMBED); |
| 112 |
memcpy(buf, RSTRING_PTR(str), capacity); |
| 113 |
RBASIC(str2)->flags &= ~(STR_SHARED | FL_USER5 | FL_USER6); |
| 114 |
RSTRING(str2)->as.heap.aux.capa = capacity; |
| 115 |
RSTRING(str2)->as.heap.ptr = buf; |
| 116 |
RSTRING(str2)->len = RSTRING_LEN(str); |
| 117 |
TERM_FILL(RSTRING_END(str2), TERM_LEN(str)); |
| 118 |
return str2; |
| 119 |
} |
| 120 |
|
| 121 |
static VALUE |
| 122 |
bug_str_s_cstr_embedded_p(VALUE self, VALUE str) |
| 123 |
{ |
| 124 |
return STR_EMBED_P(str) ? Qtrue : Qfalse; |
| 125 |
} |
| 126 |
|
| 127 |
static VALUE |
| 128 |
bug_str_s_rb_str_new_frozen(VALUE self, VALUE str) |
| 129 |
{ |
| 130 |
return rb_str_new_frozen(str); |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
Init_string_cstr(VALUE klass) |
| 135 |
{ |
| 136 |
rb_define_method(klass, "cstr_term", bug_str_cstr_term, 0); |
| 137 |
rb_define_method(klass, "cstr_unterm", bug_str_cstr_unterm, 1); |
| 138 |
rb_define_method(klass, "cstr_term_char", bug_str_cstr_term_char, 0); |
| 139 |
rb_define_method(klass, "unterminated_substring", bug_str_unterminated_substring, 2); |
| 140 |
rb_define_singleton_method(klass, "cstr_term", bug_str_s_cstr_term, 1); |
| 141 |
rb_define_singleton_method(klass, "cstr_unterm", bug_str_s_cstr_unterm, 2); |
| 142 |
rb_define_singleton_method(klass, "cstr_term_char", bug_str_s_cstr_term_char, 1); |
| 143 |
rb_define_singleton_method(klass, "cstr_noembed", bug_str_s_cstr_noembed, 1); |
| 144 |
rb_define_singleton_method(klass, "cstr_embedded?", bug_str_s_cstr_embedded_p, 1); |
| 145 |
rb_define_singleton_method(klass, "rb_str_new_frozen", bug_str_s_rb_str_new_frozen, 1); |
| 146 |
} |