textframe 0.4.1

Library to query plain text documents by unicode offset without loading them all into memory
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
/*
TextFrame
  by Maarten van Gompel <proycon@anaproy.nl>
  Digital Infrastructure, KNAW Humanities Cluster
  licensed under the GNU General Public Licence v3
*/

use filetime::FileTime;
use hmac_sha256::Hash;
use minicbor::{Decode, Encode};
use smallvec::{smallvec, SmallVec};

use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Read, Seek, SeekFrom};
use std::ops::Bound::Included;
use std::path::{Path, PathBuf};
use std::string::FromUtf8Error;
use std::time::SystemTime;

/// Handle to a frame (index in a vector)
type FrameHandle = u32;

#[derive(Debug)]
pub enum Error {
    OutOfBoundsError { begin: isize, end: isize },
    InvalidUtf8Byte(usize),
    EmptyText,
    IOError(std::io::Error),
    Utf8Error(FromUtf8Error),
    InvalidHandle,
    IndexError,
    NotLoaded,
    NoLineIndex,
}

impl fmt::Display for Error {
    /// Formats the error message for printing
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::OutOfBoundsError { begin, end } => write!(f, "Out of Bounds ({},{})", begin, end),
            Self::InvalidUtf8Byte(byte) => write!(
                f,
                "Byte does not correspond with utf-8 character boundary ({})",
                byte
            ),
            Self::EmptyText => write!(f, "text is empty"),
            Self::IOError(e) => write!(f, "{}", e),
            Self::Utf8Error(e) => write!(f, "{}", e),
            Self::NotLoaded => write!(f, "text not loaded"),
            Self::InvalidHandle => write!(f, "Invalid handle"),
            Self::IndexError => write!(f, "Index I/O error"),
            Self::NoLineIndex => write!(f, "No line index enabled"),
        }
    }
}

impl std::error::Error for Error {}

#[derive(Debug, Clone, Decode, Encode)]
pub struct PositionData<T>
where
    T: Eq + Ord + Copy,
{
    /// Unicode point
    #[n(0)]
    charpos: T,

    /// UTF-8 byte offset
    #[n(1)]
    bytepos: T,

    /// Size in bytes of this data point and all data points until the next one in the index
    #[n(2)]
    size: u8,
}

pub trait Position {
    fn charpos(&self) -> usize;
    fn bytepos(&self) -> usize;
    fn size(&self) -> u8;
}

impl Position for PositionData<u32> {
    fn charpos(&self) -> usize {
        self.charpos as usize
    }
    fn bytepos(&self) -> usize {
        self.bytepos as usize
    }
    fn size(&self) -> u8 {
        self.size
    }
}

impl Position for PositionData<u64> {
    fn charpos(&self) -> usize {
        self.charpos as usize
    }
    fn bytepos(&self) -> usize {
        self.bytepos as usize
    }
    fn size(&self) -> u8 {
        self.size
    }
}

/// This represent a TextFile and associates a file on disk with
/// immutable excerpts of it (frames) stored in memory.
pub struct TextFile {
    /// The path to the text file
    path: PathBuf,

    /// Holds loaded excerpts of the text (aka 'frames').
    frames: Vec<TextFrame>,

    /// Maps bytes to frame handles (indirection)
    frametable: BTreeMap<usize, SmallVec<[FrameHandle; 1]>>,

    /// Maps character positions to bytes
    positionindex: PositionIndex,

    /// Modification time (unix timestamp)
    metadata: std::fs::Metadata,
}

/// A frame is a fragment of loaded text
struct TextFrame {
    beginbyte: usize,
    endbyte: usize,
    text: String,
}

#[derive(Debug, Clone, Decode, Encode)]
struct PositionIndex {
    /// Length of the text file in characters
    #[n(0)]
    charsize: usize,

    /// Size of the text file in bytes
    #[n(1)]
    bytesize: usize,

    /// Maps character positions to bytes
    #[n(2)]
    positions: Positions,

    /// SHA256 checksum of the contents
    #[n(3)]
    checksum: [u8; 32],

    /// Maps lines to bytes (if enabled)
    #[n(4)]
    lines: Lines,
}

impl Default for PositionIndex {
    fn default() -> Self {
        Self {
            charsize: 0,
            bytesize: 0,
            lines: Lines::default(),
            positions: Positions::Large(Vec::default()),
            checksum: Default::default(),
        }
    }
}

