public static FilterSettings FromString(string settingsString)
        {
            if (string.IsNullOrEmpty(settingsString))
            {
                return(null);
            }

            // parse filter name
            int startCharPos = settingsString.IndexOf(SettingsListStartCharacter);

            if (startCharPos < 1)
            {
                Logger.log.Warn("Unable to deserialize string to FilterSettings object.");
                return(null);
            }

            try
            {
                while (settingsString[startCharPos - 1] == EscapeCharacter)
                {
                    startCharPos = settingsString.IndexOf(SettingsListStartCharacter, startCharPos + 1);
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                Logger.log.Warn("Unable to deserialize string to FilterSettings object.");
                return(null);
            }

            StringBuilder name = new StringBuilder(settingsString.Substring(0, startCharPos)).UnescapeString(EscapeCharacter, EscapeMapping);

            FilterSettings filterSettings = new FilterSettings();

            filterSettings.Name = name.ToString();

            // parse filter settings
            int endCharPos = settingsString.IndexOf(SettingsListEndCharacter, startCharPos);

            if (endCharPos < 0)
            {
                return(null);
            }

            string[] settings = settingsString.Substring(startCharPos + 1, endCharPos - startCharPos - 1).Split(SettingsListSeparatorCharacter);
            foreach (var setting in settings)
            {
                var kv = FilterSettingsKeyValuePair.FromString(new StringBuilder(setting).UnescapeString(EscapeCharacter, EscapeMapping).ToString());
                if (kv != null)
                {
                    filterSettings.Settings.Add(kv);
                }
            }

            return(filterSettings);
        }
Example #2
0
        public static FilterSettings FromString(string settingsString)
        {
            if (string.IsNullOrEmpty(settingsString))
            {
                return(null);
            }

            // parse filter name
            int startCharPos = settingsString.IndexOf(SettingsListStartCharacter);

            if (startCharPos < 1)
            {
                Logger.log.Warn("Unable to deserialize string to FilterSettings object.");
                return(null);
            }

            try
            {
                while (settingsString[startCharPos - 1] == EscapeCharacter)
                {
                    startCharPos = settingsString.IndexOf(SettingsListStartCharacter, startCharPos + 1);
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                Logger.log.Warn("Unable to deserialize string to FilterSettings object.");
                return(null);
            }

            StringBuilder name = new StringBuilder(settingsString.Substring(0, startCharPos));

            for (int i = 0; i < name.Length; ++i)
            {
                if (name[i] == EscapeCharacter && (i + 1) < name.Length)
                {
                    char c = name[i + 1];

                    switch (c)
                    {
                    case EscapeCharacter:
                        name.Replace($"{EscapeCharacter}{EscapeCharacter}", EscapeCharacter.ToString(), i, 1);
                        break;

                    case NewLineReplacementCharacter:
                        name.Replace($"{EscapeCharacter}{NewLineReplacementCharacter}", "\n", i, 1);
                        break;

                    case BackslashReplacementCharacter:
                        name.Replace($"{EscapeCharacter}{BackslashReplacementCharacter}", "\\", i, 1);
                        break;

                    case SettingsListStartCharacter:
                        name.Replace($"{EscapeCharacter}{SettingsListStartCharacter}", SettingsListStartCharacter.ToString(), i, 1);
                        break;

                    default:
                        name.Remove(i, 1);
                        --i;
                        break;
                    }
                }
            }

            FilterSettings filterSettings = new FilterSettings();

            filterSettings.Name = name.ToString();

            // parse filter settings
            int endCharPos = settingsString.IndexOf(SettingsListEndCharacter, startCharPos);

            if (endCharPos < 0)
            {
                return(null);
            }

            string[] settings = settingsString.Substring(startCharPos + 1, endCharPos - startCharPos - 1).Split(SettingsListSeparatorCharacter);
            foreach (var setting in settings)
            {
                var kv = FilterSettingsKeyValuePair.FromString(setting);
                if (kv != null)
                {
                    filterSettings.Settings.Add(kv);
                }
            }

            return(filterSettings);
        }