Created
October 25, 2019 08:17
-
-
Save dg-nvm/212365a16f16344031ec305932047ac9 to your computer and use it in GitHub Desktop.
Finding line endings algo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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