#[derive(Debug, Clone, Decode, Encode)]
/// Abstraction over differently sized position vectors
pub enum Positions {
    #[n(0)]
    Small(#[n(0)] Vec<PositionData<u16>>),

    #[n(1)]
    Large(#[n(0)] Vec<PositionData<u32>>),

    #[n(2)]
    Huge(#[n(0)] Vec<PositionData<u64>>),
}

impl Positions {
    pub fn new(filesize: usize) -> Self {
        if filesize < 65536 {
            Self::Small(Vec::new())
        } else if filesize < 4294967296 {
            Self::Large(Vec::new())
        } else {
            Self::Huge(Vec::new())
        }
    }

    pub fn len(&self) -> usize {
        match self {
            Self::Small(positions) => positions.len(),
            Self::Large(positions) => positions.len(),
            Self::Huge(positions) => positions.len(),
        }
    }

    pub fn bytepos(&self, index: usize) -> Option<usize> {
        match self {
            Self::Small(positions) => positions.get(index).map(|x| x.bytepos as usize),
            Self::Large(positions) => positions.get(index).map(|x| x.bytepos as usize),
            Self::Huge(positions) => positions.get(index).map(|x| x.bytepos as usize),
        }
    }
    pub fn charpos(&self, index: usize) -> Option<usize> {
        match self {
            Self::Small(positions) => positions.get(index).map(|x| x.charpos as usize),
            Self::Large(positions) => positions.get(index).map(|x| x.charpos as usize),
            Self::Huge(positions) => positions.get(index).map(|x| x.charpos as usize),
        }
    }
    pub fn size(&self, index: usize) -> Option<u8> {
        match self {
            Self::Small(positions) => positions.get(index).map(|x| x.size),
            Self::Large(positions) => positions.get(index).map(|x| x.size),
            Self::Huge(positions) => positions.get(index).map(|x| x.size),
        }
    }

    pub fn binary_search(&self, charpos: usize) -> Result<usize, usize> {
        match self {
            Self::Small(positions) => positions
                .binary_search_by_key(&charpos, |posdata: &PositionData<u16>| {
                    posdata.charpos as usize
                }),
            Self::Large(positions) => positions
                .binary_search_by_key(&charpos, |posdata: &PositionData<u32>| {
                    posdata.charpos as usize
                }),
            Self::Huge(positions) => positions
                .binary_search_by_key(&charpos, |posdata: &PositionData<u64>| {
                    posdata.charpos as usize
                }),
        }
    }

    pub fn binary_search_by_bytepos(&self, bytepos: usize) -> Result<usize, usize> {
        match self {
            Self::Small(positions) => positions
                .binary_search_by_key(&bytepos, |posdata: &PositionData<u16>| {
                    posdata.bytepos as usize
                }),
            Self::Large(positions) => positions
                .binary_search_by_key(&bytepos, |posdata: &PositionData<u32>| {
                    posdata.bytepos as usize
                }),
            Self::Huge(positions) => positions
                .binary_search_by_key(&bytepos, |posdata: &PositionData<u64>| {
                    posdata.bytepos as usize
                }),
        }
    }

    pub fn push(&mut self, charpos: usize, bytepos: usize, charsize: u8) {
        match self {
            Self::Small(positions) => positions.push(PositionData {
                charpos: charpos as u16,
                bytepos: bytepos as u16,
                size: charsize,
            }),
            Self::Large(positions) => positions.push(PositionData {
                charpos: charpos as u32,
                bytepos: bytepos as u32,
                size: charsize,
            }),
            Self::Huge(positions) => positions.push(PositionData {
                charpos: charpos as u64,
                bytepos: bytepos as u64,
                size: charsize,
            }),
        }
    }
}

#[derive(Debug, Clone, Decode, Encode)]
/// Abstraction over differently sized vectors
/// Lines start at 0, the underlying vector contains as many items as there are lines
pub enum Lines {
    #[n(0)]
    Small(#[n(0)] Vec<u16>),

    #[n(1)]
    Large(#[n(0)] Vec<u32>),

    #[n(2)]
    Huge(#[n(0)] Vec<u64>),
}

impl Lines {
    pub fn new(filesize: usize) -> Self {
        if filesize < 65536 {
            Self::Small(Vec::new())
        } else if filesize < 4294967296 {
            Self::Large(Vec::new())
        } else {
            Self::Huge(Vec::new())
        }
    }

    /// Returns the total number of lines
    pub fn len(&self) -> usize {
        match self {
            Self::Small(positions) => positions.len(),
            Self::Large(positions) => positions.len(),
            Self::Huge(positions) => positions.len(),
        }
    }

    /// Returns the byte position where a line begins
    pub fn get(&self, index: usize) -> Option<usize> {
        match self {
            Self::Small(positions) => positions.get(index).map(|x| *x as usize),
            Self::Large(positions) => positions.get(index).map(|x| *x as usize),
            Self::Huge(positions) => positions.get(index).map(|x| *x as usize),
        }
    }

    pub fn push(&mut self, line: usize) {
        match self {
            Self::Small(positions) => positions.push(line as u16),
            Self::Large(positions) => positions.push(line as u32),
            Self::Huge(positions) => positions.push(line as u64),
        }
    }
}

impl Default for Lines {
    fn default() -> Self {
        Self::Large(Vec::new())
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
/// Text file mode.
pub enum TextFileMode {
    /// Do not compute a line index (cheapest), set this if you're not interested in line-based queries
    NoLineIndex,

    /// Compute a line index (takes memory and cpu time), allows queries based on line ranges
    WithLineIndex,
}

impl Default for TextFileMode {
    fn default() -> Self {
        Self::WithLineIndex
    }
}

impl TextFile {
    /// Associates with an existing text file on disk, you can optionally provide a path to an indexfile to use for caching the position index. Is such a cache is not available, the text file is scanned once and the index created.

    /// * `path` - The text file
    /// * `indexpath` - The associated index file, acts as a cache if provided to prevent recomputation every time
    /// * `mode` - Additional options
    pub fn new(
        path: impl Into<PathBuf>,
        indexpath: Option<&Path>,
        mode: TextFileMode,
    ) -> Result<Self, Error> {
        let path: PathBuf = path.into();
        let metadata = std::fs::metadata(path.as_path()).map_err(|e| Error::IOError(e))?;
        let mut build_index = true;
        let mut positionindex = PositionIndex::default();
        if let Some(indexpath) = indexpath.as_ref() {
            if indexpath.exists() {
                let indexmetadata = std::fs::metadata(indexpath).map_err(|e| Error::IOError(e))?;
                if FileTime::from_last_modification_time(&indexmetadata)
                    >= FileTime::from_last_modification_time(&metadata)
                {
                    positionindex = PositionIndex::from_file(indexpath)?;
                    build_index = false;
                }
            }
        }
        if build_index {
            positionindex = PositionIndex::new(path.as_path(), metadata.len(), mode)?;
        }
        if let Some(indexpath) = indexpath.as_ref() {
            positionindex.to_file(indexpath)?;
        }
        Ok(Self {
            path,
            frames: Vec::new(),
            frametable: BTreeMap::new(),
            positionindex,
            metadata,
        })
    }

    /// Returns the filename on disk
    pub fn path(&self) -> &Path {
        self.path.as_path()
    }

    /// Returns a text fragment. The fragment must already be in memory or an Error::NotLoaded will be returned.
    /// Use `get_or_load()` instead if the fragment might not be loaded yet.
    ///
    /// * `begin` - The begin offset in unicode character points (0-indexed). If negative, it is interpreted relative to the end of the text.
    /// * `end` - The end offset in unicode character points (0-indexed, non-inclusive). If 0 or negative, it is interpreted relative to the end of the text.
    pub fn get(&self, begin: isize, end: isize) -> Result<&str, Error> {
        let (beginchar, endchar) = self.absolute_pos(begin, end)?;
        let beginbyte = self.chars_to_bytes(beginchar)?;
        let endbyte = self.chars_to_bytes(endchar)?;
        self.get_byterange_unchecked(beginbyte, endbyte)
    }

    /// Returns the text for a byte range, checks if the byte range is at valid UTF-8 character boundaries and returns an InvalidUtf8Bytes error if not
    pub fn get_byterange(&self, beginbyte: usize, endbyte: usize) -> Result<&str, Error> {
        self.frame(beginbyte, endbyte)
            .ok_or(Error::NotLoaded)
            .map(|frame| {
                //verify beginbyte and endbyte are at a char boundary, return error if not
                self.bytes_to_chars(beginbyte - frame.beginbyte)?;
                self.bytes_to_chars(endbyte - frame.beginbyte)?;
                Ok(
                    &frame.text.as_str()
                        [(beginbyte - frame.beginbyte)..(endbyte - frame.beginbyte)],
                )
            })?
    }

    /// Returns the text for a byte range, but may panic if the byte range is not at valid UTF-8 character offsets
    /// This is more performant than get_byterange() but can only be used if you're sure the bytes are valid
    pub fn get_byterange_unchecked(&self, beginbyte: usize, endbyte: usize) -> Result<&str, Error> {
        self.frame(beginbyte, endbyte)
            .ok_or(Error::NotLoaded)
            .map(|frame| {
                &frame.text.as_str()[(beginbyte - frame.beginbyte)..(endbyte - frame.beginbyte)]
            })
    }

    /// Returns a text fragment by lines. The fragment must already be in memory or an Error::NotLoaded will be returned.
    /// Use `get_lines_or_load()` instead if the fragment might not be loaded yet.
    ///
    /// * `begin` - The begin line (0-indexed!!). If negative, it is interpreted relative to the end of the text.
    /// * `end` - The end line (0-indexed!! non-inclusive). If 0 or negative, it is interpreted relative to the end of the text.
    ///
    /// This will return Error::NoLineIndex if no line index was computed.
    /// Trailing newline characters will always be returned.
    pub fn get_lines(&self, begin: isize, end: isize) -> Result<&str, Error> {
        let (beginbyte, endbyte) = self.line_range_to_byte_range(begin, end)?;
        self.get_byterange_unchecked(beginbyte, endbyte)
    }

    /// Returns a text fragment, the fragment will be loaded from disk into memory if needed.
    /// Use `get()` instead if you are already sure the fragment is loaded
    ///
    /// * `begin` - The begin offset in unicode character points (0-indexed). If negative, it is interpreted relative to the end of the text.
    /// * `end` - The end offset in unicode character points (0-indexed, non-inclusive). If 0 or negative, it is interpreted relative to the end of the text.
    pub fn get_or_load(&mut self, begin: isize, end: isize) -> Result<&str, Error> {
        let (beginchar, endchar) = self.absolute_pos(begin, end)?;
        let beginbyte = self.chars_to_bytes(beginchar)?;
        let endbyte = self.chars_to_bytes(endchar)?;
        match self.framehandle(beginbyte, endbyte) {
            Some(framehandle) => {
                let frame = self.resolve(framehandle)?;
                Ok(
                    &frame.text.as_str()
                        [(beginbyte - frame.beginbyte)..(endbyte - frame.beginbyte)],
                )
            }
            None => {
                self.load_abs(beginchar, endchar)?;
                self.get(begin, end)
            }
        }
    }

    /// Returns a text fragment, the fragment will be loaded from disk into memory if needed.
    /// Use `get_lines()` instead if you are already sure the fragment is loaded
    ///
    /// * `begin` - The begin line (0-indexed!!). If negative, it is interpreted relative to the end of the text.
    /// * `end` - The end line (0-indexed!! non-inclusive). If 0 or negative, it is interpreted relative to the end of the text.
    ///
    /// This will return Error::NoLineIndex if no line index was computed.
    /// Trailing newline characters will always be returned.
    pub fn get_or_load_lines(&mut self, begin: isize, end: isize) -> Result<&str, Error> {
        let beginbyte = self.line_to_bytes(begin)?;
        let endbyte = if end == 0 {
            self.positionindex.bytesize
        } else {
            self.line_to_bytes(end)?
        };
        if let Some(framehandle) = self.framehandle(beginbyte, endbyte) {
            let frame = self.resolve(framehandle)?;
            return Ok(
                &frame.text.as_str()[(beginbyte - frame.beginbyte)..(endbyte - frame.beginbyte)]
            );
        }
        self.load_frame(beginbyte, endbyte)?;
        if let Some(frame) = self.frame(beginbyte, endbyte) {
            Ok(&frame.text.as_str()[(beginbyte - frame.beginbyte)..(endbyte - frame.beginbyte)])
        } else {
            Err(Error::NotLoaded)
        }
    }

    /// Loads a particular text range into memory
    ///
    /// * `begin` - The begin offset in unicode character points (0-indexed). If negative, it is interpreted relative to the end of the text.
    /// * `end` - The end offset in unicode character points (0-indexed, non-inclusive). If 0 or negative, it is interpreted relative to the end of the text.
    pub fn load(&mut self, begin: isize, end: isize) -> Result<(), Error> {
        let (beginchar, endchar) = self.absolute_pos(begin, end)?;
        self.load_abs(beginchar, endchar)
    }

    /// Get a frame from a given handle
    fn resolve(&self, handle: FrameHandle) -> Result<&TextFrame, Error> {
        if let Some(frame) = self.frames.get(handle as usize) {
            Ok(frame)
        } else {
            Err(Error::InvalidHandle)
        }
    }

    /// Returns an existing frame handle that holds the given byte offset (if any is loaded)
    fn framehandle(&self, beginbyte: usize, endbyte: usize) -> Option<FrameHandle> {
        let mut iter = self.frametable.range((Included(&0), Included(&beginbyte)));
        // read the (double-ended) iterator backwards
        // and see if we find a frame that holds the bytes we want
        while let Some((_, framehandles)) = iter.next_back() {
            for handle in framehandles {
                if let Some(frame) = self.frames.get(*handle as usize) {
                    if frame.endbyte >= endbyte {
                        return Some(*handle);
                    }
                }
            }
        }
        None
    }

    /// Returns an existing frame that holds the given byte offset (if any is loaded)
    fn frame(&self, beginbyte: usize, endbyte: usize) -> Option<&TextFrame> {
        let mut iter = self.frametable.range((Included(&0), Included(&beginbyte)));
        // read the (double-ended) iterator backwards
        // and see if we find a frame that holds the bytes we want
        while let Some((_, framehandles)) = iter.next_back() {
            for handle in framehandles {
                if let Some(frame) = self.frames.get(*handle as usize) {
                    if frame.endbyte >= endbyte {
                        return Some(frame);
                    }
                }
            }
        }
        None
    }

    /// Loads a particular text range into memory, takes absolute offsets
    fn load_abs(&mut self, beginchar: usize, endchar: usize) -> Result<(), Error> {
        let beginbyte = self.chars_to_bytes(beginchar)?;
        let endbyte = self.chars_to_bytes(endchar)?;
        match self.load_frame(beginbyte, endbyte) {
            Ok(_handle) => Ok(()),
            Err(e) => Err(e),
        }
    }

    /// Loads a text frame from disk into memory
    fn load_frame(&mut self, beginbyte: usize, endbyte: usize) -> Result<FrameHandle, Error> {
        if beginbyte > endbyte {
            return Err(Error::OutOfBoundsError {
                begin: beginbyte as isize,
                end: endbyte as isize,
            });
        }
        let mut buffer: Vec<u8> = vec![0; endbyte - beginbyte];
        let mut file = File::open(self.path.as_path()).map_err(|e| Error::IOError(e))?;
        file.seek(SeekFrom::Start(beginbyte as u64))
            .map_err(|e| Error::IOError(e))?;
        file.read_exact(&mut buffer)
            .map_err(|e| Error::IOError(e))?;
        let frame = TextFrame {
            beginbyte,
            endbyte,
            text: String::from_utf8(buffer).map_err(|e| Error::Utf8Error(e))?,
        };
        self.frames.push(frame);
        let handle = (self.frames.len() - 1) as FrameHandle;
        match self.frametable.entry(beginbyte) {
            Entry::Occupied(mut entry) => entry.get_mut().push(handle),
            Entry::Vacant(entry) => {
                entry.insert(smallvec!(handle));
            }
        }
        Ok(handle)
    }

    /// Convert a character position to byte position
    pub fn chars_to_bytes(&self, charpos: usize) -> Result<usize, Error> {
        match self.positionindex.positions.binary_search(charpos) {
            Ok(index) => {
                //exact match
                Ok(self
                    .positionindex
                    .positions
                    .bytepos(index)
                    .expect("position should exist"))
            }
            Err(0) => {
                //insertion before first item should never happen **except if a file is empty**, because the first PositionData item is always the first char
                Err(Error::EmptyText)
            }
            Err(index) => {
                //miss, compute from the item just before, index (>0) will be the item just after the failure
                let charpos2 = self
                    .positionindex
                    .positions
                    .charpos(index - 1)
                    .expect("position should exist");
                let charoffset = charpos - charpos2;
                let bytepos = self
                    .positionindex
                    .positions
                    .bytepos(index - 1)
                    .expect("position should exist")
                    + (self
                        .positionindex
                        .positions
                        .size(index - 1)
                        .expect("position should exist") as usize
                        * charoffset);
                if bytepos > self.positionindex.bytesize {
                    Err(Error::OutOfBoundsError {
                        begin: bytepos as isize,
                        end: 0,
                    })
                } else {
                    Ok(bytepos)
                }
            }
        }
    }

    /// Convert a UTF-8 byte position to a character position. Returns `Error::InvalidUtf8Byte` if the byte is not at a character boundary
    pub fn bytes_to_chars(&self, bytepos: usize) -> Result<usize, Error> {
        if bytepos > self.positionindex.bytesize {
            return Err(Error::OutOfBoundsError {
                begin: bytepos as isize,
                end: 0,
            });
        }

        match self
            .positionindex
            .positions
            .binary_search_by_bytepos(bytepos)
        {
            Ok(index) => Ok(self.positionindex.positions.charpos(index).unwrap()),
            Err(0) => {
                //insertion before first item should never happen **except if a file is empty**, because the first PositionData item is always the first byte
                Err(Error::EmptyText)
            }
            Err(index) => {
                let prev_byte = self.positionindex.positions.bytepos(index - 1).unwrap();
                let prev_char = self.positionindex.positions.charpos(index - 1).unwrap();
                let size = self.positionindex.positions.size(index - 1).unwrap() as usize;
                if (bytepos - prev_byte) % size == 0 {
                    Ok(prev_char + (bytepos - prev_byte) / size)
                } else {
                    Err(Error::InvalidUtf8Byte(bytepos))
                }
            }
        }
    }

    /// Convert a line number (0-indexed!! first line is 0!) to bytes position.
    /// Relative lines numbers (negative) are supported here as well.
    /// This will return an `Error::IndexError` if no line index was computed/loaded.
    pub fn line_to_bytes(&self, line: isize) -> Result<usize, Error> {
        let num_lines = self.positionindex.lines.len();

        if num_lines == 0 {
            return Err(Error::NoLineIndex);
        }

        // Handle negative indexing
        let line = if line < 0 {
            let abs = line.unsigned_abs();
            if abs > num_lines {
                return Err(Error::OutOfBoundsError {
                    begin: line,
                    end: 0,
                });
            }
            num_lines - abs
        } else {
            line as usize
        };

        // One past the last line = end of file
        if line == num_lines {
            return Ok(self.positionindex.bytesize);
        }

        self.positionindex
            .lines
            .get(line)
            .ok_or(Error::OutOfBoundsError {
                begin: line as isize,
                end: 0,
            })
    }

    pub fn line_range_to_byte_range(
        &self,
        begin: isize,
        end: isize,
    ) -> Result<(usize, usize), Error> {
        let beginbyte = self.line_to_bytes(begin)?;
        let endbyte = if end == 0 {
            self.positionindex.bytesize
        } else {
            self.line_to_bytes(end)?
        };

        Ok((beginbyte, endbyte))
    }

    /// Converts relative character offset to an absolute one. If the offset is already absolute, it will be returned as is.
    ///
    /// * `begin` - The begin offset in unicode character points (0-indexed). If negative, it is interpreted relative to the end of the text.
    /// * `end` - The end offset in unicode character points (0-indexed, non-inclusive). If 0 or negative, it is interpreted relative to the end of the text.
    pub fn absolute_pos(&self, mut begin: isize, mut end: isize) -> Result<(usize, usize), Error> {
        if begin < 0 {
            begin += self.positionindex.charsize as isize;
        }

        if end <= 0 {
            end += self.positionindex.charsize as isize;
        }

        if begin < 0 || end < 0 || begin > end {
            return Err(Error::OutOfBoundsError { begin, end });
        }

        Ok((begin as usize, end as usize))
    }

    /// Converts relative line offset into absolute character-based one. If the offset is already absolute, it will
    /// be returned as is.
    ///
    /// * `begin` - The begin offset in line numbers. If negative, it is interpreted relative to
    ///   the end of the text
    /// * `end` - The end offset in line numbers. If zero or negative, it is interpreted relative to
    ///   the end of the text
    pub fn absolute_line_pos(
        &self,
        mut begin: isize,
        mut end: isize,
    ) -> Result<(usize, usize), Error> {
        if begin < 0 {
            begin += self.positionindex.lines.len() as isize;
        }

        if end <= 0 {
            end += self.positionindex.lines.len() as isize;
        }

        if begin < 0 || end < 0 || begin > end {
            return Err(Error::OutOfBoundsError { begin, end });
        }

        let beginbyte = self.line_to_bytes(begin)?;
        let endbyte = self.line_to_bytes(end)?;

        Ok((
            self.bytes_to_chars(beginbyte)?,
            self.bytes_to_chars(endbyte)?,
        ))
    }

    /// Returns the length of the total text file in characters, i.e. the number of character in the text
    pub fn len(&self) -> usize {
        self.positionindex.charsize
    }

    /// Returns the length of the total text file in bytes
    pub fn len_utf8(&self) -> usize {
        self.positionindex.bytesize
    }

    /// Returns the unix timestamp when the file was last modified
    pub fn mtime(&self) -> u64 {
        if let Ok(modified) = self.metadata.modified() {
            modified
                .duration_since(SystemTime::UNIX_EPOCH)
                .expect("invalid file timestamp (before unix epoch)")
                .as_secs()
        } else {
            0
        }
    }

    /// Returns the SHA-256 checksum
    pub fn checksum(&self) -> &[u8; 32] {
        &self.positionindex.checksum
    }

    /// Returns the SHA-256 checksum as a digest string
    pub fn checksum_digest(&self) -> String {
        format!("{:x}", HexDigest(self.checksum()))
    }
}

impl PositionIndex {
    /// Build a new positionindex for a given text file
    fn new(textfile: &Path, filesize: u64, options: TextFileMode) -> Result<Self, Error> {
        let mut charpos = 0;
        let mut bytepos = 0;
        let mut prevcharsize = 0;
        let textfile = File::open(textfile).map_err(|e| Error::IOError(e))?;

        // read with a line by line reader to prevent excessive read() syscalls and handle UTF-8 properly
        let mut reader = BufReader::new(textfile);
        let mut positions = Positions::new(filesize as usize);
        let mut lines = Lines::new(filesize as usize);
        let mut line = String::new();
        let mut checksum = Hash::new();
        loop {
            let read_bytes = reader.read_line(&mut line).map_err(|e| Error::IOError(e))?;
            if read_bytes == 0 {
                //EOF
                break;
            } else {
                checksum.update(&line);
                if options == TextFileMode::WithLineIndex {
                    lines.push(bytepos);
                }
                for char in line.chars() {
                    let charsize = char.len_utf8() as u8;
                    if charsize != prevcharsize {
                        positions.push(charpos, bytepos, charsize);
                    }
                    charpos += 1;
                    bytepos += charsize as usize;
                    prevcharsize = charsize;
                }
                //clear buffer for next read
                line.clear();
            }
        }
        let checksum = checksum.finalize();
        if options == TextFileMode::WithLineIndex {
            //the last 'line' marks the end position
            lines.push(bytepos);
        }
        Ok(PositionIndex {
            charsize: charpos,
            bytesize: bytepos,
            positions,
            checksum,
            lines,
        })
    }

    /// Save a positionindex to file
    fn to_file(&mut self, path: &Path) -> Result<(), Error> {
        let file = File::create(path).map_err(|e| Error::IOError(e))?;
        let writer = BufWriter::new(file);
        let writer = minicbor::encode::write::Writer::new(writer);
        minicbor::encode(self, writer).map_err(|_| Error::IndexError)?;
        Ok(())
    }

    /// Load a positionindex from file (quicker than recomputing)
    fn from_file(path: &Path) -> Result<Self, Error> {
        let file = File::open(path).map_err(|e| Error::IOError(e))?;
        let mut reader = BufReader::new(file);
        let mut buffer: Vec<u8> = Vec::new(); //will hold the entire CBOR file!!!
        reader
            .read_to_end(&mut buffer)
            .map_err(|e| Error::IOError(e))?;
        Ok(minicbor::decode(&buffer).map_err(|_| Error::IndexError)?)
    }
}

struct HexDigest<'a>(&'a [u8; 32]);

// You can choose to implement multiple traits, like Lower and UpperHex
impl fmt::LowerHex for HexDigest<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.0 {
            write!(f, "{:02x}", byte)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    // all single byte-characters, for baseline testing
    const EXAMPLE_ASCII_TEXT: &str = "
Article 1

All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

Article 2

Everyone is entitled to all the rights and freedoms set forth in this Declaration, without distinction of any kind, such as race, colour, sex, language, religion, political or other opinion, national or social origin, property, birth or other status. Furthermore, no distinction shall be made on the basis of the political, jurisdictional or international status of the country or territory to which a person belongs, whether it be independent, trust, non-self-governing or under any other limitation of sovereignty.

Article 3

Everyone has the right to life, liberty and security of person.

Article 4

No one shall be held in slavery or servitude; slavery and the slave trade shall be prohibited in all their forms.
";

    // multi-byte characters (mixed with single-byte)
    const EXAMPLE_UNICODE_TEXT: &str = "
第一条

人人生而自由,在尊严和权利上一律平等。他们赋有理性和良心,并应以兄弟关系的精神相对待。
第二条

人人有资格享有本宣言所载的一切权利和自由,不分种族、肤色、性别、语言、宗教、政治或其他见解、国籍或社会出身、财产、出生或其他身分等任何区别。

并且不得因一人所属的国家或领土的政治的、行政的或者国际的地位之不同而有所区别,无论该领土是独立领土、托管领土、非自治领土或者处于其他任何主权受限制的情况之下。
第三条

人人有权享有生命、自由和人身安全。
第四条

任何人不得使为奴隶或奴役;一切形式的奴隶制度和奴隶买卖,均应予以禁止。
";
    const EXAMPLE_3_TEXT: &str = "ПРИВЕТ";

    fn setup_ascii() -> NamedTempFile {
        let mut file = tempfile::NamedTempFile::new().expect("temp file");
        write!(file, "{}", EXAMPLE_ASCII_TEXT).expect("write must work");
        file
    }

    fn setup_unicode() -> NamedTempFile {
        let mut file = tempfile::NamedTempFile::new().expect("temp file");
        write!(file, "{}", EXAMPLE_UNICODE_TEXT).expect("write must work");
        file
    }

    fn setup_3() -> NamedTempFile {
        let mut file = tempfile::NamedTempFile::new().expect("temp file");
        write!(file, "{}", EXAMPLE_3_TEXT).expect("write must work");
        file
    }

    fn setup_empty() -> NamedTempFile {
        let file = tempfile::NamedTempFile::new().expect("temp file");
        file
    }

    #[test]
    pub fn test001_init_ascii() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert_eq!(textfile.len(), 914);
        assert_eq!(textfile.len_utf8(), 914);
    }

    #[test]
    pub fn test001_init_unicode() {
        let file = setup_unicode();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert_eq!(textfile.len(), 271);
        assert_eq!(textfile.len_utf8(), 771);
    }

    #[test]
    pub fn test002_load_ascii() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(0, 0).expect("text should exist");
        assert_eq!(text, EXAMPLE_ASCII_TEXT);
    }

    #[test]
    pub fn test002_load_ascii_explicit() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.load(0, 0).is_ok());
        let text = textfile.get(0, 0).expect("text should exist");
        assert_eq!(text, EXAMPLE_ASCII_TEXT);
    }

    #[test]
    pub fn test002_load_unicode() {
        let file = setup_unicode();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(0, 0).expect("text should exist");
        assert_eq!(text, EXAMPLE_UNICODE_TEXT);
    }

    #[test]
    pub fn test002_load_unicode_tiny() {
        let file = setup_3();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(0, 0).expect("text should exist");
        assert_eq!(text, EXAMPLE_3_TEXT);
    }

    #[test]
    pub fn test003_subpart_of_loaded_frame() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.load(0, 0).is_ok());
        let text = textfile.get(1, 10).expect("text should exist");
        assert_eq!(text, "Article 1");
    }

    #[test]
    pub fn test004_excerpt_in_frame() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(1, 10).expect("text should exist");
        assert_eq!(text, "Article 1");
    }

