change one func in kirill

This commit is contained in:
2025-10-31 15:20:59 +07:00
parent a1d23aec48
commit 619953e6cc

View File

@@ -32,43 +32,39 @@ static long long parse_ll(const char *s) {
return v; return v;
} }
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;
long long col = 0; /* position in current line (counts non-newline bytes) */
size_t out_pos = 0; size_t out_pos = 0;
size_t i = 0;
while (i < input_len && out_pos < output_size - 1) { for (size_t i = 0; i < input_len && out_pos < output_size - 1; i++) {
unsigned char c = (unsigned char)input[i]; unsigned char c = (unsigned char)input[i];
/* Try to form a non-overlapping pair with the next character. if (c == '\n') {
Do not treat newline as part of a pair (preserve line structure). */ /* write newline, reset column counter */
if (i + 1 < input_len) { output[out_pos++] = '\n';
unsigned char next = (unsigned char)input[i + 1]; col = 0;
if (c != '\n' && next != '\n' && c == next && total_replacements < max_replacements) { continue;
/* 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;
}
} }
/* No valid pair -> write current char */ col++;
output[out_pos++] = (char)c; unsigned char outc = c;
i++;
if ((col % 3) == 0 && total_replacements < max_replacements) {
outc = ' ';
total_replacements++;
}
output[out_pos++] = (char)outc;
} }
/* If we stopped early but still have room, copy remaining bytes (without forming pairs) /* If there is space, terminate string */
until output buffer full or input ends. This preserves trailing data. */ if (out_pos < output_size)
while (i < input_len && out_pos < output_size - 1) { output[out_pos] = '\0';
output[out_pos++] = input[i++]; else if (output_size > 0)
} output[output_size - 1] = '\0';
output[out_pos] = '\0';
return total_replacements; return total_replacements;
} }