referenceFromUsfmIdentifier function

Reference referenceFromUsfmIdentifier(
  1. String identifier
)

Parses a USFM verse or range identifier.

Supported standard shapes are JHN 3:16, JHN 3:16-17, and JHN 3:16-4:1. The package's reversible cross-book extension, JHN-ACT 21:25-1:2, is also supported. Syntax and book identifiers are case-sensitive. Throws FormatException for malformed input, unknown book identifiers, invalid coordinates, or non-ascending ranges. A verse may have a single lowercase subdivision, as in JHN 1:5a or JHN 1:5a-5b. USFM reference syntax and verse bridge examples are documented at https://ubsicap.github.io/usfm/linking/index.html.

Implementation

Reference referenceFromUsfmIdentifier(String identifier) {
  final crossBookMatch = _usfmCrossBookReferencePattern.firstMatch(identifier);
  if (crossBookMatch != null) {
    final start = _parseVerseEndpoint(
      identifier,
      formatName: 'USFM',
      bookIdentifier: crossBookMatch[1]!,
      chapterToken: crossBookMatch[3]!,
      verseToken: crossBookMatch[4]!,
      subdivision: crossBookMatch[5],
      bookLookup: bibleBookFromUsfmIdentifier,
    );
    final end = _parseVerseEndpoint(
      identifier,
      formatName: 'USFM',
      bookIdentifier: crossBookMatch[2]!,
      chapterToken: crossBookMatch[6]!,
      verseToken: crossBookMatch[7]!,
      subdivision: crossBookMatch[8],
      bookLookup: bibleBookFromUsfmIdentifier,
    );
    return _checkedRange(start, end, identifier, formatName: 'USFM');
  }

  final match = _usfmSameBookReferencePattern.firstMatch(identifier);
  if (match == null) {
    throw FormatException(
      'Invalid USFM reference identifier; expected BOOK CHAPTER:VERSE, '
      'BOOK CHAPTER:VERSE-VERSE, BOOK CHAPTER:VERSE-CHAPTER:VERSE, or '
      'BOOK-BOOK CHAPTER:VERSE-CHAPTER:VERSE',
      identifier,
    );
  }

  final start = _parseVerseEndpoint(
    identifier,
    formatName: 'USFM',
    bookIdentifier: match[1]!,
    chapterToken: match[2]!,
    verseToken: match[3]!,
    subdivision: match[4],
    bookLookup: bibleBookFromUsfmIdentifier,
  );
  if (match[6] == null) return start;

  final end = _parseVerseEndpoint(
    identifier,
    formatName: 'USFM',
    bookIdentifier: match[1]!,
    chapterToken: match[5] ?? match[2]!,
    verseToken: match[6]!,
    subdivision: match[7],
    bookLookup: bibleBookFromUsfmIdentifier,
  );
  return _checkedRange(start, end, identifier, formatName: 'USFM');
}