Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZfIbX-0006Hr-Pz for pgsql-bugs@arkaria.postgresql.org; Fri, 25 Sep 2015 02:19:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZfIbV-0001fW-J6 for pgsql-bugs@arkaria.postgresql.org; Fri, 25 Sep 2015 02:19:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZfIbU-0001fH-Qv for pgsql-bugs@postgresql.org; Fri, 25 Sep 2015 02:19:16 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZfIbS-0003Tq-CE for pgsql-bugs@postgresql.org; Fri, 25 Sep 2015 02:19:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t8P2JB9Z027191; Thu, 24 Sep 2015 22:19:11 -0400 From: Tom Lane To: Thomas Munro cc: Jeff Janes , PostgreSQL Bugs Subject: Re: BUG #13636: psql numericlocale adds comma where it ought not In-reply-to: References: <20150924202545.26913.85050@wrigleys.postgresql.org> <21049.1443137840@sss.pgh.pa.us> Comments: In-reply-to Thomas Munro message dated "Fri, 25 Sep 2015 11:46:42 +1200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <27107.1443147327.0@sss.pgh.pa.us> Date: Thu, 24 Sep 2015 22:19:11 -0400 Message-ID: <27190.1443147551@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27107.1443147327.1@sss.pgh.pa.us> Thomas Munro writes: > On Fri, Sep 25, 2015 at 11:37 AM, Tom Lane wrote: >> Indeed. It looks like the author of format_numeric_locale() never >> heard of e-format output. There's some other pretty crummy code in >> there, but that's the core problem ... > Does this look reasonable? I thought it needed a rather more thoroughgoing revision: there is no need for it to assume so much about what is in the column, and good reason for it not to. (For instance, I note that psql will try to apply this code to "money" columns, which may be a bad idea, but there can definitely be stuff in there that doesn't look like a regular number.) It should muck with digits immediately following the sign, and nothing else. There was some other useless inefficiency too. I came up with the attached. I would have borrowed your regression test additions, except I'm afraid they will fail if the prevailing locale isn't C. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="numeric-locale-fixes.patch"; charset="us-ascii" Content-ID: <27107.1443147327.2@sss.pgh.pa.us> Content-Description: numeric-locale-fixes.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c index cab9e6e..f7343d1 100644 *** a/src/bin/psql/print.c --- b/src/bin/psql/print.c *************** *** 39,46 **** */ volatile bool cancel_pressed =3D false; =20=20 static char *decimal_point; ! static char *grouping; static char *thousands_sep; =20=20 static char default_footer[100]; --- 39,47 ---- */ volatile bool cancel_pressed =3D false; =20=20 + /* info for locale-aware numeric formatting; set up by setDecimalLocale()= */ static char *decimal_point; ! static int groupdigits; static char *thousands_sep; =20=20 static char default_footer[100]; *************** static void IsPagerNeeded(const printTab *** 196,239 **** static void print_aligned_vertical(const printTableContent *cont, FILE *f= out); =20=20 =20=20 static int integer_digits(const char *my_str) { ! int frac_len; !=20 ! if (my_str[0] =3D=3D '-') my_str++; !=20 ! frac_len =3D strchr(my_str, '.') ? strlen(strchr(my_str, '.')) : 0; !=20 ! return strlen(my_str) - frac_len; } =20=20 ! /* Return additional length required for locale-aware numeric output */ static int additional_numeric_locale_len(const char *my_str) { int int_len =3D integer_digits(my_str), len =3D 0; - int groupdigits =3D atoi(grouping); =20=20 ! if (int_len > 0) ! /* Don't count a leading separator */ ! len =3D (int_len / groupdigits - (int_len % groupdigits =3D=3D 0)) * ! strlen(thousands_sep); =20=20 if (strchr(my_str, '.') !=3D NULL) ! len +=3D strlen(decimal_point) - strlen("."); =20=20 return len; } =20=20 - static int - strlen_with_numeric_locale(const char *my_str) - { - return strlen(my_str) + additional_numeric_locale_len(my_str); - } -=20 /* * Returns the appropriately formatted string in a new allocated block, * caller must free --- 197,231 ---- static void print_aligned_vertical(const printTableContent *cont, FILE *f= out); =20=20 =20=20 + /* Count number of digits in integral part of number */ static int integer_digits(const char *my_str) { ! /* ignoring any sign ... */ ! if (my_str[0] =3D=3D '-' || my_str[0] =3D=3D '+') my_str++; ! /* ... count initial integral digits */ ! return strspn(my_str, "0123456789"); } =20=20 ! /* Compute additional length required for locale-aware numeric output */ static int additional_numeric_locale_len(const char *my_str) { int int_len =3D integer_digits(my_str), len =3D 0; =20=20 ! /* Account for added thousands_sep instances */ ! if (int_len > groupdigits) ! len +=3D ((int_len - 1) / groupdigits) * strlen(thousands_sep); =20=20 + /* Account for possible additional length of decimal_point */ if (strchr(my_str, '.') !=3D NULL) ! len +=3D strlen(decimal_point) - 1; =20=20 return len; } =20=20 /* * Returns the appropriately formatted string in a new allocated block, * caller must free *************** strlen_with_numeric_locale(const char *m *** 241,293 **** static char * format_numeric_locale(const char *my_str) { int i, - j, - int_len =3D integer_digits(my_str), leading_digits; ! int groupdigits =3D atoi(grouping); ! int new_str_start =3D 0; ! char *new_str =3D pg_malloc(strlen_with_numeric_locale(my_str) + 1); =20=20 ! leading_digits =3D (int_len % groupdigits !=3D 0) ? ! int_len % groupdigits : groupdigits; =20=20 ! if (my_str[0] =3D=3D '-') /* skip over sign, affects grouping ! * calculations */ { ! new_str[0] =3D my_str[0]; my_str++; - new_str_start =3D 1; } =20=20 ! for (i =3D 0, j =3D new_str_start;; i++, j++) { ! /* Hit decimal point? */ ! if (my_str[i] =3D=3D '.') { ! strcpy(&new_str[j], decimal_point); ! j +=3D strlen(decimal_point); ! /* add fractional part */ ! strcpy(&new_str[j], &my_str[i] + 1); ! break; } =20=20 ! /* End of string? */ ! if (my_str[i] =3D=3D '\0') ! { ! new_str[j] =3D '\0'; ! break; ! } =20=20 ! /* Add separator? */ ! if (i !=3D 0 && (i - leading_digits) % groupdigits =3D=3D 0) ! { ! strcpy(&new_str[j], thousands_sep); ! j +=3D strlen(thousands_sep); ! } =20=20 ! new_str[j] =3D my_str[i]; ! } =20=20 return new_str; } --- 233,283 ---- static char * format_numeric_locale(const char *my_str) { + int new_len =3D strlen(my_str) + additional_numeric_locale_len(my_str); + char *new_str =3D pg_malloc(new_len + 1); + int int_len =3D integer_digits(my_str); int i, leading_digits; ! int new_str_pos =3D 0; =20=20 ! /* number of digits in first thousands group */ ! leading_digits =3D int_len % groupdigits; ! if (leading_digits =3D=3D 0) ! leading_digits =3D groupdigits; =20=20 ! /* process sign */ ! if (my_str[0] =3D=3D '-' || my_str[0] =3D=3D '+') { ! new_str[new_str_pos++] =3D my_str[0]; my_str++; } =20=20 ! /* process integer part of number */ ! for (i =3D 0; i < int_len; i++) { ! /* Time to insert separator? */ ! if (i > 0 && --leading_digits =3D=3D 0) { ! strcpy(&new_str[new_str_pos], thousands_sep); ! new_str_pos +=3D strlen(thousands_sep); ! leading_digits =3D groupdigits; } + new_str[new_str_pos++] =3D my_str[i]; + } =20=20 ! /* handle decimal point if any */ ! if (my_str[i] =3D=3D '.') ! { ! strcpy(&new_str[new_str_pos], decimal_point); ! new_str_pos +=3D strlen(decimal_point); ! i++; ! } =20=20 ! /* copy the rest (fractional digits and/or exponent, and \0 terminator) = */ ! strcpy(&new_str[new_str_pos], &my_str[i]); =20=20 ! /* assert we didn't underestimate new_len (an overestimate is OK) */ ! assert(strlen(new_str) <=3D new_len); =20=20 return new_str; } *************** setDecimalLocale(void) *** 3241,3250 **** decimal_point =3D pg_strdup(extlconv->decimal_point); else decimal_point =3D "."; /* SQL output standard */ if (*extlconv->grouping && atoi(extlconv->grouping) > 0) ! grouping =3D pg_strdup(extlconv->grouping); else ! grouping =3D "3"; /* most common */ =20=20 /* similar code exists in formatting.c */ if (*extlconv->thousands_sep) --- 3231,3241 ---- decimal_point =3D pg_strdup(extlconv->decimal_point); else decimal_point =3D "."; /* SQL output standard */ +=20 if (*extlconv->grouping && atoi(extlconv->grouping) > 0) ! groupdigits =3D atoi(extlconv->grouping); else ! groupdigits =3D 3; /* most common */ =20=20 /* similar code exists in formatting.c */ if (*extlconv->thousands_sep) ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--