cdx 0.1.23

Library and application for text file manipulation and command line data mining, a little like the gnu textutils
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
//! Transforms that turn values into other values

use crate::prelude::*;
use crate::util::FakeSlice;
use base64::Engine;
use base64::engine::general_purpose;
use std::sync::Mutex;

/// Transform a value to another value, in the context of a (typically unused) `TextLine`
pub trait Trans {
    /// Transform `src` into a new value, and append to `dst`
    fn trans(&mut self, src: &[u8], cont: &TextLine, dst: &mut Vec<u8>) -> Result<()>;
    /// Resolve any named columns, typically needed only if `TextLine` is used in `trans`
    fn lookup(&mut self, _field_names: &[&str]) -> Result<()> {
        Ok(())
    }
}

#[derive(Default, Clone, Debug)]
struct SelectTrans {
    cols: ColumnSet,
    in_mode: TextFileMode,
    out_mode: TextFileMode,
    text: TextLine,
}

impl SelectTrans {
    #[must_use]
    const fn mode(delim: char) -> TextFileMode {
        TextFileMode {
            head_mode: crate::util::HeadMode::No,
            col_mode: crate::util::QuoteMode::Plain,
            delim: crate::util::auto_escape(delim),
            line_break: b'\n',
            repl: b' ',
        }
    }

    fn new(spec: &str) -> Result<Self> {
        let mut chars = spec.chars();
        match (chars.next(), chars.next()) {
            (Some(in_delim), Some(out_delim)) => {
                let in_mode = Self::mode(in_delim);
                let out_mode = Self::mode(out_delim);
                let cols = ColumnSet::from_spec(chars.as_str())?;
                Ok(Self { cols, in_mode, out_mode, text: TextLine::default() })
            }
            _ => err!("Invalid select spec : {}", spec),
        }
    }
}

impl Trans for SelectTrans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        self.text.line.clear();
        self.text.line.extend_from_slice(src);
        self.text.split(&self.in_mode);
        self.cols.write3(dst, &self.text, &self.out_mode)
    }
    fn lookup(&mut self, _field_names: &[&str]) -> Result<()> {
        self.cols.lookup(&[])?;
        Ok(())
    }
}
// get ranges of bytes
struct BytesTrans {
    v: Vec<FakeSlice>,
}
impl BytesTrans {
    fn new(spec: &str) -> Result<Self> {
        let mut v = Vec::new();
        for x in spec.split(',') {
            v.push(FakeSlice::new(x)?);
        }
        Ok(Self { v })
    }
}
impl Trans for BytesTrans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        for x in &self.v {
            dst.extend(x.get_safe(src));
        }
        Ok(())
    }
}

// transform to and from base64
struct Base64Trans {
    encode: bool,
}
impl Base64Trans {
    const fn new(encode: bool) -> Self {
        Self { encode }
    }
}
impl Trans for Base64Trans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        if self.encode {
            dst.resize(src.len() * 4 / 3 + 4, 0);
            let bytes_written = general_purpose::STANDARD.encode_slice(src, dst)?;
            dst.truncate(bytes_written);
        } else {
            general_purpose::STANDARD.decode_vec(src, dst)?;
        }
        Ok(())
    }
}

// make lowercase, ascii
struct LowerTrans {}
impl Trans for LowerTrans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        for x in src {
            dst.push(x.to_ascii_lowercase());
        }
        Ok(())
    }
}

#[derive(Default)]
// make lowercase, utf8
struct LowerUtfTrans {
    tmp: String,
}
impl Trans for LowerUtfTrans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        String::from_utf8_lossy(src).as_ref().assign_lower(&mut self.tmp);
        dst.extend(self.tmp.as_bytes());
        Ok(())
    }
}

#[derive(Default)]
// make uppercase, utf8
struct UpperUtfTrans {
    tmp: String,
}
impl Trans for UpperUtfTrans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        String::from_utf8_lossy(src).as_ref().assign_upper(&mut self.tmp);
        dst.extend(self.tmp.as_bytes());
        Ok(())
    }
}

// make uppercase, ascii
struct UpperTrans {}
impl Trans for UpperTrans {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        for x in src {
            dst.push(x.to_ascii_uppercase());
        }
        Ok(())
    }
}

// normalize space, ascii
struct NormSpace {}
impl Trans for NormSpace {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        let mut need_space = false;
        for x in src {
            if *x <= b' ' {
                need_space = true;
            } else {
                if need_space && !dst.is_empty() {
                    dst.push(b' ');
                    need_space = false;
                }
                dst.push(*x);
            }
        }
        Ok(())
    }
}