    #[test]
    pub fn test004_end_excerpt_in_frame() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(-7, 0).expect("text should exist");
        assert_eq!(text, "forms.\n");
    }

    #[test]
    pub fn test004_excerpt_in_frame_unicode() {
        let file = setup_unicode();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(1, 4).expect("text should exist");
        assert_eq!(text, "第一条");
    }

    #[test]
    pub fn test004_end_excerpt_in_frame_unicode() {
        let file = setup_unicode();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load(-3, 0).expect("text should exist");
        assert_eq!(text, "止。\n");
    }

    #[test]
    pub fn test005_out_of_bounds() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.load(0, 0).is_ok());
        assert!(textfile.get(1, 999).is_err());
    }

    #[test]
    pub fn test006_checksum() {
        let file = setup_ascii();
        /*
        // compute reference
        let output = std::process::Command::new("sha256sum")
            .arg(file.path())
            .output()
            .expect("Failed to execute command");
        let refsum = String::from_utf8_lossy(&output.stdout).to_owned();
        eprintln!(refsum);
        */
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert_eq!(
            textfile.checksum_digest(),
            "c6b079e561f19702d63111a3201d4850e9649b8a3ef1929d6530a780f3815215"
        );
    }

    #[test]
    pub fn test007_positionindex_size() {
        let file = setup_3();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.load(0, 0).is_ok());
        assert_eq!(textfile.positionindex.positions.len(), 1);
    }

    #[test]
    pub fn test008_line_ascii() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load_lines(1, 2).expect("text should exist"); //actual first line is empty in example, this is line 2
        assert_eq!(text, "Article 1\n");
    }

    #[test]
    pub fn test008_empty_line_ascii() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile.get_or_load_lines(0, 1).expect("text should exist"); //actual first line is empty
        assert_eq!(text, "\n");
    }

    #[test]
    pub fn test008_empty_last_line_ascii() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile
            .get_or_load_lines(-1, 0)
            .expect("text should exist"); //actual last line is empty in example without trailing newline
        assert_eq!(text, "");
    }

    #[test]
    pub fn test008_empty_last_line() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        let text = textfile
            .get_or_load_lines(-2, -1)
            .expect("text should exist");
        assert_eq!(text, "No one shall be held in slavery or servitude; slavery and the slave trade shall be prohibited in all their forms.\n");
    }

    #[test]
    pub fn test008_all_lines() {
        let file = setup_unicode();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.load(0, 0).is_ok());
        let text = textfile.get_lines(0, 0).expect("text shoulde exist");
        assert_eq!(text, EXAMPLE_UNICODE_TEXT);
    }

    #[test]
    pub fn test009_line_out_of_bounds() {
        let file = setup_ascii();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.load(0, 0).is_ok());
        assert!(textfile.get_lines(1, 999).is_err());
    }

    #[test]
    pub fn test010_bytes_to_chars_ascii() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        // ASCII: 1 byte = 1 char
        assert_eq!(textfile.bytes_to_chars(0).unwrap(), 0);
        assert_eq!(textfile.bytes_to_chars(10).unwrap(), 10);
        assert_eq!(textfile.bytes_to_chars(914).unwrap(), 914);
    }

    #[test]
    pub fn test010_bytes_to_chars_unicode() {
        let file = setup_unicode();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        // First char is newline (1 byte)
        assert_eq!(textfile.bytes_to_chars(0).unwrap(), 0);
        assert_eq!(textfile.bytes_to_chars(1).unwrap(), 1);
        // Chinese chars are 3 bytes each
        // byte 1 = char 1 (第), byte 4 = char 2 (一), byte 7 = char 3 (条)
        assert_eq!(textfile.bytes_to_chars(4).unwrap(), 2);
        assert_eq!(textfile.bytes_to_chars(7).unwrap(), 3);
        // End of file
        assert_eq!(textfile.bytes_to_chars(771).unwrap(), 271);
    }

    #[test]
    pub fn test010_bytes_to_chars_roundtrip() {
        let file = setup_unicode();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        // chars_to_bytes and bytes_to_chars should be inverses
        for charpos in [0, 1, 10, 50, 100, 200, 271] {
            let bytepos = textfile.chars_to_bytes(charpos).unwrap();
            let back = textfile.bytes_to_chars(bytepos).unwrap();
            assert_eq!(back, charpos, "roundtrip failed for charpos {}", charpos);
        }
    }

    #[test]
    pub fn test010_bytes_to_chars_out_of_bounds() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.bytes_to_chars(9999).is_err());
    }

    #[test]
    pub fn test010_get_byterange() {
        let file = setup_unicode();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        textfile.load(0, 0).unwrap();
        let text = textfile.get_byterange(1, 4).expect("text should exist");
        assert_eq!(text, "第");
    }

    #[test]
    pub fn test010_get_invalid_byterange() {
        let file = setup_unicode();
        let mut textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        textfile.load(0, 0).unwrap();
        assert!(matches!(
            textfile.get_byterange(1, 3), //this would slice inside 第 and is invalid
            Err(Error::InvalidUtf8Byte(..))
        ));
    }

    #[test]
    pub fn test011_absolute_line_pos() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        // Line 0 starts at char 0
        let (begin, end) = textfile.absolute_line_pos(0, 1).unwrap();
        assert_eq!(begin, 0);
        // first line only contains a '\n'
        assert_eq!(end, 1);
        // Line 1 starts at char 1 (after the initial newline)
        let (begin, end) = textfile.absolute_line_pos(1, 2).unwrap();
        assert_eq!(begin, 1);
        assert_eq!(end, 11);
    }

    #[test]
    pub fn test011_absolute_line_pos_negative() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        // -2, 0 should give the last two lines (the very last line is empty)
        let (begin, end) = textfile.absolute_line_pos(-2, 0).unwrap();
        assert_eq!(begin, textfile.len() - 114);
        assert_eq!(end, textfile.len());
    }

    #[test]
    pub fn test011_absolute_line_pos_full() {
        let file = setup_unicode();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        // 0, 0 should span the entire file
        let (begin, end) = textfile.absolute_line_pos(0, 0).unwrap();
        assert_eq!(begin, 0);
        assert_eq!(end, textfile.len());
    }

    #[test]
    pub fn test011_absolute_line_pos_no_line_index() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, TextFileMode::NoLineIndex).expect("file must load");
        assert!(matches!(
            textfile.absolute_line_pos(0, 1),
            Err(Error::NoLineIndex)
        ));
    }

    #[test]
    pub fn test011_absolute_line_pos_out_of_bounds() {
        let file = setup_ascii();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(textfile.absolute_line_pos(0, 9999).is_err());
        assert!(textfile.absolute_line_pos(-9999, 0).is_err());
    }

    #[test]
    pub fn test012_empty_file() {
        let file = setup_empty();
        let textfile =
            TextFile::new(file.path(), None, Default::default()).expect("file must load");
        assert!(matches!(textfile.bytes_to_chars(0), Err(Error::EmptyText)));
        assert!(matches!(textfile.chars_to_bytes(0), Err(Error::EmptyText)));
    }
}