giftcurs-commits
[Top][All Lists]
Advanced

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

[giFTcurs-commits] giFTcurs/src test_utf8.c parse.c format.c parse.h


From: Christian Häggström
Subject: [giFTcurs-commits] giFTcurs/src test_utf8.c parse.c format.c parse.h
Date: Tue, 04 Nov 2003 10:55:38 -0500

CVSROOT:        /cvsroot/giftcurs
Module name:    giFTcurs
Branch:         
Changes by:     Christian Häggström <address@hidden>    03/11/04 10:55:38

Modified files:
        src            : test_utf8.c parse.c format.c parse.h 

Log message:
        Made the inverse of vstrlen, called str_occupy (in search for a better 
name). Complete test suite included :)

Patches:
Index: giFTcurs/src/format.c
diff -u giFTcurs/src/format.c:1.70 giFTcurs/src/format.c:1.71
--- giFTcurs/src/format.c:1.70  Sat Oct 18 09:20:09 2003
+++ giFTcurs/src/format.c       Tue Nov  4 10:55:37 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: format.c,v 1.70 2003/10/18 13:20:09 saturn Exp $
+ * $Id: format.c,v 1.71 2003/11/04 15:55:37 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -847,21 +847,10 @@
        return c;
 }
 
-static int utf8_visual_to_offset(const char *str, int visual)
-{
-       const char *p;
-
-       for (p = str; visual > 0; p = g_utf8_next_char(p), visual--) {
-               if (g_unichar_iswide(g_utf8_get_char(p)))
-                       visual--;
-       }
-       return p - str;
-}
-
 static void string_append_len(GString *str, const char *string, int width)
 {
        if (utf8)
-               width = utf8_visual_to_offset(string, width);
+               width = str_occupy(string, width, 0);
        g_string_append_len(str, string, width);
 }
 
@@ -1004,8 +993,8 @@
 
                                /* TODO: handle wide characters on the 
boundaries */
                                if (utf8) {
-                                       start = utf8_visual_to_offset(str->str 
+ progress_mark_offset, start);
-                                       end = utf8_visual_to_offset(str->str + 
progress_mark_offset, end);
+                                       start = str_occupy(str->str + 
progress_mark_offset, start, 0);
+                                       end = str_occupy(str->str + 
progress_mark_offset, end, 0);
                                }
                                start += progress_mark_offset;
                                end += progress_mark_offset;
Index: giFTcurs/src/parse.c
diff -u giFTcurs/src/parse.c:1.152 giFTcurs/src/parse.c:1.153
--- giFTcurs/src/parse.c:1.152  Tue Nov  4 10:37:48 2003
+++ giFTcurs/src/parse.c        Tue Nov  4 10:55:37 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: parse.c,v 1.152 2003/11/04 15:37:48 weinholt Exp $
+ * $Id: parse.c,v 1.153 2003/11/04 15:55:37 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -366,9 +366,31 @@
                return strlen(str);
        }
 }
+
+int str_occupy(const char *str, int visual, int greedy)
+{
+       const char *p;
+
+       /* Note. continue scanning even if visual == 0, so that
+        * combining characters are includes as well. */
+
+       for (p = str; *p; p = g_utf8_next_char(p)) {
+               int w = mk_wcwidth(g_utf8_get_char(p));
+               if (visual == 1 && w == 2 && greedy)
+                       visual--;
+               else if ((visual -= w) < 0)
+                       break;
+       }
+       return p - str;
+}
 #else
 glong vstrlen(const char *str)
 {
        return strlen(str);
+}
+
+int str_occupy(const char *str, int visual, int greedy)
+{
+       return visual;
 }
 #endif
Index: giFTcurs/src/parse.h
diff -u giFTcurs/src/parse.h:1.93 giFTcurs/src/parse.h:1.94
--- giFTcurs/src/parse.h:1.93   Fri Oct 31 19:11:33 2003
+++ giFTcurs/src/parse.h        Tue Nov  4 10:55:38 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: parse.h,v 1.93 2003/11/01 00:11:33 saturn Exp $
+ * $Id: parse.h,v 1.94 2003/11/04 15:55:38 saturn Exp $
  */
 #ifndef _PARSE_H
 #define _PARSE_H
@@ -86,5 +86,9 @@
 
 /* Visual strlen(). Returns how many chars will be viewed on the screen. */
 glong vstrlen(const char *str);
+
+/* Returns the number of bytes from str required to fill n slots on screen */
+/* greedy tells whether or not to include wide characters on the boundary */
+int str_occupy(const char *str, int n, int greedy);
 
 #endif
Index: giFTcurs/src/test_utf8.c
diff -u giFTcurs/src/test_utf8.c:1.1 giFTcurs/src/test_utf8.c:1.2
--- giFTcurs/src/test_utf8.c:1.1        Fri Oct 31 19:11:32 2003
+++ giFTcurs/src/test_utf8.c    Tue Nov  4 10:55:37 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: test_utf8.c,v 1.1 2003/11/01 00:11:32 saturn Exp $
+ * $Id: test_utf8.c,v 1.2 2003/11/04 15:55:37 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -39,6 +39,7 @@
 #ifdef WIDE_NCURSES
 
 static int test_vstrlen(void);
