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,
size_t output_size, long long max_replacements) {
long long total_replacements = 0;
int at_line_start = 1;
unsigned char line_key = 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];
if (at_line_start) {
line_key = c;
at_line_start = 0;
/* Try to form a non-overlapping pair with the next character.
Do not treat newline as part of a pair (preserve line structure). */
if (i + 1 < input_len) {
unsigned char next = (unsigned char)input[i + 1];
if (c != '\n' && next != '\n' && c == next && total_replacements < max_replacements) {
/* write first char of pair */
output[out_pos++] = (char)c;
if (out_pos >= output_size - 1) break;
/* write space instead of second char */
output[out_pos++] = ' ';
total_replacements++;
i += 2; /* skip both chars (non-overlapping) */
continue;
}
}
unsigned char outc = c;
if (c == '\n') {
at_line_start = 1;
} else if (c == line_key && total_replacements < max_replacements) {
outc = ' ';
total_replacements++;
}
/* No valid pair -> write current char */
output[out_pos++] = (char)c;
i++;
}
output[out_pos++] = (char)outc;
/* 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';