* encoding.c (rb_enc_nth): moved to string.c.
* string.c (rb_enc_nth): moved from string.c. use search_nonascii for ASCII compatible string. (str_nth): wrong optimization removed to fix "a".force_encoding("EUC-JP").slice!(0,10) returns "a\x00\x00\x00\x00\x00\x00\x00\x00\x00" git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0040ed4020
commit
0831222a91
10
ChangeLog
10
ChangeLog
@ -1,3 +1,13 @@
|
|||||||
|
Sat Feb 16 03:43:18 2008 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* encoding.c (rb_enc_nth): moved to string.c.
|
||||||
|
|
||||||
|
* string.c (rb_enc_nth): moved from string.c. use search_nonascii
|
||||||
|
for ASCII compatible string.
|
||||||
|
(str_nth): wrong optimization removed to fix
|
||||||
|
"a".force_encoding("EUC-JP").slice!(0,10) returns
|
||||||
|
"a\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
||||||
|
|
||||||
Sat Feb 16 00:21:49 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Feb 16 00:21:49 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* range.c (rb_range_beg_len): check if responds to "begin" and "end"
|
* range.c (rb_range_beg_len): check if responds to "begin" and "end"
|
||||||
|
22
encoding.c
22
encoding.c
@ -720,28 +720,6 @@ rb_obj_encoding(VALUE obj)
|
|||||||
return rb_enc_from_encoding(enc);
|
return rb_enc_from_encoding(enc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char*
|
|
||||||
rb_enc_nth(const char *p, const char *e, int nth, rb_encoding *enc)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if (rb_enc_mbmaxlen(enc) == 1) {
|
|
||||||
p += nth;
|
|
||||||
}
|
|
||||||
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
|
|
||||||
p += nth * rb_enc_mbmaxlen(enc);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (c=0; p<e && nth--; c++) {
|
|
||||||
int n = rb_enc_mbclen(p, e, enc);
|
|
||||||
|
|
||||||
p += n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (char*)p;
|
|
||||||
}
|
|
||||||
|
|
||||||
long
|
long
|
||||||
rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
|
rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
|
||||||
{
|
{
|
||||||
|
53
string.c
53
string.c
@ -891,20 +891,57 @@ rb_str_s_try_convert(VALUE dummy, VALUE str)
|
|||||||
return rb_check_string_type(str);
|
return rb_check_string_type(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char*
|
||||||
|
rb_enc_nth(const char *p, const char *e, int nth, rb_encoding *enc)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
|
||||||
|
if (rb_enc_mbmaxlen(enc) == 1) {
|
||||||
|
p += nth;
|
||||||
|
}
|
||||||
|
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
|
||||||
|
p += nth * rb_enc_mbmaxlen(enc);
|
||||||
|
}
|
||||||
|
else if (rb_enc_asciicompat(enc)) {
|
||||||
|
const char *p2, *e2;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
while (p < e && 0 < nth) {
|
||||||
|
e2 = p + nth;
|
||||||
|
if (e < e2)
|
||||||
|
return (char *)e;
|
||||||
|
if (ISASCII(*p)) {
|
||||||
|
p2 = search_nonascii(p, e2);
|
||||||
|
if (!p2)
|
||||||
|
return (char *)e2;
|
||||||
|
nth -= p2 - p;
|
||||||
|
p = p2;
|
||||||
|
}
|
||||||
|
n = rb_enc_mbclen(p, e, enc);
|
||||||
|
p += n;
|
||||||
|
nth--;
|
||||||
|
}
|
||||||
|
if (nth != 0)
|
||||||
|
return (char *)e;
|
||||||
|
return (char *)p;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (c=0; p<e && nth--; c++) {
|
||||||
|
int n = rb_enc_mbclen(p, e, enc);
|
||||||
|
|
||||||
|
p += n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (p > e) p = e;
|
||||||
|
return (char*)p;
|
||||||
|
}
|
||||||
|
|
||||||
static char*
|
static char*
|
||||||
str_nth(const char *p, const char *e, int nth, rb_encoding *enc, int singlebyte)
|
str_nth(const char *p, const char *e, int nth, rb_encoding *enc, int singlebyte)
|
||||||
{
|
{
|
||||||
if (singlebyte)
|
if (singlebyte)
|
||||||
p += nth;
|
p += nth;
|
||||||
else {
|
else {
|
||||||
if (rb_enc_asciicompat(enc)) {
|
|
||||||
const char *p2 = search_nonascii(p, e);
|
|
||||||
|
|
||||||
if (!p2 || p + nth < p2)
|
|
||||||
return (char*)p + nth;
|
|
||||||
nth -= p2 - p;
|
|
||||||
p = p2;
|
|
||||||
}
|
|
||||||
p = rb_enc_nth(p, e, nth, enc);
|
p = rb_enc_nth(p, e, nth, enc);
|
||||||
}
|
}
|
||||||
if (!p) return 0;
|
if (!p) return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user