public string EnsureValidId(string id, bool autoForceUniqueID = false)
{
if (string.IsNullOrEmpty(id))
{
return id;
}
if (char.IsDigit(id[0]))
{
if (autoForceUniqueID)
{
return EnsureValidId("id" + id, true);
}
throw new SvgIDWrongFormatException("ID cannot start with a digit: '" + id + "'.");
}
if (this._idValueMap.ContainsKey(id))
{
if(autoForceUniqueID)
{
var match = regex.Match(id);
int number;
if (match.Success && int.TryParse(match.Value.Substring(1), out number))
{
id = regex.Replace(id, "#" + (number + 1));
}
else
{
id += "#1";
}
return EnsureValidId(id, true);
}
throw new SvgIDExistsException("An element with the same ID already exists: '" + id + "'.");
}
return id;
}
private static readonly Regex regex = new Regex(@"#\d+$");