change one func

This commit is contained in:
2025-10-31 15:07:26 +07:00
parent 5dae4a8963
commit 11d317662f

View File

@@ -35,27 +35,37 @@ static long long parse_ll(const char *s) {
long long process_data(const char *input, size_t input_len, char *output, long long process_data(const char *input, size_t input_len, char *output,
size_t output_size, long long max_replacements) { size_t output_size, long long max_replacements) {
long long total_replacements = 0; long long total_replacements = 0;
int at_line_start = 1;
unsigned char line_key = 0;
size_t out_pos = 0; size_t out_pos = 0;
size_t i = 0;
for (size_t i = 0; i < input_len && out_pos < output_size - 1; i++) { while (i < input_len && out_pos < output_size - 1) {
unsigned char c = (unsigned char)input[i]; unsigned char c = (unsigned char)input[i];
if (at_line_start) { /* Try to form a non-overlapping pair with the next character.
line_key = c; Do not treat newline as part of a pair (preserve line structure). */
at_line_start = 0; if (i + 1 < input_len) {
} unsigned char next = (unsigned char)input[i + 1];
if (c != '\n' && next != '\n' && c == next && total_replacements < max_replacements) {
unsigned char outc = c; /* write first char of pair */
if (c == '\n') { output[out_pos++] = (char)c;
at_line_start = 1; if (out_pos >= output_size - 1) break;
} else if (c == line_key && total_replacements < max_replacements) { /* write space instead of second char */
outc = ' '; output[out_pos++] = ' ';
total_replacements++; total_replacements++;
i += 2; /* skip both chars (non-overlapping) */
continue;
}
} }
output[out_pos++] = (char)outc; /* No valid pair -> write current char */
output[out_pos++] = (char)c;
i++;
}
/* If we stopped early but still have room, copy remaining bytes (without forming pairs)
until output buffer full or input ends. This preserves trailing data. */
while (i < input_len && out_pos < output_size - 1) {
output[out_pos++] = input[i++];
} }
output[out_pos] = '\0'; output[out_pos] = '\0';