bigtext 0.19.0

Free editor for huge text and log files (1-50+ GB): opens them without loading them into memory, 4 GB/s search, atomic save
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
//! Exporting a CSV table, or some of its columns, as JSON, JSON Lines, a Markdown table or an
//! Excel workbook (.xlsx), streamed record by record. The header names the fields; without one
//! the columns are called `column1`, `column2`, ….

use std::io::{self, Seek, Write};

use crate::csv::{field_value, for_each_record, Dialect, ScanEnd};
use crate::jobs::{CancelToken, Progress};
use crate::text::TextBuf;

/// An Excel sheet holds at most this many rows and columns.
pub const XLSX_MAX_ROWS: u64 = 1_048_576;
pub const XLSX_MAX_COLS: usize = 16_384;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Format {
    Json,
    JsonLines,
    Markdown,
    Xlsx,
}

impl Format {
    pub const ALL: [Format; 4] = [
        Format::Json,
        Format::JsonLines,
        Format::Markdown,
        Format::Xlsx,
    ];

    pub fn name(self) -> &'static str {
        match self {
            Format::Json => "JSON",
            Format::JsonLines => "JSON Lines",
            Format::Markdown => "Markdown table",
            Format::Xlsx => "Excel workbook (.xlsx)",
        }
    }

    pub fn ext(self) -> &'static str {
        match self {
            Format::Json => "json",
            Format::JsonLines => "jsonl",
            Format::Markdown => "md",
            Format::Xlsx => "xlsx",
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Exported {
    pub rows: u64,
    /// Rows or columns past Excel's limits were left out.
    pub truncated: bool,
}

fn json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

fn markdown_cell(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('|', "\\|")
        .replace("\r\n", "<br>")
        .replace(['\n', '\r'], "<br>")
}

fn xml_text(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\t' | '\n' | '\r' => out.push(c),
            // Not allowed in XML 1.0.
            c if (c as u32) < 0x20 || c == '\u{FFFE}' || c == '\u{FFFF}' => {}
            c => out.push(c),
        }
    }
    out
}

/// "A", "B", …, "Z", "AA", … for a 0-based column.
fn column_letters(mut col: usize) -> String {
    let mut s = Vec::new();
    loop {
        s.push(b'A' + (col % 26) as u8);
        if col < 26 {
            break;
        }
        col = col / 26 - 1;
    }
    s.reverse();
    String::from_utf8(s).unwrap_or_default()
}

/// A number Excel can store without changing how it reads: no leading zeros or plus signs.
fn as_number(s: &str) -> Option<f64> {
    let t = s.trim();
    if t != s
        || t.is_empty()
        || t.starts_with('+')
        || (t.len() > 1 && t.starts_with('0') && !t.starts_with("0."))
        || t.starts_with("-0") && t.len() > 2 && !t.starts_with("-0.")
    {
        return None;
    }
    let n: f64 = t.parse().ok()?;
    n.is_finite().then_some(n)
}

const XLSX_PARTS: [(&str, &str); 4] = [
    (
        "[Content_Types].xml",
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/></Types>"#,
    ),
    (
        "_rels/.rels",
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#,
    ),
    (
        "xl/workbook.xml",
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/></sheets></workbook>"#,
    ),
    (
        "xl/_rels/workbook.xml.rels",
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>"#,
    ),
];

/// What to export.
#[derive(Clone, Copy, Debug)]
pub struct Spec<'a> {
    pub dialect: Dialect,
    /// These columns, in this order (all when `None`).
    pub cols: Option<&'a [usize]>,
    /// The first record names the columns.
    pub header: bool,
    pub format: Format,
}

