[macruby-changes] [903] MacRuby/branches/experimental
source_changes at macosforge.org
source_changes at macosforge.org
Thu Mar 12 22:56:07 PDT 2009
Revision: 903
http://trac.macosforge.org/projects/ruby/changeset/903
Author: lsansonetti at apple.com
Date: 2009-03-12 22:56:06 -0700 (Thu, 12 Mar 2009)
Log Message:
-----------
better ungetc
Modified Paths:
--------------
MacRuby/branches/experimental/include/ruby/io.h
MacRuby/branches/experimental/io.c
Modified: MacRuby/branches/experimental/include/ruby/io.h
===================================================================
--- MacRuby/branches/experimental/include/ruby/io.h 2009-03-13 04:54:54 UTC (rev 902)
+++ MacRuby/branches/experimental/include/ruby/io.h 2009-03-13 05:56:06 UTC (rev 903)
@@ -24,14 +24,20 @@
#include "ruby/encoding.h"
typedef struct rb_io_t {
+ // The streams.
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
+
+ // Additional information.
CFStringRef path;
pid_t pid;
int lineno;
int fd;
+
+ // For ungetc.
UInt8 *ungetc_buf;
long ungetc_buf_len;
+ long ungetc_buf_pos;
} rb_io_t;
#define HAVE_RB_IO_T 1
Modified: MacRuby/branches/experimental/io.c
===================================================================
--- MacRuby/branches/experimental/io.c 2009-03-13 04:54:54 UTC (rev 902)
+++ MacRuby/branches/experimental/io.c 2009-03-13 05:56:06 UTC (rev 903)
@@ -316,6 +316,7 @@
io_struct->fd = fd;
io_struct->ungetc_buf = NULL;
io_struct->ungetc_buf_len = 0;
+ io_struct->ungetc_buf_pos = 0;
rb_objc_keep_for_exit_finalize((VALUE)io);
@@ -744,12 +745,13 @@
// First let's check if there is something to read in our ungets buffer.
if (io_struct->ungetc_buf_len > 0) {
data_read = MIN(io_struct->ungetc_buf_len, len);
- memcpy(buffer, io_struct->ungetc_buf, data_read);
+ memcpy(buffer, &io_struct->ungetc_buf[io_struct->ungetc_buf_pos],
+ data_read);
io_struct->ungetc_buf_len -= len;
+ io_struct->ungetc_buf_pos += len;
if (io_struct->ungetc_buf_len == 0) {
xfree(io_struct->ungetc_buf);
io_struct->ungetc_buf = NULL;
- return true;
}
}
@@ -943,7 +945,7 @@
CFDataIncreaseLength(data, size);
UInt8 *buf = CFDataGetMutableBytePtr(data);
- if (!rb_io_read_internal(io_struct, buf, len)) {
+ if (!rb_io_read_internal(io_struct, buf, size)) {
rb_eof_error();
}
@@ -1322,13 +1324,31 @@
len = RSTRING_LEN(c);
}
- GC_WB(&io_struct->ungetc_buf, xrealloc(io_struct->ungetc_buf,
- io_struct->ungetc_buf_len + len));
+ if (len > io_struct->ungetc_buf_pos) {
+ const long delta = io_struct->ungetc_buf_len
+ - io_struct->ungetc_buf_pos;
- memcpy(&io_struct->ungetc_buf[io_struct->ungetc_buf_len], bytes, len);
+ // Reallocate the buffer.
+ GC_WB(&io_struct->ungetc_buf, xrealloc(io_struct->ungetc_buf,
+ delta + len));
- io_struct->ungetc_buf_len += len;
+ // Shift the buffer.
+ memmove(&io_struct->ungetc_buf[delta],
+ &io_struct->ungetc_buf[io_struct->ungetc_buf_pos], delta);
+ }
+ // Update position.
+ io_struct->ungetc_buf_pos -= len;
+ if (io_struct->ungetc_buf_pos < 0) {
+ io_struct->ungetc_buf_pos = 0;
+ }
+
+ // Copy the bytes at the position.
+ memcpy(&io_struct->ungetc_buf[io_struct->ungetc_buf_pos], bytes, len);
+
+ // Update buffer size.
+ io_struct->ungetc_buf_len += len - io_struct->ungetc_buf_pos;
+
return Qnil;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090312/5c48ddb5/attachment.html>
More information about the macruby-changes
mailing list