[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: localcharset: optimize code for native Windows.
From: |
Bruno Haible |
Subject: |
Re: localcharset: optimize code for native Windows. |
Date: |
Tue, 17 Dec 2019 18:46:07 +0100 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-166-generic; KDE/5.18.0; x86_64; ; ) |
Eli Zaretskii wrote:
> But your optimization drops LC_ALL entirely, which may not be a good
> idea, because LC_CTYPE might not be set.
All locale categories are set. The initial value is "C" or "POSIX", for each
locale category independently.
> And what is the rationale for trying to optimize this? Are these
> functions supposed to be used in tight time-critical loops or
> something?
Redundant code is better omitted, also in code that is not time-critical.
And yes, locale_charset() is used in a loop, in wcswidth().
Maybe this program helps you understand how locale categories work:
=================================== foo.c =====================================
#include <locale.h>
#include <stdio.h>
#define ENGLISH "English_United States"
#define GERMAN "German_Germany"
#define FRENCH "French_France"
#define ENCODING ".1252"
static const char LOCALE1[] = ENGLISH ENCODING;
static const char LOCALE2[] = GERMAN ENCODING;
static const char LOCALE3[] = FRENCH ENCODING;
int
main (int argc, char *argv[])
{
printf ("0 LC_CTYPE => %s\n", setlocale (LC_CTYPE, NULL));
printf ("0 LC_NUMERIC => %s\n", setlocale (LC_NUMERIC, NULL));
printf ("0 LC_ALL => %s\n", setlocale (LC_ALL, NULL));
if (setlocale (LC_NUMERIC, LOCALE1) == NULL)
{
fprintf (stderr, "Skipping test: LOCALE1 not recognized\n");
return 77;
}
printf ("1 LC_CTYPE => %s\n", setlocale (LC_CTYPE, NULL));
printf ("1 LC_NUMERIC => %s\n", setlocale (LC_NUMERIC, NULL));
printf ("1 LC_ALL => %s\n", setlocale (LC_ALL, NULL));
if (setlocale (LC_ALL, LOCALE2) == NULL)
{
fprintf (stderr, "Skipping test: LOCALE2 not recognized\n");
return 77;
}
printf ("2 LC_CTYPE => %s\n", setlocale (LC_CTYPE, NULL));
printf ("2 LC_NUMERIC => %s\n", setlocale (LC_NUMERIC, NULL));
printf ("2 LC_ALL => %s\n", setlocale (LC_ALL, NULL));
if (setlocale (LC_NUMERIC, LOCALE3) == NULL)
{
fprintf (stderr, "Skipping test: LOCALE3 not recognized\n");
return 77;
}
printf ("3 LC_CTYPE => %s\n", setlocale (LC_CTYPE, NULL));
printf ("3 LC_NUMERIC => %s\n", setlocale (LC_NUMERIC, NULL));
printf ("3 LC_ALL => %s\n", setlocale (LC_ALL, NULL));
return 0;
}
===============================================================================
produces this output (on mingw):
0 LC_CTYPE => C
0 LC_NUMERIC => C
0 LC_ALL => C
1 LC_CTYPE => C
1 LC_NUMERIC => English_United States.1252
1 LC_ALL => LC_COLLATE=C;LC_CTYPE=C;LC_MONETARY=C;LC_NUMERIC=English_United
States.1252;LC_TIME=C
2 LC_CTYPE => German_Germany.1252
2 LC_NUMERIC => German_Germany.1252
2 LC_ALL => German_Germany.1252
3 LC_CTYPE => German_Germany.1252
3 LC_NUMERIC => French_France.1252
3 LC_ALL =>
LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=French_France.1252;LC_TIME=German_Germany.1252