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

Attachments

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

Replies

Subject Author
Re: [gentoo-dev] [New eclass] twisted-r1.eclass Alex Xu <alex_y_xu@×××××.ca>
Re: [gentoo-dev] [New eclass] twisted-r1.eclass "Michał Górny" <mgorny@g.o>