Skip to content

Commit

Permalink
Deduplicate code in config.c/apply_superset_style
Browse files Browse the repository at this point in the history
  • Loading branch information
mendelmaleh committed Apr 14, 2020
1 parent a36bc02 commit c890875
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,31 @@ bool apply_style(struct mako_style *target, const struct mako_style *style) {
return true;
}

// Find unique specifiers in the given format
void find_unique_specifiers(char *format, char *target_format, char *target_format_pos) {
char *format_pos = format;
char current_specifier[3] = {0};
while (*format_pos) {
format_pos = strstr(format_pos, "%");
if (!format_pos) {
break;
}

// We only want to add the format specifier to the target if we
// haven't already seen it.
// Need to copy the specifier into its own string to use strstr
// here, because there's no way to limit how much of the string
// it uses in the comparison.
memcpy(&current_specifier, format_pos, 2);
if (!strstr(target_format, current_specifier)) {
memcpy(target_format_pos, format_pos, 2);
}

++format_pos; // Enough to move to the next match.
target_format_pos += 2; // This needs to go to the next slot.
}
}

// Given a config and a style in which to store the information, this will
// calculate a style that has the maximum value of all the configured criteria
// styles (including the default as a base), for values where it makes sense to
Expand Down Expand Up @@ -355,8 +380,7 @@ bool apply_superset_style(
free(target->title_format);

// The "format" and "title_format" need enough space for one of each specifier.
target->format = calloc(1, (2 * strlen(VALID_FORMAT_SPECIFIERS)) + 1);
target->title_format = calloc(1, (2 * strlen(VALID_FORMAT_SPECIFIERS)) + 1);
target->format = target->title_format = calloc(1, (2 * strlen(VALID_FORMAT_SPECIFIERS)) + 1);
char *target_format_pos = target->format;
char *target_format_pos_title = target->title_format;

Expand Down Expand Up @@ -398,51 +422,11 @@ bool apply_superset_style(

// We do need to be safe about these two though.
if (style->spec.format) {
char *format_pos = style->format;
char current_specifier[3] = {0};
while (*format_pos) {
format_pos = strstr(format_pos, "%");
if (!format_pos) {
break;
}

// We only want to add the format specifier to the target if we
// haven't already seen it.
// Need to copy the specifier into its own string to use strstr
// here, because there's no way to limit how much of the string
// it uses in the comparison.
memcpy(&current_specifier, format_pos, 2);
if (!strstr(target->format, current_specifier)) {
memcpy(target_format_pos, format_pos, 2);
}

++format_pos; // Enough to move to the next match.
target_format_pos += 2; // This needs to go to the next slot.
}
find_unique_specifiers(style->format, target->format, target_format_pos);
}

if (style->spec.title_format) {
char *format_pos = style->title_format;
char current_specifier[3] = {0};
while (*format_pos) {
format_pos = strstr(format_pos, "%");
if (!format_pos) {
break;
}

// We only want to add the format specifier to the target if we
// haven't already seen it.
// Need to copy the specifier into its own string to use strstr
// here, because there's no way to limit how much of the string
// it uses in the comparison.
memcpy(&current_specifier, format_pos, 2);
if (!strstr(target->title_format, current_specifier)) {
memcpy(target_format_pos_title, format_pos, 2);
}

++format_pos; // Enough to move to the next match.
target_format_pos_title += 2; // This needs to go to the next slot.
}
find_unique_specifiers(style->title_format, target->format, target_format_pos_title);
}
}

Expand Down

0 comments on commit c890875

Please sign in to comment.