Ejemplo n.º 1
0
        public static bool TryParse(string toCheck, int maxNumberOfReplays, bool throwOnExceedingMax, out CustomReplayFormat customReplayFormat)
        {
            if (string.IsNullOrWhiteSpace(toCheck))
            {
                customReplayFormat = null;
                return(false);
            }

            if (maxNumberOfReplays <= 0)
            {
                customReplayFormat = null;
                return(false);
            }

            var match = _escapeCharacter.Match(toCheck);

            var customFormatStringBuilder = new StringBuilder();
            List <Tuple <CustomReplayNameSyntax, string> > customReplayFormatSections = new List <Tuple <CustomReplayNameSyntax, string> >();

            var matchCounter          = 0;
            var previousMatchIndexEnd = 0;

            while (match.Success)
            {
                var matchIndex    = match.Index;
                var literalFormat = toCheck.Substring(previousMatchIndexEnd, matchIndex - previousMatchIndexEnd);

                if (matchIndex + 1 >= toCheck.Length)
                {
                    customReplayFormat = null;
                    return(false);
                }

                var stringContainingFormatSpecifier = toCheck.Substring(matchIndex + 1);
                var formatRegex = _formatRegexes.FirstOrDefault(r => r.Key.IsMatch(stringContainingFormatSpecifier));

                if (formatRegex.Equals(default(KeyValuePair <Regex, CustomReplayNameSyntax>)))
                {
                    customReplayFormat = null;
                    return(false);
                }
                var formatSpecifier = formatRegex.Key.Match(stringContainingFormatSpecifier).Groups[1].Value;
                customFormatStringBuilder.Append($"{{{matchCounter++}}}{{{matchCounter++}}}");
                customReplayFormatSections.Add(Tuple.Create(CustomReplayNameSyntax.None, literalFormat));
                customReplayFormatSections.Add(Tuple.Create(formatRegex.Value, formatSpecifier));
                previousMatchIndexEnd = matchIndex + 1 + formatSpecifier.Length;
                match = match.NextMatch();
                while (match.Success && match.Index < previousMatchIndexEnd)
                {
                    match = match.NextMatch();
                }
            }
            customFormatStringBuilder.Append($"{{{matchCounter++}}}");
            customReplayFormatSections.Add(Tuple.Create(CustomReplayNameSyntax.None, toCheck.Substring(previousMatchIndexEnd)));
            customReplayFormat = new CustomReplayFormat(customFormatStringBuilder.ToString(), customReplayFormatSections, maxNumberOfReplays, throwOnExceedingMax);
            return(true);
        }
Ejemplo n.º 2
0
        public bool Equals(CustomReplayFormat other)
        {
            if (other == null)
            {
                return(false);
            }

            return
                (CustomFormat == other.CustomFormat &&
                 CustomFormatSections.SequenceEqual(other.CustomFormatSections) &&
                 MaxNumberOfReplays == other.MaxNumberOfReplays &&
                 ThrowOnExceedingMax == other.ThrowOnExceedingMax);
        }