changeset 1103:94b9265663f6

New utility function: a_Utf8_end_of_char()
author corvid <corvid@lavabit.com>
date Sun, 17 May 2009 12:04:13 -0400
parents c44c405a772f
children a450688d276e
files src/ui.cc src/utf8.cc src/utf8.hh
diffstat 3 files changed, 30 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/ui.cc	Sat May 16 13:19:27 2009 -0400
+++ b/src/ui.cc	Sun May 17 12:04:13 2009 -0400
@@ -29,6 +29,7 @@
 #include "ui.hh"
 #include "msg.h"
 #include "timeout.hh"
+#include "utf8.hh"
 
 using namespace fltk;
 
@@ -1068,17 +1069,13 @@
    snprintf(title, 128, "Dillo: %s", label);
    if (*label) {
       // Make a label for this tab
-      size_t tab_chars = 18;
+      size_t tab_chars = 18, label_len = strlen(label);
+
+      if (label_len > tab_chars)
+         tab_chars = a_Utf8_end_of_char(label, tab_chars - 1) + 1;
       snprintf(title, tab_chars + 1, "%s", label);
-      if (strlen(label) > tab_chars) {
-         while (label[tab_chars] & 0x80 && !(label[tab_chars] & 0x40) &&
-                tab_chars < 23) {
-            // In the middle of a multibyte UTF-8 character.
-            title[tab_chars] = label[tab_chars];
-            tab_chars++;
-         }
+      if (label_len > tab_chars)
          snprintf(title + tab_chars, 4, "...");
-      }
       // Avoid unnecessary redraws
       if (strcmp(this->label(), title)) {
          this->copy_label(title);
--- a/src/utf8.cc	Sat May 16 13:19:27 2009 -0400
+++ b/src/utf8.cc	Sun May 17 12:04:13 2009 -0400
@@ -16,6 +16,26 @@
 // C++ functions with C linkage ----------------------------------------------
 
 /*
+ * Return index of the last byte of the UTF-8-encoded character that str + i
+ * points to or into.
+ */
+uint_t a_Utf8_end_of_char(const char *str, uint_t i)
+{
+   /* We can almost get what we want from utf8fwd(p+1,...)-1, but that
+    * does not work for the last character in a string, and the fn makes some
+    * assumptions that do not suit us.
+    * Here's something very simpleminded instead:
+    */
+   if (str && *str && (str[i] & 0x80)) {
+      int internal_bytes = (str[i] & 0x40) ? 0 : 1;
+
+      while (((str[i + 1] & 0xc0) == 0x80) && (++internal_bytes < 4))
+         i++;
+   }
+   return i;
+}
+
+/*
  * Write UTF-8 encoding of ucs into buf and return number of bytes written.
  */
 int a_Utf8_encode(unsigned int ucs, char *buf)
--- a/src/utf8.hh	Sat May 16 13:19:27 2009 -0400
+++ b/src/utf8.hh	Sun May 17 12:04:13 2009 -0400
@@ -5,6 +5,10 @@
 extern "C" {
 #endif /* __cplusplus */
 
+
+#include "d_size.h"
+
+uint_t a_Utf8_end_of_char(const char *str, uint_t i);
 int a_Utf8_encode(unsigned int ucs, char *buf);
 int a_Utf8_test(const char* src, unsigned int srclen);