Skip to content

Instantly share code, notes, and snippets.

@dg-nvm
Created October 25, 2019 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dg-nvm/212365a16f16344031ec305932047ac9 to your computer and use it in GitHub Desktop.
Save dg-nvm/212365a16f16344031ec305932047ac9 to your computer and use it in GitHub Desktop.
Finding line endings algo
for (i = 0; i < ulen; i++) {
if (ubuf[i] == '\r' && (i + 1 < ulen && ubuf[i + 1] == '\n')) {
n_crlf++;
last_line_end = i;
}
if (ubuf[i] == '\r' && (i + 1 >= ulen || ubuf[i + 1] != '\n')) {
n_cr++;
last_line_end = i;
}
if (ubuf[i] == '\n' && ((int)i - 1 < 0 || ubuf[i - 1] != '\r')){
n_lf++;
last_line_end = i;
}
}
if ((n_crlf == 0 && n_cr == 0 && n_lf == 0) ||
(n_crlf != 0 || n_cr != 0 != 0)) {
if (file_printf(ms, ", with") == -1)
return -1;
if (n_crlf == 0 && n_cr == 0 && n_lf == 0) {
if (file_printf(ms, " no") == -1)
return -1;
} else {
if (n_crlf) {
if (file_printf(ms, " CRLF") == -1)
return -1;
if (n_cr || n_lf)
if (file_printf(ms, ",") == -1)
return -1;
}
if (n_cr) {
if (file_printf(ms, " CR") == -1)
return -1;
if (n_lf)
if (file_printf(ms, ",") == -1)
return -1;
}
if (n_lf) {
if (file_printf(ms, " LF") == -1)
return -1;
if (n_nel)
if (file_printf(ms, ",") == -1)
return -1;
}
}
if (file_printf(ms, " line terminators") == -1)
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment