Skip to content

Instantly share code, notes, and snippets.

@tripulse
Last active December 15, 2021 06:29
Show Gist options
  • Save tripulse/9a0fa8574caea687f57c5a582fc6cf44 to your computer and use it in GitHub Desktop.
Save tripulse/9a0fa8574caea687f57c5a582fc6cf44 to your computer and use it in GitHub Desktop.
struct strnsplit {
const char* ptr;
size_t len;
};
const char* strnsplit(
const char* restrict str,
int c,
size_t len,
size_t *part_len,
struct strnsplit* ctx
) {
if(str != NULL) {
ctx->ptr = str;
ctx->len = len;
}
if(ctx->len == 0) {
*part_len = 0;
return NULL;
}
const char* begin = ctx->ptr;
do {
--ctx->len;
if(*ctx->ptr++ == c)
break;
} while(ctx->len > 0);
*part_len = ctx->ptr - begin;
return begin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment