string.c: make array in WANTARRAY
* string.c (WANTARRAY): make array for the result in method functions and pass it to enumerator functions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59736 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5dab63aa4b
commit
ded7e1c2a1
80
string.c
80
string.c
@ -7582,7 +7582,8 @@ enumerator_wantarray(const char *method)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define WANTARRAY(m) enumerator_wantarray(m)
|
#define WANTARRAY(m, size) \
|
||||||
|
(enumerator_wantarray(m) ? rb_ary_new_capa(size) : 0)
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
chomp_newline(const char *p, const char *e, rb_encoding *enc)
|
chomp_newline(const char *p, const char *e, rb_encoding *enc)
|
||||||
@ -7598,14 +7599,13 @@ chomp_newline(const char *p, const char *e, rb_encoding *enc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
|
||||||
{
|
{
|
||||||
rb_encoding *enc;
|
rb_encoding *enc;
|
||||||
VALUE line, rs, orig = str, opts = Qnil, chomp = Qfalse;
|
VALUE line, rs, orig = str, opts = Qnil, chomp = Qfalse;
|
||||||
const char *ptr, *pend, *subptr, *subend, *rsptr, *hit, *adjusted;
|
const char *ptr, *pend, *subptr, *subend, *rsptr, *hit, *adjusted;
|
||||||
long pos, len, rslen;
|
long pos, len, rslen;
|
||||||
int rsnewline = 0;
|
int rsnewline = 0;
|
||||||
VALUE ary = 0;
|
|
||||||
|
|
||||||
if (rb_scan_args(argc, argv, "01:", &rs, &opts) == 0)
|
if (rb_scan_args(argc, argv, "01:", &rs, &opts) == 0)
|
||||||
rs = rb_rs;
|
rs = rb_rs;
|
||||||
@ -7618,11 +7618,8 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
chomp = (chomp != Qundef && RTEST(chomp));
|
chomp = (chomp != Qundef && RTEST(chomp));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wantarray)
|
|
||||||
ary = rb_ary_new();
|
|
||||||
|
|
||||||
if (NIL_P(rs)) {
|
if (NIL_P(rs)) {
|
||||||
if (wantarray) {
|
if (ary) {
|
||||||
rb_ary_push(ary, str);
|
rb_ary_push(ary, str);
|
||||||
return ary;
|
return ary;
|
||||||
}
|
}
|
||||||
@ -7669,7 +7666,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
if (!subptr) break;
|
if (!subptr) break;
|
||||||
line = rb_str_subseq(str, subptr - ptr,
|
line = rb_str_subseq(str, subptr - ptr,
|
||||||
subend - subptr + (chomp ? 0 : rslen));
|
subend - subptr + (chomp ? 0 : rslen));
|
||||||
if (wantarray) {
|
if (ary) {
|
||||||
rb_ary_push(ary, line);
|
rb_ary_push(ary, line);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -7714,7 +7711,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
line = rb_str_subseq(str, subptr - ptr, subend - subptr);
|
line = rb_str_subseq(str, subptr - ptr, subend - subptr);
|
||||||
if (wantarray) {
|
if (ary) {
|
||||||
rb_ary_push(ary, line);
|
rb_ary_push(ary, line);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -7729,7 +7726,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
pend = chomp_newline(subptr, pend, enc);
|
pend = chomp_newline(subptr, pend, enc);
|
||||||
}
|
}
|
||||||
line = rb_str_subseq(str, subptr - ptr, pend - subptr);
|
line = rb_str_subseq(str, subptr - ptr, pend - subptr);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
rb_ary_push(ary, line);
|
rb_ary_push(ary, line);
|
||||||
else
|
else
|
||||||
rb_yield(line);
|
rb_yield(line);
|
||||||
@ -7737,7 +7734,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if (wantarray)
|
if (ary)
|
||||||
return ary;
|
return ary;
|
||||||
else
|
else
|
||||||
return orig;
|
return orig;
|
||||||
@ -7800,7 +7797,8 @@ rb_str_each_line(int argc, VALUE *argv, VALUE str)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_lines(int argc, VALUE *argv, VALUE str)
|
rb_str_lines(int argc, VALUE *argv, VALUE str)
|
||||||
{
|
{
|
||||||
return rb_str_enumerate_lines(argc, argv, str, WANTARRAY("lines"));
|
VALUE ary = WANTARRAY("lines", 0);
|
||||||
|
return rb_str_enumerate_lines(argc, argv, str, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
@ -7810,21 +7808,17 @@ rb_str_each_byte_size(VALUE str, VALUE args, VALUE eobj)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_str_enumerate_bytes(VALUE str, int wantarray)
|
rb_str_enumerate_bytes(VALUE str, VALUE ary)
|
||||||
{
|
{
|
||||||
long i;
|
long i;
|
||||||
VALUE ary;
|
|
||||||
|
|
||||||
if (wantarray)
|
|
||||||
ary = rb_ary_new2(RSTRING_LEN(str));
|
|
||||||
|
|
||||||
for (i=0; i<RSTRING_LEN(str); i++) {
|
for (i=0; i<RSTRING_LEN(str); i++) {
|
||||||
if (wantarray)
|
if (ary)
|
||||||
rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
|
rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
|
||||||
else
|
else
|
||||||
rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
|
rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
|
||||||
}
|
}
|
||||||
if (wantarray)
|
if (ary)
|
||||||
return ary;
|
return ary;
|
||||||
else
|
else
|
||||||
return str;
|
return str;
|
||||||
@ -7866,7 +7860,8 @@ rb_str_each_byte(VALUE str)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_bytes(VALUE str)
|
rb_str_bytes(VALUE str)
|
||||||
{
|
{
|
||||||
return rb_str_enumerate_bytes(str, WANTARRAY("bytes"));
|
VALUE ary = WANTARRAY("bytes", RSTRING_LEN(str));
|
||||||
|
return rb_str_enumerate_bytes(str, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
@ -7876,28 +7871,24 @@ rb_str_each_char_size(VALUE str, VALUE args, VALUE eobj)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_str_enumerate_chars(VALUE str, int wantarray)
|
rb_str_enumerate_chars(VALUE str, VALUE ary)
|
||||||
{
|
{
|
||||||
VALUE orig = str;
|
VALUE orig = str;
|
||||||
VALUE substr;
|
VALUE substr;
|
||||||
long i, len, n;
|
long i, len, n;
|
||||||
const char *ptr;
|
const char *ptr;
|
||||||
rb_encoding *enc;
|
rb_encoding *enc;
|
||||||
VALUE ary;
|
|
||||||
|
|
||||||
str = rb_str_new_frozen(str);
|
str = rb_str_new_frozen(str);
|
||||||
ptr = RSTRING_PTR(str);
|
ptr = RSTRING_PTR(str);
|
||||||
len = RSTRING_LEN(str);
|
len = RSTRING_LEN(str);
|
||||||
enc = rb_enc_get(str);
|
enc = rb_enc_get(str);
|
||||||
|
|
||||||
if (wantarray)
|
|
||||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
|
||||||
|
|
||||||
if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) {
|
if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) {
|
||||||
for (i = 0; i < len; i += n) {
|
for (i = 0; i < len; i += n) {
|
||||||
n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
|
n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
|
||||||
substr = rb_str_subseq(str, i, n);
|
substr = rb_str_subseq(str, i, n);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
rb_ary_push(ary, substr);
|
rb_ary_push(ary, substr);
|
||||||
else
|
else
|
||||||
rb_yield(substr);
|
rb_yield(substr);
|
||||||
@ -7907,14 +7898,14 @@ rb_str_enumerate_chars(VALUE str, int wantarray)
|
|||||||
for (i = 0; i < len; i += n) {
|
for (i = 0; i < len; i += n) {
|
||||||
n = rb_enc_mbclen(ptr + i, ptr + len, enc);
|
n = rb_enc_mbclen(ptr + i, ptr + len, enc);
|
||||||
substr = rb_str_subseq(str, i, n);
|
substr = rb_str_subseq(str, i, n);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
rb_ary_push(ary, substr);
|
rb_ary_push(ary, substr);
|
||||||
else
|
else
|
||||||
rb_yield(substr);
|
rb_yield(substr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RB_GC_GUARD(str);
|
RB_GC_GUARD(str);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
return ary;
|
return ary;
|
||||||
else
|
else
|
||||||
return orig;
|
return orig;
|
||||||
@ -7956,40 +7947,37 @@ rb_str_each_char(VALUE str)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_chars(VALUE str)
|
rb_str_chars(VALUE str)
|
||||||
{
|
{
|
||||||
return rb_str_enumerate_chars(str, WANTARRAY("chars"));
|
VALUE ary = WANTARRAY("chars", rb_str_strlen(str));
|
||||||
|
return rb_str_enumerate_chars(str, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_str_enumerate_codepoints(VALUE str, int wantarray)
|
rb_str_enumerate_codepoints(VALUE str, VALUE ary)
|
||||||
{
|
{
|
||||||
VALUE orig = str;
|
VALUE orig = str;
|
||||||
int n;
|
int n;
|
||||||
unsigned int c;
|
unsigned int c;
|
||||||
const char *ptr, *end;
|
const char *ptr, *end;
|
||||||
rb_encoding *enc;
|
rb_encoding *enc;
|
||||||
VALUE ary = 0;
|
|
||||||
|
|
||||||
if (single_byte_optimizable(str))
|
if (single_byte_optimizable(str))
|
||||||
return rb_str_enumerate_bytes(str, wantarray);
|
return rb_str_enumerate_bytes(str, ary);
|
||||||
|
|
||||||
str = rb_str_new_frozen(str);
|
str = rb_str_new_frozen(str);
|
||||||
ptr = RSTRING_PTR(str);
|
ptr = RSTRING_PTR(str);
|
||||||
end = RSTRING_END(str);
|
end = RSTRING_END(str);
|
||||||
enc = STR_ENC_GET(str);
|
enc = STR_ENC_GET(str);
|
||||||
|
|
||||||
if (wantarray)
|
|
||||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
|
||||||
|
|
||||||
while (ptr < end) {
|
while (ptr < end) {
|
||||||
c = rb_enc_codepoint_len(ptr, end, &n, enc);
|
c = rb_enc_codepoint_len(ptr, end, &n, enc);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
rb_ary_push(ary, UINT2NUM(c));
|
rb_ary_push(ary, UINT2NUM(c));
|
||||||
else
|
else
|
||||||
rb_yield(UINT2NUM(c));
|
rb_yield(UINT2NUM(c));
|
||||||
ptr += n;
|
ptr += n;
|
||||||
}
|
}
|
||||||
RB_GC_GUARD(str);
|
RB_GC_GUARD(str);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
return ary;
|
return ary;
|
||||||
else
|
else
|
||||||
return orig;
|
return orig;
|
||||||
@ -8037,11 +8025,12 @@ rb_str_each_codepoint(VALUE str)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_codepoints(VALUE str)
|
rb_str_codepoints(VALUE str)
|
||||||
{
|
{
|
||||||
return rb_str_enumerate_codepoints(str, WANTARRAY("codepoints"));
|
VALUE ary = WANTARRAY("codepoints", rb_str_strlen(str));
|
||||||
|
return rb_str_enumerate_codepoints(str, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary)
|
||||||
{
|
{
|
||||||
regex_t *reg_grapheme_cluster = NULL;
|
regex_t *reg_grapheme_cluster = NULL;
|
||||||
static regex_t *reg_grapheme_cluster_utf8 = NULL;
|
static regex_t *reg_grapheme_cluster_utf8 = NULL;
|
||||||
@ -8049,10 +8038,9 @@ rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
|||||||
rb_encoding *enc = rb_enc_from_index(encidx);
|
rb_encoding *enc = rb_enc_from_index(encidx);
|
||||||
int unicode_p = rb_enc_unicode_p(enc);
|
int unicode_p = rb_enc_unicode_p(enc);
|
||||||
const char *ptr, *end;
|
const char *ptr, *end;
|
||||||
VALUE ary = 0;
|
|
||||||
|
|
||||||
if (!unicode_p) {
|
if (!unicode_p) {
|
||||||
return rb_str_enumerate_chars(str, wantarray);
|
return rb_str_enumerate_chars(str, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* synchronize */
|
/* synchronize */
|
||||||
@ -8074,9 +8062,6 @@ rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
|||||||
ptr = RSTRING_PTR(str);
|
ptr = RSTRING_PTR(str);
|
||||||
end = RSTRING_END(str);
|
end = RSTRING_END(str);
|
||||||
|
|
||||||
if (wantarray)
|
|
||||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
|
||||||
|
|
||||||
while (ptr < end) {
|
while (ptr < end) {
|
||||||
VALUE grapheme_cluster;
|
VALUE grapheme_cluster;
|
||||||
OnigPosition len = onig_match(reg_grapheme_cluster,
|
OnigPosition len = onig_match(reg_grapheme_cluster,
|
||||||
@ -8087,13 +8072,13 @@ rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
grapheme_cluster = rb_enc_str_new(ptr, len, enc);
|
grapheme_cluster = rb_enc_str_new(ptr, len, enc);
|
||||||
if (wantarray)
|
if (ary)
|
||||||
rb_ary_push(ary, grapheme_cluster);
|
rb_ary_push(ary, grapheme_cluster);
|
||||||
else
|
else
|
||||||
rb_yield(grapheme_cluster);
|
rb_yield(grapheme_cluster);
|
||||||
ptr += len;
|
ptr += len;
|
||||||
}
|
}
|
||||||
if (wantarray)
|
if (ary)
|
||||||
return ary;
|
return ary;
|
||||||
else
|
else
|
||||||
return str;
|
return str;
|
||||||
@ -8135,7 +8120,8 @@ rb_str_each_grapheme_cluster(VALUE str)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_grapheme_clusters(VALUE str)
|
rb_str_grapheme_clusters(VALUE str)
|
||||||
{
|
{
|
||||||
return rb_str_enumerate_grapheme_clusters(str, WANTARRAY("grapheme_clusters"));
|
VALUE ary = WANTARRAY("grapheme_clusters", rb_str_strlen(str));
|
||||||
|
return rb_str_enumerate_grapheme_clusters(str, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
|
Loading…
x
Reference in New Issue
Block a user