reduced code duplication

master
taiyu 9 years ago
parent 9d69d880e6
commit e9e09d123c

@ -53,8 +53,9 @@ char *strip_comments(char *str) {
list_t *split_string(const char *str, const char *delims) { list_t *split_string(const char *str, const char *delims) {
list_t *res = create_list(); list_t *res = create_list();
int i, j; int i, j;
for (i = 0, j = 0; i < strlen(str) + 1; ++i) { int len = strlen(str);
if (strchr(delims, str[i]) || i == strlen(str)) { for (i = 0, j = 0; i < len + 1; ++i) {
if (strchr(delims, str[i]) || i == len) {
if (i - j == 0) { if (i - j == 0) {
continue; continue;
} }
@ -63,7 +64,7 @@ list_t *split_string(const char *str, const char *delims) {
left[i - j] = 0; left[i - j] = 0;
list_add(res, left); list_add(res, left);
j = i + 1; j = i + 1;
while (j <= strlen(str) && str[j] && strchr(delims, str[j])) { while (j <= len && str[j] && strchr(delims, str[j])) {
j++; j++;
i++; i++;
} }
@ -111,40 +112,44 @@ int unescape_string(char *string) {
for (i = 0; string[i]; ++i) { for (i = 0; string[i]; ++i) {
if (string[i] == '\\') { if (string[i] == '\\') {
--len; --len;
int shift = 0;
switch (string[++i]) { switch (string[++i]) {
case '0': case '0':
string[i - 1] = '\0'; string[i - 1] = '\0';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'a': case 'a':
string[i - 1] = '\a'; string[i - 1] = '\a';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'b': case 'b':
string[i - 1] = '\b'; string[i - 1] = '\b';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 't': case 't':
string[i - 1] = '\t'; string[i - 1] = '\t';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'n': case 'n':
string[i - 1] = '\n'; string[i - 1] = '\n';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'v': case 'v':
string[i - 1] = '\v'; string[i - 1] = '\v';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'f': case 'f':
string[i - 1] = '\f'; string[i - 1] = '\f';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'r': case 'r':
string[i - 1] = '\r'; string[i - 1] = '\r';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
} }
if (shift) {
memmove(string + i, string + i + shift, len - i);
}
} }
} }
return len; return len;

Loading…
Cancel
Save