Project

General

Profile

Feature #6129 » patch2.diff

Glass_saga (Masaki Matsushita), 03/18/2012 05:23 PM

View differences:

configure.in
1426 1426
	      setuid setgid daemon select_large_fdset setenv unsetenv\
1427 1427
              mktime timegm gmtime_r clock_gettime gettimeofday poll ppoll\
1428 1428
              pread sendfile shutdown sigaltstack dl_iterate_phdr\
1429
              dup3 pipe2 posix_memalign memalign)
1429
              dup3 pipe2 posix_memalign memalign memmem)
1430 1430

  
1431 1431
AC_CACHE_CHECK(for unsetenv returns a value, rb_cv_unsetenv_return_value,
1432 1432
  [AC_TRY_COMPILE([
......
2726 2726
    AC_MSG_ERROR([failed])
2727 2727
    ])])
2728 2728
fi
2729
AC_CACHE_CHECK(for broken memmem, rb_cv_broken_memmem,
2730
  [AC_TRY_RUN([
2731
#include <string.h>
2732

  
2733
int
2734
main()
2735
{
2736
  char *str = "hogefugafoobar";
2737
  char *rs = "foo";
2738
  char *empty = "";
2739
  char *p;
2740

  
2741
  p = memmem(str, strlen(str), rs, strlen(rs));
2742
  if (p == str+8) {
2743
    p = memmem(str, strlen(str), empty, strlen(empty));
2744
    if (p == str)
2745
      return 0;
2746
    else
2747
      return 1;
2748
  }
2749
  else {
2750
    return 1;
2751
  }
2752
}
2753
  ],
2754
  rb_cv_broken_memmem=no,
2755
  rb_cv_broken_memmem=yes,
2756
  rb_cv_broken_memmem=yes)
2757
])
2758

  
2759
if test "$rb_cv_broken_memmem" = yes; then
2760
  AC_DEFINE(BROKEN_MEMMEM, 1)
2761
fi
2729 2762

  
2730 2763
CPPFLAGS="$CPPFLAGS "'$(DEFS)'
2731 2764
test -z "$CPPFLAGS" || CPPFLAGS="$CPPFLAGS "; CPPFLAGS="$CPPFLAGS"'${cppflags}'
string.c
5967 5967
    return rb_str_split_m(1, &sep, str);
5968 5968
}
5969 5969

  
5970
static VALUE rb_str_valid_encoding_p(VALUE str);
5971

  
5972
static const char *line_yield(VALUE str, const char *substr, long sublen){
5973
    VALUE line = rb_str_new5(str, substr, sublen);
5974

  
5975
    OBJ_INFECT(line, str);
5976
    rb_enc_cr_str_copy_for_substr(line, str);
5977
    rb_yield(line);
5978

  
5979
    return substr + sublen;
5980
}
5970 5981

  
5971 5982
/*
5972 5983
 *  call-seq:
......
6046 6057
		continue;
6047 6058
	    }
6048 6059
	    p = p0 + rb_enc_mbclen(p0, pend, enc);
6049
	    line = rb_str_new5(str, s, p - s);
6050
	    OBJ_INFECT(line, str);
6051
	    rb_enc_cr_str_copy_for_substr(line, str);
6052
	    rb_yield(line);
6060
	    line_yield(str, s, p - s);
6053 6061
	    str_mod_check(str, ptr, len);
6054 6062
	    s = p;
6055 6063
	}
......
6058 6066

  
6059 6067
    enc = rb_enc_check(str, rs);
6060 6068
    rslen = RSTRING_LEN(rs);
6061
    if (rslen == 0) {
6069

  
6070
    if (rslen == 0)
6062 6071
	newline = '\n';
6072

  
6073
#if defined(HAVE_MEMMEM) && !defined(BROKEN_MEMMEM)
6074
    if (rb_str_valid_encoding_p(str) && rb_str_valid_encoding_p(rs)) {
6075
	int rspara = 0;
6076
	long sublen, rest = len;
6077
	const char *e, *pp, *rsptr, *ss = s;
6078

  
6079
	if (rslen == 0) {
6080
	    rspara = 1;
6081
	    rs = rb_usascii_str_new("\n\n", 2);
6082
	    if (!rb_enc_asciicompat(enc))
6083
		rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
6084
	    rslen = RSTRING_LEN(rs);
6085
	}
6086

  
6087
	rsptr = RSTRING_PTR(rs);
6088

  
6089
	while (e = memmem(ss, rest, rsptr, rslen)) {
6090
	    if (rspara) {
6091
		p = e;
6092
		rb_enc_codepoint_len(p, pend, &n, enc);
6093
		while (p < pend && rb_enc_codepoint(p, pend, enc) == newline)
6094
		    p += n;
6095
		sublen = p-ss;
6096
	    }
6097
	    else {
6098
		sublen = e-ss+rslen;
6099
	    }
6100
	    pp = rb_enc_left_char_head(ss, e, ss+rest, enc);
6101
	    if (pp == e) {
6102
		s = line_yield(str, s, sublen);
6103
		str_mod_check(str, ptr, len);
6104
	    }
6105
	    ss += sublen;
6106
	    rest -= sublen;
6107
	}
6108
	goto finish;
6063 6109
    }
6064
    else {
6110
#endif
6111
    if (rslen != 0)
6065 6112
	newline = rb_enc_codepoint(RSTRING_PTR(rs), RSTRING_END(rs), enc);
6066
    }
6067 6113

  
6068 6114
    while (p < pend) {
6069 6115
	unsigned int c = rb_enc_codepoint_len(p, pend, &n, enc);
......
6082 6128
	if (c == newline &&
6083 6129
	    (rslen <= 1 ||
6084 6130
	     (pend - p >= rslen && memcmp(RSTRING_PTR(rs), p, rslen) == 0))) {
6085
	    line = rb_str_new5(str, s, p - s + (rslen ? rslen : n));
6086
	    OBJ_INFECT(line, str);
6087
	    rb_enc_cr_str_copy_for_substr(line, str);
6088
	    rb_yield(line);
6131
	    s = line_yield(str, s, p - s + (rslen ? rslen : n));
6089 6132
	    str_mod_check(str, ptr, len);
6090
	    s = p + (rslen ? rslen : n);
6091 6133
	}
6092 6134
	p += n;
6093 6135
    }