built-in printf() not supporting \n or \t ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

built-in printf() not supporting \n or \t ?

Dominique Devienne
Obviously it's ugly to use concatenation and char() to format a string
literal with tabs and newlines.
Is there a better way? Why doesn't printf() support newlines and tabs like
it's C cousin? --DD

PS: Built-in printf() also doesn't support positional params, to "emulate"
newline with printf( '%1$s1: %2$s%1$s2: %3$s%1$s' , char(10), 'one',
'two'), but that's not too readable either, in any case

sqlite> select printf('\n1: %s\n2: %s\n', 'one', 'two');
\n1: one\n2: two\n
sqlite> select char(10)||printf('1: %s', 'one')||char(10)||printf('2: %s',
'two')||char(10);

1: one
2: two

sqlite> select printf('\t1: %s\t2: %s\t', 'one', 'two');
\t1: one\t2: two\t
sqlite> select char(9)||printf('1: %s', 'one')||char(9)||printf('2: %s',
'two')||char(9);
        1: one  2: two
sqlite>
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: built-in printf() not supporting \n or \t ?

Richard Damon
On 11/28/19 10:24 AM, Dominique Devienne wrote:

> Obviously it's ugly to use concatenation and char() to format a string
> literal with tabs and newlines.
> Is there a better way? Why doesn't printf() support newlines and tabs like
> it's C cousin? --DD
>
> PS: Built-in printf() also doesn't support positional params, to "emulate"
> newline with printf( '%1$s1: %2$s%1$s2: %3$s%1$s' , char(10), 'one',
> 'two'), but that's not too readable either, in any case
>
> sqlite> select printf('\n1: %s\n2: %s\n', 'one', 'two');
> \n1: one\n2: two\n
> sqlite> select char(10)||printf('1: %s', 'one')||char(10)||printf('2: %s',
> 'two')||char(10);
>
> 1: one
> 2: two
>
> sqlite> select printf('\t1: %s\t2: %s\t', 'one', 'two');
> \t1: one\t2: two\t
> sqlite> select char(9)||printf('1: %s', 'one')||char(9)||printf('2: %s',
> 'two')||char(9);
>         1: one  2: two
> sqlite>

A couple of things to note:

\n and \t are not 'printf' features, but C string features, that \ is an
escape introducer for compiling a string, and if followed by a letter
like n or t it builds a string with the special value represented by
that function. The \n does NOT make it into the string itself, only the
code for a newline. You are not typing your line into a C compiler, but
the sqlite shell program, so it build strings differently. 

ISO C also does not support positional arguments, that is an extension
that many compiler provide as I believe it is a common extension used in
linux.

You also don't NEED positional arguements, you could use

printf('%s1: %s%s2: %s', char(10), 'one', char(10), 'two')


There is a discusson of why SQLite uses its own printf here:
https://sqlite.org/printf.html

--
Richard Damon

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: built-in printf() not supporting \n or \t ?

Keith Medcalf

On Thursday, 28 November, 2019 09:21, Richard Damon <[hidden email]> wrote:

>\n and \t are not 'printf' features, but C string features, that \ is an
>escape introducer for compiling a string, and if followed by a letter
>like n or t it builds a string with the special value represented by
>that function. The \n does NOT make it into the string itself, only the
>code for a newline. You are not typing your line into a C compiler, but
>the sqlite shell program, so it build strings differently.

As far as I know the only place that the \ escape introducer is parsed in SQLite3 is in the arguments to the dot commands in the CLI.  This is so, for example, one may use commands like:

.seperator \t \n

otherwise you would not be able to set the seperator characters to non-printable characters.

Note that for that particular case the CLI is a "presentation" layer tool, so one would expect that behaviour.  Otherwise, SQLite3 is not a "presentation" layer tool but rather a "data storage" layer tool and as such does not muck about with data that it is given or returns.

--
The fact that there's a Highway to Hell but only a Stairway to Heaven says a lot about anticipated traffic volume.



_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users