diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-03-14 22:56:56 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-03-14 22:56:56 -0400 |
commit | 73e7025bd8eed941a068f0a7a71e02dca8d38d1c (patch) | |
tree | 1d0a799fe763433d91b9fdb1415553588e9b7794 /src/test | |
parent | 1a1832eb085e5bca198735e5d0e766a3cb61b8fc (diff) | |
download | postgresql-73e7025bd8eed941a068f0a7a71e02dca8d38d1c.tar.gz postgresql-73e7025bd8eed941a068f0a7a71e02dca8d38d1c.zip |
Extend format() to handle field width and left/right alignment.
This change adds some more standard sprintf() functionality to format().
Pavel Stehule, reviewed by Dean Rasheed and Kyotaro Horiguchi
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/regress/expected/text.out | 115 | ||||
-rw-r--r-- | src/test/regress/sql/text.sql | 21 |
2 files changed, 130 insertions, 6 deletions
diff --git a/src/test/regress/expected/text.out b/src/test/regress/expected/text.out index b7565830d6f..4b1c62bf53c 100644 --- a/src/test/regress/expected/text.out +++ b/src/test/regress/expected/text.out @@ -209,7 +209,7 @@ ERROR: too few arguments for format select format('Hello %s'); ERROR: too few arguments for format select format('Hello %x', 20); -ERROR: unrecognized conversion specifier "x" +ERROR: unrecognized conversion type specifier "x" -- check literal and sql identifiers select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello'); format @@ -256,12 +256,14 @@ select format('%1$s %4$s', 1, 2, 3); ERROR: too few arguments for format select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); ERROR: too few arguments for format -select format('%1s', 1); -ERROR: unterminated conversion specifier +select format('%0$s', 'Hello'); +ERROR: format specifies argument 0, but arguments are numbered from 1 +select format('%*0$s', 'Hello'); +ERROR: format specifies argument 0, but arguments are numbered from 1 select format('%1$', 1); -ERROR: unterminated conversion specifier +ERROR: unterminated format specifier select format('%1$1', 1); -ERROR: unrecognized conversion specifier "1" +ERROR: unterminated format specifier -- check mix of positional and ordered placeholders select format('Hello %s %1$s %s', 'World', 'Hello again'); format @@ -328,3 +330,106 @@ from generate_series(1,200) g(i); 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200 (1 row) +-- check field widths and left, right alignment +select format('>>%10s<<', 'Hello'); + format +---------------- + >> Hello<< +(1 row) + +select format('>>%10s<<', NULL); + format +---------------- + >> << +(1 row) + +select format('>>%10s<<', ''); + format +---------------- + >> << +(1 row) + +select format('>>%-10s<<', ''); + format +---------------- + >> << +(1 row) + +select format('>>%-10s<<', 'Hello'); + format +---------------- + >>Hello << +(1 row) + +select format('>>%-10s<<', NULL); + format +---------------- + >> << +(1 row) + +select format('>>%1$10s<<', 'Hello'); + format +---------------- + >> Hello<< +(1 row) + +select format('>>%1$-10I<<', 'Hello'); + format +---------------- + >>"Hello" << +(1 row) + +select format('>>%2$*1$L<<', 10, 'Hello'); + format +---------------- + >> 'Hello'<< +(1 row) + +select format('>>%2$*1$L<<', 10, NULL); + format +---------------- + >> NULL<< +(1 row) + +select format('>>%2$*1$L<<', -10, NULL); + format +---------------- + >>NULL << +(1 row) + +select format('>>%*s<<', 10, 'Hello'); + format +---------------- + >> Hello<< +(1 row) + +select format('>>%*1$s<<', 10, 'Hello'); + format +---------------- + >> Hello<< +(1 row) + +select format('>>%-s<<', 'Hello'); + format +----------- + >>Hello<< +(1 row) + +select format('>>%10L<<', NULL); + format +---------------- + >> NULL<< +(1 row) + +select format('>>%2$*1$L<<', NULL, 'Hello'); + format +------------- + >>'Hello'<< +(1 row) + +select format('>>%2$*1$L<<', 0, 'Hello'); + format +------------- + >>'Hello'<< +(1 row) + diff --git a/src/test/regress/sql/text.sql b/src/test/regress/sql/text.sql index a96e9f7d1e7..c4ed74b39d4 100644 --- a/src/test/regress/sql/text.sql +++ b/src/test/regress/sql/text.sql @@ -78,7 +78,8 @@ select format('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); -- should fail select format('%1$s %4$s', 1, 2, 3); select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); -select format('%1s', 1); +select format('%0$s', 'Hello'); +select format('%*0$s', 'Hello'); select format('%1$', 1); select format('%1$1', 1); -- check mix of positional and ordered placeholders @@ -97,3 +98,21 @@ select format('Hello', variadic NULL); -- variadic argument allows simulating more than FUNC_MAX_ARGS parameters select format(string_agg('%s',','), variadic array_agg(i)) from generate_series(1,200) g(i); +-- check field widths and left, right alignment +select format('>>%10s<<', 'Hello'); +select format('>>%10s<<', NULL); +select format('>>%10s<<', ''); +select format('>>%-10s<<', ''); +select format('>>%-10s<<', 'Hello'); +select format('>>%-10s<<', NULL); +select format('>>%1$10s<<', 'Hello'); +select format('>>%1$-10I<<', 'Hello'); +select format('>>%2$*1$L<<', 10, 'Hello'); +select format('>>%2$*1$L<<', 10, NULL); +select format('>>%2$*1$L<<', -10, NULL); +select format('>>%*s<<', 10, 'Hello'); +select format('>>%*1$s<<', 10, 'Hello'); +select format('>>%-s<<', 'Hello'); +select format('>>%10L<<', NULL); +select format('>>%2$*1$L<<', NULL, 'Hello'); +select format('>>%2$*1$L<<', 0, 'Hello'); |