+static int test_str_occupy(void);
 static int test_addstr(void);
 
 static void timeout_(int sig)
@@ -54,9 +55,14 @@
        signal(SIGALRM, timeout_);
        alarm(MAX_TIME);
        srand(time(NULL));
+
+       utf8 = TRUE;
+
        if (test_vstrlen() == FAIL)
                ok = FAIL;
-#if 0 /* FIXME: Doesn't seem to work for some strange reason */
+       if (test_str_occupy() == FAIL)
+               ok = FAIL;
+#if 0                                                  /* FIXME: Doesn't seem 
to work for some strange reason */
        if (test_addstr() == FAIL)
                ok = FAIL;
 #endif
@@ -83,15 +89,13 @@
        int ret = PASS;
        int i;
 
-       utf8 = TRUE;
-
        for (i = 0; i < G_N_ELEMENTS(tests); i++) {
                int returned;
-               
+
                returned = vstrlen(tests[i].test);
                if (returned != tests[i].expected_len) {
                        printf("vstrlen for test of %s returned %d; expected 
%d\n",
-                                tests[i].test_name, returned, 
tests[i].expected_len);
+                                  tests[i].test_name, returned, 
tests[i].expected_len);
                        ret = FAIL;
                }
        }
@@ -99,6 +103,65 @@
        return ret;
 }
 
+static int test_str_occupy(void)
+{
+       int ret = PASS;
+       int i, j;
+
+       for (i = 0; i < G_N_ELEMENTS(tests); i++) {
+               int lim = vstrlen(tests[i].test);
+
+               g_assert(strlen(tests[i].test) < 255);
+
+               for (j = 0; j < lim; j++) {
+                       char buf[256];
+                       int offset_lo, offset_hi, returned_lo, returned_hi;
+
+                       strcpy(buf, tests[i].test);
+                       offset_hi = str_occupy(tests[i].test, j, 1);
+                       buf[offset_hi] = '\0';
+                       returned_hi = vstrlen(buf);
+
+                       strcpy(buf, tests[i].test);
+                       offset_lo = str_occupy(tests[i].test, j, 0);
+                       buf[offset_lo] = '\0';
+                       returned_lo = vstrlen(buf);
+
+                       if (offset_hi == offset_lo) {
+                               /* Not a multibyte boundary */
+                               if (j != returned_hi || j != returned_lo) {
+                                       printf("str_occupy for test of %s 
cutted at %d,%d; expected %d\n",
+                                                  tests[i].test_name, 
returned_lo, returned_hi, j);
+                                       ret = FAIL;
+                               }
+                               /* Include one more character and see if length 
increases */
+                               if (tests[i].test[offset_lo]) {
+                                       strcpy(buf, tests[i].test);
+                                       *(char *) g_utf8_next_char(buf + 
offset_lo) = '\0';
+                                       returned_hi = vstrlen(buf);
+                                       if (returned_hi <= returned_lo || 
returned_hi > returned_lo + 2) {
+                                               printf("str_occupy for test of 
%s cutted too early at %d\n",
+                                                          tests[i].test_name, 
returned_lo);
+                                               ret = FAIL;
+                                       }
+                               }
+                       } else if (offset_hi > offset_lo) {
+                               /* Multibyte boundary */
+                               if (j != returned_hi - 1 || j != returned_lo + 
1) {
+                                       printf("str_occupy for test of %s 
cutted at %d,%d; expected %d,%d\n",
+                                                  tests[i].test_name, 
returned_lo, returned_hi, j - 1, j + 1);
+                                       ret = FAIL;
+                               }
+                       } else {
+                               printf("str_occupy returns lower value when 
greedy is true\n");
+                               ret = FAIL;
+                       }
+               }
+       }
+
+       return ret;
+}
+
 static int test_addstr(void)
 {
        int ret = PASS;
@@ -121,8 +184,7 @@
 
                if (mvaddstr(0, 0, tests[i].test) != OK) {
                        endwin();
-                       printf("ncurses test for %s failed. addstr returned 
ERR\n",
-                                tests[i].test_name);
+                       printf("ncurses test for %s failed. addstr returned 
ERR\n", tests[i].test_name);
                        fflush(stdout);
                        initscr();
                        ret = FAIL;
@@ -132,7 +194,7 @@
                if (x != tests[i].expected_len) {
                        endwin();
                        printf("ncurses test for %s failed. addstr moved cursor 
%d steps; expected %d\n",
-                                tests[i].test_name, x, tests[i].expected_len);
+                                  tests[i].test_name, x, 
tests[i].expected_len);
                        fflush(stdout);
                        initscr();
                        ret = FAIL;




reply via email to

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