in src/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration/Extensions/StringExtensions.cs [126:179]
public static string RemoveRoslynDuplicateString(this string value)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
string specialCharRegex = "[^a-zA-Z0-9_]";
// Only alphanumeric or underscore characters are allowed in an identifier name.
// So look for special character other than these.
Match match = Regex.Match(value, specialCharRegex);
if (!match.Success)
{
return value;
}
string firstOccurredSpecialCharacter = match.Value;
var specialCharIndex = value.IndexOf(firstOccurredSpecialCharacter[0]);
// @ is allowed at the start of the identifier name, so look for special character again in the substring
// not including first char.
// e.g. if a param name is @skip@skip, roslyn will compile it as @skip@skip@skip.
if (firstOccurredSpecialCharacter == "@")
{
match = Regex.Match(value.Substring(1), specialCharRegex);
if (!match.Success)
{
return value;
}
specialCharIndex = value.IndexOf(match.Value[0], 1);
}
// Divide the string after special character into two halves and check if they are equal.
// If equal take only first half of string. if not equal return value as is.
var valueLengthStartingWithSpecialCharToEnd = value.Length - specialCharIndex;
// If the length is odd then its a mismatch, return value as is.
if (valueLengthStartingWithSpecialCharToEnd % 2 != 0)
{
return value;
}
var firstHalf = value.Substring(specialCharIndex, valueLengthStartingWithSpecialCharToEnd / 2);
var secondHalf = value.Substring(specialCharIndex + valueLengthStartingWithSpecialCharToEnd / 2,
valueLengthStartingWithSpecialCharToEnd / 2);
return firstHalf == secondHalf ? value.Substring(0, specialCharIndex + firstHalf.Length)
: value;
}