Skip to main content

rl_lang/docs/entries/tutorial/t2/
p_a3.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static ADV_STRING_PARSING: ConceptEntry = ConceptEntry {
4    name: "3. parsing strings",
5    summary: "parsing strings",
6    category: ConceptCategory::Tooling,
7    prerequisites: &[],
8    descriptions: &[
9        DescriptionEntry {
10            kind: DescriptionKind::Explanation,
11            title: None,
12            description: "parsing means taking raw text and turning it into structured data your program can work with. it is one of the most common real-world tasks. you already know split from std::str - it is the foundation of CSV parsing",
13            examples: &[
14                "get split from std::str\n\ndec string row = \"1;pending;1750000000;buy groceries\"\ndec arr[string] fields = split(row, \";\")\n\nprintln(fields[0]) // 1\nprintln(fields[1]) // pending\nprintln(fields[2]) // 1750000000\nprintln(fields[3]) // buy groceries",
15            ],
16            expected_output: &[],
17        },
18        DescriptionEntry {
19            kind: DescriptionKind::Explanation,
20            title: None,
21            description: "to parse a full CSV string you split it into lines first, then split each line into fields. you get an array of rows where each row is an array of strings",
22            examples: &[
23                "get split from std::str\n\ndec string csv = \"1;pending;buy milk\\n2;done;write code\"\ndec arr[string]        lines = split(csv, \"\\n\")\ndec arr[arr[string]]   rows  = []\n\n// we will fill this in with arr_push soon",
24            ],
25            expected_output: &[],
26        },
27        DescriptionEntry {
28            kind: DescriptionKind::Explanation,
29            title: None,
30            description: "trim is important when parsing. files often have trailing newlines or spaces that will silently break comparisons if you do not strip them first",
31            examples: &[
32                "get split, trim from std::str\n\ndec string line = \"  1;pending;buy milk  \\n\"\ndec string clean = trim(line)\ndec arr[string] fields = split(clean, \";\")\nprintln(fields[1]) // pending  (not \"pending  \\n\")",
33            ],
34            expected_output: &[],
35        },
36        DescriptionEntry {
37            kind: DescriptionKind::Explanation,
38            title: None,
39            description: "is_empty lets you skip blank lines - files often end with a trailing newline that produces an empty string when split",
40            examples: &[
41                "get split, trim, is_empty from std::str\n\ndec string csv   = \"row1\\nrow2\\n\"\ndec arr[string] lines = split(csv, \"\\n\")\n\nfor line in lines {\n    if (is_empty(trim(line))) { continue }\n    println(line) // row1, row2 - trailing empty line skipped\n}",
42            ],
43            expected_output: &[],
44        },
45        DescriptionEntry {
46            kind: DescriptionKind::Explanation,
47            title: None,
48            description: "exercise: write a function csv_parse_row in csv.rl that takes a single CSV line as a string and returns an array of trimmed fields. test it in main.rl\n\nexpected output:\n  [1, pending, 1750000000, buy groceries]",
49            examples: &[
50                "// csv.rl\nget split, trim from std::str\n\nCONST string DELIMITER = \";\"\n\nfn csv_parse_row(string line) -> arr[string] {\n    get arr_map from std::array\n    dec arr[string] fields = split(line, DELIMITER)\n    return arr_map(fields, fn(string f) -> string { return trim(f) })\n}\n\n// main.rl\nget csv\nget concat from std::str\n\nfn main() {\n    dec arr[string] row = csv_parse_row(\"1;pending;1750000000;buy groceries\")\n    println(row)\n}",
51            ],
52            expected_output: &[],
53        },
54    ],
55    pitfalls: &[],
56    related: &[],
57    related_stdlib: &[],
58    since: None,
59};