0

I would like to modify all the function which are of the following kind:

returnType functionName(parameters){
    OLD_LOG; // Always the first line of the function
    //stuff to do

    return result; // may not be here in case of function returning void
} // The ending } is not always at the beginning of the line (but is always the first not white space of the line and has the same number of white space before than 'returnType' does)

by

returnType functionName(parameters){
    NEW_LOG("functionName"); // the above function name

    //stuff to do

    END_LOG();
    return result; //if any return (if possible, END_LOG() should appear just before any return, or at the end of the function if there is no return)
}

There is a at least a hundred of those functions.

Therefore I would like to know if it is possible to do that using a "look for/replace" in a text editor supporting regex for exemple, or anything else.

Thank you

azerty
  • 698
  • 7
  • 28

3 Answers3

1

here is an attempt for the same

Regex

/(?<=\s)(\w+)(?=\()(.*\{\n.*)(OLD_LOG;)(.*)(\n\})/s

Test String

returnType functionName(parameters){
    OLD_LOG;
    //stuff to do

}

Replace string

\1 \2NEW_LOG("\1");\n\4\n    END_LOG();\5

Result

returnType functionName (parameters){
    NEW_LOG("functionName");

    //stuff to do

    END_LOG();
}

live demo here


I have updated the regex to include optional return statement & optional spaces

Regex

/(?<=\s)(\w+)(?=\()(.*\{\n.*)(OLD_LOG;)(.*?)(?=(?:\s*)return|(?:\n\s*\}))/s

Replace string

\1 \2NEW_LOG("\1");\n\4\n    END_LOG();

demo for return statement

demo for optional spaces

see if this works for you

pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • Hi, I updated the question (i realized I forgot an important point: END_LOG() should appear just before any return, or at the end of the function if there is no return) Do you think your regex could be easily adapted to such thing ? – azerty Aug 11 '14 at 10:14
  • And the ending } is not always at the beginning of the line (but is always the first not white space of the line and has the same number of white space before than 'returnType' does) – azerty Aug 11 '14 at 10:41
  • I have updated the same, secondly do not worry for spaces before returnType because be are starting our regex from a look-behind. – pushpraj Aug 11 '14 at 11:59
  • Thanks, but there is still one more problem. Maybe I was not clear enough, but I want every return to be preceded by NEW_LOG("functionName"); see exemple: http://regex101.com/r/xB7uX2/5 – azerty Aug 11 '14 at 13:27
  • that may not be so easy with single regex, it may take lot of time time to adjust for all possible scenarios you may perhaps replace the instances of return with the following http://regex101.com/r/xB7uX2/7, I would suggest you to go for multiple replacements. – pushpraj Aug 12 '14 at 03:13
1

Find

(\n([^\S\n]*)[^\s].*\s([^\s\(]+)\s*\(.*\)\s*\{\s*\n)(\s*)OLD\_LOG;((.*\s*\n)*?)(\s*return\s.*\r\n)?\2\}

Replace with

\1\4NEW\_LOG\(\"\3\"\);\5\4END_LOG\(\);\r\n\7\2\}

Notice that \n and \r\n are used. If your code file uses a different newline format, you need to modify accordingly.

Limitations of this replace are these assumptions:

1) OLD_LOG; is just one line below the function name.

2) Function has return type (any non space character before the function name is okay).

3) Function name and { are at the same line.

4) Ending } has the same number of white space before than 'returnType' does, and there is no such } inside the function.

5) Last return is just one line above the ending }.

cshu
  • 5,654
  • 28
  • 44
  • I updated the question (i realized I forgot an important point: END_LOG() should appear just before any return, or at the end of the function if there is no return) Do you think your regex could be easily adapted to such thing ? – azerty Aug 11 '14 at 10:27
  • And the ending } is not always at the beginning of the line (but is always the first not white space of the line and has the same number of white space before than 'returnType' does) – azerty Aug 11 '14 at 10:41
  • I can't make it work on either notepad++, Visual studio nor here: http://regex101.com/r/eT4bB7/1 Any idea why ? – azerty Aug 11 '14 at 14:23
  • In this case there are two `return`, does that means inserting two `END_LOG();`? This is like code generation, it's difficult. – cshu Aug 11 '14 at 14:33
  • Yes, I would like to have a END_LOG() before every return(); – azerty Aug 11 '14 at 16:41
0

It may be faster to use an editor with multiple carets support (e.g. Sublime Text, IntelliJ):

enter image description here

Community
  • 1
  • 1
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • But I think that this won't allow me to put the name of the function in the edited text. – azerty Aug 11 '14 at 09:22
  • Of course you will be able to do so. 1) place a cursor before each _returnType_ in your file, 2) jump to function name using `Ctrl+Right` 3) select function name 4) go to next line and create a new one 5) start `NEW_…`. All this will occurs on each cursor location – Édouard Lopez Aug 11 '14 at 09:32