75 lines
2.5 KiB
C#
75 lines
2.5 KiB
C#
using System.Text.RegularExpressions;
|
|
using System.Text;
|
|
using LightSetupMd5;
|
|
using System.Linq;
|
|
|
|
namespace LightSetupMd5
|
|
{
|
|
internal class Md5Format
|
|
{
|
|
private static Regex reg = new Regex("\\{(.+?)\\}");
|
|
|
|
public static string GetFormat(string fileFormat, Dictionary<string, string> extendPara)
|
|
{
|
|
return getCodeStringArray(fileFormat, extendPara);
|
|
}
|
|
|
|
/**
|
|
* 获取字段替换值
|
|
*
|
|
* @param from 来源字符串
|
|
* @param targets 值对象
|
|
* @return 处理后的值对象
|
|
*/
|
|
public static string getCodeStringArray(string from, Dictionary<string, string> targets)
|
|
{
|
|
return getFormat(from, String.Empty, new StringFormatHandleDictionary(targets));
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="format"></param>
|
|
/// <param name="defaultField"></param>
|
|
/// <param name="fieldFormatHandle"></param>
|
|
/// <returns></returns>
|
|
public static string getFormat(string format, string defaultField, StringFormatHandle fieldFormatHandle)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
MatchCollection matches = reg.Matches(format);
|
|
int start = 0;
|
|
foreach (Match match in matches)
|
|
{
|
|
if (!match.Success)
|
|
{
|
|
continue;
|
|
}
|
|
int index = match.Index;
|
|
int len = index - start;
|
|
if (len > 0)
|
|
{
|
|
sb.Append(format, start, index - start);
|
|
}
|
|
string group = match.Groups[0].Value;
|
|
string fieldFull = match.Groups[1].Value;
|
|
if (fieldFull.StartsWith("0") && !string.IsNullOrEmpty(defaultField))
|
|
{
|
|
fieldFull = defaultField + fieldFull.Substring(1);
|
|
}
|
|
string[] fields = fieldFull.Split(":");
|
|
string field = fields[0];
|
|
string fieldFormat = string.Empty;
|
|
if (fields.Length > 1)
|
|
{
|
|
fieldFormat = fieldFull.Substring(field.Length + 1);
|
|
}
|
|
fieldFormatHandle.addPos(sb, group, fieldFull, field, fieldFormat);
|
|
start = index + match.Length;
|
|
}
|
|
sb.Append(format.Substring(start));
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |