Files
caliverse_server/ServerCommon/Helper/MetaTextStringHelper.cs
2025-05-01 07:20:41 +09:00

77 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using FUNCTION_NAME = System.String;
namespace ServerCommon;
public static class MetaTextStringHelper
{
public delegate string META_TEXT_STRING_FUNC(object caller, params object[] parameters);
private static readonly Dictionary<FUNCTION_NAME, META_TEXT_STRING_FUNC> m_methods = new();
// 초기 입력 문자열 저장
private static string m_input_string = string.Empty;
private static StringBuilder m_result_builder = new StringBuilder();
public static bool parseAndExecute(string funcName, object caller, params object[] parameters)
{
// 등록된 함수가 있는지 확인
if (m_methods.ContainsKey(funcName))
{
string func_result = m_methods[funcName](caller, parameters); // 함수 실행
// {순번: 함수명} 패턴을 찾아 함수 결과로 대체
string pattern = $@"\{{\d+\s*:\s*{funcName}\}}";
// 패턴을 찾고 함수 결과로 대체
m_result_builder = new StringBuilder(Regex.Replace(m_result_builder.ToString(), pattern, func_result));
return true;
}
return false;
}
public static void inputString(string input)
{
m_input_string = input;
m_result_builder = new StringBuilder(input); // 입력 문자열로 초기화
}
public static string getResult()
{
return m_result_builder.ToString();
}
public static bool registerFunc(string name, META_TEXT_STRING_FUNC func)
{
if (true == m_methods.ContainsKey(name))
{
return false;
}
m_methods[name] = func;
return true;
}
// 문자열 내에 {순번: 함수명} 구조가 존재하는지 확인해주는 함수 !!!, 존재할 경우 true 반환 한다.
public static bool isContainsMetaTextStringFunctionPattern(string input)
{
// {숫자: 함수명} 구조를 찾는 정규 표현식을 설정 한다.
var pattern = @"\{\d+\s*:\s*[a-zA-Z_]\w*\}";
// 정규 표현식으로 입력 문자열에서 해당 패턴이 존재하는지 확인 한다. !!!
return Regex.IsMatch(input, pattern);
}
}