Project

General

Profile

Feature #6670 ยป string_bytes_to_array.patch

zzak (Zachary Scott), 11/19/2012 11:59 AM

View differences:

string.c
6238 6238
    return str;
6239 6239
}
6240 6240

  
6241
static VALUE
6242
rb_str_bytes(VALUE str)
6243
{
6244
    long i;
6245
    VALUE ary = rb_ary_new();
6246

  
6247
    for (i=0; i<RSTRING_LEN(str); i++) {
6248
	rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
6249
    }
6250
    return ary;
6251
}
6241 6252

  
6242 6253
/*
6243 6254
 *  call-seq:
......
7916 7927
    rb_define_method(rb_cString, "oct", rb_str_oct, 0);
7917 7928
    rb_define_method(rb_cString, "split", rb_str_split_m, -1);
7918 7929
    rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
7919
    rb_define_method(rb_cString, "bytes", rb_str_each_byte, 0);
7930
    rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
7920 7931
    rb_define_method(rb_cString, "chars", rb_str_each_char, 0);
7921 7932
    rb_define_method(rb_cString, "codepoints", rb_str_each_codepoint, 0);
7922 7933
    rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
7923
- 
string.c
6227 6227
 */
6228 6228

  
6229 6229
static VALUE
6230
rb_str_each_byte(VALUE str)
6230
rb_str_enumerate_bytes(VALUE str, VALUE return_enumerator_p)
6231 6231
{
6232 6232
    long i;
6233
    VALUE ary, yieldp;
6234

  
6235
    if (return_enumerator_p) {
6236
	RETURN_ENUMERATOR(str, 0, 0);
6237
    }
6238

  
6239
    if (rb_block_given_p()) {
6240
	yieldp = Qtrue;
6241
    }
6242
    else {
6243
	yieldp = Qfalse;
6244
	ary = rb_ary_new2(RSTRING_LEN(str));
6245
    }
6233 6246

  
6234
    RETURN_ENUMERATOR(str, 0, 0);
6235 6247
    for (i=0; i<RSTRING_LEN(str); i++) {
6236
	rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
6248
	if (yieldp) {
6249
	    rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
6250
	}
6251
	else {
6252
	    rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
6253
	}
6237 6254
    }
6238
    return str;
6255
    return ary;
6239 6256
}
6240 6257

  
6241 6258
static VALUE
6242
rb_str_bytes(VALUE str)
6259
rb_str_each_byte(VALUE str)
6243 6260
{
6244
    long i;
6245
    VALUE ary = rb_ary_new();
6261
    return rb_str_enumerate_bytes(str, Qtrue);
6262
}
6246 6263

  
6247
    for (i=0; i<RSTRING_LEN(str); i++) {
6248
	rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
6249
    }
6250
    return ary;
6264
static VALUE
6265
rb_str_bytes(VALUE str)
6266
{
6267
    return rb_str_enumerate_bytes(str, Qfalse);
6251 6268
}
6252 6269

  
6253 6270
/*
test/ruby/test_string.rb
625 625
    assert_equal(65, res[0])
626 626
    assert_equal(66, res[1])
627 627
    assert_equal(67, res[2])
628

  
629
    assert_equal 65, S("ABC").each_byte.next
630
  end
631

  
632
  def test_bytes
633
    res = []
634
    S("ABC").bytes {|x| res << x }
635
    assert_equal(65, res[0])
636
    assert_equal(66, res[1])
637
    assert_equal(67, res[2])
638

  
639
    assert_equal [65, 66, 67], S("ABC").bytes
628 640
  end
629 641

  
630 642
  def test_each_line
631
- 
string.c
6252 6252
	    rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
6253 6253
	}
6254 6254
    }
6255
    return ary;
6255
    if (yieldp) {
6256
	return str;
6257
    }
6258
    else {
6259
	return ary;
6260
    }
6256 6261
}
6257 6262

  
6258 6263
static VALUE
test/ruby/test_string.rb
621 621

  
622 622
  def test_each_byte
623 623
    res = []
624
    S("ABC").each_byte {|x| res << x }
624
    s = S("ABC")
625
    assert_equal s.object_id, s.each_byte {|x| res << x }.object_id
625 626
    assert_equal(65, res[0])
626 627
    assert_equal(66, res[1])
627 628
    assert_equal(67, res[2])
......
631 632

  
632 633
  def test_bytes
633 634
    res = []
634
    S("ABC").bytes {|x| res << x }
635
    s = S("ABC")
636
    assert_equal s.object_id, s.bytes {|x| res << x }.object_id
635 637
    assert_equal(65, res[0])
636 638
    assert_equal(66, res[1])
637 639
    assert_equal(67, res[2])
638
- 
string.c
6291 6291
 */
