Revision
4338
Author
martinlagardette@apple.com
Date
2010-07-08 18:37:55 -0700 (Thu, 08 Jul 2010)

Log Message

Fix `IO#gets` (and #770)

Modified Paths

Diff

Modified: MacRuby/trunk/io.c (4337 => 4338)


--- MacRuby/trunk/io.c	2010-07-09 00:03:00 UTC (rev 4337)
+++ MacRuby/trunk/io.c	2010-07-09 01:37:55 UTC (rev 4338)
@@ -1362,13 +1362,17 @@
     if (line_limit != -1) {
 	rb_bstr_resize(bstr, line_limit);
 	uint8_t *bytes = rb_bstr_bytes(bstr);
-	rb_io_read_internal(io_struct, bytes, line_limit);
-#if 0 // TODO
-	CFRange r = CFStringFind((CFStringRef)bstr, (CFStringRef)sep, 0);
-	if (r.location != kCFNotFound) {
-	    CFDataSetLength(data, r.location);
+	long r = rb_io_read_internal(io_struct, bytes, line_limit);
+
+	CFRange range = CFStringFind((CFStringRef)bstr, (CFStringRef)sep, 0);
+	if (range.location != kCFNotFound) {
+	    rb_io_create_buf(io_struct);
+	    long rest_size = r - (range.location + 1);
+	    CFDataAppendBytes(io_struct->buf, bytes + range.location + 1, rest_size);
+	    r = range.location + 1;
 	}
-#endif
+	// Resize the buffer to whatever was actually read (can be different from asked size)
+	rb_bstr_resize(bstr, r);
     }
     else {
 	const char *sepstr = RSTRING_PTR(sep);