1 /* Message list test for ASCII character set.
2 Copyright (C) 2001-2002, 2005-2006, 2023 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 /* Specification. */
24 #include "msgl-ascii.h"
25
26 #include "c-ctype.h"
27
28
29 /* This file's structure parallels msgl-iconv.c. */
30
31
32 bool
33 is_ascii_string (const char *string)
34 {
35 for (; *string; string++)
36 if (!c_isascii ((unsigned char) *string))
37 return false;
38 return true;
39 }
40
41 bool
42 is_ascii_string_desc (string_desc_t string)
43 {
44 size_t len = string_desc_length (string);
45 size_t i;
46 for (i = 0; i < len; i++)
47 if (!c_isascii ((unsigned char) string_desc_char_at (string, i)))
48 return false;
49 return true;
50 }
51
52 bool
53 is_ascii_string_list (string_list_ty *slp)
54 {
55 size_t i;
56
57 if (slp != NULL)
58 for (i = 0; i < slp->nitems; i++)
59 if (!is_ascii_string (slp->item[i]))
60 return false;
61 return true;
62 }
63
64 bool
65 is_ascii_message (message_ty *mp)
66 {
67 const char *p = mp->msgstr;
68 const char *p_end = p + mp->msgstr_len;
69
70 for (; p < p_end; p++)
71 if (!c_isascii ((unsigned char) *p))
72 return false;
73
74 if (!is_ascii_string_list (mp->comment))
75 return false;
76 if (!is_ascii_string_list (mp->comment_dot))
77 return false;
78
79 /* msgid and msgid_plural are normally ASCII, so why checking?
80 Because in complete UTF-8 environments they can be UTF-8, not ASCII. */
81 if (!is_ascii_string (mp->msgid))
82 return false;
83 if (mp->msgid_plural != NULL && !is_ascii_string (mp->msgid_plural))
84 return false;
85
86 /* Likewise for msgctxt. */
87 if (mp->msgctxt != NULL && !is_ascii_string (mp->msgctxt))
88 return false;
89
90 /* Likewise for the prev_* fields. */
91 if (mp->prev_msgctxt != NULL && !is_ascii_string (mp->prev_msgctxt))
92 return false;
93 if (mp->prev_msgid != NULL && !is_ascii_string (mp->prev_msgid))
94 return false;
95 if (mp->prev_msgid_plural != NULL && !is_ascii_string (mp->prev_msgid_plural))
96 return false;
97
98 return true;
99 }
100
101 bool
102 is_ascii_message_list (message_list_ty *mlp)
103 {
104 size_t j;
105
106 for (j = 0; j < mlp->nitems; j++)
107 if (!is_ascii_message (mlp->item[j]))
108 return false;
109
110 return true;
111 }
112
113 bool
114 is_ascii_msgdomain_list (msgdomain_list_ty *mdlp)
115 {
116 size_t k;
117
118 for (k = 0; k < mdlp->nitems; k++)
119 if (!is_ascii_message_list (mdlp->item[k]->messages))
120 return false;
121
122 return true;
123 }