ctb_formats_dceutils/
tools.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Transformation and inspection helper tools for DCE data.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29/// Splits a string by a delimiter, merging segments when the delimiter is escaped with a backslash.
30pub fn explode_escaped(delimiter: char, s: &str) -> Vec<String> {
31    ctb_utilities::string::explode_escaped(s, &delimiter.to_string())
32}
33
34#[cfg(test)]
35#[allow(
36    clippy::panic,
37    clippy::expect_used,
38    clippy::unwrap_used,
39    clippy::unwrap_in_result,
40    clippy::panic_in_result_fn,
41    clippy::indexing_slicing,
42    clippy::arithmetic_side_effects,
43    reason = "Standard repository test boilerplate"
44)]
45mod tests {
46    use super::*;
47
48    #[crate::ctb_test]
49    fn test_explode_escaped() {
50        assert_eq!(explode_escaped(',', "a, b, c"), vec!["a", "b", "c"]);
51        assert_eq!(explode_escaped(',', "a\\,b, c"), vec!["a,b", "c"]);
52        assert_eq!(explode_escaped(',', "a\\,b\\,c, d"), vec!["a,b,c", "d"]);
53        assert_eq!(explode_escaped(',', "a\\"), vec!["a\\"]);
54    }
55}