mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Scan the coderange in the given encoding
This commit is contained in:
parent
457a4913be
commit
391abc543c
3 changed files with 46 additions and 9 deletions
|
@ -7,8 +7,22 @@ enc_str_buf_cat(VALUE str, VALUE str2)
|
||||||
return rb_enc_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2), rb_enc_get(str2));
|
return rb_enc_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2), rb_enc_get(str2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
str_conv_enc_opts(VALUE str, VALUE from, VALUE to, VALUE ecflags, VALUE ecopts)
|
||||||
|
{
|
||||||
|
rb_encoding *from_enc = NIL_P(from) ? NULL : rb_to_encoding(from);
|
||||||
|
rb_encoding *to_enc = NIL_P(to) ? NULL : rb_to_encoding(to);
|
||||||
|
int flags = NUM2INT(ecflags);
|
||||||
|
if (!NIL_P(ecopts)) {
|
||||||
|
Check_Type(ecopts, T_HASH);
|
||||||
|
OBJ_FREEZE(ecopts);
|
||||||
|
}
|
||||||
|
return rb_str_conv_enc_opts(str, from_enc, to_enc, flags, ecopts);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Init_string_enc_str_buf_cat(VALUE klass)
|
Init_string_enc_str_buf_cat(VALUE klass)
|
||||||
{
|
{
|
||||||
rb_define_method(klass, "enc_str_buf_cat", enc_str_buf_cat, 1);
|
rb_define_method(klass, "enc_str_buf_cat", enc_str_buf_cat, 1);
|
||||||
|
rb_define_method(klass, "str_conv_enc_opts", str_conv_enc_opts, 4);
|
||||||
}
|
}
|
||||||
|
|
32
string.c
32
string.c
|
@ -697,6 +697,18 @@ rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
|
||||||
ENC_CODERANGE_SET(dest, ENC_CODERANGE(src));
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE(src));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
enc_coderange_scan(VALUE str, rb_encoding *enc, int encidx)
|
||||||
|
{
|
||||||
|
if (rb_enc_mbminlen(enc) > 1 && rb_enc_dummy_p(enc) &&
|
||||||
|
rb_enc_mbminlen(enc = get_actual_encoding(encidx, str)) == 1) {
|
||||||
|
return ENC_CODERANGE_BROKEN;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
rb_enc_str_coderange(VALUE str)
|
rb_enc_str_coderange(VALUE str)
|
||||||
{
|
{
|
||||||
|
@ -705,14 +717,7 @@ rb_enc_str_coderange(VALUE str)
|
||||||
if (cr == ENC_CODERANGE_UNKNOWN) {
|
if (cr == ENC_CODERANGE_UNKNOWN) {
|
||||||
int encidx = ENCODING_GET(str);
|
int encidx = ENCODING_GET(str);
|
||||||
rb_encoding *enc = rb_enc_from_index(encidx);
|
rb_encoding *enc = rb_enc_from_index(encidx);
|
||||||
if (rb_enc_mbminlen(enc) > 1 && rb_enc_dummy_p(enc) &&
|
cr = enc_coderange_scan(str, enc, encidx);
|
||||||
rb_enc_mbminlen(enc = get_actual_encoding(encidx, str)) == 1) {
|
|
||||||
cr = ENC_CODERANGE_BROKEN;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cr = coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str),
|
|
||||||
enc);
|
|
||||||
}
|
|
||||||
ENC_CODERANGE_SET(str, cr);
|
ENC_CODERANGE_SET(str, cr);
|
||||||
}
|
}
|
||||||
return cr;
|
return cr;
|
||||||
|
@ -954,6 +959,15 @@ static VALUE str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long
|
||||||
rb_encoding *from, rb_encoding *to,
|
rb_encoding *from, rb_encoding *to,
|
||||||
int ecflags, VALUE ecopts);
|
int ecflags, VALUE ecopts);
|
||||||
|
|
||||||
|
static inline bool
|
||||||
|
is_enc_ascii_string(VALUE str, rb_encoding *enc)
|
||||||
|
{
|
||||||
|
int encidx = rb_enc_to_index(enc);
|
||||||
|
if (rb_enc_get_index(str) == encidx)
|
||||||
|
return is_ascii_string(str);
|
||||||
|
return enc_coderange_scan(str, enc, encidx) == ENC_CODERANGE_7BIT;
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
|
rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
|
||||||
{
|
{
|
||||||
|
@ -964,7 +978,7 @@ rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags,
|
||||||
if (!to) return str;
|
if (!to) return str;
|
||||||
if (!from) from = rb_enc_get(str);
|
if (!from) from = rb_enc_get(str);
|
||||||
if (from == to) return str;
|
if (from == to) return str;
|
||||||
if ((rb_enc_asciicompat(to) && is_ascii_string(str)) ||
|
if ((rb_enc_asciicompat(to) && is_enc_ascii_string(str, from)) ||
|
||||||
to == rb_ascii8bit_encoding()) {
|
to == rb_ascii8bit_encoding()) {
|
||||||
if (STR_ENC_GET(str) != to) {
|
if (STR_ENC_GET(str) != to) {
|
||||||
str = rb_str_dup(str);
|
str = rb_str_dup(str);
|
||||||
|
|
|
@ -13,4 +13,13 @@ class Test_StringEncStrBufCat < Test::Unit::TestCase
|
||||||
assert_equal(:unknown, Bug::String.new(cr_unknown_str).coderange, "an assertion for following tests")
|
assert_equal(:unknown, Bug::String.new(cr_unknown_str).coderange, "an assertion for following tests")
|
||||||
assert_equal(:valid, Bug::String.new(a8_str).enc_str_buf_cat(cr_unknown_str).coderange, Bug6509)
|
assert_equal(:valid, Bug::String.new(a8_str).enc_str_buf_cat(cr_unknown_str).coderange, Bug6509)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_str_conv_enc
|
||||||
|
str = Bug::String.new("aaa".encode("US-ASCII"))
|
||||||
|
assert_same(str, str.str_conv_enc_opts("UTF-8", "US-ASCII", 0, nil))
|
||||||
|
|
||||||
|
str = Bug::String.new("aaa".encode("UTF-16LE").force_encoding("UTF-8"))
|
||||||
|
assert_predicate(str, :ascii_only?) # cache coderange
|
||||||
|
assert_equal("aaa", str.str_conv_enc_opts("UTF-16LE", "UTF-8", 0, nil))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue