浏览代码

Add funciton to handle duplicate naming

/main
Jens Holm 6 年前
当前提交
49ac046f
共有 1 个文件被更改,包括 48 次插入1 次删除
  1. 49
      com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs

49
com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs


using System;
using System;
using System.Text.RegularExpressions;
using UnityEditor.Graphing;
using UnityEditor.Graphing.Util;
using UnityEngine;

}
return s_LegacyTypeRemapping;
}
// Sanitize a name to not conflict with other names.
// `existingNames` is the collection of names that the sanitized name should not collide with.
// `duplicateFormat` is the format applied to handle cases with overlapping names. The string supplied should
// be a formattable string where the first argument is the base name, and the second argument the
// `duplication number`.
// An example of input ({"name", "name (1)"}, "{0} ({1})", name) should yield the output "name (2)".
public static string SanitizeName(IEnumerable<string> existingNames, string duplicateFormat, string name)
{
if (!existingNames.Contains(name))
return name;
string escapedDuplicateFormat = Regex.Escape(duplicateFormat);
// Escaped format will escape string interpolation, so the escape caracters must be removed for these.
escapedDuplicateFormat = escapedDuplicateFormat.Replace(@"\{0}", @"{0}");
escapedDuplicateFormat = escapedDuplicateFormat.Replace(@"\{1}", @"{1}");
var baseRegex = new Regex(string.Format(escapedDuplicateFormat, @"^(.*)", @"(\d+)"));
var baseMatch = baseRegex.Match(name);
if (baseMatch.Success)
name = baseMatch.Groups[1].Value;
string baseNameExpression= string.Format(@"^{0}", Regex.Escape(name));
var regex = new Regex(string.Format(escapedDuplicateFormat, baseNameExpression, @"(\d+)") + "$");
var existingDuplicateNumbers = existingNames.Select(existingName => regex.Match(existingName)).Where(m => m.Success).Select(m => int.Parse(m.Groups[1].Value)).Where(n => n > 0).Distinct().ToList();
var duplicateNumber = 1;
existingDuplicateNumbers.Sort();
if (existingDuplicateNumbers.Any() && existingDuplicateNumbers.First() == 1)
{
duplicateNumber = existingDuplicateNumbers.Last() + 1;
for (var i = 1; i < existingDuplicateNumbers.Count; i++)
{
if (existingDuplicateNumbers[i - 1] != existingDuplicateNumbers[i] - 1)
{
duplicateNumber = existingDuplicateNumbers[i - 1] + 1;
break;
}
}
}
return string.Format(duplicateFormat, name, duplicateNumber);
}
}
}
正在加载...
取消
保存