Coverage Report

Created: 2026-12-01 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/augeas/gnulib/lib/localeinfo.c
Line
Count
Source
1
/* locale information
2
3
   Copyright 2016-2026 Free Software Foundation, Inc.
4
5
   This file is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as
7
   published by the Free Software Foundation; either version 2.1 of the
8
   License, or (at your option) any later version.
9
10
   This file is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17
18
/* Written by Paul Eggert.  */
19
20
#include <config.h>
21
22
#include <localeinfo.h>
23
24
#include <verify.h>
25
26
#include <limits.h>
27
#include <locale.h>
28
#include <stdcountof.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <uchar.h>
32
#include <wchar.h>
33
34
/* The sbclen implementation relies on this.  */
35
verify (MB_LEN_MAX <= SCHAR_MAX);
36
37
/* Return true if the locale uses UTF-8.  */
38
39
static bool
40
is_using_utf8 (void)
41
0
{
42
0
  mbstate_t state; mbszero (&state);
43
0
  char32_t wc;
44
0
  return mbrtoc32 (&wc, "\xc4\x80", 2, &state) == 2 && wc == 0x100;
45
0
}
46
47
/* Return true if the locale is compatible enough with the C locale so
48
   that the locale is single-byte, bytes are in collating-sequence
49
   order, and there are no multi-character collating elements.  */
50
51
static bool
52
using_simple_locale (bool multibyte)
53
0
{
54
  /* The native character set is known to be compatible with
55
     the C locale.  The following test isn't perfect, but it's good
56
     enough in practice, as only ASCII and EBCDIC are in common use
57
     and this test correctly accepts ASCII and rejects EBCDIC.  */
58
0
  enum { native_c_charset =
59
0
    ('\b' == 8 && '\t' == 9 && '\n' == 10 && '\v' == 11 && '\f' == 12
60
0
     && '\r' == 13 && ' ' == 32 && '!' == 33 && '"' == 34 && '#' == 35
61
0
     && '%' == 37 && '&' == 38 && '\'' == 39 && '(' == 40 && ')' == 41
62
0
     && '*' == 42 && '+' == 43 && ',' == 44 && '-' == 45 && '.' == 46
63
0
     && '/' == 47 && '0' == 48 && '9' == 57 && ':' == 58 && ';' == 59
64
0
     && '<' == 60 && '=' == 61 && '>' == 62 && '?' == 63 && 'A' == 65
65
0
     && 'Z' == 90 && '[' == 91 && '\\' == 92 && ']' == 93 && '^' == 94
66
0
     && '_' == 95 && 'a' == 97 && 'z' == 122 && '{' == 123 && '|' == 124
67
0
     && '}' == 125 && '~' == 126)
68
0
  };
69
70
0
  if (!native_c_charset || multibyte)
71
0
    return false;
72
73
  /* As a heuristic, use strcoll to compare native character order.
74
     If this agrees with byte order the locale should be simple.
75
     This heuristic should work for all known practical locales,
76
     although it would be invalid for artificially-constructed locales
77
     where the native order is the collating-sequence order but there
78
     are multi-character collating elements.  */
79
0
  for (int i = 0; i < UCHAR_MAX; i++)
80
0
    if (0 <= strcoll (((char []) {i, 0}), ((char []) {i + 1, 0})))
81
0
      return false;
82
83
0
  return true;
84
0
}
85
86
/* Initialize *LOCALEINFO from the current locale.  */
87
88
void
89
init_localeinfo (struct localeinfo *localeinfo)
90
0
{
91
0
  localeinfo->multibyte = MB_CUR_MAX > 1;
92
0
  localeinfo->simple = using_simple_locale (localeinfo->multibyte);
93
0
  localeinfo->using_utf8 = is_using_utf8 ();
94
95
0
  for (int i = CHAR_MIN; i <= CHAR_MAX; i++)
96
0
    {
97
0
      char c = i;
98
0
      unsigned char uc = i;
99
0
      mbstate_t state; mbszero (&state);
100
0
      char32_t wc;
101
0
      size_t len = mbrtoc32 (&wc, &c, 1, &state);
102
0
      localeinfo->sbclen[uc] = len <= 1 ? 1 : - (int) - len;
103
0
      localeinfo->sbctowc[uc] = len <= 1 ? wc : WEOF;
104
0
    }
105
0
}
106
107
/* The set of char32_t values C such that there's a useful locale
108
   somewhere where C != c32toupper (C) && C != c32tolower (c32toupper (C)).
109
   For example, 0x00B5 (U+00B5 MICRO SIGN) is in this table, because
110
   c32toupper (0x00B5) == 0x039C (U+039C GREEK CAPITAL LETTER MU), and
111
   c32tolower (0x039C) == 0x03BC (U+03BC GREEK SMALL LETTER MU).  */
