Skip to content

Commit 5642b9a

Browse files
committed
Deduplicate code in config.c/apply_superset_style
1 parent dcd4609 commit 5642b9a

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

config.c

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,31 @@ bool apply_style(struct mako_style *target, const struct mako_style *style) {
328328
return true;
329329
}
330330

331+
// Find unique specifiers in the given format
332+
void find_unique_specifiers(char *format, char *target_format, char *target_format_pos) {
333+
char *format_pos = format;
334+
char current_specifier[3] = {0};
335+
while (*format_pos) {
336+
format_pos = strstr(format_pos, "%");
337+
if (!format_pos) {
338+
break;
339+
}
340+
341+
// We only want to add the format specifier to the target if we
342+
// haven't already seen it.
343+
// Need to copy the specifier into its own string to use strstr
344+
// here, because there's no way to limit how much of the string
345+
// it uses in the comparison.
346+
memcpy(&current_specifier, format_pos, 2);
347+
if (!strstr(target_format, current_specifier)) {
348+
memcpy(target_format_pos, format_pos, 2);
349+
}
350+
351+
++format_pos; // Enough to move to the next match.
352+
target_format_pos += 2; // This needs to go to the next slot.
353+
}
354+
}
355+
331356
// Given a config and a style in which to store the information, this will
332357
// calculate a style that has the maximum value of all the configured criteria
333358
// styles (including the default as a base), for values where it makes sense to
@@ -398,51 +423,11 @@ bool apply_superset_style(
398423

399424
// We do need to be safe about these two though.
400425
if (style->spec.format) {
401-
char *format_pos = style->format;
402-
char current_specifier[3] = {0};
403-
while (*format_pos) {
404-
format_pos = strstr(format_pos, "%");
405-
if (!format_pos) {
406-
break;
407-
}
408-
409-
// We only want to add the format specifier to the target if we
410-
// haven't already seen it.
411-
// Need to copy the specifier into its own string to use strstr
412-
// here, because there's no way to limit how much of the string
413-
// it uses in the comparison.
414-
memcpy(&current_specifier, format_pos, 2);
415-
if (!strstr(target->format, current_specifier)) {
416-
memcpy(target_format_pos, format_pos, 2);
417-
}
418-
419-
++format_pos; // Enough to move to the next match.
420-
target_format_pos += 2; // This needs to go to the next slot.
421-
}
426+
find_unique_specifiers(style->format, target->format, target_format_pos);
422427
}
423428

424429
if (style->spec.title_format) {
425-
char *format_pos = style->title_format;
426-
char current_specifier[3] = {0};
427-
while (*format_pos) {
428-
format_pos = strstr(format_pos, "%");
429-
if (!format_pos) {
430-
break;
431-
}
432-
433-
// We only want to add the format specifier to the target if we
434-
// haven't already seen it.
435-
// Need to copy the specifier into its own string to use strstr
436-
// here, because there's no way to limit how much of the string
437-
// it uses in the comparison.
438-
memcpy(&current_specifier, format_pos, 2);
439-
if (!strstr(target->title_format, current_specifier)) {
440-
memcpy(target_format_pos_title, format_pos, 2);
441-
}
442-
443-
++format_pos; // Enough to move to the next match.
444-
target_format_pos_title += 2; // This needs to go to the next slot.
445-
}
430+
find_unique_specifiers(style->title_format, target->format, target_format_pos_title);
446431
}
447432
}
448433

0 commit comments

Comments
 (0)