6292 6292

  
6293 6293
static VALUE
6294
rb_str_each_char(VALUE str)
6294
rb_str_enumerate_chars(VALUE str, VALUE return_enumerator_p)
6295 6295
{
6296 6296
    VALUE orig = str;
6297 6297
    long i, len, n;
6298 6298
    const char *ptr;
6299 6299
    rb_encoding *enc;
6300
    VALUE ary, yieldp;
6300 6301

  
6301
    RETURN_ENUMERATOR(str, 0, 0);
6302
    if (return_enumerator_p) {
6303
	RETURN_ENUMERATOR(str, 0, 0);
6304
    }
6305

  
6306
    if (rb_block_given_p()) {
6307
	yieldp = Qtrue;
6308
    }
6309
    else {
6310
	yieldp = Qfalse;
6311
	ary = rb_ary_new();
6312
    }
6302 6313
    str = rb_str_new4(str);
6303 6314
    ptr = RSTRING_PTR(str);
6304 6315
    len = RSTRING_LEN(str);
......
6308 6319
      case ENC_CODERANGE_7BIT:
6309 6320
	for (i = 0; i < len; i += n) {
6310 6321
	    n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
6311
	    rb_yield(rb_str_subseq(str, i, n));
6322
	    if (yieldp) {
6323
		rb_yield(rb_str_subseq(str, i, n));
6324
	    }
6325
	    else {
6326
		rb_ary_push(ary, rb_str_subseq(str, i, n));
6327
	    }
6312 6328
	}
6313 6329
	break;
6314 6330
      default:
6315 6331
	for (i = 0; i < len; i += n) {
6316 6332
	    n = rb_enc_mbclen(ptr + i, ptr + len, enc);
6317
	    rb_yield(rb_str_subseq(str, i, n));
6333
	    if (yieldp) {
6334
		rb_yield(rb_str_subseq(str, i, n));
6335
	    }
6336
	    else {
6337
		rb_ary_push(ary, rb_str_subseq(str, i, n));
6338
	    }
6318 6339
	}
6319 6340
    }
6320
    return orig;
6341
    if (yieldp) {
6342
	return orig;
6343
    }
6344
    else {
6345
	return ary;
6346
    }
