Struct cdx::TextLine [−][src]
Expand description
A line of a text file, broken into fields.
Access to the lines
and parts
is allowed, but should seldom be necessary
line
includes the trailing newline, but no field contains the newline
An empty line contains one empty field
use std::io::BufRead;
let mut data = b"one\ttwo\tthree\n";
let mut dp = &data[..];
let mut line = cdx::TextLine::new();
let eof = line.read(&mut dp).unwrap();
assert_eq!(eof, false);
assert_eq!(line.strlen(), 14);
line.split(b'\t');
assert_eq!(line.len(), 3);
assert_eq!(line.get(1), b"two");
Fields
line: Vec<u8>
The whole input line, with newline
parts: Vec<FakeSlice>
the individual columns
Implementations
assign TextLine into existing TextLine, avoiding allocation if possible
Examples found in repository
src/bin/cdx/uniq_main.rs (line 144)
≺ ≻
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
fn assign(&mut self, tmp: &mut TextLine, new: &TextLine) {
match self.which {
Which::First => {}
Which::Last => tmp.assign(new),
Which::Min => {
if self.comp.comp_cols(tmp, new) == Ordering::Greater {
tmp.assign(new);
}
}
Which::Max => {
if self.comp.comp_cols(tmp, new) == Ordering::Less {
tmp.assign(new);
}
}
}
}
fn lookup(&mut self, fieldnames: &[&str]) -> Result<()> {
self.comp.lookup(fieldnames)
}
}
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 7] = [
arg! {"agg", "a", "Col,Spec", "Merge value from this column, in place."},
arg! {"agg-pre", "", "NewCol,SrcCol,Spec", "Merge value from SrcCol into new column, before other columns."},
arg! {"agg-post", "", "NewCol,SrcCol,Spec", "Merge value from SrcCol into new column, after other columns."},
arg! {"key", "k", "Spec", "How to compare adjacent lines"},
arg! {"count", "c", "ColName,Position", "Write the count of matching line."},
arg! {"which", "w", "(First,Last,Min,Max)[,LineCompare]", "Which of the matching lines should be printed."},
arg! {"agg-help", "", "", "Print help for aggregators"},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut agg = AggList::new();
let mut comp = LineCompList::new();
let mut count = Count::default();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else if x.name == "count" {
count.get_count(&x.value)?;
} else if x.name == "which" {
count.get_which(&x.value)?;
} else if x.name == "agg" {
agg.push_replace(&x.value)?;
} else if x.name == "agg-post" {
agg.push_append(&x.value)?;
} else if x.name == "agg-pre" {
agg.push_prefix(&x.value)?;
} else if x.name == "agg-help" {
AggMaker::help();
return Ok(());
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
comp.lookup(&f.names())?;
count.lookup(&f.names())?;
let mut c_write = Writer::new(f.delim());
if !agg.is_empty() {
if count.pos == CountPos::Begin {
agg.push_first_prefix(&format!("{},1,count", count.name))?;
}
if count.pos == CountPos::End {
agg.push_append(&format!("{},1,count", count.name))?;
}
agg.lookup(&f.names())?;
agg.fill(&mut c_write, f.header());
c_write.lookup(&f.names())?;
}
let mut w = get_writer("-")?;
if f.has_header() {
let mut ch = ColumnHeader::new();
if agg.is_empty() {
if count.pos == CountPos::Begin {
ch.push(&count.name)?;
}
ch.push_all(f.header())?;
if count.pos == CountPos::End {
ch.push(&count.name)?;
}
} else {
c_write.add_names(&mut ch, f.header())?;
}
w.write_all(ch.get_head(f.delim()).as_bytes())?;
}
if f.is_done() {
return Ok(());
}
f.do_split = comp.need_split();
let mut matches = 1;
if !agg.is_empty() {
agg.add(f.curr_line());
let mut tmp = f.curr_line().clone();
loop {
if f.getline()? {
c_write.write(&mut w, &tmp)?;
break;
}
if comp.equal_cols(f.prev_line(1), f.curr_line()) {
count.assign(&mut tmp, f.curr_line());
agg.add(f.curr_line());
} else {
c_write.write(&mut w, &tmp)?;
tmp.assign(f.curr_line());
agg.reset();
agg.add(f.curr_line());
}
}
} else if count.which == Which::Last {
loop {
if f.getline()? {
count.write(&mut w, matches, &f.prev_line(1).line, f.delim())?;
break;
}
if comp.equal_cols(f.prev_line(1), f.curr_line()) {
matches += 1;
} else {
count.write(&mut w, matches, &f.prev_line(1).line, f.delim())?;
matches = 1;
}
}
} else if count.which == Which::First && count.is_plain() {
f.write_curr(&mut w)?;
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
} else {
let mut tmp = f.curr_line().clone();
loop {
if f.getline()? {
count.write(&mut w, matches, &tmp.line, f.delim())?;
break;
}
if comp.equal_cols(f.prev_line(1), f.curr_line()) {
count.assign(&mut tmp, f.curr_line());
matches += 1;
} else {
count.write(&mut w, matches, &tmp.line, f.delim())?;
tmp.assign(f.curr_line());
matches = 1;
}
}
}
Ok(())
}
make a new TextLine
Examples found in repository
src/lib.rs (line 738)
≺ ≻
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
pub fn new() -> Self {
Self {
file: Infile::default(),
line: TextLine::new(),
cont: InfileContext::new(),
do_split: true,
}
}
/// make a new Reader
pub fn new_open(name: &str) -> Result<Self> {
let mut tmp = Self {
file: get_reader(name)?,
line: TextLine::new(),
cont: InfileContext::new(),
do_split: true,
};
tmp.cont.read_header(&mut *tmp.file, &mut tmp.line)?;
Ok(tmp)
}
/// get current line
pub const fn curr(&self) -> &TextLine {
&self.line
}
/// get current line
pub const fn curr_line(&self) -> &TextLine {
&self.line
}
/// get current line contents, without the trailing newline
pub fn curr_nl(&self) -> &[u8] {
&self.line.line[0..self.line.line.len() - 1]
}
/// get header
pub const fn header(&self) -> &StringLine {
&self.cont.header
}
/// get header
pub const fn header_line(&self) -> &String {
&self.cont.header.line
}
/// has header
pub const fn has_header(&self) -> bool {
self.cont.has_header
}
/// get column names
pub fn names(&self) -> Vec<&str> {
self.cont.header.vec()
}
/// open file for reading
pub fn open(&mut self, name: &str) -> Result<()> {
self.file = get_reader(name)?;
self.cont.read_header(&mut *self.file, &mut self.line)
}
/// was the file zero bytes?
pub const fn is_empty(&self) -> bool {
self.cont.is_empty
}
/// get delimiter
pub const fn delim(&self) -> u8 {
self.cont.delim
}
/// have we read all the lines?
pub const fn is_done(&self) -> bool {
self.cont.is_done
}
/// write the current text line with newline
pub fn write(&self, w: &mut impl Write) -> Result<()> {
w.write_all(&self.line.line)?;
Ok(())
}
/// write header
pub fn write_header(&self, w: &mut impl Write) -> Result<()> {
w.write_all(self.cont.header.line.as_bytes())?;
Ok(())
}
/// get the next line of text. Return true if no more data available.
pub fn getline(&mut self) -> Result<bool> {
if !self.cont.is_done {
if self.line.read(&mut *self.file)? {
self.cont.is_done = true;
} else if self.do_split {
self.line.split(self.cont.delim);
}
}
Ok(self.cont.is_done)
}
}
#[derive(Debug, Default)]
/// File reader for text file broken into lines with columns
/// previous N lines are stil available
pub struct LookbackReader {
file: Infile,
lines: Vec<TextLine>,
/// context
pub cont: InfileContext,
/// Automatically split each line into columns
pub do_split: bool,
curr: usize,
}
impl LookbackReader {
/// make a new LookbackReader
pub fn new(lookback: usize) -> Self {
let mut lines: Vec<TextLine> = Vec::new();
lines.resize(lookback + 1, TextLine::new());
Self {
file: Infile::default(),
lines,
cont: InfileContext::new(),
do_split: true,
curr: 0,
}
}
pub const fn iter(&self) -> TextLineIter<'_>ⓘNotable traits for TextLineIter<'a>impl<'a> Iterator for TextLineIter<'a> type Item = &'a [u8];
pub const fn iter(&self) -> TextLineIter<'_>ⓘNotable traits for TextLineIter<'a>impl<'a> Iterator for TextLineIter<'a> type Item = &'a [u8];
Notable traits for TextLineIter<'a>
impl<'a> Iterator for TextLineIter<'a> type Item = &'a [u8];
Iterator over columns in the line
How many column in the line
Examples found in repository
src/lib.rs (line 464)
≺ ≻
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
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.line.len() {
None
} else {
self.index += 1;
Some(&self.line[self.index - 1])
}
}
}
impl<'a> Iterator for StringLineIter<'a> {
// we will be counting with usize
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.line.parts.len() {
None
} else {
self.index += 1;
Some(&self.line[self.index - 1])
}
}
}
/// Input file. Wrapped in a type so I can 'impl Debug'
pub struct Infile(
/// The file being read
io::BufReader<Box<dyn Read>>,
);
impl Infile {
/// create a new input file
pub fn new(f: io::BufReader<Box<dyn Read>>) -> Self {
Self(f)
}
}
impl Default for Infile {
fn default() -> Self {
Self::new(io::BufReader::new(Box::new(io::empty())))
}
}
impl fmt::Debug for Infile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Infile")
}
}
impl Deref for Infile {
type Target = io::BufReader<Box<dyn Read>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Infile {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AsRef<io::BufReader<Box<dyn Read>>> for Infile {
fn as_ref(&self) -> &io::BufReader<Box<dyn Read>> {
&self.0
}
}
impl AsMut<io::BufReader<Box<dyn Read>>> for Infile {
fn as_mut(&mut self) -> &mut io::BufReader<Box<dyn Read>> {
&mut self.0
}
}
type Outfile = io::BufWriter<Box<dyn Write>>;
/// Make an Outfile from a file name
pub fn get_writer2<P: AsRef<Path>>(name: P) -> Result<Outfile> {
let name = name.as_ref().as_os_str();
let inner: Box<dyn Write> = {
if name == OsStr::new("-") {
Box::new(io::stdout())
} else if name == OsStr::new("--") {
Box::new(io::stderr())
} else {
Box::new(fs::OpenOptions::new().write(true).create(true).open(name)?)
}
};
Ok(io::BufWriter::new(inner))
}
/// Make an Outfile from a file name
pub fn get_writer(name: &str) -> Result<Outfile> {
let inner: Box<dyn Write> = {
if name == "-" {
Box::new(io::stdout())
} else if name == "--" {
Box::new(io::stderr())
} else {
Box::new(fs::OpenOptions::new().write(true).create(true).open(name)?)
}
};
Ok(io::BufWriter::new(inner))
}
// should return Cow<>
fn unescape_vec(data: &[u8]) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::with_capacity(data.len());
let mut last_was_slash = false;
for x in data {
if last_was_slash {
ret.push(match x {
b'n' => b'\n',
b't' => b'\t',
b's' => b' ',
ch => *ch,
});
last_was_slash = false;
} else if x == &b'\\' {
last_was_slash = true;
} else {
ret.push(*x);
}
}
if last_was_slash {
ret.push(b'\\');
}
ret
}
/// Make an Infile from a file name
pub fn get_reader2<P: AsRef<Path>>(name: P) -> Result<Infile> {
let name = name.as_ref().as_os_str();
let inner: Box<dyn Read> = {
if name == OsStr::new("-") {
// unsafe { Box::new(std::fs::File::from_raw_fd(1)) }
Box::new(io::stdin())
// } else if name.starts_with("s3://") {
// Box::new(open_s3_file(name)?)
} else if name.as_bytes().starts_with(b"<<") {
Box::new(std::io::Cursor::new(unescape_vec(&name.as_bytes()[2..])))
} else {
Box::new(fs::File::open(name)?)
}
};
let mut outer = io::BufReader::new(inner);
let start = outer.fill_buf()?;
if start.starts_with(&[0x1fu8, 0x8bu8, 0x08u8]) {
outer = io::BufReader::new(Box::new(MultiGzDecoder::new(outer)));
}
Ok(Infile::new(outer))
}
/// Make an Infile from a file name
pub fn get_reader(name: &str) -> Result<Infile> {
let inner: Box<dyn Read> = {
if name == "-" {
// unsafe { Box::new(std::fs::File::from_raw_fd(1)) }
Box::new(io::stdin())
// } else if name.starts_with("s3://") {
// Box::new(open_s3_file(name)?)
} else if let Some(stripped) = name.strip_prefix("<<") {
Box::new(std::io::Cursor::new(unescape_vec(stripped.as_bytes())))
} else {
Box::new(fs::File::open(name)?)
}
};
let mut outer = io::BufReader::new(inner);
let start = outer.fill_buf()?;
if start.starts_with(&[0x1fu8, 0x8bu8, 0x08u8]) {
outer = io::BufReader::new(Box::new(MultiGzDecoder::new(outer)));
}
Ok(Infile::new(outer))
}
#[derive(Debug, Default)]
/// shared context for any input file type
pub struct InfileContext {
/// CDX header, contructed if necessary
pub header: StringLine,
/// delimter
pub delim: u8,
/// have we read all the btes of the file
pub is_done: bool,
/// is the file length zero
pub is_empty: bool,
/// was there a CDX header?
pub has_header: bool,
}
/// create appropriate header from first line of file
pub fn make_header(line: &[u8]) -> StringLine {
let mut s = StringLine::new();
if is_cdx(line) {
s.line = String::from_utf8_lossy(&line[5..]).to_string();
} else {
s.line = String::new();
for x in 1..=line.split(|ch| *ch == b'\t').count() {
s.line.push_str(&format!("c{}\t", x));
}
s.line.pop();
}
s.split(b'\t');
s
}
// FIXME -- specify delimiter
// if CDX and specified and different, then strip header
/// Reader header line, if any, and first line of text
impl InfileContext {
const fn new() -> Self {
Self {
header: StringLine::new(),
delim: b'\t',
is_done: true,
is_empty: true,
has_header: false,
}
}
fn read_header(&mut self, file: &mut impl BufRead, line: &mut TextLine) -> Result<()> {
if self.header.read(file)? {
self.is_done = true;
self.is_empty = true;
return Ok(());
}
self.is_done = false;
self.is_empty = false;
if self.header.line.starts_with(" CDX") {
self.has_header = true;
self.delim = self.header.line.as_bytes()[4];
self.header.split(self.delim);
self.header.parts.remove(0);
if line.read(file)? {
self.is_done = true;
return Ok(());
}
line.split(self.delim);
} else {
self.delim = b'\t';
line.line = self.header.line.as_bytes().to_vec();
line.split(self.delim);
let mut head_str = String::new();
for i in 1..=line.len() {
head_str += "c";
head_str += &i.to_string();
head_str += "\t";
}
head_str.pop();
head_str += "\n";
let mut fake_head = head_str.as_bytes();
self.header.read(&mut fake_head)?;
self.header.split(self.delim);
}
Ok(())
}
Get one column. Return an empty column if index is too big.
Examples found in repository
More examples
src/comp.rs (line 495)
≺ ≻
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
fn comp_cols(
&mut self,
left: &TextLine,
right: &TextLine,
left_file: usize,
right_file: usize,
) -> Ordering {
self.comp.comp(
left.get(self.cols[left_file].num),
right.get(self.cols[right_file].num),
)
}
/// compare lines
fn equal_cols(
&mut self,
left: &TextLine,
right: &TextLine,
left_file: usize,
right_file: usize,
) -> bool {
self.comp.equal(
left.get(self.cols[left_file].num),
right.get(self.cols[right_file].num),
)
}
/// compare lines
fn comp_lines(
&mut self,
left: &[u8],
right: &[u8],
delim: u8,
left_file: usize,
right_file: usize,
) -> Ordering {
self.comp.comp(
get_col(left, self.cols[left_file].num, delim),
get_col(right, self.cols[right_file].num, delim),
)
}
/// compare lines
fn equal_lines(
&mut self,
left: &[u8],
right: &[u8],
delim: u8,
left_file: usize,
right_file: usize,
) -> bool {
self.comp.equal(
get_col(left, self.cols[left_file].num, delim),
get_col(right, self.cols[right_file].num, delim),
)
}
/// resolve named columns; illegal to call any of the others with a file that has not been looked up
fn lookup(&mut self, fieldnames: &[&str], file_num: usize) -> Result<()> {
while self.cols.len() < (file_num + 1) {
self.cols.push(self.cols[self.cols.len() - 1].clone());
}
self.cols[file_num].lookup(fieldnames)
}
fn fill_cache_cols(&mut self, item: &mut Item, value: &TextLine) {
self.comp.fill_cache(item, value.get(self.cols[0].num))
}
fn fill_cache_line(&mut self, item: &mut Item, value: &[u8], delim: u8) {
self.comp
.fill_cache(item, get_col(item.get(value), self.cols[0].num, delim))
}
fn set(&mut self, value: &[u8]) {
self.comp.set(value)
}
fn comp_self_cols(&mut self, right: &TextLine) -> Ordering {
self.comp.comp_self(right.get(self.cols[0].num))
}
fn equal_self_cols(&mut self, right: &TextLine) -> bool {
self.comp.equal_self(right.get(self.cols[0].num))
}
Additional examples can be found in:
Read a new line from a file, should generally be followed by split
Examples found in repository
src/lib.rs (line 696)
≺ ≻
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
fn read_header(&mut self, file: &mut impl BufRead, line: &mut TextLine) -> Result<()> {
if self.header.read(file)? {
self.is_done = true;
self.is_empty = true;
return Ok(());
}
self.is_done = false;
self.is_empty = false;
if self.header.line.starts_with(" CDX") {
self.has_header = true;
self.delim = self.header.line.as_bytes()[4];
self.header.split(self.delim);
self.header.parts.remove(0);
if line.read(file)? {
self.is_done = true;
return Ok(());
}
line.split(self.delim);
} else {
self.delim = b'\t';
line.line = self.header.line.as_bytes().to_vec();
line.split(self.delim);
let mut head_str = String::new();
for i in 1..=line.len() {
head_str += "c";
head_str += &i.to_string();
head_str += "\t";
}
head_str.pop();
head_str += "\n";
let mut fake_head = head_str.as_bytes();
self.header.read(&mut fake_head)?;
self.header.split(self.delim);
}
Ok(())
}
}
#[derive(Debug, Default)]
/// File reader for text file broken into lines with columns
pub struct Reader {
file: Infile,
/// The current line of text
pub line: TextLine,
/// context
pub cont: InfileContext,
/// automatically split each TextLine
pub do_split: bool,
}
impl Reader {
/// make a new Reader
pub fn new() -> Self {
Self {
file: Infile::default(),
line: TextLine::new(),
cont: InfileContext::new(),
do_split: true,
}
}
/// make a new Reader
pub fn new_open(name: &str) -> Result<Self> {
let mut tmp = Self {
file: get_reader(name)?,
line: TextLine::new(),
cont: InfileContext::new(),
do_split: true,
};
tmp.cont.read_header(&mut *tmp.file, &mut tmp.line)?;
Ok(tmp)
}
/// get current line
pub const fn curr(&self) -> &TextLine {
&self.line
}
/// get current line
pub const fn curr_line(&self) -> &TextLine {
&self.line
}
/// get current line contents, without the trailing newline
pub fn curr_nl(&self) -> &[u8] {
&self.line.line[0..self.line.line.len() - 1]
}
/// get header
pub const fn header(&self) -> &StringLine {
&self.cont.header
}
/// get header
pub const fn header_line(&self) -> &String {
&self.cont.header.line
}
/// has header
pub const fn has_header(&self) -> bool {
self.cont.has_header
}
/// get column names
pub fn names(&self) -> Vec<&str> {
self.cont.header.vec()
}
/// open file for reading
pub fn open(&mut self, name: &str) -> Result<()> {
self.file = get_reader(name)?;
self.cont.read_header(&mut *self.file, &mut self.line)
}
/// was the file zero bytes?
pub const fn is_empty(&self) -> bool {
self.cont.is_empty
}
/// get delimiter
pub const fn delim(&self) -> u8 {
self.cont.delim
}
/// have we read all the lines?
pub const fn is_done(&self) -> bool {
self.cont.is_done
}
/// write the current text line with newline
pub fn write(&self, w: &mut impl Write) -> Result<()> {
w.write_all(&self.line.line)?;
Ok(())
}
/// write header
pub fn write_header(&self, w: &mut impl Write) -> Result<()> {
w.write_all(self.cont.header.line.as_bytes())?;
Ok(())
}
/// get the next line of text. Return true if no more data available.
pub fn getline(&mut self) -> Result<bool> {
if !self.cont.is_done {
if self.line.read(&mut *self.file)? {
self.cont.is_done = true;
} else if self.do_split {
self.line.split(self.cont.delim);
}
}
Ok(self.cont.is_done)
}
}
#[derive(Debug, Default)]
/// File reader for text file broken into lines with columns
/// previous N lines are stil available
pub struct LookbackReader {
file: Infile,
lines: Vec<TextLine>,
/// context
pub cont: InfileContext,
/// Automatically split each line into columns
pub do_split: bool,
curr: usize,
}
impl LookbackReader {
/// make a new LookbackReader
pub fn new(lookback: usize) -> Self {
let mut lines: Vec<TextLine> = Vec::new();
lines.resize(lookback + 1, TextLine::new());
Self {
file: Infile::default(),
lines,
cont: InfileContext::new(),
do_split: true,
curr: 0,
}
}
/// get delimiter
pub const fn delim(&self) -> u8 {
self.cont.delim
}
/// get column names
pub fn names(&self) -> Vec<&str> {
self.cont.header.vec()
}
/// open file for reading
pub fn open(&mut self, name: &str) -> Result<()> {
self.file = get_reader(name)?;
self.cont.read_header(&mut *self.file, &mut self.lines[0])
}
/// The full text of the header, without the trailing newline
pub const fn header_line(&self) -> &String {
&self.cont.header.line
}
/// was file zero bytes?
pub const fn is_empty(&self) -> bool {
self.cont.is_empty
}
/// have we hit EOF?
pub const fn is_done(&self) -> bool {
self.cont.is_done
}
fn incr(&mut self) {
self.curr += 1;
if self.curr >= self.lines.len() {
self.curr = 0;
}
}
/// get next line of text
pub fn getline(&mut self) -> Result<bool> {
self.incr();
if self.lines[self.curr].read(&mut *self.file)? {
self.cont.is_done = true;
} else if self.do_split {
self.lines[self.curr].split(self.cont.delim);
}
Ok(self.cont.is_done)
}
split the line into columns hypothetically you could split on one delimiter, do some work, then split on a different delimiter.
Examples found in repository
src/agg.rs (line 331)
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
fn result(&mut self, w: &mut dyn Write) -> Result<()> {
self.data.split(self.delim);
if self.do_sort {
self.data.parts.sort_by(|a, b| self.comp.comp(a.get(&self.data.line), b.get(&self.data.line)));
}
if self.do_uniq {
self.data.parts.dedup_by(|a, b| self.comp.equal(a.get(&self.data.line), b.get(&self.data.line)));
}
if self.do_count {
write!(w, "{}", self.data.parts.len())?;
}
else {
let mut num_written = 0;
for x in &self.data.parts {
if x.len() >= self.min_len && x.len() <= self.max_len {
if num_written > 0 {
w.write_all(&[self.out_delim])?;
}
w.write_all(x.get(&self.data.line))?;
num_written += 1;
if num_written >= self.max_parts {
break;
}
}
}
}
Ok(())
}
More examples
src/lib.rs (line 700)
≺ ≻
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
fn read_header(&mut self, file: &mut impl BufRead, line: &mut TextLine) -> Result<()> {
if self.header.read(file)? {
self.is_done = true;
self.is_empty = true;
return Ok(());
}
self.is_done = false;
self.is_empty = false;
if self.header.line.starts_with(" CDX") {
self.has_header = true;
self.delim = self.header.line.as_bytes()[4];
self.header.split(self.delim);
self.header.parts.remove(0);
if line.read(file)? {
self.is_done = true;
return Ok(());
}
line.split(self.delim);
} else {
self.delim = b'\t';
line.line = self.header.line.as_bytes().to_vec();
line.split(self.delim);
let mut head_str = String::new();
for i in 1..=line.len() {
head_str += "c";
head_str += &i.to_string();
head_str += "\t";
}
head_str.pop();
head_str += "\n";
let mut fake_head = head_str.as_bytes();
self.header.read(&mut fake_head)?;
self.header.split(self.delim);
}
Ok(())
}
}
#[derive(Debug, Default)]
/// File reader for text file broken into lines with columns
pub struct Reader {
file: Infile,
/// The current line of text
pub line: TextLine,
/// context
pub cont: InfileContext,
/// automatically split each TextLine
pub do_split: bool,
}
impl Reader {
/// make a new Reader
pub fn new() -> Self {
Self {
file: Infile::default(),
line: TextLine::new(),
cont: InfileContext::new(),
do_split: true,
}
}
/// make a new Reader
pub fn new_open(name: &str) -> Result<Self> {
let mut tmp = Self {
file: get_reader(name)?,
line: TextLine::new(),
cont: InfileContext::new(),
do_split: true,
};
tmp.cont.read_header(&mut *tmp.file, &mut tmp.line)?;
Ok(tmp)
}
/// get current line
pub const fn curr(&self) -> &TextLine {
&self.line
}
/// get current line
pub const fn curr_line(&self) -> &TextLine {
&self.line
}
/// get current line contents, without the trailing newline
pub fn curr_nl(&self) -> &[u8] {
&self.line.line[0..self.line.line.len() - 1]
}
/// get header
pub const fn header(&self) -> &StringLine {
&self.cont.header
}
/// get header
pub const fn header_line(&self) -> &String {
&self.cont.header.line
}
/// has header
pub const fn has_header(&self) -> bool {
self.cont.has_header
}
/// get column names
pub fn names(&self) -> Vec<&str> {
self.cont.header.vec()
}
/// open file for reading
pub fn open(&mut self, name: &str) -> Result<()> {
self.file = get_reader(name)?;
self.cont.read_header(&mut *self.file, &mut self.line)
}
/// was the file zero bytes?
pub const fn is_empty(&self) -> bool {
self.cont.is_empty
}
/// get delimiter
pub const fn delim(&self) -> u8 {
self.cont.delim
}
/// have we read all the lines?
pub const fn is_done(&self) -> bool {
self.cont.is_done
}
/// write the current text line with newline
pub fn write(&self, w: &mut impl Write) -> Result<()> {
w.write_all(&self.line.line)?;
Ok(())
}
/// write header
pub fn write_header(&self, w: &mut impl Write) -> Result<()> {
w.write_all(self.cont.header.line.as_bytes())?;
Ok(())
}
/// get the next line of text. Return true if no more data available.
pub fn getline(&mut self) -> Result<bool> {
if !self.cont.is_done {
if self.line.read(&mut *self.file)? {
self.cont.is_done = true;
} else if self.do_split {
self.line.split(self.cont.delim);
}
}
Ok(self.cont.is_done)
}
}
#[derive(Debug, Default)]
/// File reader for text file broken into lines with columns
/// previous N lines are stil available
pub struct LookbackReader {
file: Infile,
lines: Vec<TextLine>,
/// context
pub cont: InfileContext,
/// Automatically split each line into columns
pub do_split: bool,
curr: usize,
}
impl LookbackReader {
/// make a new LookbackReader
pub fn new(lookback: usize) -> Self {
let mut lines: Vec<TextLine> = Vec::new();
lines.resize(lookback + 1, TextLine::new());
Self {
file: Infile::default(),
lines,
cont: InfileContext::new(),
do_split: true,
curr: 0,
}
}
/// get delimiter
pub const fn delim(&self) -> u8 {
self.cont.delim
}
/// get column names
pub fn names(&self) -> Vec<&str> {
self.cont.header.vec()
}
/// open file for reading
pub fn open(&mut self, name: &str) -> Result<()> {
self.file = get_reader(name)?;
self.cont.read_header(&mut *self.file, &mut self.lines[0])
}
/// The full text of the header, without the trailing newline
pub const fn header_line(&self) -> &String {
&self.cont.header.line
}
/// was file zero bytes?
pub const fn is_empty(&self) -> bool {
self.cont.is_empty
}
/// have we hit EOF?
pub const fn is_done(&self) -> bool {
self.cont.is_done
}
fn incr(&mut self) {
self.curr += 1;
if self.curr >= self.lines.len() {
self.curr = 0;
}
}
/// get next line of text
pub fn getline(&mut self) -> Result<bool> {
self.incr();
if self.lines[self.curr].read(&mut *self.file)? {
self.cont.is_done = true;
} else if self.do_split {
self.lines[self.curr].split(self.cont.delim);
}
Ok(self.cont.is_done)
}
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for TextLine
impl UnwindSafe for TextLine
Blanket Implementations
Mutably borrows from an owned value. Read more