Skip to content

Commit bd15782

Browse files
committed
Enable multi-byte thousands_sep and decimal_point for numericsep.
1 parent fbc11b9 commit bd15782

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

src/bin/psql/print.c

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.68 2005/07/14 15:54:21 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.69 2005/07/14 21:12:41 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -50,76 +50,79 @@ pg_local_malloc(size_t size)
5050
}
5151

5252
static int
53-
num_numericseps(const char *my_str)
53+
integer_digits(const char *my_str)
5454
{
55-
int old_len, dec_len, int_len;
56-
int groupdigits = atoi(grouping);
55+
int frac_len;
5756

5857
if (my_str[0] == '-')
5958
my_str++;
6059

61-
old_len = strlen(my_str);
62-
dec_len = strchr(my_str, '.') ? strlen(strchr(my_str, '.')) : 0;
60+
frac_len = strchr(my_str, '.') ? strlen(strchr(my_str, '.')) : 0;
61+
62+
return strlen(my_str) - frac_len;
63+
}
64+
65+
static int
66+
len_numericseps(const char *my_str)
67+
{
68+
int int_len = integer_digits(my_str), sep_len;
69+
int groupdigits = atoi(grouping);
6370

64-
int_len = old_len - dec_len;
6571
if (int_len % groupdigits != 0)
66-
return int_len / groupdigits;
72+
sep_len = int_len / groupdigits;
6773
else
68-
return int_len / groupdigits - 1; /* no leading separator */
74+
sep_len = int_len / groupdigits - 1; /* no leading separator */
75+
76+
return sep_len * strlen(thousands_sep) -
77+
strlen(".") + strlen(decimal_point);
6978
}
7079

7180
static int
7281
len_with_numericsep(const char *my_str)
7382
{
74-
return strlen(my_str) + num_numericseps(my_str);
83+
return strlen(my_str) + len_numericseps(my_str);
7584
}
7685

7786
static void
7887
format_numericsep(char *my_str)
7988
{
80-
int i, j, digits_before_sep, old_len, new_len, dec_len, int_len;
81-
char *new_str;
82-
char *dec_value;
89+
int i, j, int_len = integer_digits(my_str), leading_digits;
8390
int groupdigits = atoi(grouping);
91+
char *new_str;
8492

8593
if (my_str[0] == '-')
8694
my_str++;
87-
88-
old_len = strlen(my_str);
89-
dec_len = strchr(my_str, '.') ? strlen(strchr(my_str, '.')) : 0;
90-
int_len = old_len - dec_len;
91-
digits_before_sep = int_len % groupdigits;
92-
93-
new_len = int_len + int_len / groupdigits + dec_len;
94-
if (digits_before_sep == 0)
95-
new_len--; /* no leading separator */
95+
96+
new_str = pg_local_malloc(len_numericseps(my_str) + 1);
9697

97-
new_str = pg_local_malloc(new_len + 1);
98+
leading_digits = (int_len % groupdigits != 0) ?
99+
int_len % groupdigits : groupdigits;
98100

99101
for (i=0, j=0; ; i++, j++)
100102
{
101-
/* hit decimal point */
103+
/* Hit decimal point? */
102104
if (my_str[i] == '.')
103105
{
104-
new_str[j] = *decimal_point;
105-
new_str[j+1] = '\0';
106-
dec_value = strchr(my_str, '.');
107-
strcat(new_str, ++dec_value);
106+
strcpy(&new_str[j], decimal_point);
107+
j += strlen(decimal_point);
108+
/* add fractional part */
109+
strcpy(&new_str[j], &my_str[i] + 1);
108110
break;
109111
}
110112

111-
/* end of string */
113+
/* End of string? */
112114
if (my_str[i] == '\0')
113115
{
114116
new_str[j] = '\0';
115117
break;
116118
}
117119

118-
/* add separator? */
119-
if (i != 0 &&
120-
(i - (digits_before_sep ? digits_before_sep : groupdigits))
121-
% groupdigits == 0)
122-
new_str[j++] = *thousands_sep;
120+
/* Add separator? */
121+
if (i != 0 && (i - leading_digits) % groupdigits == 0)
122+
{
123+
strcpy(&new_str[j], thousands_sep);
124+
j += strlen(thousands_sep);
125+
}
123126

124127
new_str[j] = my_str[i];
125128
}
@@ -396,7 +399,7 @@ print_aligned_text(const char *title, const char *const *headers,
396399
int numericseps;
397400

398401
if (opt_align[i % col_count] == 'r' && opt_numericsep)
399-
numericseps = num_numericseps(*ptr);
402+
numericseps = len_numericseps(*ptr);
400403
else
401404
numericseps = 0;
402405

@@ -613,7 +616,7 @@ print_aligned_vertical(const char *title, const char *const *headers,
613616
int numericseps;
614617

615618
if (opt_align[i % col_count] == 'r' && opt_numericsep)
616-
numericseps = num_numericseps(*ptr);
619+
numericseps = len_numericseps(*ptr);
617620
else
618621
numericseps = 0;
619622

@@ -1711,7 +1714,6 @@ setDecimalLocale(void)
17111714

17121715
extlconv = localeconv();
17131716

1714-
/* These are treated as single-byte strings in the code */
17151717
if (*extlconv->decimal_point)
17161718
decimal_point = strdup(extlconv->decimal_point);
17171719
else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy