/// <summary> /// Converts the string representation of a verse to its RawVerse equivalent. /// </summary> /// <param name="s">A string containing a verse to convert.</param> /// <returns> /// A RawVerse equivalent to the verse contained in <paramref name="s"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="s"/> is null.</exception> internal static RawVerse Parse(string input) { if (input == null) { throw new ArgumentNullException("s is null"); } RawVerse result = new RawVerse(); Regex regex = new Regex( @"(?<book>(?:[1-3]\s?)?[a-z][a-z\s]+)?(?<n1>\s*[0-9]+[a-c]?\s*)?(?::(?<n2>\s*[0-9]+[a-c]?\s*))?", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase ); Match match = regex.Match(input.Trim()); if (match == null || string.IsNullOrWhiteSpace(match.Value)) { result.Book = null; result.N1 = null; result.N2 = null; result.HasValue = false; result.NumberCount = 0; } else { result.Book = match.Groups["book"] == null || string.IsNullOrWhiteSpace(match.Groups["book"].Value) ? null : match.Groups["book"].Value.Trim(); result.N1 = match.Groups["n1"] == null || string.IsNullOrWhiteSpace(match.Groups["n1"].Value) ? null : match.Groups["n1"].Value.Trim(); result.N2 = match.Groups["n2"] == null || string.IsNullOrWhiteSpace(match.Groups["n2"].Value) ? null : match.Groups["n2"].Value.Trim(); result.NumberCount = (result.N1 == null ? 0 : 1) + (result.N2 == null ? 0 : 1); result.HasValue = true; } return(result); }
/// <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); }
/// <summary> /// Converts the string representation of a verse to its RawVerse equivalent. /// </summary> /// <param name="s">A string containing a verse to convert.</param> /// <returns> /// A RawVerse equivalent to the verse contained in <paramref name="s"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="s"/> is null.</exception> internal static RawVerse Parse(string input) { if (input == null) throw new ArgumentNullException("s is null"); RawVerse result = new RawVerse(); Regex regex = new Regex( @"(?<book>(?:[1-3]\s?)?[a-z][a-z\s]+)?(?<n1>\s*[0-9]+[a-c]?\s*)?(?::(?<n2>\s*[0-9]+[a-c]?\s*))?", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase ); Match match = regex.Match(input.Trim()); if (match == null || string.IsNullOrWhiteSpace(match.Value)) { result.Book = null; result.N1 = null; result.N2 = null; result.HasValue = false; result.NumberCount = 0; } else { result.Book = match.Groups["book"] == null || string.IsNullOrWhiteSpace(match.Groups["book"].Value) ? null : match.Groups["book"].Value.Trim(); result.N1 = match.Groups["n1"] == null || string.IsNullOrWhiteSpace(match.Groups["n1"].Value) ? null : match.Groups["n1"].Value.Trim(); result.N2 = match.Groups["n2"] == null || string.IsNullOrWhiteSpace(match.Groups["n2"].Value) ? null : match.Groups["n2"].Value.Trim(); result.NumberCount = (result.N1 == null ? 0 : 1) + (result.N2 == null ? 0 : 1); result.HasValue = true; } return result; }