internal static Range Create(ScriptureInfo scriptureInfo, Data.RawRange range, int offset, RepositoryMode mode)
        {
            Range result = new Range();

            result._scriptureInfo = scriptureInfo;
            result.Offset         = offset;

            result.Start = Verse.Create(scriptureInfo, range.FirstBook, range.FirstChapter, range.FirstVerse, range.FirstVerseSuffix, VersePosition.First, mode);
            result.End   = Verse.Create(scriptureInfo, range.SecondBook, range.SecondChapter, range.SecondVerse, range.SecondVerseSuffix, VersePosition.Last, mode);

            int firstChapterLimitsOffset = 1 - result.Start.VerseNumber;

            result._offsetChapterLimits =
                OffsetChapterLimits.Create(
                    firstChapterLimitsOffset,
                    scriptureInfo.ChapterLimits.Where(c => result.Start <= c && c <= result.End)
                    );

            result.Length =
                result._offsetChapterLimits.Select(c => c.ChapterLimits).Sum(c => c.EndVerseNumber)
                + firstChapterLimitsOffset
                - (result._offsetChapterLimits.Last().ChapterLimits.EndVerseNumber - result.End.VerseNumber);

            if (mode != RepositoryMode.Optimistic && (result.Start == null || result.End == null))
            {
                result = null;
            }

            return(result);
        }
 public void FirstVerseStringTest()
 {
     RawRange target = new RawRange();
     string expected = null;
     string actual;
     target.FirstVerseString = expected;
     target.FirstVerseSuffix = null;
     actual = target.FirstVerseString;
     Assert.AreEqual(expected, actual);
 }
        public void GetRangeTest_Optimistic_ChapterTooLarge()
        {
            RawRange input = new RawRange
            {
                FirstBook = "gen",
                FirstChapterString = "1",
                FirstVerseString = null,

                SecondBook = "gen",
                SecondChapterString = "90",
                SecondVerseString = null
            };

            RepositoryMode mode = RepositoryMode.Optimistic;
            Range expected = new Range
            {
                Start = new Verse
                {
                    Index = 1,
                    BookName = "Genesis",
                    BookNumber = 1,
                    ChapterNumber = 1,
                    VerseNumber = 1,
                    Suffix = null
                },
                End = new Verse
                {
                    Index = 1533,
                    BookName = "Genesis",
                    BookNumber = 1,
                    ChapterNumber = 50,
                    VerseNumber = 26,
                    Suffix = null

                }
            };
            Range actual;
            //actual = target.GetRange(s, mode);
            actual = Range.Create(si, input, mode);
            Assert.AreEqual(expected, actual);
        }
        public void GetRangeTest_Safe_Basic()
        {
            RawRange input = new RawRange
            {
                FirstBook = "gen",
                FirstChapterString = "1",
                FirstVerseString = "1",

                SecondBook = "gen",
                SecondChapterString = "1",
                SecondVerseString = "2"
            };

            RepositoryMode mode = RepositoryMode.Safe;
            Range expected = //null;
                new Range
            {
                Start = new Verse
                {
                    Index = 1,
                    BookName = "Genesis",
                    BookNumber = 1,
                    ChapterNumber = 1,
                    VerseNumber = 1,
                    Suffix = null
                },
                End = new Verse
                {
                    Index = 2,
                    BookName = "Genesis",
                    BookNumber = 1,
                    ChapterNumber = 1,
                    VerseNumber = 2,
                    Suffix = null

                }
            };
            Range actual;
            //actual = target.GetRange(s, mode);
            actual = Range.Create(si, input, mode);
            Assert.AreEqual(expected, actual);
        }
