Skip to content

Instantly share code, notes, and snippets.

@chrispsn
Last active November 6, 2018 12:33
Show Gist options
  • Select an option

  • Save chrispsn/2e8e3c64d83760cca40463d7c5182f4f to your computer and use it in GitHub Desktop.

Select an option

Save chrispsn/2e8e3c64d83760cca40463d7c5182f4f to your computer and use it in GitHub Desktop.
ASCII separator characters in action
// https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
const FS = String.fromCharCode(28); // File separator; end of file. Or between a concatenation of what might otherwise be separate files.
const GS = String.fromCharCode(29); // Group separator; between sections of data. Not needed in simple data files.
const RS = String.fromCharCode(30); // Record separator; end of a record or row.
const US = String.fromCharCode(31); // Unit separator; between fields of a record, or members of a row.
function print_to_string_table(str) {
let o = "";
for (var i = 0; i < str.length; i++) {
const char = str[i];
switch (char) {
case RS: o += "\n"; break;
case US: o += "\t"; break;
default: o += char;
}
};
return o;
}
function print_to_html_table(str) {
let o = "<table>";
if (str.length > 0) o += "<tr><td>";
for (var i = 0; i < str.length; i++) {
const char = str[i];
switch (char) {
case RS: o += "<tr><td>"; break;
case US: o += "<td>"; break;
default: o += char;
}
};
o += "</table>";
return o;
}
const test = "hello" + US + "world" + RS + "it's" + US + "me" + RS + "margarita" + US + "man";
console.log(print_to_string_table(test));
console.log(print_to_html_table(test));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment