Skip to content

Instantly share code, notes, and snippets.

@OrangeTide
Created September 13, 2022 17:29
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 OrangeTide/6f087a13ec31208deabe1348e2fa1598 to your computer and use it in GitHub Desktop.
Save OrangeTide/6f087a13ec31208deabe1348e2fa1598 to your computer and use it in GitHub Desktop.
Using strcspn to process control character delimiters
#include <stdio.h>
#include <string.h>
void
process(const char *input)
{
const char delim[] = { 0x0b, 0x1c, 0x0d, 0 };
int count = 0;
while (input[0] != '\0') {
size_t n = strcspn(input, delim);
printf("piece[%d] type=%#x: \"%.*s\"\n", ++count, input[n], n, input);
if (input[n]) {
n++; /* move past the delim we found, don't move if we hit nul terminator */
}
input += n;
}
}
int
main(int argc, char *argv[])
{
if (argc > 1) {
int i;
for (i = 1; i < argc; i++) {
process(argv[i]);
}
} else {
process("This is\x0d""a test\x0b wow \x1c\x1c!");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment