mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH] implement strftime GNU extension padding specifiers '_', '-' and '0'
Date: Sat, 9 Dec 2017 17:56:50 -0500	[thread overview]
Message-ID: <20171209225650.GA1627@brightrain.aerifal.cx> (raw)
In-Reply-To: <20161122082908.11584-1-timo.teras@iki.fi>

On Tue, Nov 22, 2016 at 10:29:08AM +0200, Timo Teräs wrote:
> ---
> For review. GNU and most BSDs seem to implement these extensions.
> 
> This applies them to numbers only. There might be few cases where this
> could be applied more, but I think covers most of the uses. Is there any
> test suite I could verify this against?

I'm reasonably conviced this doesn't break anything -- for now I just
did a test of all the standard formats before and after, with explicit
padding and width specifiers for the year ones specified to support
it, and didn't see any change in output. I'm not sure all of them are
right now, but they're no worse with the patch, so that's ok.

I do have some questions about inefficiency:

>  src/time/strftime.c | 22 ++++++++++++++--------
>  1 file changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/src/time/strftime.c b/src/time/strftime.c
> index e103e02..14fe6f5 100644
> --- a/src/time/strftime.c
> +++ b/src/time/strftime.c
> @@ -48,12 +48,12 @@ static int week_num(const struct tm *tm)
>  const char *__tm_to_tzname(const struct tm *);
>  size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
>  
> -const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
> +const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc, int pad)
>  {
>  	nl_item item;
>  	long long val;
>  	const char *fmt = "-";
> -	int width = 2;
> +	int width = 2, def_pad = '0';
>  
>  	switch (f) {
>  	case 'a':
> @@ -79,15 +79,14 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *
>  	case 'C':
>  		val = (1900LL+tm->tm_year) / 100;
>  		goto number;
> +	case 'e':
> +		def_pad = '_';
>  	case 'd':
>  		val = tm->tm_mday;
>  		goto number;
>  	case 'D':
>  		fmt = "%m/%d/%y";
>  		goto recu_strftime;
> -	case 'e':
> -		*l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday);
> -		return *s;

This looks like a nice change.

>  	case 'F':
>  		fmt = "%Y-%m-%d";
>  		goto recu_strftime;
> @@ -200,7 +199,12 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *
>  		return 0;
>  	}
>  number:
> -	*l = snprintf(*s, sizeof *s, "%0*lld", width, val);
> +	switch (pad ? pad : def_pad) {
> +	case '-': *l = snprintf(*s, sizeof *s, "%lld", val); break;
> +	case '_': *l = snprintf(*s, sizeof *s, "%*lld", width, val); break;
> +	case '0':
> +	default:  *l = snprintf(*s, sizeof *s, "%0*lld", width, val); break;
> +	}

This looks like gratuitous duplication of the call point; instead, the
format string can just vary, and there are only two possibilities:
%*lld and %0*lld. The '-' case can just be implemented by setting
width to 0.

>  	return *s;
>  nl_strcat:
>  	fmt = __nl_langinfo_l(item, loc);
> @@ -221,7 +225,7 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st
>  	char buf[100];
>  	char *p;
>  	const char *t;
> -	int plus;
> +	int pad, plus;
>  	unsigned long width;
>  	for (l=0; l<n; f++) {
>  		if (!*f) {
> @@ -233,6 +237,8 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st
>  			continue;
>  		}
>  		f++;
> +		pad = 0;
> +		if (*f == '-' || *f == '_' || *f == '0') pad = *f++;
>  		if ((plus = (*f == '+'))) f++;

I think this is OK since POSIX leaves it unspecified what happens with
more than one flag character.

>  		width = strtoul(f, &p, 10);
>  		if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
> @@ -242,7 +248,7 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st
>  		}
>  		f = p;
>  		if (*f == 'E' || *f == 'O') f++;
> -		t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
> +		t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
>  		if (!t) break;
>  		if (width) {
>  			for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
> -- 
> 2.10.2

If you're tired of working and waiting on this and just want me to
commit it as-is and make any improvements later, just let me know.
I'll try to adapt the (freeform, non-checking) tests I did into
something that can go into libc-test.

Rich


  parent reply	other threads:[~2017-12-09 22:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-22  8:29 Timo Teräs
2016-12-20 20:13 ` Rich Felker
2016-12-21  0:27   ` Felix Janda
2016-12-21  3:20     ` Rich Felker
2017-01-06  6:59       ` [PATCH v2] " Timo Teräs
2016-12-21  6:01     ` [PATCH] " Timo Teras
2017-12-09 22:56 ` Rich Felker [this message]
2017-12-09 23:27   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171209225650.GA1627@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).