Gentoo Archives: gentoo-dev

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

Attachments

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