1 |
Steve Long wrote: |
2 |
> No, that's about the size of it-- if you you'd like to tie it to ASCII, |
3 |
> irrespective of locale, that's fair enough. |
4 |
> |
5 |
I had a feeling this statement was incorrect (what zlin said about a-z) so I |
6 |
asked dalias in #bash who is knowledgeable about locale stuff: |
7 |
|
8 |
<igli> dalias; have a q re: l10n. if we want to restrict allowed chars to |
9 |
ASCII (for config) without affecting user's LC_CTYPE, what's the best way? |
10 |
<dalias> igli, where do you want to restrict chars? |
11 |
<igli> example i've seen is in checking a var |
12 |
<dalias> checking a var to make sure it's all ascii? |
13 |
<igli> yeah |
14 |
<igli> [A-Za-z] isn't locale-safe is it? |
15 |
<dalias> match it to a regex |
16 |
<igli> hmm ok |
17 |
<dalias> it's safe if LC_COLLATE=C |
18 |
<igli> ah thanks :D |
19 |
<dalias> alternatively |
20 |
<dalias> the most safe way is just to write all chars explicitly |
21 |
<dalias> [ABCDEF...xyz] |
22 |
<igli> hehe |
23 |
<igli> ok nice one, i'll pass it on. |
24 |
<dalias> anyway posix guarantees that the collation order in C locale is |
25 |
ascii order |
26 |
<dalias> even if the host's character encoding is something idiotic like |
27 |
ebcdic |
28 |
|
29 |
The odd bit (for me) is that [A-Za-z] is affected by LC_COLLATE not |
30 |
LC_CTYPE, which makes sense if you think of it as a comparison. [[:class:]] |
31 |
is ofc CTYPE. |
32 |
|
33 |
So setting LC_COLLATE=C would appear to make sense, as long as you are never |
34 |
dealing with user filenames, but only the portage tree (which seems a bit |
35 |
yuck). A better general implementation might be a function that checks |
36 |
against the explicit list, since that would have no implications for file |
37 |
handling: |
38 |
|
39 |
isAlphaASCII() { |
40 |
for i; do |
41 |
[[ $i = *[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzxyz]* ]] \ |
42 |
&& return 1 |
43 |
done |
44 |
return 0 |
45 |
} |
46 |
|
47 |
I /guess/ in sh the test line would be: |
48 |
case "$i" in *[!ABC..xyz]* ) return 1;; esac |
49 |
|
50 |
|
51 |
-- |
52 |
gentoo-dev@g.o mailing list |