6347
}
6348

  
6349
static VALUE
6350
rb_str_each_char(VALUE str)
6351
{
6352
    return rb_str_enumerate_chars(str, Qtrue);
6353
}
6354

  
6355
static VALUE
6356
rb_str_chars(VALUE str)
6357
{
6358
    return rb_str_enumerate_chars(str, Qfalse);
6321 6359
}
6322 6360

  
6323 6361
/*
......
7950 7988
    rb_define_method(rb_cString, "split", rb_str_split_m, -1);
7951 7989
    rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
7952 7990
    rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
7953
    rb_define_method(rb_cString, "chars", rb_str_each_char, 0);
7991
    rb_define_method(rb_cString, "chars", rb_str_chars, 0);
7954 7992
    rb_define_method(rb_cString, "codepoints", rb_str_each_codepoint, 0);
7955 7993
    rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
7956 7994
    rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
test/ruby/test_string.rb
641 641
    assert_equal [65, 66, 67], S("ABC").bytes
642 642
  end
643 643

  
644
  def test_each_char
645
    res = []
646
    s = S("ABC")
647
    assert_equal s.object_id, s.each_char {|x| res << x }.object_id
648
    assert_equal("A", res[0])
649
    assert_equal("B", res[1])
650
    assert_equal("C", res[2])
651

  
652
    assert_equal "A", S("ABC").each_char.next
653
  end
654

  
655
  def test_chars
656
    res = []
657
    s = S("ABC")
658
    assert_equal s.object_id, s.chars {|x| res << x }.object_id
659
    assert_equal("A", res[0])
660
    assert_equal("B", res[1])
661
    assert_equal("C", res[2])
662

  
663
    assert_equal ["A", "B", "C"], S("ABC").chars
664
  end
665

  
644 666
  def test_each_line
645 667
    save = $/
646 668
    $/ = "\n"
647
- 
string.c
6358 6358
    return rb_str_enumerate_chars(str, Qfalse);
6359 6359
}
6360 6360

  
6361
static VALUE
6362
rb_str_enumerate_codepoints(VALUE str, VALUE return_enumerator_p)
6363
{
6364
    VALUE orig = str;
6365
    int n;
6366
    unsigned int c;
6367
    const char *ptr, *end;
6368
    rb_encoding *enc;
6369
    VALUE ary, yieldp;
6370

  
6371
    if (single_byte_optimizable(str))
6372
	return rb_str_enumerate_bytes(str, return_enumerator_p);
6373

  
6374
    if (return_enumerator_p) {
6375
	RETURN_ENUMERATOR(str, 0, 0);
6376
    }
6377

  
6378
    if (rb_block_given_p()) {
6379
	yieldp = Qtrue;
6380
    }
6381
    else {
6382
	yieldp = Qfalse;
6383
	ary = rb_ary_new();
6384
    }
6385

  
6386
    str = rb_str_new4(str);
6387
    ptr = RSTRING_PTR(str);
6388
    end = RSTRING_END(str);
6389
    enc = STR_ENC_GET(str);
6390
    while (ptr < end) {
6391
	c = rb_enc_codepoint_len(ptr, end, &n, enc);
6392
	if (yieldp) {
6393
	    rb_yield(UINT2NUM(c));
6394
	}
6395
	else {
6396
	    rb_ary_push(ary, UINT2NUM(c));
6397
	}
6398
	ptr += n;
6399
    }
6400
    if (yieldp) {
6401
	return orig;
6402
    }
6403
    else {
6404
	return ary;
6405
    }
6406
}
6407

  
6361 6408
/*
6362 6409
 *  call-seq:
6363 6410
 *     str.codepoints {|integer| block }        -> str
......
6382 6429
static VALUE
6383 6430
rb_str_each_codepoint(VALUE str)
6384 6431
{
6385
    VALUE orig = str;
6386
    int n;
6387
    unsigned int c;
6388
    const char *ptr, *end;
6389
    rb_encoding *enc;
6432
    return rb_str_enumerate_codepoints(str, Qtrue);
6433
}
6390 6434

  
6391
    if (single_byte_optimizable(str)) return rb_str_each_byte(str);
6392
    RETURN_ENUMERATOR(str, 0, 0);
6393
    str = rb_str_new4(str);
6394
    ptr = RSTRING_PTR(str);
6395
    end = RSTRING_END(str);
6396
    enc = STR_ENC_GET(str);
6397
    while (ptr < end) {
6398
	c = rb_enc_codepoint_len(ptr, end, &n, enc);
6399
	rb_yield(UINT2NUM(c));
6400
	ptr += n;
6401
    }
6402
    return orig;
6435
static VALUE
6436
rb_str_codepoints(VALUE str)
6437
{
6438
    return rb_str_enumerate_codepoints(str, Qfalse);
6403 6439
}
6404 6440

  
6405 6441
static long
......
7989 8025
    rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
7990 8026
    rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
7991 8027
    rb_define_method(rb_cString, "chars", rb_str_chars, 0);
7992
    rb_define_method(rb_cString, "codepoints", rb_str_each_codepoint, 0);
8028
    rb_define_method(rb_cString, "codepoints", rb_str_codepoints, 0);
7993 8029
    rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
7994 8030
    rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
7995 8031
    rb_define_method(rb_cString, "concat", rb_str_concat, 1);
test/ruby/test_string.rb
663 663
    assert_equal ["A", "B", "C"], S("ABC").chars
664 664
  end
665 665

  
666
  def test_each_codepoint
667
    # Single byte optimization
668
    assert_equal 65, S("ABC").each_codepoint.next
669

  
670
    res = []
671
    s = S("\u3042\u3044\u3046")
672
    assert_equal s.object_id, s.each_codepoint {|x| res << x }.object_id
673
    assert_equal(0x3042, res[0])
674
    assert_equal(0x3044, res[1])
675
    assert_equal(0x3046, res[2])
676

  
677
    assert_equal 0x3042, S("\u3042\u3044\u3046").each_codepoint.next
678
  end
679

  
680
  def test_codepoints
681
    # Single byte optimization
682
    assert_equal [65, 66, 67], S("ABC").codepoints
683

  
684
    res = []
685
    s = S("\u3042\u3044\u3046")
686
    assert_equal s.object_id, s.codepoints {|x| res << x }.object_id
687
    assert_equal(0x3042, res[0])
688
    assert_equal(0x3044, res[1])
689
    assert_equal(0x3046, res[2])
690

  
691
    assert_equal [0x3042, 0x3044, 0x3046], S("\u3042\u3044\u3046").codepoints
692
  end
693

  
666 694
  def test_each_line
667 695
    save = $/
668 696
    $/ = "\n"
669
- 
string.c
6208 6208
}
6209 6209

  
6210 6210

  
6211
/*
6212
 *  call-seq:
6213
 *     str.bytes {|fixnum| block }        -> str
6214
 *     str.bytes                          -> an_enumerator
6215
 *
6216
 *     str.each_byte {|fixnum| block }    -> str
6217
 *     str.each_byte                      -> an_enumerator
6218
 *
6219
 *  Passes each byte in <i>str</i> to the given block, or returns
6220
 *  an enumerator if no block is given.
6221
 *
6222
 *     "hello".each_byte {|c| print c, ' ' }
6223
 *
6224
 *  <em>produces:</em>
6225
 *
6226
 *     104 101 108 108 111
6227
 */
