Svg.SvgElementIdManager.EnsureValidId C# (CSharp) Method

EnsureValidId() public method

Ensures that the specified ID is valid within the containing SvgDocument.
/// The ID cannot start with a digit. /// An element with the same ID already exists within the containing . ///
public EnsureValidId ( string id, bool autoForceUniqueID = false ) : string
id string A containing the ID to validate.
autoForceUniqueID bool Creates a new unique id .
return string
        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+$");