HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

String splitting in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
stringsplittingstackoverflow

Problem

I am trying to improve my C skills, and I hope someone might be able to provide me with some feedback on the following code.

It is just basic string splitting, it should behave the similar to Ruby's String#split or Clojure's clojure.string.split. I couldn't think of a simple/efficient way to create an array of variable-size strings so I went the callback route.

Anyway, any and all feedback is greatly appreciated, thank you! Check out the code:

void strsplit(char *str, char *delim, int limit, void (*cb)(char *s, int idx))
{
  char *search = strdup(str);

  if (limit == 1) {
    cb(search, 0);
  }
  else {
    int i = 0, count = 0, len = strlen(str), len_delim = strlen(delim);
    char *segment = NULL, *leftover = NULL;
    limit = limit > 0 ? limit - 1 : len;

    segment = strtok(search, delim);
    for (i = 0; segment != NULL && i < limit; i++) {
      count += strlen(segment) + len_delim;
      cb(segment, i);
      segment = strtok(NULL, delim);
    }

    if (len != limit && count < len) {
      leftover = (char*) malloc(len - count + 1);
      memcpy(leftover, str + count, len - count);
      leftover[len - count] = '\0';
      cb(leftover, i);
      free(leftover);
    }
  }
}


Also see the code with a test framework.

Solution

I'd suggest not rolling your own implementation; just use strtok_r properly and save yourself some time. For memory allocation, you can either use the offsets into the string in-place, or use strndup to get copies of each token as you find it.

Context

StackExchange Code Review Q#11265, answer score: 5

Revisions (0)

No revisions yet.