See http://archives.postgresql.org/pgsql-bugs/2012-02/msg00164.php

This need function for count characters, not bytes. I find this pg_wcswidth but
it is not used anywhere and broken. So, I fix it and also change it prototype
for remove type casting.

PS: Please help, next week release will be with Russian translation again 
(thank you,
Alexander!) and this tiny bug is slightly annoying.

>From aaa828e05691ca58067d74cea43dabe8863ccdf7 Mon Sep 17 00:00:00 2001
From: Sergey Burladyan <eshkin...@gmail.com>
Date: Thu, 23 Feb 2012 04:09:10 +0400
Subject: [PATCH] Fix NLS text width and pg_wcswidth function

---
 src/bin/psql/describe.c |    4 ++--
 src/bin/psql/mbprint.c  |    7 ++++---
 src/bin/psql/mbprint.h  |    2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index b2c54b5..2b63df4 100644
*** a/src/bin/psql/describe.c
--- b/src/bin/psql/describe.c
*************** describeOneTableDetails(const char *sche
*** 2068,2074 ****
  			if (i == 0)
  				printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
  			else
! 				printfPQExpBuffer(&buf, "%*s  %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
  			if (i < tuples - 1)
  				appendPQExpBuffer(&buf, ",");
  
--- 2068,2074 ----
  			if (i == 0)
  				printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
  			else
! 				printfPQExpBuffer(&buf, "%*s  %s", pg_wcswidth(s, strlen(s), pset.encoding), "", PQgetvalue(result, i, 0));
  			if (i < tuples - 1)
  				appendPQExpBuffer(&buf, ",");
  
*************** describeOneTableDetails(const char *sche
*** 2109,2115 ****
  									  ct, PQgetvalue(result, i, 0));
  				else
  					printfPQExpBuffer(&buf, "%*s  %s",
! 									  (int) strlen(ct), "",
  									  PQgetvalue(result, i, 0));
  				if (i < tuples - 1)
  					appendPQExpBuffer(&buf, ",");
--- 2109,2115 ----
  									  ct, PQgetvalue(result, i, 0));
  				else
  					printfPQExpBuffer(&buf, "%*s  %s",
! 									  pg_wcswidth(ct, strlen(ct), pset.encoding), "",
  									  PQgetvalue(result, i, 0));
  				if (i < tuples - 1)
  					appendPQExpBuffer(&buf, ",");
diff --git a/src/bin/psql/mbprint.c b/src/bin/psql/mbprint.c
index 248a4db..477e35d 100644
*** a/src/bin/psql/mbprint.c
--- b/src/bin/psql/mbprint.c
*************** mb_utf_validate(unsigned char *pwcs)
*** 173,179 ****
   * only appear on one line. OTOH it is easier to use if this applies to you.
   */
  int
! pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding)
  {
  	int			width = 0;
  
--- 173,179 ----
   * only appear on one line. OTOH it is easier to use if this applies to you.
   */
  int
! pg_wcswidth(const char *pwcs, size_t len, int encoding)
  {
  	int			width = 0;
  
*************** pg_wcswidth(const unsigned char *pwcs, s
*** 182,196 ****
  		int			chlen,
  					chwidth;
  
! 		chlen = PQmblen((const char *) pwcs, encoding);
  		if (chlen > len)
  			break;				/* Invalid string */
  
! 		chwidth = PQdsplen((const char *) pwcs, encoding);
  
  		if (chwidth > 0)
  			width += chwidth;
  		pwcs += chlen;
  	}
  	return width;
  }
--- 182,197 ----
  		int			chlen,
  					chwidth;
  
! 		chlen = PQmblen(pwcs, encoding);
  		if (chlen > len)
  			break;				/* Invalid string */
  
! 		chwidth = PQdsplen(pwcs, encoding);
  
  		if (chwidth > 0)
  			width += chwidth;
  		pwcs += chlen;
+ 		len -= chlen;
  	}
  	return width;
  }
diff --git a/src/bin/psql/mbprint.h b/src/bin/psql/mbprint.h
index f729ef0..5c13d97 100644
*** a/src/bin/psql/mbprint.h
--- b/src/bin/psql/mbprint.h
*************** struct lineptr
*** 10,16 ****
  };
  
  extern unsigned char *mbvalidate(unsigned char *pwcs, int encoding);
! extern int	pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding);
  extern void pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, struct lineptr * lines, int count);
  extern void pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
  		   int *width, int *height, int *format_size);
--- 10,16 ----
  };
  
  extern unsigned char *mbvalidate(unsigned char *pwcs, int encoding);
! extern int	pg_wcswidth(const char *pwcs, size_t len, int encoding);
  extern void pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, struct lineptr * lines, int count);
  extern void pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
  		   int *width, int *height, int *format_size);
-- 
1.7.9

-- 
Sergey Burladyan
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to