6228

  
6229 6211
static VALUE
6230 6212
rb_str_enumerate_bytes(VALUE str, VALUE return_enumerator_p)
6231 6213
{
......
6260 6242
    }
6261 6243
}
6262 6244

  
6245
/*
6246
 *  call-seq:
6247
 *     str.bytes {|fixnum| block }        -> str
6248
 *     str.bytes                          -> an_enumerator
6249
 *
6250
 *     str.each_byte {|fixnum| block }    -> str
6251
 *     str.each_byte                      -> an_enumerator
6252
 *
6253
 *  Passes each byte in <i>str</i> to the given block, or returns
6254
 *  an enumerator if no block is given.
6255
 *
6256
 *     "hello".each_byte {|c| print c, ' ' }
6257
 *
6258
 *  <em>produces:</em>
6259
 *
6260
 *     104 101 108 108 111
6261
 */
6262

  
6263 6263
static VALUE
6264 6264
rb_str_each_byte(VALUE str)
6265 6265
{
......
6272 6272
    return rb_str_enumerate_bytes(str, Qfalse);
6273 6273
}
6274 6274

  
6275
/*
6276
 *  call-seq:
6277
 *     str.chars {|cstr| block }        -> str
6278
 *     str.chars                        -> an_enumerator
6279
 *
6280
 *     str.each_char {|cstr| block }    -> str
6281
 *     str.each_char                    -> an_enumerator
6282
 *
6283
 *  Passes each character in <i>str</i> to the given block, or returns
6284
 *  an enumerator if no block is given.
6285
 *
6286
 *     "hello".each_char {|c| print c, ' ' }
6287
 *
6288
 *  <em>produces:</em>
6289
 *
6290
 *     h e l l o
6291
 */
6292 6275

  
6293 6276
static VALUE
6294 6277
rb_str_enumerate_chars(VALUE str, VALUE return_enumerator_p)
......
6346 6329
    }
6347 6330
}
6348 6331

  
6332
/*
6333
 *  call-seq:
6334
 *     str.chars {|cstr| block }        -> str
6335
 *     str.chars                        -> an_enumerator
6336
 *
6337
 *     str.each_char {|cstr| block }    -> str
6338
 *     str.each_char                    -> an_enumerator
6339
 *
6340
 *  Passes each character in <i>str</i> to the given block, or returns
6341
 *  an enumerator if no block is given.
6342
 *
6343
 *     "hello".each_char {|c| print c, ' ' }
6344
 *
6345
 *  <em>produces:</em>
6346
 *
6347
 *     h e l l o
6348
 */