112
static unsigned short int const lonesome_lower[] =
113
  {
114
    0x00B5, 0x0131, 0x017F, 0x01C5, 0x01C8, 0x01CB, 0x01F2, 0x0345,
115
    0x03C2, 0x03D0, 0x03D1, 0x03D5, 0x03D6, 0x03F0, 0x03F1,
116
117
#if !defined __STDC_ISO_10646__ || __STDC_ISO_10646__ < 200305L
118
    /* U+03F2 GREEK LUNATE SIGMA SYMBOL lacks a specific uppercase
119
       counterpart in locales predating Unicode 4.0.0 (April 2003).  */
120
    0x03F2,
121
#endif
122
123
    0x03F5, 0x1C80, 0x1C81, 0x1C82, 0x1C83, 0x1C84, 0x1C85, 0x1C86,
124
    0x1C87, 0x1C88, 0x1E9B, 0x1FBE,
125
  };
126
127
/* Verify that the worst multibyte case fits.  This is 1 for c32toupper, 1 for
128
   c32tolower, and 1 for each entry in LONESOME_LOWER.  */
129
verify (1 + 1 + countof (lonesome_lower) <= CASE_FOLDED_BUFSIZE);
130
131
/* Whether char32_t values are Unicode code points.
132
   It is OK if only UTF-16 is supported,
133
   since this file converts only single-byte encodings to char32_t
134
   and in practice these encodings convert to characters in the BMP.  */
135
#ifdef GL_CHAR32_T_IS_UNICODE
136
# define CHAR32_T_IS_UNICODE GL_CHAR32_T_IS_UNICODE /* uchar-h-c23 */
137
#elif defined __STDC_ISO_10646__
138
0
# define CHAR32_T_IS_UNICODE 1 /* glibc, musl libc, Cygwin */
139
#else
140
# define CHAR32_T_IS_UNICODE 0
141
#endif
142
143
/* Find the characters equal to C after case-folding, other than C
144
   itself, and store them into FOLDED.  Return the number of characters
145
   stored; this is zero if C is WEOF.  */
146
147
int
148
case_folded_counterparts (wint_t c, char32_t folded[CASE_FOLDED_BUFSIZE])
149
0
{
150
0
  int n = 0;
151
0
  wint_t uc = c32toupper (c);
152
153
0
  if (CHAR32_T_IS_UNICODE || 1 < MB_CUR_MAX)
154
0
    {
155
      /* char32_t is Unicode, or this is a multibyte locale where
156
         it is impractical to look for all case-folded counterparts
157
         and where guessing Unicode will not produce false positives
158
         though it may miss some case-folded counterparts.  */
159
0
      wint_t lc = c32tolower (uc);
160
0
      if (uc != c)
161
0
        folded[n++] = uc;
162
0
      if (lc != uc && lc != c && c32toupper (lc) == uc)
163
0
        folded[n++] = lc;
164
0
      for (int i = 0; i < countof (lonesome_lower); i++)
165
0
        {
166
0
          wint_t li = lonesome_lower[i];
167
0
          if (li != lc && li != uc && li != c && c32toupper (li) == uc)
168
0
            folded[n++] = li;
169
0
        }
170
0
    }
171
0
  else if (c != WEOF)
172
0
    {
173
      /* A single-byte locale where it is not known that char32_t is Unicode,
174
         and C is not WEOF.  Check all 255 possibilities for counterparts.  */
175
0
        for (int i = 1; i <= UCHAR_MAX; i++)
176
0
          {
177
0
            wint_t li = btoc32 (i);
178
0
            if (li != c && c32toupper (li) == uc)
179
0
              folded[n++] = li;
180
0
          }
181
0
    }
182
183
0
  return n;
184
0
}