// normalize space, ascii
#[derive(Default)]
struct NormSpaceUtf8 {
    tmp: String,
}
impl Trans for NormSpaceUtf8 {
    fn trans(&mut self, src: &[u8], _cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        let mut need_space = false;
        self.tmp.clear();
        for x in String::from_utf8_lossy(src).chars() {
            if x.is_whitespace() || x == std::char::REPLACEMENT_CHARACTER {
                need_space = true;
            } else {
                if need_space && !self.tmp.is_empty() {
                    self.tmp.push(' ');
                    need_space = false;
                }
                self.tmp.push(x);
            }
        }
        dst.extend(self.tmp.bytes());
        Ok(())
    }
}

#[derive(Debug, Default, Copy, Clone)]
/// Transform Modifiers
pub struct TransSettings {
    utf8: bool,
}

/// A Trans with some context
pub struct Transform {
    /// original spec
    pub spec: String,
    /// utf8 (vs ascii) version if available
    pub conf: TransSettings,
    /// The Trans
    pub trans: Box<dyn Trans>,
}
impl fmt::Debug for Transform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Transform {}", self.spec)
    }
}
impl Clone for Transform {
    fn clone(&self) -> Self {
        TransMaker::make(&self.spec).unwrap()
    }
}

impl Transform {
    fn trans(&mut self, src: &[u8], cont: &TextLine, dst: &mut Vec<u8>) -> Result<()> {
        self.trans.trans(src, cont, dst)
    }
    fn lookup(&mut self, field_names: &[&str]) -> Result<()> {
        self.trans.lookup(field_names)
    }
}

#[derive(Default, Clone, Debug)]
/// a chain of transforms
pub struct TransList {
    v: Vec<Transform>,
    tmp1: Vec<u8>,
    tmp2: Vec<u8>,
}

impl TransList {
    /// new
    pub fn new(spec: &str) -> Result<Self> {
        let mut s = Self::default();
        for x in spec.split('+') {
            s.push(x)?;
        }
        Ok(s)
    }
    /// add new trans
    pub fn push(&mut self, spec: &str) -> Result<()> {
        self.v.push(TransMaker::make(spec)?);
        Ok(())
    }
    /// Transform `src` into a new value, and append to `dst`
    pub fn trans<'a>(&'a mut self, src: &'a [u8], cont: &'a TextLine) -> Result<&'a [u8]> {
        if self.v.is_empty() {
            return Ok(src);
        }
        let mut use_1 = true;
        for (i, x) in self.v.iter_mut().enumerate() {
            if i == 0 {
                self.tmp1.clear();
                x.trans(src, cont, &mut self.tmp1)?;
            } else if i % 2 == 0 {
                self.tmp1.clear();
                x.trans(&self.tmp2, cont, &mut self.tmp1)?;
                use_1 = true;
            } else {
                self.tmp2.clear();
                x.trans(&self.tmp1, cont, &mut self.tmp2)?;
                use_1 = false;
            }
        }
        if use_1 { Ok(&self.tmp1) } else { Ok(&self.tmp2) }
    }

    /// Resolve any named columns
    pub fn lookup(&mut self, field_names: &[&str]) -> Result<()> {
        for x in &mut self.v {
            x.lookup(field_names)?;
        }
        Ok(())
    }
}

type MakerBox = Box<dyn Fn(&TransSettings, &str) -> Result<Box<dyn Trans>> + Send>;
/// A named constructor for a [Trans], used by [`TransMaker`]
struct TransMakerItem {
    /// name of Trans
    tag: &'static str,
    /// what this matcher does
    help: &'static str,
    /// Create a dyn Trans from a pattern
    maker: MakerBox,
}

struct TransMakerAlias {
    old_name: &'static str,
    new_name: &'static str,
}

static TRANS_MAKER: Mutex<Vec<TransMakerItem>> = Mutex::new(Vec::new());
static TRANS_ALIAS: Mutex<Vec<TransMakerAlias>> = Mutex::new(Vec::new());
const MODIFIERS: &[&str] = &["utf8"];

/// Makes a [Trans]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Default, Hash)]
pub struct TransMaker {}

