This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

i18n: cp/call.c: get width of translated string right


This patch adds a helper function to intl.c which computes the length
of a string that's been returned by gettext, using wcswidth().  It
compiles, and if I inject a string which has a strlen() different from
its width when displayed, it seems to give the right answer.  When the
necessary functions are not available it falls back on strlen, which
will hopefully not be too far off.

Bootstrapping i686-linux, will apply if successful.

zw

        * configure.in: Check for wchar.h, mbstowcs, and wcswidth.
        * configure, config.in: Regenerate.
        * intl.c (gcc_gettext_width): New function.
        * intl.h: Prototype it.
cp:
        * call.c (print_z_candidates): Use gcc_gettext_width, not
        strlen, to determine how much padding to use.

===================================================================
Index: configure.in
--- configure.in	12 Apr 2003 02:16:44 -0000	1.658
+++ configure.in	12 Apr 2003 05:48:10 -0000
@@ -688,7 +688,7 @@ AC_HEADER_SYS_WAIT
 AC_CHECK_HEADERS(limits.h stddef.h string.h strings.h stdlib.h time.h \
 		 fcntl.h unistd.h sys/file.h sys/time.h \
 		 sys/resource.h sys/param.h sys/times.h sys/stat.h \
-		 direct.h malloc.h langinfo.h ldfcn.h)
+		 direct.h malloc.h langinfo.h ldfcn.h wchar.h)
 
 # Check for thread headers.
 AC_CHECK_HEADER(thread.h, [have_thread_h=yes], [have_thread_h=])
@@ -787,7 +787,7 @@ dnl gcc_AC_C_ENUM_BF_UNSIGNED
 AC_CHECK_FUNCS(times clock dup2 kill getrlimit setrlimit atoll atoq \
 	sysconf strsignal putc_unlocked fputc_unlocked fputs_unlocked \
 	fwrite_unlocked fprintf_unlocked getrusage nl_langinfo lstat \
-        scandir alphasort gettimeofday)
+        scandir alphasort gettimeofday mbstowcs wcswidth)
 
 AC_CHECK_TYPE(ssize_t, int)
 
===================================================================
Index: intl.c
--- intl.c	16 Dec 2002 18:19:39 -0000	1.6
+++ intl.c	12 Apr 2003 05:48:10 -0000
@@ -45,4 +45,35 @@ gcc_init_libintl ()
   (void) textdomain ("gcc");
 }
 
+#if defined HAVE_WCHAR_H && defined HAVE_MBSTOWCS && defined HAVE_WCSWIDTH
+#include <wchar.h>
+
+/* Returns the width in columns of MSGSTR, which came from gettext.
+   This is for indenting subsequent output.  */
+
+size_t
+gcc_gettext_width (msgstr)
+     const char *msgstr;
+{
+  size_t nwcs = mbstowcs (0, msgstr, 0);
+  wchar_t *wmsgstr = alloca ((nwcs + 1) * sizeof (wchar_t));
+
+  mbstowcs (wmsgstr, msgstr, nwcs + 1);
+  return wcswidth (wmsgstr, nwcs);
+}
+
+#else  /* no wcswidth */
+
+/* We don't have any way of knowing how wide the string is.  Guess
+   the length of the string.  */
+
+size_t
+gcc_gettext_width (msgstr)
+     const char *msgstr;
+{
+  return strlen (msgstr);
+}
+
 #endif
+
+#endif /* ENABLE_NLS */
===================================================================
Index: intl.h
--- intl.h	19 Oct 2001 15:17:36 -0000	1.4
+++ intl.h	12 Apr 2003 05:48:10 -0000
@@ -39,6 +39,7 @@
 
 #ifdef ENABLE_NLS
 extern void gcc_init_libintl PARAMS ((void));
+extern size_t gcc_gettext_width PARAMS ((const char *));
 #else
 /* Stubs.  */
 # undef textdomain
===================================================================
Index: cp/call.c
--- cp/call.c	7 Apr 2003 05:33:12 -0000	1.375
+++ cp/call.c	12 Apr 2003 05:48:12 -0000
@@ -2508,9 +2508,9 @@ print_z_candidates (struct z_candidate *
   print_z_candidate (str, candidates);
   if (candidates->next)
     {
-      /* Indent successive candidates by the length of the translation of
-	 the above string.  */
-      size_t len = strlen (str) + 1;
+      /* Indent successive candidates by the width of the translation
+	 of the above string.  */
+      size_t len = gcc_gettext_width (str) + 1;
       char *spaces = alloca (len);
       memset (spaces, ' ', len-1);
       spaces[len] = '\0';


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]