Skip to content

Commit

Permalink
use named regex for replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
shemogumbe committed Apr 19, 2024
1 parent 5a88b85 commit 246e6c5
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions CodeSnippetsReflection/StringExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public static class StringExtensions
/// <param name="doubleQuoteEscapeSequence">The string value to replace double-quotes</param>
/// <param name="singleQuoteEscapeSequence">The string value to replace single-quotes</param>
/// <returns></returns>
private static readonly Regex ImportPathRegex = new Regex(@"(\w+)[A-Z]?(\w*)\((\w+Id)='(\{[^{}]+\})'\)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200));
private static readonly Regex SnakeCaseRegex = new Regex(@"(\B[A-Z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200));

public static string EscapeQuotesInLiteral(this string stringLiteral,
string doubleQuoteEscapeSequence,
string singleQuoteEscapeSequence)
Expand Down Expand Up @@ -61,7 +64,7 @@ public static string ToPascalCase(this string str)
return pascalCaseString;
}

public static string ToSnakeCase(this string str)
public static string ToSnakeCase(this string str)
{
if (string.IsNullOrEmpty(str)) return str;
StringBuilder snakeCaseBuilder = new StringBuilder();
Expand Down Expand Up @@ -89,28 +92,28 @@ public static string ToSnakeCase(this string str)
return snakeCaseBuilder.ToString();
}

public static string CleanUpImportPath(this string input)
{
string pattern = @"(\w+)[A-Z]?(\w*)\((\w+Id)='(\{[^{}]+\})'\)";

string result = Regex.Replace(input, pattern, m =>
{
string firstPart = m.Groups[1].Value;
string secondPart = m.Groups[2].Value;
string idPart = m.Groups[3].Value;
public static string CleanUpImportPath(this string input)
{
string result = StringExtensions.ImportPathRegex.Replace(input, m =>
{
string firstPart = m.Groups[1].Value;
string secondPart = m.Groups[2].Value;
string idPart = m.Groups[3].Value;

// Given Id e.g appIdd, groupID - convert to snake case
secondPart = Regex.Replace(secondPart, @"(\B[A-Z])", x => "_" + x.Value.ToLower(), RegexOptions.Compiled, TimeSpan.FromSeconds(60));
idPart = Regex.Replace(idPart, @"(\B[A-Z])", x => "_" + x.Value.ToLower(), RegexOptions.Compiled, TimeSpan.FromSeconds(60));
// Given Id e.g appIdd, groupID - convert to snake case
secondPart = StringExtensions.SnakeCaseRegex.Replace(secondPart, x => "_" + x.Value.ToLower());
idPart = StringExtensions.SnakeCaseRegex.Replace(idPart, x => "_" + x.Value.ToLower());

return $"{firstPart}_{secondPart}_with_{idPart}";
}, RegexOptions.Compiled, TimeSpan.FromSeconds(60));
result = result.Replace("$", "");

return result;
}
return $"{firstPart}_{secondPart}_with_{idPart}";
});

result = result.Replace("$", "");


return result;
}


public static string EscapeQuotes(this string stringValue)
{
return stringValue.Replace("\\\"", "\"")//try to unescape quotes in case the input string is already escaped to avoid double escaping.
Expand Down

0 comments on commit 246e6c5

Please sign in to comment.