6349

  
6349 6350
static VALUE
6350 6351
rb_str_each_char(VALUE str)
6351 6352
{
......
6358 6359
    return rb_str_enumerate_chars(str, Qfalse);
6359 6360
}
6360 6361

  
6362

  
6361 6363
static VALUE
6362 6364
rb_str_enumerate_codepoints(VALUE str, VALUE return_enumerator_p)
6363 6365
{
6364
- 
string.c
6072 6072
}
6073 6073

  
6074 6074

  
6075
/*
6076
 *  call-seq:
6077
 *     str.each_line(separator=$/) {|substr| block }   -> str
6078
 *     str.each_line(separator=$/)                     -> an_enumerator
6079
 *
6080
 *     str.lines(separator=$/) {|substr| block }       -> str
6081
 *     str.lines(separator=$/)                         -> an_enumerator
6082
 *
6083
 *  Splits <i>str</i> using the supplied parameter as the record separator
6084
 *  (<code>$/</code> by default), passing each substring in turn to the supplied
6085
 *  block. If a zero-length record separator is supplied, the string is split
6086
 *  into paragraphs delimited by multiple successive newlines.
6087
 *
6088
 *  If no block is given, an enumerator is returned instead.
6089
 *
6090
 *     print "Example one\n"
6091
 *     "hello\nworld".each_line {|s| p s}
6092
 *     print "Example two\n"
6093
 *     "hello\nworld".each_line('l') {|s| p s}
6094
 *     print "Example three\n"
6095
 *     "hello\n\n\nworld".each_line('') {|s| p s}
6096
 *
6097
 *  <em>produces:</em>
6098
 *
6099
 *     Example one
6100
 *     "hello\n"
6101
 *     "world"
6102
 *     Example two
6103
 *     "hel"
6104
 *     "l"
6105
 *     "o\nworl"
6106
 *     "d"
6107
 *     Example three
6108
 *     "hello\n\n\n"
6109
 *     "world"
6110
 */
6111

  
6112 6075
static VALUE
6113
rb_str_each_line(int argc, VALUE *argv, VALUE str)
6076
rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE return_enumerator_p)
6114 6077
{
6115 6078
    rb_encoding *enc;
6116 6079
    VALUE rs;
......
6120 6083
    VALUE line;
6121 6084
    int n;
6122 6085
    VALUE orig = str;
6086
    VALUE ary, yieldp;
6123 6087

  
6124 6088
    if (argc == 0) {
6125 6089
	rs = rb_rs;
......
6127 6091
    else {
6128 6092
	rb_scan_args(argc, argv, "01", &rs);
6129 6093
    }
6130
    RETURN_ENUMERATOR(str, argc, argv);
6094

  
6095
    if (return_enumerator_p) {
6096
	RETURN_ENUMERATOR(str, argc, argv);
6097
    }
6098

  
6099
    if (rb_block_given_p()) {
6100
	yieldp = Qtrue;
6101
    }
6102
    else {
6103
	yieldp = Qfalse;
6104
	ary = rb_ary_new2(RSTRING_LEN(str));
6105
    }
6106

  
6131 6107
    if (NIL_P(rs)) {
6132
	rb_yield(str);
6133
	return orig;
6108
	if (yieldp) {
6109
	    rb_yield(str);
6110
	    return orig;
6111
	}
6112
	else {
6113
	    rb_ary_push(ary, str);
6114
	    return ary;
6115
	}
6134 6116
    }
6135 6117
    str = rb_str_new4(str);
6136 6118
    ptr = p = s = RSTRING_PTR(str);
......
6153 6135
	    line = rb_str_new5(str, s, p - s);
6154 6136
	    OBJ_INFECT(line, str);
6155 6137
	    rb_enc_cr_str_copy_for_substr(line, str);
6156
	    rb_yield(line);
6138
	    if (yieldp) {
6139
		rb_yield(line);
6140
	    }
6141
	    else {
6142
		rb_ary_push(ary, line);
6143
	    }
6157 6144
	    str_mod_check(str, ptr, len);
6158 6145
	    s = p;
6159 6146
	}
......
6189 6176
	    line = rb_str_new5(str, s, p - s + (rslen ? rslen : n));
6190 6177
	    OBJ_INFECT(line, str);
6191 6178
	    rb_enc_cr_str_copy_for_substr(line, str);
6192
	    rb_yield(line);
6179
	    if (yieldp) {
6180
		rb_yield(line);
6181
	    }
6182
	    else {
6183
		rb_ary_push(ary, line);
6184
	    }
6193 6185
	    str_mod_check(str, ptr, len);
6194 6186
	    s = p + (rslen ? rslen : n);
6195 6187
	}
