Skip to content

Commit

Permalink
Simplify implementation of stumpless_get_priority_string
Browse files Browse the repository at this point in the history
  • Loading branch information
m3g4d1v3r committed Nov 7, 2024
1 parent 0b5c3d1 commit e3f4b99
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/prival.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,45 @@ const char *
stumpless_get_priority_string( int prival ) {
const char *facility;
const char *severity;
char *facility_suffix;
char *severity_suffix;
const char *aux;

size_t priority_string_size;
char* priority_string;

facility = stumpless_get_facility_string( get_facility( prival ) );
severity = stumpless_get_severity_string( get_severity( prival ) );

facility_suffix = strrchr(facility, '_');
severity_suffix = strrchr(severity, '_');
aux = facility;
facility = strrchr(facility, '_');
if (facility == NULL)
facility = aux;

Check warning on line 76 in src/prival.c

View check run for this annotation

Codecov / codecov/patch

src/prival.c#L76

Added line #L76 was not covered by tests
else
facility++; // inc by 1 to skip '_'

if (facility_suffix == NULL || severity_suffix == NULL) {
return stumpless_get_prival_string( prival );
}
// then, we increment the pointer by 1 in order to skip the '_'
facility_suffix++;
severity_suffix++;
aux = severity;
severity = strrchr(severity, '_');
if (severity == NULL)
severity = aux;

Check warning on line 83 in src/prival.c

View check run for this annotation

Codecov / codecov/patch

src/prival.c#L83

Added line #L83 was not covered by tests
else
severity++; // inc by 1 to skip '_'

size_t len_facility_suffix = strlen(facility_suffix);
size_t len_severity_suffix = strlen(severity_suffix);
size_t len_facility = strlen(facility);
size_t len_severity = strlen(severity);

// +1 for '.' formatting, +1 for termination
priority_string_size = ( len_facility_suffix + len_severity_suffix + 2 );
priority_string_size = ( len_facility + len_severity + 2 );
priority_string = alloc_mem( priority_string_size );

for (size_t idx = 0; idx < len_facility_suffix; idx++) {
priority_string[idx] = tolower(facility_suffix[idx]);
for (size_t idx = 0; idx < len_facility; idx++) {
priority_string[idx] = tolower(facility[idx]);
}
priority_string[len_facility_suffix] = '.';

for (size_t idx = 0; idx < len_severity_suffix; idx++) {
priority_string[len_facility_suffix + 1 + idx] = tolower(severity_suffix[idx]);
priority_string[len_facility] = '.';

for (size_t idx = 0; idx < len_severity; idx++) {
priority_string[len_facility + 1 + idx] = tolower(severity[idx]);
}

priority_string[priority_string_size - 1] = '\0';

return priority_string;
Expand Down

0 comments on commit e3f4b99

Please sign in to comment.