mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	merge revision(s) 51344: [Backport #11387]
* string.c (rb_str_reverse): reversed string is not a substring, and should not set coderange of the original string. [ruby-dev:49189] [Bug #11387] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@51520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									8fcdc4e23b
								
							
						
					
					
						commit
						00d499992b
					
				
					 4 changed files with 21 additions and 15 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,9 @@
 | 
			
		|||
Tue Aug 11 01:11:02 2015  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* string.c (rb_str_reverse): reversed string is not a substring,
 | 
			
		||||
	  and should not set coderange of the original string.
 | 
			
		||||
	  [ruby-dev:49189] [Bug #11387]
 | 
			
		||||
 | 
			
		||||
Tue Aug 11 00:42:53 2015  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* parse.y (lambda_body): pop cmdarg stack for lookahead
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										21
									
								
								string.c
									
										
									
									
									
								
							
							
						
						
									
										21
									
								
								string.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -4703,13 +4703,14 @@ rb_str_reverse(VALUE str)
 | 
			
		|||
    rb_encoding *enc;
 | 
			
		||||
    VALUE rev;
 | 
			
		||||
    char *s, *e, *p;
 | 
			
		||||
    int single = 1;
 | 
			
		||||
    int cr;
 | 
			
		||||
 | 
			
		||||
    if (RSTRING_LEN(str) <= 1) return rb_str_dup(str);
 | 
			
		||||
    enc = STR_ENC_GET(str);
 | 
			
		||||
    rev = rb_str_new_with_class(str, 0, RSTRING_LEN(str));
 | 
			
		||||
    s = RSTRING_PTR(str); e = RSTRING_END(str);
 | 
			
		||||
    p = RSTRING_END(rev);
 | 
			
		||||
    cr = ENC_CODERANGE(str);
 | 
			
		||||
 | 
			
		||||
    if (RSTRING_LEN(str) > 1) {
 | 
			
		||||
	if (single_byte_optimizable(str)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -4717,21 +4718,22 @@ rb_str_reverse(VALUE str)
 | 
			
		|||
		*--p = *s++;
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
	else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID) {
 | 
			
		||||
	else if (cr == ENC_CODERANGE_VALID) {
 | 
			
		||||
	    while (s < e) {
 | 
			
		||||
		int clen = rb_enc_fast_mbclen(s, e, enc);
 | 
			
		||||
 | 
			
		||||
		if (clen > 1 || (*s & 0x80)) single = 0;
 | 
			
		||||
		p -= clen;
 | 
			
		||||
		memcpy(p, s, clen);
 | 
			
		||||
		s += clen;
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
	    cr = rb_enc_asciicompat(enc) ?
 | 
			
		||||
		ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
 | 
			
		||||
	    while (s < e) {
 | 
			
		||||
		int clen = rb_enc_mbclen(s, e, enc);
 | 
			
		||||
 | 
			
		||||
		if (clen > 1 || (*s & 0x80)) single = 0;
 | 
			
		||||
		if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
 | 
			
		||||
		p -= clen;
 | 
			
		||||
		memcpy(p, s, clen);
 | 
			
		||||
		s += clen;
 | 
			
		||||
| 
						 | 
				
			
			@ -4740,15 +4742,8 @@ rb_str_reverse(VALUE str)
 | 
			
		|||
    }
 | 
			
		||||
    STR_SET_LEN(rev, RSTRING_LEN(str));
 | 
			
		||||
    OBJ_INFECT(rev, str);
 | 
			
		||||
    if (ENC_CODERANGE(str) == ENC_CODERANGE_UNKNOWN) {
 | 
			
		||||
	if (single) {
 | 
			
		||||
	    ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
	    ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
    rb_enc_cr_str_copy_for_substr(rev, str);
 | 
			
		||||
    str_enc_copy(rev, str);
 | 
			
		||||
    ENC_CODERANGE_SET(rev, cr);
 | 
			
		||||
 | 
			
		||||
    return rev;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1142,7 +1142,12 @@ class TestM17N < Test::Unit::TestCase
 | 
			
		|||
  end
 | 
			
		||||
 | 
			
		||||
  def test_reverse
 | 
			
		||||
    assert_equal(u("\xf0jihgfedcba"), u("abcdefghij\xf0").reverse)
 | 
			
		||||
    bug11387 = '[ruby-dev:49189] [Bug #11387]'
 | 
			
		||||
    s1 = u("abcdefghij\xf0")
 | 
			
		||||
    s2 = s1.reverse
 | 
			
		||||
    assert_not_predicate(s1, :valid_encoding?, bug11387)
 | 
			
		||||
    assert_equal(u("\xf0jihgfedcba"), s2)
 | 
			
		||||
    assert_not_predicate(s2, :valid_encoding?, bug11387)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_reverse_bang
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
#define RUBY_VERSION "2.2.3"
 | 
			
		||||
#define RUBY_RELEASE_DATE "2015-08-11"
 | 
			
		||||
#define RUBY_PATCHLEVEL 152
 | 
			
		||||
#define RUBY_PATCHLEVEL 153
 | 
			
		||||
 | 
			
		||||
#define RUBY_RELEASE_YEAR 2015
 | 
			
		||||
#define RUBY_RELEASE_MONTH 8
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue