Coverage Report

Created: 2026-01-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/unicase/u8-tolower.c
Line
Count
Source
1
/* Lowercase mapping for UTF-8 strings (locale dependent).
2
   Copyright (C) 2009-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5
   This file is free software.
6
   It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
7
   You can redistribute it and/or modify it under either
8
     - the terms of the GNU Lesser General Public License as published
9
       by the Free Software Foundation, either version 3, or (at your
10
       option) any later version, or
11
     - the terms of the GNU General Public License as published by the
12
       Free Software Foundation; either version 2, or (at your option)
13
       any later version, or
14
     - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
15
16
   This file is distributed in the hope that it will be useful,
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
   Lesser General Public License and the GNU General Public License
20
   for more details.
21
22
   You should have received a copy of the GNU Lesser General Public
23
   License and of the GNU General Public License along with this
24
   program.  If not, see <https://www.gnu.org/licenses/>.  */
25
26
#include <config.h>
27
28
/* Specification.  */
29
#include "unicase.h"
30
31
#include <stddef.h>
32
33
#include "unicase/unicasemap.h"
34
#include "unicase/special-casing.h"
35
36
uint8_t *
37
u8_tolower (const uint8_t *s, size_t n, const char *iso639_language,
38
            uninorm_t nf,
39
            uint8_t *resultbuf, size_t *lengthp)
40
6.13M
{
41
6.13M
  return u8_casemap (s, n,
42
6.13M
                     unicase_empty_prefix_context, unicase_empty_suffix_context,
43
6.13M
                     iso639_language,
44
                     uc_tolower, offsetof (struct special_casing_rule, lower[0]),
45
6.13M
                     nf,
46
6.13M
                     resultbuf, lengthp);
47
6.13M
}
48
49
50
#ifdef TEST
51
52
#include <locale.h>
53
#include <stdio.h>
54
#include <stdlib.h>
55
#include <string.h>
56
57
/* Read the contents of an input stream, and return it, terminated with a NUL
58
   byte. */
59
char *
60
read_file (FILE *stream)
61
{
62
#define BUFSIZE 4096
63
  char *buf = NULL;
64
  int alloc = 0;
65
  int size = 0;
66
67
  while (! feof (stream))
68
    {
69
      if (size + BUFSIZE > alloc)
70
        {
71
          alloc = alloc + alloc / 2;
72
          if (alloc < size + BUFSIZE)
73
            alloc = size + BUFSIZE;
74
          buf = realloc (buf, alloc);
75
          if (buf == NULL)
76
            {
77
              fprintf (stderr, "out of memory\n");
78
              exit (1);
79
            }
80
        }
81
      int count = fread (buf + size, 1, BUFSIZE, stream);
82
      if (count == 0)
83
        {
84
          if (ferror (stream))
85
            {
86
              perror ("fread");
87
              exit (1);
88
            }
89
        }
90
      else
91
        size += count;
92
    }
93
  buf = realloc (buf, size + 1);
94
  if (buf == NULL)
95
    {
96
      fprintf (stderr, "out of memory\n");
97
      exit (1);
98
    }
99
  buf[size] = '\0';
100
  return buf;
101
#undef BUFSIZE
102
}
103
104
int
105
main (int argc, char * argv[])
106
{
107
  setlocale (LC_ALL, "");
108
  if (argc == 1)
109
    {
110
      /* Display the lower case of the input string.  */
111
      char *input = read_file (stdin);
112
      int length = strlen (input);
113
      size_t output_length;
114
      uint8_t *output =
115
        u8_tolower ((uint8_t *) input, length, uc_locale_language (),
116
                    NULL,
117
                    NULL, &output_length);
118
119
      fwrite (output, 1, output_length, stdout);
120
121
      return 0;
122
    }
123
  else
124
    return 1;
125
}
126
127
#endif /* TEST */