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;
}
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) {
long long total_replacements = 0;
long long col = 0; /* position in current line (counts non-newline bytes) */
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];
/* 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;
}
if (c == '\n') {
/* write newline, reset column counter */
output[out_pos++] = '\n';
col = 0;
continue;
}
/* No valid pair -> write current char */
output[out_pos++] = (char)c;
i++;
col++;
unsigned char outc = c;
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)
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++];
}
/* If there is space, terminate string */
if (out_pos < output_size)
output[out_pos] = '\0';
else if (output_size > 0)
output[output_size - 1] = '\0';
output[out_pos] = '\0';
return total_replacements;
}