Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正規表現をコンパイル済みみして高速化 #335

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions AILZ80ASM/AILight/AIName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,33 @@ namespace AILZ80ASM.AILight
public static class AIName
{
private static readonly string RegexPatternLabelValidate = @"^[a-zA-Z0-9_]+$";
private static readonly Regex CompiledRegexPatternLabelValidate = new Regex(
RegexPatternLabelValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternMacroValidate = @"^[a-zA-Z0-9_()]+$";
private static readonly Regex CompiledRegexPatternMacroValidate = new Regex(
RegexPatternMacroValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternLocalLabelNOValidate = @"^[a-zA-Z0-9_]+$";
private static readonly Regex CompiledRegexPatternLocalLabelNOValidate = new Regex(
RegexPatternLocalLabelNOValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternLocalLabelATValidate = @"^@[0-9]+$";
private static readonly Regex CompiledRegexPatternLocalLabelATValidate = new Regex(
RegexPatternLocalLabelATValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternLocalLabelANValidate = @"^@@(?<labelValue>([0-9]*))$";
private static readonly Regex CompiledRegexPatternLocalLabelANValidate = new Regex(
RegexPatternLocalLabelANValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternLabelInvalid = @"^[0-9]";
private static readonly Regex CompiledRegexPatternLabelInvalid = new Regex(
RegexPatternLabelInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternCharMapInvalid = @"^@[a-zA-Z0-9_]+$";
private static readonly Regex CompiledRegexPatternCharMapInvalid = new Regex(
RegexPatternCharMapInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase
);

public static bool DeclareLabelValidate(string target, AsmLoad asmLoad)
{
Expand Down Expand Up @@ -296,7 +317,7 @@ public static bool ValidateCharMapName(string target, AsmLoad asmLoad)
return false;
}

return Regex.Match(target, RegexPatternCharMapInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success;
return CompiledRegexPatternCharMapInvalid.Match(target).Success;
}

/// <summary>
Expand Down Expand Up @@ -343,8 +364,8 @@ private static bool ValidateName(string target, AsmLoad asmLoad)
return false;
}

return Regex.Match(target, RegexPatternLabelValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success &&
!Regex.Match(target, RegexPatternLabelInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success;
return CompiledRegexPatternLabelValidate.Match(target).Success &&
!CompiledRegexPatternLabelInvalid.Match(target).Success;
}

/// <summary>
Expand Down Expand Up @@ -381,8 +402,8 @@ private static bool ValidateNameForMacroName(string target, AsmLoad asmLoad)
return false;
}

return Regex.Match(target, RegexPatternMacroValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success &&
!Regex.Match(target, RegexPatternLabelInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success;
return CompiledRegexPatternMacroValidate.Match(target).Success &&
!CompiledRegexPatternLabelInvalid.Match(target).Success;
}

private static bool ValidateNameForLocalLabel(string target, AsmLoad asmLoad)
Expand All @@ -398,7 +419,7 @@ private static bool ValidateNameForLocalLabel(string target, AsmLoad asmLoad)
return false;
}

var regex = Regex.Match(target, RegexPatternLocalLabelANValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase);
var regex = CompiledRegexPatternLocalLabelANValidate.Match(target);
if (regex.Success)
{
var valueString= regex.Groups["labelValue"].Value;
Expand All @@ -416,8 +437,8 @@ private static bool ValidateNameForLocalLabel(string target, AsmLoad asmLoad)
}

// @と通常ラベルの処理
return Regex.Match(target, RegexPatternLocalLabelATValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success ||
Regex.Match(target, RegexPatternLocalLabelNOValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success;
return CompiledRegexPatternLocalLabelATValidate.Match(target).Success ||
CompiledRegexPatternLocalLabelNOValidate.Match(target).Success;
}

/// <summary>
Expand All @@ -444,8 +465,8 @@ private static bool ValidateNameForFunction(string target, AsmLoad asmLoad)
return false;
}

return Regex.Match(target, RegexPatternLabelValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success &&
!Regex.Match(target, RegexPatternLabelInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success;
return CompiledRegexPatternLabelValidate.Match(target).Success &&
!CompiledRegexPatternLabelInvalid.Match(target).Success;
}

/// <summary>
Expand All @@ -472,8 +493,8 @@ private static bool ValidateNameForFunctionArgument(string target, AsmLoad asmLo
return false;
}

return Regex.Match(target, RegexPatternLabelValidate, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success &&
!Regex.Match(target, RegexPatternLabelInvalid, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success;
return CompiledRegexPatternLabelValidate.Match(target).Success &&
!CompiledRegexPatternLabelInvalid.Match(target).Success;
}

}
Expand Down
89 changes: 71 additions & 18 deletions AILZ80ASM/AILight/AIValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,32 +234,85 @@ public enum ArgumentTypeEnum
/// オペレーション判別・正規表現用
/// </summary>
private static readonly string RegexPatternOperation = $"^(?<operation>({OperationKeysString}))";
private static readonly Regex CompiledRegexPatternOperation = new Regex(
RegexPatternOperation, RegexOptions.Compiled | RegexOptions.IgnoreCase
AILight marked this conversation as resolved.
Show resolved Hide resolved
);

/// <summary>
/// オペレーション判別・正規表現用(文字列のみ)
/// </summary>
private static readonly string RegexPatternWordOperation = $"^(?<operation>({WordOperationKeysString}))";
private static readonly Regex CompiledRegexPatternWordOperation = new Regex(
RegexPatternWordOperation, RegexOptions.Compiled | RegexOptions.IgnoreCase
AILight marked this conversation as resolved.
Show resolved Hide resolved
);

/// <summary>
/// オペレーション判別・正規表現用(記号のみ)
/// </summary>
private static readonly string RegexPatternSymbolOperation = $"^(?<operation>({SymbolOperationKeysString}))";
private static readonly Regex CompiledRegexPatternSymbolOperation = new Regex(
RegexPatternSymbolOperation, RegexOptions.Compiled | RegexOptions.IgnoreCase
AILight marked this conversation as resolved.
Show resolved Hide resolved
);

private static readonly string RegexPatternHexadecimal_HD = @"^\$(?<value>([0-9A-Fa-f]+))$";
private static readonly Regex CompiledRegexPatternHexadecimal_HD = new Regex(
RegexPatternHexadecimal_HD, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);

private static readonly string RegexPatternHexadecimal_HX = @"^0x(?<value>([0-9A-Fa-f]+))$";
private static readonly Regex CompiledRegexPatternHexadecimal_HX = new Regex(
RegexPatternHexadecimal_HX, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);

private static readonly string RegexPatternHexadecimal_TH = @"^(?<value>([0-9A-Fa-f]+))H$";
private static readonly Regex CompiledRegexPatternHexadecimal_TH = new Regex(
RegexPatternHexadecimal_TH, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);

private static readonly string RegexPatternOctal_HO = @"^0o(?<value>([0-7]+))$";
private static readonly Regex CompiledRegexPatternOctal_HO = new Regex(
RegexPatternOctal_HO, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternOctal_TO = @"^(?<value>([0-7]+))O$";
private static readonly Regex CompiledRegexPatternOctal_TO = new Regex(
RegexPatternOctal_TO, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternBinaryNumber_HB = @"^0b(?<value>([01_]+))$";
private static readonly Regex CompiledRegexPatternBinaryNumber_HB = new Regex(
RegexPatternBinaryNumber_HB, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternBinaryNumber_TB = @"^(?<value>([01_]+))B$";
private static readonly Regex CompiledRegexPatternBinaryNumber_TB = new Regex(
RegexPatternBinaryNumber_TB, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternBinaryNumber_HP = @"^%(?<value>([01_]+))$";
private static readonly Regex CompiledRegexPatternBinaryNumber_HP = new Regex(
RegexPatternBinaryNumber_HP, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternDigit_N = @"^(?<value>(\+|\-|)(\d+))$";
private static readonly Regex CompiledRegexPatternDigit_N = new Regex(
RegexPatternDigit_N, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternDigit_D = @"^(?<value>(\+|\-|)(\d+))D$";
private static readonly Regex CompiledRegexPatternDigit_D = new Regex(
RegexPatternDigit_D, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternValue = @"^(?<value>[0-9a-zA-Z_\$#@\.]+)";
private static readonly Regex CompiledRegexPatternValue = new Regex(
RegexPatternValue, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternFunction = @"^(?<function>[0-9a-zA-Z_]+\s*\()";
private static readonly Regex CompiledRegexPatternFunction = new Regex(
RegexPatternFunction, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternFunctionWithNamespace = @"^(?<function>[0-9a-zA-Z_]+\.[0-9a-zA-Z_]+\s*\()";
private static readonly Regex CompiledRegexPatternFunctionWithNamespace = new Regex(
RegexPatternFunctionWithNamespace, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);
private static readonly string RegexPatternSyntaxSuger_AL = @"^\.@@(?<direction>(B|F))";
private static readonly Regex CompiledRegexPatternSyntaxSuger_AL = new Regex(
RegexPatternSyntaxSuger_AL, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase
);

private string Value { get; set; } = "";
private int ValueInt32 { get; set; } = default(int);
Expand Down Expand Up @@ -324,15 +377,15 @@ public AIValue(string value, ValueTypeEnum valueType)
/// <returns></returns>
public static bool TryParseFormula(ref string target, out string resultFormula)
{
var matchOperation = Regex.Match(target, RegexPatternOperation, RegexOptions.IgnoreCase);
var matchOperation = CompiledRegexPatternOperation.Match(target);
if (!matchOperation.Success)
{
resultFormula = "";
return false;
}

// 記号のみチェック
var matchSymbol = Regex.Match(target, RegexPatternSymbolOperation, RegexOptions.IgnoreCase);
var matchSymbol = CompiledRegexPatternSymbolOperation.Match(target);
if (matchSymbol.Success)
{
resultFormula = matchSymbol.Groups["operation"].Value;
Expand All @@ -341,7 +394,7 @@ public static bool TryParseFormula(ref string target, out string resultFormula)
}

// 文字列演算子のチェック
if (!Regex.IsMatch(target, $"{RegexPatternWordOperation}($|\\s+)", RegexOptions.IgnoreCase))
if (!CompiledRegexPatternWordOperation.IsMatch(target))
{
resultFormula = "";
return false;
Expand All @@ -360,7 +413,7 @@ public static bool TryParseFormula(ref string target, out string resultFormula)

if (!string.IsNullOrEmpty(nextTarget))
{
var matchNextSymbol = Regex.Match(nextTarget, RegexPatternSymbolOperation, RegexOptions.IgnoreCase);
var matchNextSymbol = CompiledRegexPatternSymbolOperation.Match(nextTarget);
if (matchNextSymbol.Success)
{
var operationType = OperationTypeTable[matchNextSymbol.Groups["operation"].Value.ToLower()];
Expand Down Expand Up @@ -407,12 +460,12 @@ public static bool TryParseFunction(ref string target, out string resultFunction
{
var functionName = default(string);

var matched = Regex.Match(target, RegexPatternFunction, RegexOptions.Singleline | RegexOptions.IgnoreCase);
var matched = CompiledRegexPatternFunction.Match(target);
if (matched.Success)
{
functionName = matched.Groups["function"].Value;
}
var matchedWithNamespace = Regex.Match(target, RegexPatternFunctionWithNamespace, RegexOptions.Singleline | RegexOptions.IgnoreCase);
var matchedWithNamespace = CompiledRegexPatternFunctionWithNamespace.Match(target);
if (matchedWithNamespace.Success)
{
functionName = matchedWithNamespace.Groups["function"].Value;
Expand Down Expand Up @@ -472,7 +525,7 @@ public static bool TryParseFunction(ref string target, out string resultFunction
/// <returns></returns>
public static bool TryParseSyntaxSuger(ref string target, out string[] resultValues)
{
var matched = Regex.Match(target, RegexPatternSyntaxSuger_AL, RegexOptions.Singleline | RegexOptions.IgnoreCase);
var matched = CompiledRegexPatternSyntaxSuger_AL.Match(target);
if (matched.Success)
{
var direction = matched.Groups["direction"].Value;
Expand Down Expand Up @@ -503,7 +556,7 @@ public static bool TryParseSyntaxSuger(ref string target, out string[] resultVal
/// <returns></returns>
public static bool TryParseValue(ref string target, out string resultValue)
{
var matched = Regex.Match(target, RegexPatternValue, RegexOptions.Singleline | RegexOptions.IgnoreCase);
var matched = CompiledRegexPatternValue.Match(target);
if (matched.Success)
{
resultValue = matched.Groups["value"].Value;
Expand Down Expand Up @@ -1901,9 +1954,9 @@ private static bool TryParse16(string value, out string result)
{
var matched = default(Match);

if ((matched = Regex.Match(value, RegexPatternHexadecimal_HD, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success ||
(matched = Regex.Match(value, RegexPatternHexadecimal_HX, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success ||
(matched = Regex.Match(value, RegexPatternHexadecimal_TH, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success)
if ((matched = CompiledRegexPatternHexadecimal_HD.Match(value)).Success ||
(matched = CompiledRegexPatternHexadecimal_HX.Match(value)).Success ||
(matched = CompiledRegexPatternHexadecimal_TH.Match(value)).Success)
{
result = matched.Groups["value"].Value.Replace("_", "");
return true;
Expand All @@ -1923,9 +1976,9 @@ private static bool TryParse2(string value, out string result)
{
var matched = default(Match);

if ((matched = Regex.Match(value, RegexPatternBinaryNumber_HB, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success ||
(matched = Regex.Match(value, RegexPatternBinaryNumber_TB, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success ||
(matched = Regex.Match(value, RegexPatternBinaryNumber_HP, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success)
if ((matched = CompiledRegexPatternBinaryNumber_HB.Match(value)).Success ||
(matched = CompiledRegexPatternBinaryNumber_TB.Match(value)).Success ||
(matched = CompiledRegexPatternBinaryNumber_HP.Match(value)).Success)
{
result = matched.Groups["value"].Value.Replace("_", "");
return true;
Expand All @@ -1946,8 +1999,8 @@ private static bool TryParse8(string value, out string result)
{
var matched = default(Match);

if ((matched = Regex.Match(value, RegexPatternOctal_HO, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success ||
(matched = Regex.Match(value, RegexPatternOctal_TO, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success)
if ((matched = CompiledRegexPatternOctal_HO.Match(value)).Success ||
(matched = CompiledRegexPatternOctal_TO.Match(value)).Success)
{
result = matched.Groups["value"].Value.Replace("_", "");
return true;
Expand All @@ -1968,8 +2021,8 @@ private static bool TryParse10(string value, out string result)
{
var matched = default(Match);

if ((matched = Regex.Match(value, RegexPatternDigit_N, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success ||
(matched = Regex.Match(value, RegexPatternDigit_D, RegexOptions.Singleline | RegexOptions.IgnoreCase)).Success)
if ((matched = CompiledRegexPatternDigit_N.Match(value)).Success ||
(matched = CompiledRegexPatternDigit_D.Match(value)).Success)
{
result = matched.Groups["value"].Value.Replace("_", "");
return true;
Expand Down
Loading