......
6201 6193
	line = rb_str_new5(str, s, pend - s);
6202 6194
	OBJ_INFECT(line, str);
6203 6195
	rb_enc_cr_str_copy_for_substr(line, str);
6204
	rb_yield(line);
6196
	if (yieldp) {
6197
	    rb_yield(line);
6198
	}
6199
	else {
6200
	    rb_ary_push(ary, line);
6201
	}
6202
    }
6203

  
6204
    if (yieldp) {
6205
	return orig;
6205 6206
    }
6207
    else {
6208
	return ary;
6209
    }
6210
}
6211

  
6212
/*
6213
 *  call-seq:
6214
 *     str.each_line(separator=$/) {|substr| block }   -> str
6215
 *     str.each_line(separator=$/)                     -> an_enumerator
6216
 *
6217
 *     str.lines(separator=$/) {|substr| block }       -> str
6218
 *     str.lines(separator=$/)                         -> an_enumerator
6219
 *
6220
 *  Splits <i>str</i> using the supplied parameter as the record separator
6221
 *  (<code>$/</code> by default), passing each substring in turn to the supplied
6222
 *  block. If a zero-length record separator is supplied, the string is split
6223
 *  into paragraphs delimited by multiple successive newlines.
6224
 *
6225
 *  If no block is given, an enumerator is returned instead.
6226
 *
6227
 *     print "Example one\n"
6228
 *     "hello\nworld".each_line {|s| p s}
6229
 *     print "Example two\n"
6230
 *     "hello\nworld".each_line('l') {|s| p s}
6231
 *     print "Example three\n"
6232
 *     "hello\n\n\nworld".each_line('') {|s| p s}
6233
 *
6234
 *  <em>produces:</em>
6235
 *
6236
 *     Example one
6237
 *     "hello\n"
6238
 *     "world"
6239
 *     Example two
6240
 *     "hel"
6241
 *     "l"
6242
 *     "o\nworl"
6243
 *     "d"
6244
 *     Example three
6245
 *     "hello\n\n\n"
6246
 *     "world"
6247
 */
6206 6248

  
6207
    return orig;
6249
static VALUE
6250
rb_str_each_line(int argc, VALUE *argv, VALUE str)
6251
{
6252
    return rb_str_enumerate_lines(argc, argv, str, Qtrue);
6253
}
6254

  
6255
static VALUE
6256
rb_str_lines(int argc, VALUE *argv, VALUE str)
6257
{
6258
    return rb_str_enumerate_lines(argc, argv, str, Qfalse);
6208 6259
}
6209 6260

  
6210 6261

  
......
8024 8075
    rb_define_method(rb_cString, "hex", rb_str_hex, 0);
8025 8076
    rb_define_method(rb_cString, "oct", rb_str_oct, 0);
8026 8077
    rb_define_method(rb_cString, "split", rb_str_split_m, -1);
8027
    rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
8078
    rb_define_method(rb_cString, "lines", rb_str_lines, -1);
8028 8079
    rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
8029 8080
    rb_define_method(rb_cString, "chars", rb_str_chars, 0);
8030 8081
    rb_define_method(rb_cString, "codepoints", rb_str_codepoints, 0);
test/ruby/test_string.rb
695 695
    save = $/
696 696
    $/ = "\n"
697 697
    res=[]
698
    S("hello\nworld").lines.each {|x| res << x}
698
    S("hello\nworld").each_line {|x| res << x}
699 699
    assert_equal(S("hello\n"), res[0])
