mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [setlocale]: return only one copy if all six parts of locale are same
Date: Sat, 18 Mar 2017 08:33:35 -0400	[thread overview]
Message-ID: <20170318123335.GO1693@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAPG2z0-d6_3fOx408S1vxbjTuLuKRGd5JK++_BfoX3K-Ct8-5w@mail.gmail.com>

On Sat, Mar 18, 2017 at 07:37:57AM +0000, He X wrote:
> As i suggest on IRC, here's the patch.

> --- musl-1.1.16/src/locale/setlocale.c	2017-03-17 17:49:15.767952411 +0000
> +++ musl-1.1.16/src/locale/setlocale.c	2017-03-17 17:49:15.767952411 +0000
> @@ -48,16 +48,33 @@
>  			}
>  		}
>  		char *s = buf;
> -		for (i=0; i<LC_ALL; i++) {
> + 		const struct __locale_map *flm =
> + 			libc.global_locale.cat[0];
> + 		const char *fpart = flm ? flm->name : "C";
> + 		size_t l = strlen(fpart);
> + 		memcpy(s, fpart, l);
> + 		s[l] = 0;
> + 		i=1;
> + 		do {
>  			const struct __locale_map *lm =
>  				libc.global_locale.cat[i];
>  			const char *part = lm ? lm->name : "C";
> -			size_t l = strlen(part);
> -			memcpy(s, part, l);
> -			s[l] = ';';
> -			s += l+1;
> + 			if (strcmp(s, part)) break;
> + 			i++;
> + 		} while (i<LC_ALL);
> + 
> + 		if (i != LC_ALL) {
> + 			for (i=0; i<LC_ALL; i++) {
> + 				const struct __locale_map *lm =
> + 					libc.global_locale.cat[i];
> + 				const char *part = lm ? lm->name : "C";
> + 				size_t l = strlen(part);
> + 				memcpy(s, part, l);
> + 				s[l] = ';';
> + 				s += l+1;
> + 			}
> + 			*--s = 0;
>  		}
> -		*--s = 0;
>  		UNLOCK(lock);
>  		return buf;
>  	}

I think the results of this patch can be achieved much more simply.
Just compare lm pointers (rather than string contents) for equality at
each iteration in the current loop, counting how many were equal to
libc.global_locale.cat[0]. If that number equals LC_ALL, return
libc.global_locale.cat[0]->name (or "C" if the lm is null) rather than
buf.

Rich


  reply	other threads:[~2017-03-18 12:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-18  7:37 He X
2017-03-18 12:33 ` Rich Felker [this message]
2017-03-18 13:36   ` He X
2017-03-18 13:48     ` He X
2017-03-18 14:39       ` Rich Felker
2017-03-18 15:00         ` He X

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=20170318123335.GO1693@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).