0

I'm parsing .h and .cpp files and I need to find/replace all non-Hungarian notated variables with their Hungarian equivalents. "Augh, why?!" you ask? My employer requires Hungarian notation, 'nuff said.

Let's just deal with ints for now.

Given any of these cases...

int row;      // no hungarian prefix
int nrow(9);  // incorrect capitalization
int number;   // hmm...
int nnumber = getValue(); // uh oh!

They should be changed to:

int nRow;
int nRow(9);    // obviously ctor args and assignments shouldn't change
int nNumber;
int nNumber = getValue();

I'm shooting for just a single one-line call to s/// ideally.

For added challenge, if someone can get something to change ALL instances of this variable after the "type check" with int, that would earn you some brownie points.

Here's what I have so far:

s/(int\s+)(?!n)(\w+)/$1n\u$2/g;

This doesn't match things like int nrow or int number though.

Thanks in advance!

kevlar1818
  • 3,055
  • 6
  • 29
  • 43
  • 4
    Can we recommend a new employer instead? – Bo Persson Aug 02 '11 at 21:34
  • 3
    That's not how Hungarian notation should be used. Here's the original whitepaper: http://msdn.microsoft.com/en-us/library/Aa260976 and here's a joelonsoftware post about it: http://www.joelonsoftware.com/articles/Wrong.html – IronMensan Aug 02 '11 at 21:44
  • @IronMensan great article! goes straight to my bookmark. – SJuan76 Aug 03 '11 at 07:33
  • In your example you are mapping _different_ names to the same name with your renaming!! For example `number` and `nnumber` both become `nNumber`. A sure way to break working code... – hexcoder Aug 04 '11 at 08:19

1 Answers1

8

No.

More explicitly, you are trying to compile a program using a regex. This does not work that way.

For example, your one-line already forgets of function parameters, and it cannot parse any user defined type (struct, enum). Not to mention that .cpp suggests C++ suggest classes.

Also, what happens with method / function / inline comments?

My advice would be find somewhere a grammar compiler and pass it the c++ grammar, so for every definition you get the value and type written down to a file. Then you can have regex fun with it. You may try also to write every time each variable is used in order to replace them later automatically.

Yet a lot more complex that a simple regex, but that simple regex will fail so much that at the end you will be changing the code manually.

As a positive note, maybe when you tell your boss how much the chane does cost maybe he will think about it better.

Community
  • 1
  • 1
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • I'm not trying to "compile a program using regex". I have no idea where you got that from. In fact I don't PLAN on having it compile after I'm done, but at least I can go back and make some quick changes to syntax and be good. – kevlar1818 Aug 02 '11 at 21:53
  • 5
    Yes you are going to compile it, but you don't yet know it. In order to know if the id variable of line 30 is the nId from line 10 or the sId from line 20, you need to know in which method/function you are. Also, the issues with program defined types, classes. You will need to parse the program, build the symbol table... the fact that you produce machine code or a modified source code is not relevant. – SJuan76 Aug 02 '11 at 22:06