700 700
    assert_equal(S("world"),   res[1])
701 701

  
702 702
    res=[]
703
    S("hello\n\n\nworld").lines(S('')).each {|x| res << x}
703
    S("hello\n\n\nworld").each_line(S('')) {|x| res << x}
704 704
    assert_equal(S("hello\n\n\n"), res[0])
705 705
    assert_equal(S("world"),       res[1])
706 706

  
707 707
    $/ = "!"
708 708

  
709 709
    res=[]
710
    S("hello!world").lines.each {|x| res << x}
710
    S("hello!world").each_line {|x| res << x}
711 711
    assert_equal(S("hello!"), res[0])
712 712
    assert_equal(S("world"),  res[1])
713 713

  
......
716 716
    s = nil
717 717
    "foo\nbar".each_line(nil) {|s2| s = s2 }
718 718
    assert_equal("foo\nbar", s)
719

  
720
    assert_equal "hello\n", S("hello\nworld").each_line.next
721
    assert_equal "hello\nworld", S("hello\nworld").each_line(nil).next
722
  end
723
  
724
  def test_lines
725
    res=[]
726
    S("hello\nworld").lines {|x| res << x}
727
    assert_equal(S("hello\n"), res[0])
728
    assert_equal(S("world"),  res[1])
729

  
730
    assert_equal ["hello\n", "world"], S("hello\nworld").lines
731
    assert_equal ["hello\nworld"], S("hello\nworld").lines(nil)
719 732
  end
720 733

  
721 734
  def test_empty?
722
- 
string.c
6101 6101
    }
6102 6102
    else {
6103 6103
	yieldp = Qfalse;
6104
	ary = rb_ary_new2(RSTRING_LEN(str));
6104
	ary = rb_ary_new();
6105 6105
    }
6106 6106

  
6107 6107
    if (NIL_P(rs)) {
6108
- 
string.c
6092 6092
	rb_scan_args(argc, argv, "01", &rs);
6093 6093
    }
6094 6094

  
6095
    if (return_enumerator_p) {
6096
	RETURN_ENUMERATOR(str, argc, argv);
6097
    }
6098

  
6099 6095
    if (rb_block_given_p()) {
6100 6096
	yieldp = Qtrue;
6101 6097
    }
6102 6098
    else {
6099
	if (return_enumerator_p)
6100
	    RETURN_ENUMERATOR(str, argc, argv);
6101

  
6103 6102
	yieldp = Qfalse;
6104 6103
	ary = rb_ary_new();
6105 6104
    }
......
6265 6264
    long i;
6266 6265
    VALUE ary, yieldp;
6267 6266

  
6268
    if (return_enumerator_p) {
6269
	RETURN_ENUMERATOR(str, 0, 0);
6270
    }
6271

  
6272 6267
    if (rb_block_given_p()) {
6273 6268
	yieldp = Qtrue;
6274 6269
    }
6275 6270
    else {
6271
	if (return_enumerator_p)
6272
	    RETURN_ENUMERATOR(str, 0, 0);
6273

  
6276 6274
	yieldp = Qfalse;
6277 6275
	ary = rb_ary_new2(RSTRING_LEN(str));
6278 6276
    }
......
6333 6331
    rb_encoding *enc;
6334 6332
    VALUE ary, yieldp;
6335 6333

  
6336
    if (return_enumerator_p) {
6337
	RETURN_ENUMERATOR(str, 0, 0);
6338
    }
6339

  
6340 6334
    if (rb_block_given_p()) {
6341 6335
	yieldp = Qtrue;
6342 6336
    }
6343 6337
    else {
6338
	if (return_enumerator_p)
6339
	    RETURN_ENUMERATOR(str, 0, 0);
6340

  
6344 6341
	yieldp = Qfalse;
6345 6342
	ary = rb_ary_new();
6346 6343
    }
6344

  
6347 6345
    str = rb_str_new4(str);
6348 6346
    ptr = RSTRING_PTR(str);
6349 6347
    len = RSTRING_LEN(str);
......
6424 6422
    if (single_byte_optimizable(str))
6425 6423
	return rb_str_enumerate_bytes(str, return_enumerator_p);