Exemple #5
0
        /// <summary>
        /// Converts the string representation of a verse range to its RawRange equivalent.
        /// </summary>
        /// <param name="s">A string containing a verse range to convert.</param>
        /// <param name="currentBook">The current book.</param>
        /// <param name="currentChapter">The current chapter.</param>
        /// <param name="currentVerse">The current verse.</param>
        /// <returns>
        /// A RawRange equivalent to the verse range contained in <paramref name="s"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="s"/> is null.</exception>
        internal static RawRange Parse(string input, string currentBook, string currentChapter, string currentVerse)
        {
            if (input == null)
                throw new ArgumentNullException("s is null.");

            if (string.IsNullOrWhiteSpace(currentBook))
                currentBook = null;

            if (string.IsNullOrWhiteSpace(currentChapter))
                currentChapter = null;

            if (string.IsNullOrWhiteSpace(currentVerse))
                currentVerse = null;

            RawRange result = new RawRange();

            string[] segs = input.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            string seg0 = segs[0];
            string seg1 = segs.Length == 2 ? segs[1] : string.Empty;

            RawVerse first = RawVerse.Parse(seg0);
            RawVerse second = RawVerse.Parse(seg1);

            if (!first.HasValue)
                throw new FormatException("s is not in the correct format.");
            else
            {
                // Set FirstBook
                if (first.Book == null && currentBook == null)
                    throw new ArgumentNullException("currentBook", "currentBook cannot be null when a book is not provided by s");
                else if (first.Book != null)
                {
                    result.FirstBook = first.Book;
                    result.IsFirstBookExplicit = true;
                    currentChapter = null;
                    currentVerse = null;
                }
                else
                {
                    result.FirstBook = currentBook;
                    result.IsFirstBookExplicit = false;
                }

                // Set ChapterNumber and VerseNumber
                if (first.NumberCount == 1)
                {
                    //if (string.IsNullOrEmpty(currentChapter))
                    if (string.IsNullOrEmpty(currentVerse))
                    {
                        result.FirstChapterString = first.N1;
                        result.SecondChapterString = null;
                    }
                    else
                    {
                        result.FirstChapterString = currentChapter;
                        result.FirstVerseString = first.N1;
                    }
                }
                else
                {
                    result.FirstChapterString = first.N1;
                    result.FirstVerseString = first.N2;
                }
            }

            // Parse second segment
            if (!second.HasValue)
            {
                result.SecondBook = result.FirstBook;
                result.SecondChapterString = result.FirstChapterString;
                result.SecondVerseString = result.FirstVerseString;
            }
            else
            {
                result.SecondBook = second.Book == null ? result.FirstBook : second.Book;

                if (second.NumberCount == 0)
                {
                    result.SecondChapterString = result.FirstChapterString;
                    result.SecondVerseString = result.FirstVerseString;
                }
                else if (second.NumberCount == 1)
                {
                    if (string.IsNullOrWhiteSpace(result.FirstVerseString))
                    {
                        result.SecondChapterString = second.N1;
                        result.SecondVerseString = null;
                    }
                    else
                    {
                        result.SecondChapterString = result.FirstChapterString;
                        result.SecondVerseString = second.N1;
                    }
                }
                else
                {
                    result.SecondChapterString = second.N1;
                    result.SecondVerseString = second.N2;
                }
            }

            // Check for abbreviated second chapter
            if (result.FirstVerseString == null && result.SecondVerseString == null && result.FirstBook == result.SecondBook && result.SecondChapter < result.FirstChapter && result.SecondChapter.ToString().Length < result.FirstChapter.ToString().Length)
            {
                result.SecondChapter = int.Parse( result.FirstChapter.ToString().Substring(0, result.FirstChapter.ToString().Length - result.SecondChapter.ToString().Length) + result.SecondChapter.ToString());
            }

            // Check for abbreviated second verse
            if (result.FirstBook == result.SecondBook && result.FirstChapterString != null && result.FirstChapter == result.SecondChapter && result.SecondVerse < result.FirstVerse && result.SecondVerse.ToString().Length < result.FirstVerse.ToString().Length)
            {
                result.SecondVerse = int.Parse(result.FirstVerse.ToString().Substring(0, result.FirstVerse.ToString().Length - result.SecondVerse.ToString().Length) + result.SecondVerse.ToString());
            }

            return result;
        }
