Last active
January 27, 2022 17:14
-
-
Save tmsampson/6394b5f12a0eafb289d26d1d207ae48d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//---------------------------------------------------------------------------------------------------------- | |
//---------------------------------------------------------------------------------------------------------- | |
// Overview | |
//---------------------------------------------------------------------------------------------------------- | |
// GeneratePreProcessorDefinition takes an input string and returns a C++ style pre-processor definition | |
//---------------------------------------------------------------------------------------------------------- | |
// Usage | |
//---------------------------------------------------------------------------------------------------------- | |
// string PreProcessorDefinition = GeneratePreProcessorDefinition("Input String"); | |
//---------------------------------------------------------------------------------------------------------- | |
// Examples | |
//---------------------------------------------------------------------------------------------------------- | |
// MyPluginName --> SOME_PREFIX_MY_PLUGIN_NAME | |
// MyPluginName1 --> SOME_PREFIX_MY_PLUGIN_NAME_1 | |
// MyPluginAIModule --> SOME_PREFIX_MY_PLUGIN_AI_MODULE | |
// AIWidget --> SOME_PREFIX_AI_WIDGET | |
// AI1Widget --> SOME_PREFIX_AI_1_WIDGET | |
// AIWidgetFoo --> SOME_PREFIX_AI_WIDGET_FOO | |
// MyPluginName1Module --> SOME_PREFIX_MY_PLUGIN_NAME_1_MODULE | |
// My Plugin Name --> SOME_PREFIX_MY_PLUGIN_NAME | |
// My Plugin1 Name --> SOME_PREFIX_MY_PLUGIN_1_NAME | |
// My Plugin 1Name --> SOME_PREFIX_MY_PLUGIN_1_NAME | |
// lowercase string with spaces --> SOME_PREFIX_LOWERCASE_STRING_WITH_SPACES | |
//---------------------------------------------------------------------------------------------------------- | |
//---------------------------------------------------------------------------------------------------------- | |
static string GeneratePreProcessorDefinition(string Input) | |
{ | |
Func<char, bool> IsSpaceOrSeparator = C => (C == ' ' || C == '_' || C == '-'); | |
string Output = "SOME_PREFIX_"; | |
char[] InputChars = Input.ToCharArray(); | |
for(int i = 0; i < InputChars.Length; ++i) | |
{ | |
char CurrentChar = InputChars[i]; | |
if(IsSpaceOrSeparator(CurrentChar)) | |
{ | |
continue; // Ignore spaces | |
} | |
else if(i > 0) | |
{ | |
char PreviousChar = InputChars[i - 1]; | |
if(char.IsUpper(CurrentChar) && i < (InputChars.Length - 1)) | |
{ | |
char NextChar = InputChars[i + 1]; | |
if(char.IsLower(NextChar) || char.IsLower(PreviousChar) || IsSpaceOrSeparator(PreviousChar)) | |
{ | |
// Insert _ if uppercase and previous or next character are lowercase | |
// Insert _ if uppercase and previous character is separator | |
Output += "_"; | |
} | |
} | |
else if(char.IsNumber(CurrentChar) && !char.IsNumber(PreviousChar)) | |
{ | |
Output += "_"; // Insert _ if this is the first number in a sequence | |
} | |
else if(PreviousChar == ' ') | |
{ | |
Output += "_"; // Insert _ in place of space | |
} | |
} | |
Output += char.ToUpper(CurrentChar); | |
} | |
return Output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment