# hadid-csv
RFC 4180 CSV row encoder and parser for the Hadid programming language.
## Installation
```sh
hadid add hadid-csv
```
## Usage
```hadid
import "hadid-csv" as csv;
let cols = List<string>.Empty;
cols = list_append(cols, "Hadid");
cols = list_append(cols, "Istanbul, TR");
cols = list_append(cols, "v0.9");
let line = csv.csv_encode_row(cols);
print(line);
// -> Hadid,"Istanbul, TR",v0.9
```
# hadid-csv RFC 4180 CSV row encoder and parser for the Hadid programming language. ## Installation ```sh hadid add hadid-csv ``` ## Usage ```hadid import "hadid-csv" as csv; let cols = List<string>.Empty; cols = list_append(cols, "Hadid"); cols = list_append(cols, "Istanbul, TR"); cols = list_append(cols, "v0.9"); let line = csv.csv_encode_row(cols); print(line); // -> Hadid,"Istanbul, TR",v0.9 ```
[package]
name = "hadid-csv"
version = "0.1.0"
entry = "src/lib.had"
description = "RFC 4180 CSV row parser and encoder for Hadid"
license = "MIT"
keywords = "csv,parser,encoder,rfc4180,data"
repository = "https://github.com/DGTLHLHAT/hadid"
[dependencies]
// hadid-csv: RFC 4180 CSV row encoder and parser
fn csv_needs_quotes(field: string): bool {
let n = length(field);
for (let i = 0; i < n; i = i + 1) {
let ch = field[i];
if (ch == 44) { return true; }
if (ch == 34) { return true; }
if (ch == 10) { return true; }
if (ch == 13) { return true; }
}
return false;
}
fn csv_escape_field(field: string): string {
if (csv_needs_quotes(field) == false) {
return field;
}
match (text_replace(field, "\"", "\"\"")) {
Ok(escaped) => { return "\"" + escaped + "\""; }
Err(e) => { return "\"" + field + "\""; }
}
}
fn csv_encode_row(fields: List<string>): string {
let n = length(fields);
let line = "";
for (let i = 0; i < n; i = i + 1) {
if (i > 0) { line = line + ","; }
line = line + csv_escape_field(fields[i]);
}
return line;
}
fn csv_char(code: i32): string {
match (text_from_codepoint(code)) {
Ok(s) => { return s; }
Err(e) => { return ""; }
}
}
fn csv_parse_row(line: string): List<string> {
let res = List<string>.Empty;
let n = length(line);
let cur = "";
bool in_quotes = false;
i32 i = 0;
while (i < n) {
let ch = line[i];
if (in_quotes) {
if (ch == 34) {
if (i + 1 < n) {
if (line[i + 1] == 34) {
cur = cur + "\"";
i = i + 2;
continue;
}
}
in_quotes = false;
i = i + 1;
} else {
cur = cur + csv_char(ch);
i = i + 1;
}
} else {
if (ch == 34) {
in_quotes = true;
i = i + 1;
} else if (ch == 44) {
res = list_append(res, cur);
cur = "";
i = i + 1;
} else if (ch == 13) {
i = i + 1;
} else if (ch == 10) {
i = n;
} else {
cur = cur + csv_char(ch);
i = i + 1;
}
}
}
res = list_append(res, cur);
return res;
}
fn csv_satir_kodla(alanlar: List<string>): string {
return csv_encode_row(alanlar);
}
fn csv_satir_coz(satir: string): List<string> {
return csv_parse_row(satir);
}
| Version | Size | Downloads | BLAKE2b-256 | Status |
|---|---|---|---|---|
| v0.1.0 | 3511 B | 0 | blake2b-256:2cefbfb89d4b… | Active |