-1

The following code snippet(Though in Java) converts from CamelCase to snake_case. What should I do to do this in the C/C++ macro (i.e in the #define line)?

    String regex = "([a-z])([A-Z]+)";
    String replacement = "$1_$2";
    System.out.println("CamelCaseToSomethingElse".replaceAll(regex, replacement).toLowerCase());
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • It is possible to have macros in Java code! refer - http://stackoverflow.com/questions/6525059/can-i-have-macros-in-java-source-files – Prakadeessh Arunachalam Sep 26 '16 at 19:24
  • Identifier names, including function and variables, are lost after compilation (except for debug mode). So converting from CamelCase to snake_case is a waste of your development time. – Thomas Matthews Sep 26 '16 at 19:36

2 Answers2

1

Do this sort of transform in the build process.

Write a transform program which, given an input file of string, will produce a C/C++ header file containing defines for each transform required.

So, given an input file with an entry:

TOKEN="Hello World"

Create a transform which outputs a header file containing the following:

#define R_TOKEN="Hello World" // Regular text
#define U_TOKEN="HELLO_WORLD" // Upper case token transform
#define L_TOKEN="hello_world" // Lower case token transform
#define C_TOKEN="HelloWorld"  // Capital CamelCase token transform
#define K_TOKEN="helloWorld"  // Lowercase CamelCase token transform

And so forth...

Add the transform to your build rules, and the resultant header becomes a dependency on your source files which require it (google makedepends).

(trivial to do in make...)

simon.watts
  • 976
  • 10
  • 14
-1

What should I do to do this in the C/C++ macro (i.e in the #define line)?

You must study C++ very well, gain enough weight in the C++ community and become a member of the C++ standardization committee. Then you can propose a sound enhancement to the C++ standard which will allow this to be implemented as a macro. However don't hope for this to happen earlier than in c++2x. Therefore, revising your requirements may be a better way of solving your problem at hand.

Leon
  • 31,443
  • 4
  • 72
  • 97