private bool TryParseBook(string scriptureString, string language, out BibleBook book, out string chapterVerse, out VerseParseResult result) { //TODO: Check for language validity so we don't end up with a vague null exception book = null; chapterVerse = null; result = VerseParseResult.InvalidVerse; // attempt to extract the book from the start of the string foreach (var bibleBook in _bibleBookProvider.BibleBooks) { foreach (var name in bibleBook.GetAllIdentifiersForLanguage(language)) { if (scriptureString.StartsWith(name.ToLowerInvariant() + " ")) { book = bibleBook; chapterVerse = scriptureString.Substring(name.Length).Trim(); result = VerseParseResult.Success; return(true); } } } return(false); }
private bool TryParseVerse(string verseString, BibleChapter chapter, out int startVerse, out int endVerse, out VerseParseResult result) { startVerse = 0; endVerse = 0; result = VerseParseResult.InvalidSyntax; if (!string.IsNullOrWhiteSpace(verseString)) { var startVerseString = Regex.Match(verseString, "\\d+").Value; if (int.TryParse(startVerseString, out startVerse)) { if (startVerse > chapter.TotalVerses) { // start verse is greater than the number of verses in the chapter result = VerseParseResult.InvalidVerse; } else { var endVerseString = Regex.Match(verseString, "[-,]\\d+").Value.Replace("-", "").Replace(",", ""); if (!string.IsNullOrEmpty(endVerseString)) { if (int.TryParse(endVerseString, out int tempEndVerse)) { if (tempEndVerse > chapter.TotalVerses) { // the end verse is greater than the number of verses in the chapter result = VerseParseResult.InvalidVerse; } else if (tempEndVerse < 1 || (verseString.Contains(",") && tempEndVerse != (startVerse + 1))) { // end verse is 0 or there were two non-sequential verses separated by a comma result = VerseParseResult.InvalidSyntax; } else { endVerse = tempEndVerse; result = VerseParseResult.Success; } } } else { // no end verse specified, so use the start as the end endVerse = startVerse; result = VerseParseResult.Success; } } } } return(result == VerseParseResult.Success); }
private bool TryParseChapter(string chapterVerse, BibleBook book, out BibleChapter chapter, out string verseString, out VerseParseResult result) { chapter = null; verseString = null; result = VerseParseResult.InvalidSyntax; if (book.IsSingleChapter) { chapter = book.Chapters.FirstOrDefault(); verseString = chapterVerse; result = VerseParseResult.Success; } else { var chapterStr = Regex.Match(chapterVerse, @"\d+:").Value; if (!string.IsNullOrEmpty(chapterStr)) { var chapterNum = int.Parse(chapterStr.Replace(":", "").Trim()); if (chapterNum > 0 && chapterNum <= book.Chapters.Count) { chapter = book.SortedChapters[chapterNum - 1]; verseString = chapterVerse.Substring(chapterVerse.IndexOf(":") + 1).Trim(); result = VerseParseResult.Success; } else { result = VerseParseResult.InvalidVerse; } } } return(result == VerseParseResult.Success); }