Пример #1
0
        private static ICollection <IDictionary <string, string> > TakeToPieces(string input)
        {
            var match = AddressSegmentsHelper.FullRegex.Match(" " + input);

            Assert.IsTrue(match.Success);

            Assert.AreEqual(2 + 1, match.Groups.Count);
            Assert.GreaterOrEqual(match.Groups["Mark"].Captures.Count, match.Groups["Number"].Captures.Count);

            var total  = match.Groups["Number"].Captures.Count;
            var offset = total;

            int primaries = 2;

            IDictionary <string, string> acc = new Dictionary <string, string>();

            var skip = 0;

            foreach (var mark in Marks)
            {
                if (skip > 0)
                {
                    --skip;
                    continue;
                }
                if (offset == 0)
                {
                    break;
                }

                var capturedMark =
                    match.Groups["Mark"].Captures[total - offset].Value.Replace("-", string.Empty)
                    .Replace(",", string.Empty)
                    .Trim();
                var capturedNumber = match.Groups["Number"].Captures[total - offset].Value;
                if (!string.IsNullOrEmpty(capturedMark))
                {
                    var tail = Marks.SkipWhile(i => i.Key != mark.Key);

                    var tailHead = tail.TakeWhile(i => !capturedMark.StartsWith((string)i.Value.label));
                    skip = tailHead.Count() - primaries;

                    if (skip > 0)
                    {
                        continue;
                    }
                }
                else if (offset <= primaries && mark.Value.isPrimary != true)
                {
                    continue;
                }

                acc.Add(mark.Value.label, capturedNumber);

                --offset;
                if (mark.Value.isPrimary == true)
                {
                    --primaries;
                }
            }
            return(new[] { acc });
        }