/// Writes the table as `spec` says. Returns `None` if cancelled.
pub fn export<W: Write + Seek>(
    buf: &TextBuf,
    spec: Spec,
    out: W,
    cancel: &CancelToken,
    progress: Option<&Progress>,
) -> io::Result<Option<Exported>> {
    let Spec {
        dialect: d,
        cols,
        header,
        format,
    } = spec;
    let codec = buf.codec;
    let read_err = || io::Error::other("could not read the document");
    let mut names: Option<Vec<String>> = None;
    let mut stats = Exported::default();
    let mut err: Option<io::Error> = None;
    let mut zip = None;
    let mut plain = None;
    match format {
        Format::Xlsx => {
            let mut z = zip::ZipWriter::new(out);
            let opts = zip::write::SimpleFileOptions::default()
                .compression_method(zip::CompressionMethod::Deflated);
            for (name, text) in XLSX_PARTS {
                z.start_file(name, opts).map_err(io::Error::other)?;
                z.write_all(text.as_bytes())?;
            }
            z.start_file("xl/worksheets/sheet1.xml", opts)
                .map_err(io::Error::other)?;
            z.write_all(
                br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>"#,
            )?;
            zip = Some(z);
        }
        Format::Json => {
            let mut w = io::BufWriter::new(out);
            w.write_all(b"[")?;
            plain = Some(w);
        }
        _ => plain = Some(io::BufWriter::new(out)),
    }
    let mut xlsx_row = 0u64;
    let mut values: Vec<String> = Vec::new();
    let end = for_each_record(buf, d, cancel, progress, |r| {
        // A blank line at the very end is not a row.
        if r.content.is_empty() && r.next == r.content.end && r.fields.len() == 1 && names.is_some()
        {
            return true;
        }
        values.clear();
        let pick: Vec<usize> = match cols {
            Some(c) => c.to_vec(),
            None => (0..r
                .fields
                .len()
                .max(names.as_ref().map(Vec::len).unwrap_or(0)))
                .collect(),
        };
        for &c in &pick {
            let v = match r.fields.get(c) {
                Some(f) => match buf.read(f.clone()) {
                    Some(b) => field_value(&codec, d, &b),
                    None => {
                        err = Some(read_err());
                        return false;
                    }
                },
                None => String::new(),
            };
            values.push(v);
        }
        let first = names.is_none();
        if first {
            names = Some(if header {
                values
                    .iter()
                    .enumerate()
                    .map(|(i, v)| {
                        if v.is_empty() {
                            format!("column{}", i + 1)
                        } else {
                            v.clone()
                        }
                    })
                    .collect()
            } else {
                (1..=values.len()).map(|i| format!("column{i}")).collect()
            });
        }
        let names = names.as_ref().unwrap();
        let name = |i: usize| {
            names
                .get(i)
                .cloned()
                .unwrap_or_else(|| format!("column{}", i + 1))
        };
        let r: io::Result<()> = (|| {
            match format {
                Format::Xlsx => {
                    let z = zip.as_mut().unwrap();
                    let mut emit = |row: &[String], z: &mut zip::ZipWriter<W>| -> io::Result<()> {
                        if xlsx_row >= XLSX_MAX_ROWS {
                            stats.truncated = true;
                            return Ok(());
                        }
                        xlsx_row += 1;
                        let mut s = format!("<row r=\"{xlsx_row}\">");
                        for (i, v) in row.iter().enumerate().take(XLSX_MAX_COLS) {
                            let at = format!("{}{xlsx_row}", column_letters(i));
                            match as_number(v) {
                                Some(_) => s.push_str(&format!("<c r=\"{at}\"><v>{}</v></c>", v.trim())),
                                None if v.is_empty() => {}
                                None => s.push_str(&format!("<c r=\"{at}\" t=\"inlineStr\"><is><t xml:space=\"preserve\">{}</t></is></c>", xml_text(v))),
                            }
                        }
                        stats.truncated |= row.len() > XLSX_MAX_COLS;
                        s.push_str("</row>");
                        z.write_all(s.as_bytes())
                    };
                    if first && !header {
                        emit(names, z)?;
                    }
                    if !(first && header) {
                        emit(&values, z)?;
                        stats.rows += 1;
                    } else {
                        emit(&values, z)?;
                    }
                }
                Format::Markdown => {
                    let w = plain.as_mut().unwrap();
                    if first {
                        let head: Vec<String> =
                            (0..values.len()).map(|i| markdown_cell(&name(i))).collect();
                        writeln!(w, "| {} |", head.join(" | "))?;
                        writeln!(w, "|{}", " --- |".repeat(head.len().max(1)))?;
                        if header {
                            return Ok(());
                        }
                    }
                    let cells: Vec<String> = values.iter().map(|v| markdown_cell(v)).collect();
                    writeln!(w, "| {} |", cells.join(" | "))?;
                    stats.rows += 1;
                }
                Format::Json | Format::JsonLines => {
                    if first && header {
                        return Ok(());
                    }
                    let w = plain.as_mut().unwrap();
                    let obj: Vec<String> = values
                        .iter()
                        .enumerate()
                        .map(|(i, v)| format!("{}:{}", json_string(&name(i)), json_string(v)))
                        .collect();
                    // Records without spaces, as every JSON record bigtext writes; an array
                    // has one per line, as JSON Lines → JSON writes it.
                    if format == Format::Json {
                        w.write_all(if stats.rows == 0 { b"\n" } else { b",\n" })?;
                        write!(w, "{{{}}}", obj.join(","))?;
                    } else {
                        writeln!(w, "{{{}}}", obj.join(","))?;
                    }
                    stats.rows += 1;
                }
            }
            Ok(())
        })();
        if let Err(e) = r {
            err = Some(e);
            return false;
        }
        true
    });
    if let Some(e) = err {
        return Err(e);
    }
    match end {
        ScanEnd::Cancelled => return Ok(None),
        ScanEnd::Done => {}
        _ => return Err(read_err()),
    }
    if let Some(mut z) = zip {
        z.write_all(b"</sheetData></worksheet>")?;
        z.finish().map_err(io::Error::other)?;
    }
    if let Some(mut w) = plain {
        if format == Format::Json {
            w.write_all(if stats.rows == 0 { b"]\n" } else { b"\n]\n" })?;
        }
        w.flush()?;
    }
    Ok(Some(stats))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoding::Codec;
    use crate::file::{OpenMode, Source};
    use crate::index::LineIndex;
    use std::io::Read;
    use std::sync::Arc;

    fn buf(text: &str) -> (tempfile::TempDir, TextBuf) {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("t.csv");
        std::fs::write(&path, text).unwrap();
        let src = Arc::new(Source::open(&path, OpenMode::Auto).unwrap());
        let len = src.len();
        (dir, TextBuf::new(src, LineIndex::new(len), Codec::utf8()))
    }

    fn run(text: &str, cols: Option<&[usize]>, header: bool, f: Format) -> (Vec<u8>, Exported) {
        let (_d, b) = buf(text);
        let mut out = io::Cursor::new(Vec::new());
        let spec = Spec {
            dialect: Dialect::new(b','),
            cols,
            header,
            format: f,
        };
        let st = export(&b, spec, &mut out, &CancelToken::new(), None)
            .unwrap()
            .unwrap();
        (out.into_inner(), st)
    }

    const T: &str = "name,qty,note\nAnn,007,\"a|b\"\nBob,12.5,\"x\"\"y\"\n";

    #[test]
    fn json_and_lines() {
        let (j, st) = run(T, None, true, Format::Json);
        assert_eq!(String::from_utf8(j).unwrap(), "[\n{\"name\":\"Ann\",\"qty\":\"007\",\"note\":\"a|b\"},\n{\"name\":\"Bob\",\"qty\":\"12.5\",\"note\":\"x\\\"y\"}\n]\n");
        assert_eq!(st.rows, 2);
        let (l, _) = run(T, Some(&[1, 0]), true, Format::JsonLines);
        assert_eq!(
            String::from_utf8(l).unwrap(),
            "{\"qty\":\"007\",\"name\":\"Ann\"}\n{\"qty\":\"12.5\",\"name\":\"Bob\"}\n"
        );
        let (n, st) = run("1,2\n", None, false, Format::JsonLines);
        assert_eq!(
            (String::from_utf8(n).unwrap().as_str(), st.rows),
            ("{\"column1\":\"1\",\"column2\":\"2\"}\n", 1)
        );
        let (e, _) = run("a,b\n", None, true, Format::Json);
        assert_eq!(String::from_utf8(e).unwrap(), "[]\n");
    }

    #[test]
    fn markdown() {
        let (m, st) = run(T, None, true, Format::Markdown);
        assert_eq!(String::from_utf8(m).unwrap(), "| name | qty | note |\n| --- | --- | --- |\n| Ann | 007 | a\\|b |\n| Bob | 12.5 | x\"y |\n");
        assert_eq!(st.rows, 2);
    }

    #[test]
    fn xlsx_is_a_zip_with_the_sheet() {
        let (x, st) = run(T, None, true, Format::Xlsx);
        assert_eq!(
            st,
            Exported {
                rows: 2,
                truncated: false
            }
        );
        let mut z = zip::ZipArchive::new(io::Cursor::new(x)).unwrap();
        let mut sheet = String::new();
        z.by_name("xl/worksheets/sheet1.xml")
            .unwrap()
            .read_to_string(&mut sheet)
            .unwrap();
        assert!(sheet.contains(r#"<row r="1"><c r="A1" t="inlineStr"><is><t xml:space="preserve">name</t></is></c>"#), "{sheet}");
        // "007" stays text, 12.5 is a number.
        assert!(sheet
            .contains(r#"<c r="B2" t="inlineStr"><is><t xml:space="preserve">007</t></is></c>"#));
        assert!(sheet.contains(r#"<c r="B3"><v>12.5</v></c>"#));
        assert!(sheet.contains("x&quot;y"));
        for name in [
            "[Content_Types].xml",
            "_rels/.rels",
            "xl/workbook.xml",
            "xl/_rels/workbook.xml.rels",
        ] {
            assert!(z.by_name(name).is_ok(), "{name}");
        }
        assert_eq!(column_letters(0), "A");
        assert_eq!(column_letters(25), "Z");
        assert_eq!(column_letters(26), "AA");
        assert_eq!(column_letters(16_383), "XFD");
    }
}