fix: 修复TrimBracePairs方法可能会出现的问题

main
Carson Lin 2022-07-13 12:50:13 +08:00
parent 02f3ebd979
commit bfc13bc35f
2 changed files with 40 additions and 2 deletions

View File

@ -275,7 +275,7 @@ namespace Luban.Job.Common.Defs
public TType CreateType(string module, string type)
{
type = DefUtil.TrimBracePairs(type, true);
type = DefUtil.TrimBracePairs(type);
int sepIndex = DefUtil.IndexOfBaseTypeEnd(type);
if (sepIndex > 0)
{

View File

@ -136,7 +136,45 @@ namespace Luban.Job.Common.Utils
return -1;
}
public static string TrimBracePairs(string rawType, bool soft = false)
public static string TrimBracePairs(string rawType)
{
while (rawType.Length > 0 && rawType[0] == '(')
{
int braceDepth = 0;
int level1Left = -1;
int level1Right = -1;
for (int i = 0; i < rawType.Length; i++)
{
if (rawType[i] == '(')
{
braceDepth++;
if (level1Left < 0)
{
level1Left = i;
}
}
if (rawType[i] == ')')
{
braceDepth--;
if (level1Right < 0 && braceDepth == 0)
{
level1Right = i;
break;
}
}
}
if (level1Left >= 0 && level1Right == rawType.Length - 1)
{
rawType = rawType.Substring(1, rawType.Length - 2);
}
else
{
break;
}
}
return rawType;
}
public static string TrimBracePairs2(string rawType, bool soft = false)
{
while (rawType.Length > 0 && rawType[0] == '(')
{