Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

another string replacement with uppercase :: converting C macros to C++

by f77coder (Beadle)
on Jan 23, 2016 at 08:19 UTC ( [id://1153451]=perlquestion: print w/replies, xml ) Need Help??

f77coder has asked for the wisdom of the Perl Monks concerning the following question:

hello, i've been hacking away at this but can't get the correct regex expression and would like a little help.

old C-style macros in headers files are #ifdef __some_Long_name__ which start and end with double underlines and look like perl literals

in C++14, i'd like to batch convert these macros to standard which strips the double underscores and convert strings to upper case like this

SOME_LONG_NAME

i like one-liners, so i've tried these and many variations to escaping the underscores

perl -pi -e 's/__*__/\uc($1)/g' a.test; perl -pi -e 's/__*__/\U$1/g' a.test
thanks
  • Comment on another string replacement with uppercase :: converting C macros to C++
  • Download Code

Replies are listed 'Best First'.
Re: another string replacement with uppercase :: converting C macros to C++
by Athanasius (Archbishop) on Jan 23, 2016 at 08:30 UTC

    Hello f77coder,

    Your first try is close, but has two problems:

    1. You need to capture the text between the double underlines
    2. To run a perl function in the replacement part of a substitution, you need to add an /e modifier.
    #! perl use strict; use warnings; while (<DATA>) { s/__(\S+?)__/uc($1)/eg; print; } __DATA__ #ifdef __some_Long_name__ do stuff #endif

    Output:

    18:25 >perl 1523_SoPW.pl #ifdef SOME_LONG_NAME do stuff #endif 18:27 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      For uppercasing, of course, there is also the builtin \U.
      s/\b__(\w+)__\b/\U$1/;

        Good point! I’d forgotten about these (if I ever knew them1) — except for \Q, which I often have to use. For the record, they’re documented here:

        19:11 >perl -wE "my $s = qq[abc\Udef\Eghijk]; say $s;" abcDEFghijk 19:13 >

        Good to know, thanks!

        Update: 1I must have read about them any number of times. But unfortunately it seems that reading about ≠ knowing, unless it’s combined with using. ;-)

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The second is not farther:
      s/__(\S+)__/\U$1/g
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Excellent! Works like a charm

      Thanks you very much.

Re: another string replacement with uppercase :: converting C macros to C++
by AnomalousMonk (Archbishop) on Jan 23, 2016 at 18:09 UTC

    Beware the wily corner case. I don't know if  '#ifdef ___xxx___' and the cases that follow it are pertinent to your application, but just something to think about:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @ln = ( '#ifdef __some__Long_name__', '__w_x__y_z__', '__aa_bb__cc___dd__ xxx __pp__qq__', '#ifdef ___xxx___', '#ifdef __xxx', '#ifdef xxx__', ); ;; for my $s (@ln) { print qq{'$s'}; $s =~ s{ \b __ (\w+?) __ \b }{\U$1}xmsg; print qq{'$s' \n}; } " '#ifdef __some__Long_name__' '#ifdef SOME__LONG_NAME' '__w_x__y_z__' 'W_X__Y_Z' '__aa_bb__cc___dd__ xxx __pp__qq__' 'AA_BB__CC___DD xxx PP__QQ' '#ifdef ___xxx___' '#ifdef _XXX_' '#ifdef __xxx' '#ifdef __xxx' '#ifdef xxx__' '#ifdef xxx__'


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1153451]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-20 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found