Skip to content

Instantly share code, notes, and snippets.

@kgabis
Created November 20, 2013 09:33
Show Gist options
  • Save kgabis/7560346 to your computer and use it in GitHub Desktop.
Save kgabis/7560346 to your computer and use it in GitHub Desktop.
replaces comments with spaces
void json_remove_comments(char *string, const char *start_token, const char *end_token) {
int in_string = 0, escaped = 0, i;
char *ptr = NULL, c;
size_t start_token_len = strlen(start_token);
size_t end_token_len = strlen(end_token);
if (start_token_len == 0 || end_token_len == 0)
return;
while ((c = *string) != '\0') {
if (c == '\\' && !escaped) {
escaped = 1;
string++;
continue;
} else if (c == '\"' && !escaped) {
in_string = !in_string;
} else if (strncmp(string, start_token, start_token_len) == 0 && !in_string) {
for(i = 0; i < start_token_len; i++) string[i] = ' ';
string = string + start_token_len;
ptr = strstr(string, end_token);
if (!ptr) {
return; /* error? */
}
for (i = 0; i < (ptr - string) + end_token_len; i++)
string[i] = ' ';
string = ptr + end_token_len;
}
escaped = 0;
string++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment