qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs buffer.c


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs buffer.c
Date: Sat, 15 Dec 2007 07:24:03 +0000

CVSROOT:        /cvsroot/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        07/12/15 07:24:03

Modified files:
        .              : buffer.c 

Log message:
        added and improved some buffer functions:
        renamed eb_get_str to eb_get_contents
        fixed buffer size issues in eb_get_line
        fixed utf-8 issues in eb_get_strline
        added eb_prev_line
        added eb_goto_bol2: computes number of code points skipped
        added eb_goto_eol

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/buffer.c?cvsroot=qemacs&r1=1.15&r2=1.16

Patches:
Index: buffer.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/buffer.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- buffer.c    12 Dec 2007 11:49:02 -0000      1.15
+++ buffer.c    15 Dec 2007 07:24:03 -0000      1.16
@@ -140,7 +140,7 @@
     Page *p;
 
     if (page_index < b->nb_pages) {
-        p = b->page_table + page_index;
+        p = &b->page_table[page_index];
         len = MAX_PAGE_SIZE - p->size;
         if (len > size)
             len = size;
@@ -160,7 +160,7 @@
     if (n > 0) {
         b->nb_pages += n;
         qe_realloc(&b->page_table, b->nb_pages * sizeof(Page));
-        p = b->page_table + page_index;
+        p = &b->page_table[page_index];
         memmove(p + n, p, sizeof(Page) * (b->nb_pages - n - page_index));
         while (size > 0) {
             len = size;
@@ -1390,7 +1390,7 @@
     }
 }
 
-int eb_get_str(EditBuffer *b, char *buf, int buf_size)
+int eb_get_contents(EditBuffer *b, char *buf, int buf_size)
 {
     int len;
 
@@ -1402,19 +1402,21 @@
     return len;
 }
 
-/* get the line starting at offset 'offset' */
+/* get the line starting at offset 'offset' as an array of code points */
+/* offset is bumped to the beginning of the next line */ 
+/* returns the number of code points stored in buf, excluding '\0' */
+/* buf_size must be > 0 */
+/* XXX: cannot detect truncation */
 int eb_get_line(EditBuffer *b, unsigned int *buf, int buf_size,
                 int *offset_ptr)
 {
-    int c;
     unsigned int *buf_ptr, *buf_end;
-    int offset;
+    int c, offset;
     
     offset = *offset_ptr;
 
-    /* record line */
     buf_ptr = buf;
-    buf_end = buf + buf_size;
+    buf_end = buf + buf_size - 1;
     for (;;) {
         c = eb_nextc(b, offset, &offset);
         if (c == '\n')
@@ -1422,46 +1424,93 @@
         if (buf_ptr < buf_end)
             *buf_ptr++ = c;
     }
+    *buf_ptr = '\0';
     *offset_ptr = offset;
     return buf_ptr - buf;
 }
 
-/* get the line starting at offset 'offset' */
-/* XXX: incorrect for UTF8 */
+/* get the line starting at offset 'offset' encoded in utf-8 */
+/* offset is bumped to the beginning of the next line */ 
+/* returns the number of bytes stored in buf, excluding '\0' */
+/* buf_size must be > 0 */
+/* XXX: cannot detect truncation */
 int eb_get_strline(EditBuffer *b, char *buf, int buf_size,
                    int *offset_ptr)
 {
-    int c;
+    char utf8_buf[6];
     char *buf_ptr, *buf_end;
-    int offset;
+    int c, offset, len;
     
     offset = *offset_ptr;
 
-    /* record line */
     buf_ptr = buf;
     buf_end = buf + buf_size - 1;
     for (;;) {
         c = eb_nextc(b, offset, &offset);
         if (c == '\n')
             break;
-        if (buf_ptr < buf_end)
+        if (c < 0x80) {
+            if (buf_ptr < buf_end) {
             *buf_ptr++ = c;
+                continue;
+            }
+        } else {
+            len = utf8_encode(utf8_buf, c);
+            if (buf_ptr + len <= buf_end) {
+                memcpy(buf_ptr, utf8_buf, len);
+                buf_ptr += len;
+                continue;
+            }
+        }
+        /* overflow: skip past '\n' */ 
+        offset = eb_next_line(b, offset);
+        break;
     }
     *buf_ptr = '\0';
     *offset_ptr = offset;
     return buf_ptr - buf;
 }
 
+int eb_prev_line(EditBuffer *b, int offset)
+{
+    int offset1, seen_nl;
+
+    for (seen_nl = 0;;) {
+        if (eb_prevc(b, offset, &offset1) == '\n') {
+            if (seen_nl++)
+                break;
+        }
+        offset = offset1;
+    }
+    return offset;
+}
+
+/* return offset of the beginning of the line containing offset */
 int eb_goto_bol(EditBuffer *b, int offset)
 {
-    int c, offset1;
+    int offset1;
 
     for (;;) {
-        c = eb_prevc(b, offset, &offset1);
-        if (c == '\n')
+        if (eb_prevc(b, offset, &offset1) == '\n')
+            break;
+        offset = offset1;
+    }
+    return offset;
+}
+
+/* move to the beginning of the line containing offset */
+/* return offset of the beginning of the line containing offset */
+/* store count of characters skipped at *countp */
+int eb_goto_bol2(EditBuffer *b, int offset, int *countp)
+{
+    int count, offset1;
+
+    for (count = 0;; count++) {
+        if (eb_prevc(b, offset, &offset1) == '\n')
             break;
         offset = offset1;
     }
+    *countp = count;
     return offset;
 }
 
@@ -1479,6 +1528,20 @@
     return 0;
 }
 
+/* return offset of the end of the line containing offset */
+int eb_goto_eol(EditBuffer *b, int offset)
+{
+    int c, offset1;
+
+    for (;;) {
+        c = eb_nextc(b, offset, &offset1);
+        if (c == '\n')
+            break;
+        offset = offset1;
+    }
+    return offset;
+}
+
 int eb_next_line(EditBuffer *b, int offset)
 {
     int c;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]