I am a newbie in regex, I try to write a C# application that replace all of old cast style of c++ file to static_cast style.
For example:
(void)memcpy(&a[0],(void * )hihi, (UBYTE) V_SIZE);
(void) memcpy((VOID*)abc, (const VOID*) dafa, (uint8)NUMBER_SIZE);
(void )memcpy(
(void *)p,
&abc, (uint8)DATE_SIZE);
I expect all of them should be
static_cast<void>(memcpy(&a[0], static_cast<void * >(hihi), static_cast<UBYTE>( V_SIZE)));
static_cast<void> memcpy(static_cast<VOID*>(abc), static_cast<const VOID*> (hihi), static_cast<uint8>(NUMBER_SIZE));
static_cast<void >memcpy(
static_cast<void *>(p),
&abc, static_cast<uint8>(DATE_SIZE));
I also investigate and try with this one
List<string> castTypeList = new List<string>
{"void", "void *", "UBYTE", "uint8", "VOID*", "uint8" };
// Fix qacpp-4.7.0-3080 This expression uses old style casts
// Only apply for cpp source file (.cpp)
if ((Path.GetExtension(sourceFile) == ".cpp"))
{
foreach (string e in castTypeList)
{
File.WriteAllText(sourceFile, Regex.Replace(
File.ReadAllText(sourceFile),
@"\(" + e + @"\)([^):;\r\n},]*)",
"static_cast<" + e + @">($1)"));
}
}
The result look good, but not perfect, some string can't match and replace (You can see below picture). Is there any better solution or better idea to handle it?