Skip to content

Commit 873af1f

Browse files
avoid dry
1 parent 2372abd commit 873af1f

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

code/logic/soap.c

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -132,62 +132,45 @@ void fossil_soap_sanitize(char *input) {
132132
}
133133
}
134134

135-
// Function to check if a word is an offensive word or phrase
136-
int32_t fossil_soap_is_offensive(const char *word) {
137-
if (word == NULL || *word == '\0') return EXIT_SUCCESS;
135+
static int32_t is_in_list(const char *word, const char **list, size_t list_size) {
136+
if (!word || *word == '\0') return EXIT_SUCCESS;
138137

139-
for (size_t i = 0; i < sizeof(FOSSIL_SOAP_OFFENSIVE) / sizeof(FOSSIL_SOAP_OFFENSIVE[0]); ++i) {
140-
if (strcasecmp(word, FOSSIL_SOAP_OFFENSIVE[i]) == 0) {
141-
return EXIT_FAILURE;
142-
}
138+
for (size_t i = 0; i < list_size; ++i) {
139+
if (strcasecmp(word, list[i]) == 0) return EXIT_FAILURE;
143140
}
144141
return EXIT_SUCCESS;
145142
}
146143

147-
// Function to get the number of offensive words found in a string
148-
int32_t fossil_soap_count_offensive(const char *input) {
149-
if (input == NULL || *input == '\0') return 0;
150-
151-
int count = 0;
152-
char *copy = custom_strdup(input);
153-
if (copy == NULL) return EXIT_SUCCESS;
154-
155-
char *token = strtok(copy, " ,.!?;:"); // Tokenize the string by space and punctuation
156-
while (token != NULL) {
157-
if (fossil_soap_is_offensive(token)) {
158-
count++;
159-
}
160-
token = strtok(NULL, " ,.!?;:");
161-
}
162-
free(copy); // Free the memory allocated for the copy
163-
return count;
144+
int32_t fossil_soap_is_offensive(const char *word) {
145+
return is_in_list(word, FOSSIL_SOAP_OFFENSIVE, sizeof(FOSSIL_SOAP_OFFENSIVE) / sizeof(*FOSSIL_SOAP_OFFENSIVE));
164146
}
165147

166148
int32_t fossil_soap_is_rotbrain(const char *word) {
167-
if (word == NULL || *word == '\0') return EXIT_SUCCESS;
168-
169-
for (size_t i = 0; i < sizeof(FOSSIL_SOAP_ROTBRAIN) / sizeof(FOSSIL_SOAP_ROTBRAIN[0]); ++i) {
170-
if (strcasecmp(word, FOSSIL_SOAP_ROTBRAIN[i]) == 0) {
171-
return EXIT_FAILURE;
172-
}
173-
}
174-
return EXIT_SUCCESS;
149+
return is_in_list(word, FOSSIL_SOAP_ROTBRAIN, sizeof(FOSSIL_SOAP_ROTBRAIN) / sizeof(*FOSSIL_SOAP_ROTBRAIN));
175150
}
176151

177-
int32_t fossil_soap_count_rotbrain(const char *input) {
178-
if (input == NULL || *input == '\0') return 0;
152+
static int32_t count_matches(const char *input, const char **list, size_t list_size) {
153+
if (!input || *input == '\0') return 0;
179154

180155
int count = 0;
181156
char *copy = custom_strdup(input);
182-
if (copy == NULL) return EXIT_SUCCESS;
157+
if (!copy) return EXIT_SUCCESS;
183158

184-
char *token = strtok(copy, " ,.!?;:"); // Tokenize the string by space and punctuation
185-
while (token != NULL) {
186-
if (fossil_soap_is_rotbrain(token)) {
159+
char *token = strtok(copy, " ,.!?;:");
160+
while (token) {
161+
if (is_in_list(token, list, list_size) == EXIT_FAILURE) {
187162
count++;
188163
}
189164
token = strtok(NULL, " ,.!?;:");
190165
}
191-
free(copy); // Free the memory allocated for the copy
166+
free(copy);
192167
return count;
193168
}
169+
170+
int32_t fossil_soap_count_offensive(const char *input) {
171+
return count_matches(input, FOSSIL_SOAP_OFFENSIVE, sizeof(FOSSIL_SOAP_OFFENSIVE) / sizeof(*FOSSIL_SOAP_OFFENSIVE));
172+
}
173+
174+
int32_t fossil_soap_count_rotbrain(const char *input) {
175+
return count_matches(input, FOSSIL_SOAP_ROTBRAIN, sizeof(FOSSIL_SOAP_ROTBRAIN) / sizeof(*FOSSIL_SOAP_ROTBRAIN));
176+
}

0 commit comments

Comments
 (0)