Exemple #1
0
        /// <summary>
        /// Build a <see cref="IStringFormat"/>
        /// </summary>
        /// <param name="builderCallback"></param>
        /// <param name="separators">Separators that separates strings - at least one</param>
        /// <returns><see cref="IStringFormat"/> representing the format</returns>
        public static IStringFormat Build(Action <IStringFormatBuilder> builderCallback, params char[] separators)
        {
            var builder = new StringFormatBuilder(separators);

            builderCallback(builder);
            return(builder.Build());
        }
        /// <inheritdoc/>
        public IStringFormat Parse(string format)
        {
            var separatorsMatch = _separatorsExpression.Match(format);

            ThrowIfMissingSeparators(format, separatorsMatch);
            format = format.Substring(separatorsMatch.Groups[0].Value.Length);
            var separators = separatorsMatch.Groups[1].Captures[0].Value.ToCharArray();
            IStringFormatBuilder builder = new StringFormatBuilder(separators);

            var stringSegments = format.Split(separators);

            foreach (var stringSegment in stringSegments)
            {
                var actualStringSegment = stringSegment;
                var optional            = false;
                var dependingOnPrevious = false;
                var recurring           = false;
                HandlePrefixes(ref actualStringSegment, out optional, out dependingOnPrevious);

                if (IsStringRecurring(actualStringSegment))
                {
                    actualStringSegment = actualStringSegment.Substring(0, actualStringSegment.Length - 1);
                    recurring           = true;
                }

                if (IsVariableSegment(actualStringSegment))
                {
                    actualStringSegment = actualStringSegment.Substring(1, actualStringSegment.Length - 2);
                    builder             = BuildVariableStringSegment(builder, actualStringSegment, optional, dependingOnPrevious, recurring);
                }
                else
                {
                    builder = BuildFixedStringSegment(builder, actualStringSegment, optional, dependingOnPrevious, recurring);
                }
            }

            return(builder.Build());
        }