6426 6424

  
6427
    if (return_enumerator_p) {
6428
	RETURN_ENUMERATOR(str, 0, 0);
6429
    }
6430

  
6431 6425
    if (rb_block_given_p()) {
6432 6426
	yieldp = Qtrue;
6433 6427
    }
6434 6428
    else {
6429
	if (return_enumerator_p)
6430
	    RETURN_ENUMERATOR(str, 0, 0);
6431

  
6435 6432
	yieldp = Qfalse;
6436 6433
	ary = rb_ary_new();
6437 6434
    }
......
6491 6488
    return rb_str_enumerate_codepoints(str, Qfalse);
6492 6489
}
6493 6490

  
6491

  
6494 6492
static long
6495 6493
chopped_length(VALUE str)
6496 6494
{
6497
- 
string.c
6210 6210

  
6211 6211
/*
6212 6212
 *  call-seq:
6213
 *     str.lines(separator=$/) {|substr| block }       -> str
6214
 *     str.lines(separator=$/)                         -> an_array
6215
 *
6213 6216
 *     str.each_line(separator=$/) {|substr| block }   -> str
6214 6217
 *     str.each_line(separator=$/)                     -> an_enumerator
6215 6218
 *
6216
 *     str.lines(separator=$/) {|substr| block }       -> str
6217
 *     str.lines(separator=$/)                         -> an_enumerator
6218
 *
6219 6219
 *  Splits <i>str</i> using the supplied parameter as the record separator
6220 6220
 *  (<code>$/</code> by default), passing each substring in turn to the supplied
6221 6221
 *  block. If a zero-length record separator is supplied, the string is split
6222 6222
 *  into paragraphs delimited by multiple successive newlines.
6223 6223
 *
6224
 *  If no block is given, an enumerator is returned instead.
6224
 *  If no block is given, an array or enumerator is returned instead.
6225 6225
 *
6226 6226
 *     print "Example one\n"
6227 6227
 *     "hello\nworld".each_line {|s| p s}
......
6294 6294
/*
6295 6295
 *  call-seq:
6296 6296
 *     str.bytes {|fixnum| block }        -> str
6297
 *     str.bytes                          -> an_enumerator
6297
 *     str.bytes                          -> an_array
6298 6298
 *
6299 6299
 *     str.each_byte {|fixnum| block }    -> str
6300 6300
 *     str.each_byte                      -> an_enumerator
6301 6301
 *
6302 6302
 *  Passes each byte in <i>str</i> to the given block, or returns
6303
 *  an enumerator if no block is given.
6303
 *  an array or enumerator if no block is given.
6304 6304
 *
6305 6305
 *     "hello".each_byte {|c| print c, ' ' }
6306 6306
 *
......
6381 6381
/*
6382 6382
 *  call-seq:
6383 6383
 *     str.chars {|cstr| block }        -> str
6384
 *     str.chars                        -> an_enumerator
6384
 *     str.chars                        -> an_array
6385 6385
 *
6386 6386
 *     str.each_char {|cstr| block }    -> str
6387 6387
 *     str.each_char                    -> an_enumerator
6388 6388
 *
6389 6389
 *  Passes each character in <i>str</i> to the given block, or returns
6390
 *  an enumerator if no block is given.
6390
 *  an array or enumerator if no block is given.
6391 6391
 *
6392 6392
 *     "hello".each_char {|c| print c, ' ' }
6393 6393
 *
......
6458 6458
/*
6459 6459
 *  call-seq:
6460 6460
 *     str.codepoints {|integer| block }        -> str
6461
 *     str.codepoints                           -> an_enumerator
6461
 *     str.codepoints                           -> an_array
6462 6462
 *
6463 6463
 *     str.each_codepoint {|integer| block }    -> str
6464 6464
 *     str.each_codepoint                       -> an_enumerator
......
6467 6467
 *  also known as a <i>codepoint</i> when applied to Unicode strings to the
6468 6468
 *  given block.
6469 6469
 *
6470
 *  If no block is given, an enumerator is returned instead.
6470
 *  If no block is given, an array or enumerator is returned instead.
6471 6471
 *
6472 6472
 *     "hello\u0639".each_codepoint {|c| print c, ' ' }
6473 6473
 *
6474
-