Gentoo Archives: gentoo-dev

From: Alex Xu <alex_y_xu@×××××.ca>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [New eclass] twisted-r1.eclass
Date: Sat, 03 Aug 2013 19:47:24
Message-Id: 51FD5E41.5020700@yahoo.ca
In Reply to: Re: [gentoo-dev] [New eclass] twisted-r1.eclass by Alex Xu
1 On 03/08/13 03:37 PM, Alex Xu wrote:
2 > On 03/08/13 02:29 PM, Michał Górny wrote:
3 >> Dnia 2013-08-03, o godz. 17:54:42
4 >> Ulrich Mueller <ulm@g.o> napisał(a):
5 >>
6 >>>>>>>> On Sat, 3 Aug 2013, Michał Górny wrote:
7 >>>
8 >>>> 2. The eclass comes with a pure bash-3.2 CamelCase converter for
9 >>>> changing PNs like 'twisted-foo' into 'TwistedFoo'. The relevant code
10 >>>> can be moved to eutils as portable replacements for bash-4 ${foo^}
11 >>>> and friends.
12 >>>
13 >>>> # obtain octal ASCII code for the first letter.
14 >>>> local ord=$(printf '%o' "'${fl}")
15 >>>>
16 >>>> # check if it's [a-z]. ASCII codes are locale-safe.
17 >>>> if [[ ${ord} -ge 141 && ${ord} -le 172 ]]; then
18 >>>> # now substract 040 to make it upper-case.
19 >>>> # fun fact: in range 0141..0172, decimal '- 40' is fine.
20 >>>> local ord=$(( ${ord} - 40))
21 >>>> # and convert it back to the character.
22 >>>> fl=$(printf '\'${ord})
23 >>>> fi
24 >>>
25 >>> This looks just horrible. You do decimal arithmetic on octal numbers?
26 >>
27 >> Yes. Bash wasn't really happy to do octal arithmetic for me. Yet
28 >> in this particular case, with proper assumptions, decimal arithmetic is
29 >> practically equivalent.
30 >>
31 >
32 > # obtain decimal ASCII code for the first letter.
33 > local fl=$(printf '%d' "'${w}")
34 >
35 > # check if it's [a-z]. ASCII codes are locale-safe.
36 > if [[ ${ord} -ge 97 && ${ord} -le 122 ]]; then
37 > local ord=$(( ${ord} - 32 ))
38 > # and convert it back to the character.
39 > fl=$(printf '\'${ord})
40 > fi
41 >
42 > echo -n "${fl}${w:1}"
43 >
44 > Probably var names should be adjusted, I'm not too familiar with bash
45 > locals.
46 >
47 > printf '%d' "'twisted" outputs "116" as expected, similar to
48 > printf("%d", *"asdf qwerty") in C.
49 >
50 > Tested in Bash 4.2.45.
51 >
52 > Now time to sit back and wait for it to break in bash
53 > <obscure-version-here>.
54 >
55
56 I am dumb. Please disregard the previous message.

Attachments

File name MIME type
signature.asc application/pgp-signature