Skip to content

Instantly share code, notes, and snippets.

@alsamitech
Created March 17, 2021 04:25
Show Gist options
  • Select an option

  • Save alsamitech/796da76de76457b470ffed3cf5067b8f to your computer and use it in GitHub Desktop.

Select an option

Save alsamitech/796da76de76457b470ffed3cf5067b8f to your computer and use it in GitHub Desktop.
// returns a heap duplicate up to n bytes
char* strndupx(char* in, long unsigned int n){
char* new=malloc(n+1);
new[n]=0x0;
memcpy(new, in, n);
return new;
}
long unsigned int until_byte(char* bytes, long unsigned int n,char byte){
long unsigned int until;
for(until=0;until<n;until++){
if(bytes[until]==byte){
return until;
}
}
return n;
}
char* onesep(char* in, char sep, char** save_ptr){
long unsigned int until=until_byte(in, strlen(in), sep);
//printf("NICE: %lu %lu %s\n", strlen(in), until, in);
if(until==strlen(in))return 0;
*save_ptr=until;
return strndupx(in, until);
}
char* xsep(char* in, char sep){
static char* save;
long unsigned int len;
if(in){
save=in;
len=0;
}
char* retval=onesep(save, sep, &len);
save+=len+1;
return retval;
}
char** onetokl(char* in, char sep, long unsigned int* len){
char** tok=(char**)malloc(sizeof(char*)+1);
tok[0]=xsep(in, sep);
long unsigned int i;
for(i=1;tok[i-1]!=0;i++){
tok=(char**)realloc(tok, i*sizeof(char*)+1);
tok[i]=xsep(0, sep);
}
*len=i;
return tok;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment