-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
VerseRef.cs
1568 lines (1375 loc) · 44.7 KB
/
VerseRef.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Linq;
using System.Diagnostics;
using System.Text;
namespace SIL.Scripture
{
/// <summary>
/// Stores a reference to a specific verse in Scripture.
/// </summary>
public struct VerseRef : IComparable<VerseRef>, IComparable
{
#region Constants
#if DEBUG
public static readonly ScrVers defaultVersification = null;
#else
public static readonly ScrVers defaultVersification = ScrVers.English;
#endif
public const char verseRangeSeparator = '-';
public const char verseSequenceIndicator = ',';
public static readonly string[] verseRangeSeparators = new[] { verseRangeSeparator.ToString() };
public static readonly string[] verseSequenceIndicators = new[] { verseSequenceIndicator.ToString() };
private const int chapterDigitShifter = 1000;
private const int bookDigitShifter = chapterDigitShifter * chapterDigitShifter;
private const int bcvMaxValue = chapterDigitShifter - 1;
private const string rtlMark = "\u200f";
#endregion
#region Enumerated types
/// <summary>
/// The valid status of the VerseRef
/// </summary>
public enum ValidStatusType
{
Valid,
UnknownVersification,
OutOfRange,
VerseOutOfOrder,
VerseRepeated
}
#endregion
#region Member variables
private short bookNum;
private short chapterNum;
private short verseNum;
private string verse;
private ScrVers versification;
#endregion
#region Constructors
/// <summary>
/// Creates an empty reference with the specified versification
/// </summary>
public VerseRef(ScrVers versification)
{
bookNum = 0;
chapterNum = verseNum = -1;
verse = null;
this.versification = versification;
}
public VerseRef(string book, string chapter, string verse, ScrVers versification) : this(versification)
{
UpdateInternal(book, chapter, verse);
}
/// <summary>
/// Creates a new reference
/// <para>WARNING: This constructor creates a VerseRef that has no versification. Use with caution!</para>
/// </summary>
public VerseRef(int bbbcccvvv)
: this(bbbcccvvv / 1000000, bbbcccvvv % 1000000 / 1000, bbbcccvvv % 1000)
{
}
/// <summary>
/// Creates a new reference
/// </summary>
public VerseRef(int bbbcccvvv, ScrVers versification) : this(bbbcccvvv)
{
this.versification = versification;
}
/// <summary>
/// Creates a new reference
/// <para>WARNING: This constructor creates a VerseRef that has no versification. Use with caution!</para>
/// </summary>
public VerseRef(int bookNum, int chapterNum, int verseNum) : this(defaultVersification)
{
BookNum = bookNum;
ChapterNum = chapterNum;
VerseNum = verseNum;
}
public VerseRef(int bookNum, int chapterNum, int verseNum, ScrVers versification) :
this(bookNum, chapterNum, verseNum)
{
this.versification = versification;
}
public VerseRef(VerseRef vref)
{
bookNum = vref.bookNum;
chapterNum = vref.chapterNum;
verseNum = vref.verseNum;
verse = vref.verse;
versification = vref.versification;
}
/// <summary>
/// Creates a reference by parsing the specified string
/// <para>WARNING: This constructor creates a VerseRef that has no versification. Use with caution!</para>
/// </summary>
/// <param name="verseStr">verse string to parse (e.g. "MAT 3:11")</param>
/// <exception cref="VerseRefException"></exception>
public VerseRef(string verseStr) : this(defaultVersification)
{
Parse(verseStr);
}
/// <summary>
/// Creates a reference by parsing the specified string
/// </summary>
/// <param name="verseStr">verse string to parse (e.g. "MAT 3:11")</param>
/// <param name="versification"></param>
/// <exception cref="VerseRefException"></exception>
public VerseRef(string verseStr, ScrVers versification) : this(verseStr)
{
this.versification = versification;
}
void UpdateInternal(string bookStr, string chapterStr, string verseStr)
{
BookNum = Canon.BookIdToNumber(bookStr);
Chapter = chapterStr;
Verse = verseStr;
}
#endregion
#region Attribute Properties (access information about the reference)
/// <summary>
/// Checks to see if a VerseRef hasn't been set - all values are the default.
/// </summary>
public bool IsDefault => bookNum == 0 && chapterNum == 0 && verseNum == 0 && versification == null;
/// <summary>
/// Number of first chapter.
/// TODO bro Do we need to make this 0 for intro material?
/// </summary>
[XmlIgnore]
public int FirstChapter
{
get { return 1; }
}
[XmlIgnore]
public int LastChapter
{
get { return versification.GetLastChapter(BookNum); }
}
[XmlIgnore]
public int LastVerse
{
get { return versification.GetLastVerse(BookNum, ChapterNum); }
}
/// <summary>
/// Gets whether the verse is defined as an excluded verse in the versification.
/// </summary>
/// <remarks>Does not handle verse ranges</remarks>
[XmlIgnore]
public bool IsExcluded
{
get { return versification.IsExcluded(BBBCCCVVV); }
}
/// <summary>
/// Gets whether the verse has explicit segments defined in the versification.
/// </summary>
/// <remarks>Does not handle verse ranges</remarks>
[XmlIgnore]
public bool HasSegmentsDefined
{
get { return versification != null && versification.VerseSegments(BBBCCCVVV) != null; }
}
/// <summary>
/// Gets whether the verse contains multiple verses.
/// </summary>
[XmlIgnore]
public bool HasMultiple
{
get
{
return verse != null &&
(verse.IndexOf(verseRangeSeparator) != -1 || verse.IndexOf(verseSequenceIndicator) != -1);
}
}
/// <summary>
/// Gets or sets the book of the reference. Book is the
/// three letter abbreviation in capital letters. e.g. "MAT"
/// </summary>
[XmlIgnore]
public string Book
{
get { return Canon.BookNumberToId(BookNum, string.Empty); }
set { BookNum = Canon.BookIdToNumber(value); }
}
/// <summary>
/// Gets or sets the chapter of the reference. e.g. "3"
/// </summary>
[XmlIgnore]
public string Chapter
{
get { return IsDefault || chapterNum < 0 ? string.Empty : chapterNum.ToString(); }
set
{
short chapter;
chapterNum = short.TryParse(value, out chapter) ? chapter : (short)-1;
if (chapterNum < 0)
Trace.TraceWarning("Just failed to parse a chapter number: " + value);
}
}
/// <summary>
/// Gets or sets the verse of the reference e.g. "11"
/// </summary>
[XmlIgnore]
public string Verse
{
get => verse ?? (IsDefault || verseNum < 0 ? string.Empty : verseNum.ToString());
set => TrySetVerse(value);
}
/// <summary>
/// Value as "BBB C:V".
/// This is used for XML serialization.
/// </summary>
[XmlText]
public string Text
{
get { return ToString(); }
set
{
bookNum = 0;
chapterNum = -1;
verseNum = -1;
verse = null;
if (versification == null)
versification = defaultVersification;
try
{
Parse(value);
}
catch (VerseRefException e)
{
// Allow parse to fail during deserialization. VerseRef will just be invalid.
Console.WriteLine("Invalid deserialized reference: " + e.InvalidVerseRef);
}
}
}
/// <summary>
/// Tries to set verse and verseNum by parsing the `value` string.
/// This is used by Verse.set and TrySetVerseUnicode
/// </summary>
/// <returns><c>true</c> if the verse was set successfully </returns>
bool TrySetVerse(string value, bool romanOnly = true)
{
verse = !TryGetVerseNum(value, out verseNum, romanOnly) ? value.Replace(rtlMark, "") : null;
if (verseNum >= 0)
return true;
Trace.TraceWarning("Just failed to parse a verse number: " + value);
TryGetVerseNum(verse, out verseNum, romanOnly);
return false;
}
/// <summary>
/// Parses a verse string and gets the leading numeric portion as a number.
/// </summary>
/// <returns><c>true</c> if the entire string could be parsed as a single,
/// simple verse number (1-999); <c>false</c> if the verse string represented
/// a verse bridge, contained segment letters, or was invalid</returns>
static bool TryGetVerseNum(string verseStr, out short vNum, bool romanOnly = true)
{
if (string.IsNullOrEmpty(verseStr))
{
vNum = -1;
return true;
}
vNum = 0;
for (int i = 0; i < verseStr.Length; i++)
{
char ch = verseStr[i];
if (romanOnly ? (ch < '0' || ch > '9') : !char.IsDigit(ch))
{
if (i == 0)
vNum = -1;
return false;
}
vNum = (short)(vNum * 10 + (romanOnly ? ch - '0' : char.GetNumericValue(ch)));
if (vNum > bcvMaxValue)
{
// whoops, we got too big!
vNum = -1;
return false;
}
}
return true;
}
/// <summary>
/// Gets the reference as a comparable integer where the book,
/// chapter, and verse each occupy three digits and the verse is 0.
/// </summary>
public int BBBCCC
{
get { return GetBBBCCCVVV(bookNum, chapterNum, 0); }
}
/// <summary>
/// Gets the reference as a comparable integer where the book,
/// chapter, and verse each occupy three digits. If verse is not null
/// (i.e., this reference represents a complex reference with verse
/// segments or bridge) this cannot be used for an exact comparison.
/// </summary>
public int BBBCCCVVV
{
get { return GetBBBCCCVVV(bookNum, chapterNum, verseNum); }
}
/// <summary>
/// Returns comparable string in the format BBBCCCVVV with the segment
/// letter, if any, tacked on the end.
/// </summary>
public string BBBCCCVVVS
{
get { return BBBCCCVVV.ToString().PadLeft(9, '0') + Segment(); }
}
/// <summary>
/// Returns a long hash code for the verse reference which is guaranteed to be unique
/// provided that there are no more than 999 books, chapters and/or verses.
/// </summary>
public long LongHashCode
{
get { return ((long)BBBCCCVVV << 32) + (string.IsNullOrEmpty(verse) ? 0L : verse.GetHashCode()); }
}
/// <summary>
/// Get segments associated with this verse, if any. Otherwise, get default segments.
/// </summary>
/// <param name="defaultSegments">verse segments defined for the current language</param>
/// <returns></returns>
public string[] GetSegments(string[] defaultSegments)
{
if (versification == null)
return defaultSegments;
string[] segsForThisVerse = versification.VerseSegments(BBBCCCVVV);
return segsForThisVerse ?? defaultSegments;
}
/// <summary>
/// Get the segment from the verse.
/// </summary>
/// <param name="validSegments">valid segments defined for the language or null if not defined or available</param>
/// <returns>validated segment (according to default segments or versification, if available for verse);
/// empty string if no segment or if segment did not validate</returns>
public string Segment(string[] validSegments)
{
string seg = Segment();
if (seg.Length == 0)
return "";
validSegments = GetSegments(validSegments);
if (validSegments != null && validSegments.Length > 0)
return validSegments.Contains(seg) ? seg : "";
return seg;
}
/// <summary>
/// Get the segment number for the verse.
/// </summary>
/// <param name="validSegments">valid segments defined for the language or null if not defined or available</param>
/// <returns>returns the index of the segment (according to default segments or versification, if available for verse) or
/// -1 if no segment or an unknown segment</returns>
public int SegmentNumber(string[] validSegments)
{
string seg = Segment();
if (seg.Length == 0)
return -1;
validSegments = GetSegments(validSegments);
if (validSegments != null)
return Array.IndexOf(validSegments, seg);
return -1;
}
/// <summary>
/// Get segment from verse string.
/// </summary>
/// <returns>non-validated segment, or empty string if no segment found</returns>
public string Segment()
{
if (string.IsNullOrEmpty(verse) || !char.IsDigit(verse[0]))
return "";
bool foundSegStart = false;
StringBuilder strBldr = new StringBuilder();
for (int i = 0; i < verse.Length; i++)
{
char c = verse[i];
if (c == verseRangeSeparator || c == verseSequenceIndicator)
break;
if (!char.IsDigit(c))
{
foundSegStart = true;
strBldr.Append(c);
}
else if (foundSegStart)
break;
}
return strBldr.ToString();
}
/// <summary>
/// Simplifies this verse ref so that it has no bridging of verses or
/// verse segments like "1a".
/// </summary>
public void Simplify()
{
verse = null;
}
/// <summary>
/// Returns verse ref with no bridging, but maintaining segments like "1a".
/// </summary>
/// <returns></returns>
public VerseRef UnBridge()
{
return AllVerses().FirstOrDefault();
}
/// <summary>
/// String representing the versification (should ONLY be used for serialization/deserialization)
/// </summary>
/// <remarks>This is for backwards compatibility when ScrVers was an enumeration.</remarks>
[XmlAttribute("Versification")]
public string VersificationStr
{
get { return versification?.Name; }
set { versification = value != null ? new ScrVers(value) : null; }
}
/// <summary>
/// Gets or sets the versification of the reference.
/// Setting this value does not attempt to convert between
/// versifications. To do so, use the ChangeVersification function
/// </summary>
[XmlIgnore]
public ScrVers Versification
{
get { return versification; }
set { versification = value; }
}
/// <summary>
/// Determines if the reference is valid
/// </summary>
public bool Valid
{
get { return ValidStatus == ValidStatusType.Valid; }
}
/// <summary>
/// Get the valid status for this reference.
/// </summary>
public ValidStatusType ValidStatus
{
get { return ValidateVerse(verseRangeSeparators, verseSequenceIndicators); }
}
/// <summary>
/// Gets whether a single verse reference is valid.
/// </summary>
ValidStatusType InternalValid
{
get
{
// Unknown versification is always invalid
if (versification == null)
return ValidStatusType.UnknownVersification;
// If invalid book, reference is invalid
if (bookNum <= 0 || bookNum > Canon.LastBook)
return ValidStatusType.OutOfRange;
// If non-biblical book, any chapter/verse is valid
if (!Canon.IsCanonical(bookNum))
return ValidStatusType.Valid;
if (bookNum > versification.GetLastBook() || chapterNum <= 0 ||
chapterNum > versification.GetLastChapter(bookNum) || VerseNum < 0 ||
VerseNum > versification.GetLastVerse(bookNum, chapterNum))
{
return ValidStatusType.OutOfRange;
}
return versification.IsExcluded(BBBCCCVVV) ? ValidStatusType.OutOfRange : ValidStatusType.Valid;
}
}
/// <summary>
/// Attempts to change the versification to the specified versification
/// </summary>
/// <param name="newVersification">new versification to use</param>
public void ChangeVersification(ScrVers newVersification)
{
if (!HasMultiple)
newVersification.ChangeVersification(ref this);
else
{
VerseRef result;
newVersification.ChangeVersificationWithRanges(this, out result);
CopyFrom(result);
}
}
/// <summary>
/// Change the versification of an entry with Verse like 1-3 or 1,3a.
/// Can't really work in the most general case because the verse parts could become separate chapters.
/// </summary>
public bool ChangeVersificationWithRanges(ScrVers newVersification)
{
VerseRef temp;
bool result = newVersification.ChangeVersificationWithRanges(this, out temp);
CopyFrom(temp);
return result;
}
#endregion
#region Convert, Copy
/// <summary>
/// Makes a clone of the reference
/// </summary>
/// <returns></returns>
public VerseRef Clone()
{
// Leaving this for now to reduce code changes. Isn't really nessary when VerseRef is a struct since a = b is a copy.
return new VerseRef(this);
}
/// <summary>
/// Copy contents from vref
/// </summary>
/// <param name="vref">VerseRef to be copied from</param>
public void CopyFrom(VerseRef vref)
{
bookNum = vref.bookNum;
chapterNum = vref.chapterNum;
verseNum = vref.verseNum;
verse = vref.verse;
versification = vref.versification;
}
/// <summary>
/// Copies the verse information to this object from vref.
/// </summary>
/// <param name="vref">VerseRef to be copied from</param>
public void CopyVerseFrom(VerseRef vref)
{
verseNum = vref.verseNum;
verse = vref.verse;
}
/// <summary>
/// Parses the reference in the specified string.
/// Optionally versification can follow reference as in GEN 3:11/4
/// Throw an exception if
/// - invalid book name
/// - chapter number is missing or not a number
/// - verse number is missing or does not start with a number
/// - versifcation is invalid
/// </summary>
/// <param name="verseStr">string to parse e.g. "MAT 3:11"</param>
/// <exception cref="VerseRefException"></exception>
public void Parse(string verseStr)
{
verseStr = verseStr.Replace(rtlMark, "");
if (verseStr.IndexOf('/') >= 0)
{
string[] parts = verseStr.Split('/');
verseStr = parts[0];
if (parts.Length > 1)
{
try
{
int scrVerseCode = int.Parse(parts[1].Trim());
versification = new ScrVers((ScrVersType)scrVerseCode);
}
catch (Exception)
{
throw new VerseRefException("Invalid reference : " + verseStr) { InvalidVerseRef = verseStr };
}
}
}
string[] b_cv = verseStr.Trim().Split(' ');
if (b_cv.Length != 2)
throw new VerseRefException("Invalid reference : " + verseStr) { InvalidVerseRef = verseStr };
string[] c_v = b_cv[1].Split(':');
int cnum;
if (c_v.Length != 2 || Canon.BookIdToNumber(b_cv[0]) == 0 || !int.TryParse(c_v[0], out cnum) ||
cnum < 0 || !IsVerseParseable(c_v[1]))
{
throw new VerseRefException("Invalid reference : " + verseStr) { InvalidVerseRef = verseStr };
}
UpdateInternal(b_cv[0], c_v[0], c_v[1]);
}
public override string ToString()
{
StringBuilder toStringBuilder = new StringBuilder(20); // length of 20 should get 99.99% of references
string book = Book;
if (book.Length == 0)
return ""; // Handle empty book by just returning empty string - works around a bug in Mono 3.
toStringBuilder.Append(book).Append(' ').Append(Chapter).Append(':').Append(Verse);
return toStringBuilder.ToString();
}
/// <summary>
/// .e.g GEN 3:11/4. Parse understands this format.
/// </summary>
public string ToStringWithVersification()
{
return ToString() + "/" + (int)Versification.Type;
}
public override int GetHashCode()
{
// ENHANCE: Currently the hashcode can change when the values of the VerseRef change.
// This will create problems if/when VerseRef is ever used for a key in a hashtable.
// The best thing we could do would be to make VerseRef immutable, but that seems unlikely
// to go well.
return verse != null ? BBBCCCVVV ^ verse.GetHashCode() : BBBCCCVVV;
}
#endregion
#region Navigation: Book ( e.g. Next/Previous)
/// <summary>
/// Get or set Book based on book number. We still do quite a bit with book numbers
/// so lets leave this public.
/// </summary>
/// <exception cref="VerseRefException">If BookNum is set to an invalid value</exception>
[XmlIgnore()]
public int BookNum
{
get { return bookNum; }
set
{
if (value <= 0 || value > Canon.LastBook)
throw new VerseRefException("BookNum must be greater than zero and less than or equal to last book");
bookNum = (short)value;
}
}
/// <summary>
/// Gets chapter number. -1 if not valid
/// </summary>
/// <exception cref="VerseRefException">If ChapterNum is negative</exception>
[XmlIgnore()]
public int ChapterNum
{
get { return chapterNum; }
set
{
if (value < 0)
throw new VerseRefException("ChapterNum can not be negative");
chapterNum = (short)value;
}
}
/// <summary>
/// Gets verse start number. -1 if not valid
/// </summary>
/// <exception cref="VerseRefException">If VerseNum is negative</exception>
[XmlIgnore()]
public int VerseNum
{
get { return verseNum; }
set
{
if (value < 0)
throw new VerseRefException("VerseNum can not be negative");
verseNum = (short)value;
verse = null;
}
}
// ---------- BOOK NEXT AND PREVIOUS ----------
// NOTES ABOUT ALL NAVIGATION FUNCTIONS:
//All the navigiation funcitons have two forms. One takes a BookSet
//and constrains Next/Previous within that set. The other takes no
//no arguments and constrains Next/Previous only to the entire canon
//as Canon presents it to BookSet.
/// <summary>
/// Tries to move to the next book among a set of books present.
/// </summary>
/// <param name="present">Set of books present or selected.</param>
/// <returns>true if successful</returns>
public bool NextBook(BookSet present)
{
int curBook = bookNum;
int newBook = present.NextSelected(curBook);
if (newBook == curBook)
return false;
bookNum = (short)newBook;
chapterNum = 1;
VerseNum = 0; // Use property to reset verse string
return true;
}
/// <summary>
/// Tries to move to the next book in the entire canon superset.
/// </summary>
/// <returns>True if successful.</returns>
public bool NextBook()
{
return NextBook(BookSet.AllBooks);
}
public bool PreviousBook(BookSet present)
{
int curBook = bookNum;
int newBook = present.PreviousSelected(curBook);
if (newBook == curBook)
return false; //no previous selected book
bookNum = (short)newBook;
chapterNum = 1;
VerseNum = 1; // Use property to reset verse string
return true;
}
public bool PreviousBook()
{
return PreviousBook(BookSet.AllBooks);
}
// ---------- CHAPTER NEXT AND PREVIOUS ----------
public bool NextChapter(BookSet present, bool skipExcluded = false)
{
// If current book doesn't exist, try jump to next.
if (!present.IsSelected(bookNum))
return NextBook(present);
int newPosition = chapterNum + 1;
if (newPosition > LastChapter)
return NextBook(present);
if (skipExcluded)
{
var nextRef = versification.FirstIncludedVerse(bookNum, newPosition);
if (nextRef != null)
CopyFrom(nextRef.Value);
else
return NextBook(present);
}
else
{
chapterNum = (short)newPosition;
VerseNum = 1;
}
return true;
}
public bool NextChapter()
{
return NextChapter(BookSet.AllBooks);
}
bool PreviousBookLastChapter(BookSet present)
{
bool result = PreviousBook(present);
if (result)
chapterNum = (short)(LastChapter != Scripture.Versification.NonCanonicalLastChapterOrVerse ? LastChapter : 1);
return result;
}
public bool PreviousChapter(BookSet present)
{
// current ref doesn't exist? try find an existing one prior
if (!present.IsSelected(bookNum))
return PreviousBookLastChapter(present);
int newPosition = chapterNum - 1;
if (newPosition < FirstChapter)
return PreviousBookLastChapter(present);
VerseNum = 1; // Use property to reset verse string
chapterNum = (short)newPosition;
return true;
}
public bool PreviousChapter()
{
return PreviousChapter(BookSet.AllBooks);
}
// ---------- VERSE NEXT AND PREVIOUS ----------
/// <summary>
/// Moves to the next verse (or verse segment, if available in the current versification).
/// </summary>
/// <returns>true if successful, false if at end of scripture</returns>
public bool NextVerse(BookSet present)
{
return NextVerse(present, false);
}
/// <summary>
/// Moves to the next verse (or verse segment, if available in the current versification).
/// </summary>
/// <returns>true if successful, false if at end of scripture</returns>
public bool NextVerse(BookSet present, bool skipExcluded)
{
// avoid incrementing through a blank book
if (!present.IsSelected(bookNum))
return NextBook(present);
string[] verseSegments = Versification.VerseSegments(BBBCCCVVV);
if (verseSegments != null)
{
int segIndex = FindSegment(verseSegments);
Debug.Assert(segIndex != -1, "No valid segment found.");
if (segIndex != -1)
{
if (segIndex < verseSegments.Length - 1 &&
!verseSegments[segIndex].Equals(verseSegments[segIndex + 1]))
{
// There is another segment and it isn't identical.
// (Navigation for an identical segment gets stalled so we just want to go to the next verse.)
Verse = verseNum + verseSegments[segIndex + 1];
return true;
}
}
}
do
{
if (verseNum >= versification.GetLastVerse(bookNum, chapterNum))
{
if (NextChapter(present, skipExcluded))
{
SetVerseWithSegmentInfo(true);
return true;
}
return false;
}
VerseNum++; // Use property to reset verse string
SetVerseWithSegmentInfo(true);
} while (skipExcluded && versification.IsExcluded(BBBCCCVVV)); // search for next included verse if needed
return true;
}
public bool NextVerse()
{
return NextVerse(BookSet.AllBooks);
}
bool PreviousChapterLastVerse(BookSet present)
{
bool result;
// This current logic prevents simple nav to chapter 0:
// Book doesn't exist or we just asked for the chapter before #1
if (!present.IsSelected(bookNum) || chapterNum <= 1)
result = PreviousBookLastChapter(present);
else
{
result = true;
chapterNum--;
}
if (result)
VerseNum = LastVerse; // Use property to reset verse string
return result;
}
/// <summary>
/// Moves to the previous verse (or verse segment, if available in the current versification).
/// </summary>
/// <returns>true if successful, false if at beginning of scripture</returns>
public bool PreviousVerse(BookSet present)
{
// avoid moving through nonexistent books.
if (!present.IsSelected(bookNum))
{
if (PreviousChapterLastVerse(present))
{
SetVerseWithSegmentInfo(false);
return true;
}
return false;
}
string[] verseSegments = Versification.VerseSegments(GetBBBCCCVVV(bookNum, chapterNum, verseNum));
if (verseSegments != null)
{
int segIndex = FindSegment(verseSegments);
Debug.Assert(segIndex != -1, "No valid segment found");
if (segIndex != -1)
{
if (segIndex > 0)
{
Verse = verseNum + verseSegments[segIndex - 1];
return true;
}
}
}
// No segment information available for current verse
if (verseNum == 1 && chapterNum == 1)
VerseNum = 0; // Use property to reset verse string
else if (verseNum <= 1)
{
if (PreviousChapterLastVerse(present))
{
SetVerseWithSegmentInfo(false);
return true;
}
return false;
}
else
{
VerseNum--; // Use property to reset verse string
SetVerseWithSegmentInfo(false);
}
return true;
}
public bool PreviousVerse()
{
return PreviousVerse(BookSet.AllBooks);
}
/// <summary>
/// Set the contents of the verse with any applicable segment information after changing the reference
/// given whether the verse reference is moving forward or backward.
/// </summary>
void SetVerseWithSegmentInfo(bool movingForward)
{
string[] verseSegments = Versification.VerseSegments(GetBBBCCCVVV(bookNum, chapterNum, verseNum));
if (verseSegments != null)
{
string segment = (movingForward) ? verseSegments[0] : verseSegments[verseSegments.Length - 1];
Verse = verseNum + segment;
}
else
verse = null;
}
/// <summary>
/// Gets the index of the current segment for this verse given a list of segments for the verse.
/// </summary>
int FindSegment(string[] segments)
{