Exemple #6
0
        /// <summary>
        /// Converts the string representation of a verse range to its RawRange equivalent.
        /// </summary>
        /// <param name="s">A string containing a verse range to convert.</param>
        /// <param name="currentBook">The current book.</param>
        /// <param name="currentChapter">The current chapter.</param>
        /// <param name="currentVerse">The current verse.</param>
        /// <returns>
        /// A RawRange equivalent to the verse range contained in <paramref name="s"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="s"/> is null.</exception>
        internal static RawRange Parse(string input, string currentBook, string currentChapter, string currentVerse)
        {
            if (input == null)
            {
                throw new ArgumentNullException("s is null.");
            }

            if (string.IsNullOrWhiteSpace(currentBook))
            {
                currentBook = null;
            }

            if (string.IsNullOrWhiteSpace(currentChapter))
            {
                currentChapter = null;
            }

            if (string.IsNullOrWhiteSpace(currentVerse))
            {
                currentVerse = null;
            }

            RawRange result = new RawRange();

            string[] segs = input.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            string   seg0 = segs[0];
            string   seg1 = segs.Length == 2 ? segs[1] : string.Empty;

            RawVerse first  = RawVerse.Parse(seg0);
            RawVerse second = RawVerse.Parse(seg1);

            if (!first.HasValue)
            {
                throw new FormatException("s is not in the correct format.");
            }
            else
            {
                // Set FirstBook
                if (first.Book == null && currentBook == null)
                {
                    throw new ArgumentNullException("currentBook", "currentBook cannot be null when a book is not provided by s");
                }
                else if (first.Book != null)
                {
                    result.FirstBook           = first.Book;
                    result.IsFirstBookExplicit = true;
                    currentChapter             = null;
                    currentVerse = null;
                }
                else
                {
                    result.FirstBook           = currentBook;
                    result.IsFirstBookExplicit = false;
                }

                // Set ChapterNumber and VerseNumber
                if (first.NumberCount == 1)
                {
                    //if (string.IsNullOrEmpty(currentChapter))
                    if (string.IsNullOrEmpty(currentVerse))
                    {
                        result.FirstChapterString  = first.N1;
                        result.SecondChapterString = null;
                    }
                    else
                    {
                        result.FirstChapterString = currentChapter;
                        result.FirstVerseString   = first.N1;
                    }
                }
                else
                {
                    result.FirstChapterString = first.N1;
                    result.FirstVerseString   = first.N2;
                }
            }

            // Parse second segment
            if (!second.HasValue)
            {
                result.SecondBook          = result.FirstBook;
                result.SecondChapterString = result.FirstChapterString;
                result.SecondVerseString   = result.FirstVerseString;
            }
            else
            {
                result.SecondBook = second.Book == null ? result.FirstBook : second.Book;

                if (second.NumberCount == 0)
                {
                    result.SecondChapterString = result.FirstChapterString;
                    result.SecondVerseString   = result.FirstVerseString;
                }
                else if (second.NumberCount == 1)
                {
                    if (string.IsNullOrWhiteSpace(result.FirstVerseString))
                    {
                        result.SecondChapterString = second.N1;
                        result.SecondVerseString   = null;
                    }
                    else
                    {
                        result.SecondChapterString = result.FirstChapterString;
                        result.SecondVerseString   = second.N1;
                    }
                }
                else
                {
                    result.SecondChapterString = second.N1;
                    result.SecondVerseString   = second.N2;
                }
            }

            // Check for abbreviated second chapter
            if (result.FirstVerseString == null && result.SecondVerseString == null && result.FirstBook == result.SecondBook && result.SecondChapter < result.FirstChapter && result.SecondChapter.ToString().Length < result.FirstChapter.ToString().Length)
            {
                result.SecondChapter = int.Parse(result.FirstChapter.ToString().Substring(0, result.FirstChapter.ToString().Length - result.SecondChapter.ToString().Length) + result.SecondChapter.ToString());
            }

            // Check for abbreviated second verse
            if (result.FirstBook == result.SecondBook && result.FirstChapterString != null && result.FirstChapter == result.SecondChapter && result.SecondVerse < result.FirstVerse && result.SecondVerse.ToString().Length < result.FirstVerse.ToString().Length)
            {
                result.SecondVerse = int.Parse(result.FirstVerse.ToString().Substring(0, result.FirstVerse.ToString().Length - result.SecondVerse.ToString().Length) + result.SecondVerse.ToString());
            }

            return(result);
        }