impl TransMaker {
    /// add standard trans makers
    pub(crate) fn init() -> Result<()> {
        if !TRANS_MAKER.lock().unwrap().is_empty() {
            return err!("Double init of TransMaker not allowed");
        }
        Self::do_add_alias("lower", "lowercase")?;
        Self::do_add_alias("upper", "uppercase")?;
        Self::do_push("normspace", "normalize white space", |c, _p: &str| {
            if c.utf8 { Ok(Box::<NormSpaceUtf8>::default()) } else { Ok(Box::new(NormSpace {})) }
        })?;
        Self::do_push("lower", "make lower case", |c, _p| {
            if c.utf8 { Ok(Box::<LowerUtfTrans>::default()) } else { Ok(Box::new(LowerTrans {})) }
        })?;
        Self::do_push("upper", "make upper case", |c, _p| {
            if c.utf8 { Ok(Box::<UpperUtfTrans>::default()) } else { Ok(Box::new(UpperTrans {})) }
        })?;
        Self::do_push("from_base64", "decode base64 encoding", |_c, _p| {
            Ok(Box::new(Base64Trans::new(false)))
        })?;
        Self::do_push("to_base64", "encode base64 encoding", |_c, _p| {
            Ok(Box::new(Base64Trans::new(true)))
        })?;
        Self::do_push("bytes", "Select bytes from value", |_c, p| {
            Ok(Box::new(BytesTrans::new(p)?))
        })?;
        Self::do_push("select", "Select sub-columns from value", |_c, p| {
            Ok(Box::new(SelectTrans::new(p)?))
        })?;
        Ok(())
    }
    /// Add a new trans. If a Trans already exists by that name, replace it.
    pub fn push<F>(tag: &'static str, help: &'static str, maker: F) -> Result<()>
    where
        F: Fn(&TransSettings, &str) -> Result<Box<dyn Trans>> + Send + 'static,
    {
        Self::do_push(tag, help, maker)
    }
    /// Add a new alias. If an alias already exists by that name, replace it.
    pub fn add_alias(old_name: &'static str, new_name: &'static str) -> Result<()> {
        Self::do_add_alias(old_name, new_name)
    }
    /// Return name, replaced by its alias, if any.
    fn resolve_alias(name: &str) -> &str {
        for x in TRANS_ALIAS.lock().unwrap().iter_mut() {
            if x.new_name == name {
                return x.old_name;
            }
        }
        name
    }
    fn do_add_alias(old_name: &'static str, new_name: &'static str) -> Result<()> {
        if MODIFIERS.contains(&new_name) {
            return err!(
                "You can't add an alias named {new_name} because that is reserved for a modifier"
            );
        }
        let m = TransMakerAlias { old_name, new_name };
        let mut mm = TRANS_ALIAS.lock().unwrap();
        for x in mm.iter_mut() {
            if x.new_name == m.new_name {
                *x = m;
                return Ok(());
            }
        }
        mm.push(m);
        drop(mm);
        Ok(())
    }
    fn do_push<F>(tag: &'static str, help: &'static str, maker: F) -> Result<()>
    where
        F: Fn(&TransSettings, &str) -> Result<Box<dyn Trans>> + Send + 'static,
    {
        if MODIFIERS.contains(&tag) {
            return err!(
                "You can't add a trans named {tag} because that is reserved for a modifier"
            );
        }
        let m = TransMakerItem { tag, help, maker: Box::new(maker) };
        let mut mm = TRANS_MAKER.lock().unwrap();
        for x in mm.iter_mut() {
            if x.tag == m.tag {
                *x = m;
                return Ok(());
            }
        }
        mm.push(m);
        drop(mm);
        Ok(())
    }
    /// Print all available Transformers to stdout.
    pub fn help() {
        println!("Modifiers :");
        println!("utf8  Operations are on utf8 strings, rather than the default u8 bytes.");
        println!("Methods :");
        let mut results = Vec::new();
        for x in &*TRANS_MAKER.lock().unwrap() {
            results.push(format!("{:12}{}", x.tag, x.help));
        }
        results.sort();
        for x in results {
            println!("{x}");
        }
        println!("See also https://avjewe.github.io/cdxdoc/Transform.html.");
    }
    /// Create a Trans from a trans spec and a pattern
    pub fn make2(trans: &str, pattern: &str) -> Result<Transform> {
        let mut spec = trans.to_string();
        if !pattern.is_empty() {
            spec.push(',');
            spec.push_str(pattern);
        }
        let mut conf = TransSettings::default();
        let mut kind = "";
        if !trans.is_empty() {
            for x in trans.split('.') {
                if x.eq_ignore_ascii_case("utf8") {
                    conf.utf8 = true;
                } else {
                    kind = x;
                }
            }
            kind = Self::resolve_alias(kind);
        }
        Ok(Transform { spec, conf, trans: Self::make_box(kind, &conf, pattern)? })
    }
    /// make a dyn Trans from a named trans and a pattern
    pub fn make_box(kind: &str, conf: &TransSettings, pattern: &str) -> Result<Box<dyn Trans>> {
        for x in &*TRANS_MAKER.lock().unwrap() {
            if x.tag == kind {
                return (x.maker)(conf, pattern);
            }
        }
        err!("Unknown trans : {}", kind)
    }

    /// Create a trans from a full spec, i.e. "Trans,Pattern"
    pub fn make(spec: &str) -> Result<Transform> {
        if let Some((a, b)) = spec.split_once(',') {
            Self::make2(a, b)
        } else {
            Self::make2(spec, "")
        }
    }
}