[前][次][番号順一覧][スレッド一覧]

ruby-changes:4127

From: ko1@a...
Date: Thu, 28 Feb 2008 00:19:48 +0900 (JST)
Subject: [ruby-changes:4127] naruse - Ruby:r15617 (trunk): * string.c (rb_str_coderange_scan_restartable): coderange scaning

naruse	2008-02-28 00:19:22 +0900 (Thu, 28 Feb 2008)

  New Revision: 15617

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/string.c
    trunk/version.h

  Log:
    * string.c (rb_str_coderange_scan_restartable): coderange scaning
      for partial read.
    
    * io.c (read_all): set coderange when not convert encoding.

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/version.h?r1=15617&r2=15616&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/string.c?r1=15617&r2=15616&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15617&r2=15616&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=15617&r2=15616&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15616)
+++ ChangeLog	(revision 15617)
@@ -1,3 +1,10 @@
+Wed Feb 27 22:55:42 2008  NARUSE, Yui  <naruse@r...>
+
+	* string.c (rb_str_coderange_scan_restartable): coderange scaning
+	  for partial read.
+
+	* io.c (read_all): set coderange when not convert encoding.
+
 Wed Feb 27 03:55:58 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/extmk.rb, enc/make_encmake.rb: load current mkmf.rb even if
Index: string.c
===================================================================
--- string.c	(revision 15616)
+++ string.c	(revision 15617)
@@ -201,6 +201,61 @@
     return ENC_CODERANGE_VALID;
 }
 
+long
+rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc, int *cr)
+{
+    long c;
+    const char *p;
+    int ret;
+    p = s;
+
+    if (*cr == ENC_CODERANGE_BROKEN)
+	return e - s;
+
+    if (rb_enc_to_index(enc) == 0) {
+	/* enc is ASCII-8BIT.  ASCII-8BIT string never be broken. */
+	p = search_nonascii(p, e);
+	*cr = (!p && *cr != ENC_CODERANGE_VALID) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
+	return e - s;
+    }
+    else if (rb_enc_asciicompat(enc)) {
+	p = search_nonascii(p, e);
+	if (!p) {
+	    if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
+	    return e - s;
+	}
+	while (p < e) {
+	    int ret = rb_enc_precise_mbclen(p, e, enc);
+	    if (!MBCLEN_CHARFOUND_P(ret)) {
+		*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ret;
+		return e - s;
+	    }
+	    p += MBCLEN_CHARFOUND_LEN(ret);
+	    if (p < e) {
+		p = search_nonascii(p, e);
+		if (!p) {
+		    *cr = ENC_CODERANGE_VALID;
+		    return e - s;
+		}
+	    }
+	}
+	*cr = e < p ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_VALID;
+	return p - s;
+    }
+    else {
+	while (p < e) {
+	    int ret = rb_enc_precise_mbclen(p, e, enc);
+	    if (!MBCLEN_CHARFOUND_P(ret)) {
+		*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ret;
+		return p - s;
+	    }
+	    p += MBCLEN_CHARFOUND_LEN(ret);
+	}
+	*cr = e < p ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_VALID;
+	return p - s;
+    }
+}
+
 static void
 rb_enc_str_copy(VALUE dest, VALUE src)
 {
Index: io.c
===================================================================
--- io.c	(revision 15616)
+++ io.c	(revision 15617)
@@ -1358,6 +1358,9 @@
 {
     long bytes = 0;
     long n;
+    long pos = 0;
+    rb_encoding *enc = io_read_encoding(fptr);
+    int cr = 0;
 
     if (siz == 0) siz = BUFSIZ;
     if (NIL_P(str)) {
@@ -1370,15 +1373,21 @@
 	READ_CHECK(fptr);
 	n = io_fread(str, bytes, fptr);
 	if (n == 0 && bytes == 0) {
-            break;
+	    break;
 	}
 	bytes += n;
+	if (cr != ENC_CODERANGE_BROKEN)
+	    pos = rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
 	if (bytes < siz) break;
 	siz += BUFSIZ;
 	rb_str_resize(str, siz);
     }
     if (bytes != siz) rb_str_resize(str, bytes);
-    return io_enc_str(str, fptr);
+    str = io_enc_str(str, fptr);
+    if (fptr->enc2) {
+	ENC_CODERANGE_SET(str, cr);
+    }
+    return str;
 }
 
 void
Index: version.h
===================================================================
--- version.h	(revision 15616)
+++ version.h	(revision 15617)
@@ -1,7 +1,7 @@
 #define RUBY_VERSION "1.9.0"
-#define RUBY_RELEASE_DATE "2008-02-27"
+#define RUBY_RELEASE_DATE "2008-02-28"
 #define RUBY_VERSION_CODE 190
-#define RUBY_RELEASE_CODE 20080227
+#define RUBY_RELEASE_CODE 20080228
 #define RUBY_PATCHLEVEL 0
 
 #define RUBY_VERSION_MAJOR 1

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]