| 1 |
/* |
| 2 |
* Copyright © 2024 Pierre Le Marre <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include "src/darray.h" |
| 11 |
#include "src/utils.h" |
| 12 |
#include "test/utils-text.h" |
| 13 |
|
| 14 |
/* For each line, drop substring starting from a given needle, then drop |
| 15 |
* the line if the rest are only whitespaces. The needle must not contain |
| 16 |
* "\n". */ |
| 17 |
char * |
| 18 |
strip_lines(const char *input, size_t input_length, const char *prefix) |
| 19 |
{ |
| 20 |
darray_char buf = darray_new(); |
| 21 |
const size_t prefix_len = strlen(prefix); |
| 22 |
const char *start = input; |
| 23 |
const char *end = input + input_length; |
| 24 |
|
| 25 |
const char *next = strstr(start, prefix); |
| 26 |
size_t count; |
| 27 |
while (start < end && next != NULL) { |
| 28 |
count = (size_t)(next - start); |
| 29 |
next = start + count + prefix_len; |
| 30 |
/* Find previous non-space */ |
| 31 |
size_t i; |
| 32 |
for (i = count; i > 0; i--) { |
| 33 |
if (start[i - 1] != ' ' && start[i - 1] != '\t') |
| 34 |
break; |
| 35 |
} |
| 36 |
|
| 37 |
bool dropped = false; |
| 38 |
/* Drop line if only whitespaces */ |
| 39 |
if (i == 0 || start[i - 1] == '\n') { |
| 40 |
count = i; |
| 41 |
dropped = true; |
| 42 |
} |
| 43 |
|
| 44 |
/* Append string */ |
| 45 |
darray_append_items(buf, start, (darray_size_t) count); |
| 46 |
|
| 47 |
/* Find end of line */ |
| 48 |
if (next >= end) { |
| 49 |
start = end; |
| 50 |
break; |
| 51 |
} |
| 52 |
start = strchr(next, 0x0a); |
| 53 |
if (start == NULL) { |
| 54 |
start = end; |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
if (dropped) |
| 59 |
start++; |
| 60 |
|
| 61 |
next = strstr(start, prefix); |
| 62 |
} |
| 63 |
|
| 64 |
/* Append remaining */ |
| 65 |
if (start < end) { |
| 66 |
count = (size_t)(end - start); |
| 67 |
darray_append_items(buf, start, (darray_size_t) count); |
| 68 |
} |
| 69 |
|
| 70 |
darray_append(buf, '\0'); |
| 71 |
return darray_items(buf); |
| 72 |
} |
| 73 |
|
| 74 |
char * |
| 75 |
uncomment(const char *input, size_t input_length, const char *prefix) |
| 76 |
{ |
| 77 |
darray_char buf = darray_new(); |
| 78 |
const size_t prefix_len = strlen(prefix); |
| 79 |
const char *start = input; |
| 80 |
const char *end = input + input_length; |
| 81 |
|
| 82 |
char *next = strstr(start, prefix); |
| 83 |
size_t count; |
| 84 |
while (start < end && next != NULL) { |
| 85 |
count = (size_t)(next - start); |
| 86 |
darray_append_items(buf, start, (darray_size_t) count); |
| 87 |
|
| 88 |
/* Skip prefix */ |
| 89 |
start += count + prefix_len; |
| 90 |
|
| 91 |
/* Find end of line */ |
| 92 |
if (start >= end) |
| 93 |
break; |
| 94 |
next = strchr(start, 0x0a); |
| 95 |
if (next == NULL) |
| 96 |
break; |
| 97 |
next = strstr(next, prefix); |
| 98 |
} |
| 99 |
|
| 100 |
/* Append remaining */ |
| 101 |
if (start < end) { |
| 102 |
count = (size_t)(end - start); |
| 103 |
darray_append_items(buf, start, (darray_size_t) count); |
| 104 |
} |
| 105 |
|
| 106 |
darray_append(buf, '\0'); |
| 107 |
return darray_items(buf); |
| 108 |
} |
| 109 |
|
| 110 |
/* Split string into lines */ |
| 111 |
size_t |
| 112 |
split_lines(const char *input, size_t input_length, |
| 113 |
struct text_line *output, size_t output_length) |
| 114 |
{ |
| 115 |
const char *start = input; |
| 116 |
char *next; |
| 117 |
size_t l; |
| 118 |
size_t i = 0; |
| 119 |
|
| 120 |
for (l = 0; i < input_length && l < output_length && *start != '\0'; l++) { |
| 121 |
/* Look for newline character */ |
| 122 |
next = strchr(start, 0x0a); |
| 123 |
output[l].start = start; |
| 124 |
if (next == NULL) { |
| 125 |
/* Not found: add the rest of the string */ |
| 126 |
output[l++].length = strlen(start); |
| 127 |
break; |
| 128 |
} |
| 129 |
output[l].length = (size_t)(next - start) + 1; |
| 130 |
start = next + 1; |
| 131 |
i += output[l].length; |
| 132 |
} |
| 133 |
return l; |
| 134 |
} |
| 135 |
|
| 136 |
size_t |
| 137 |
concat_lines(struct text_line *lines, size_t length, |
| 138 |
const char *sep, char *output) |
| 139 |
{ |
| 140 |
char *out = output; |
| 141 |
size_t sep_len = strlen(sep); |
| 142 |
for (size_t i = 0; i < length; i++) { |
| 143 |
if (i > 0) { |
| 144 |
memcpy(out, sep, sep_len); |
| 145 |
out += sep_len; |
| 146 |
} |
| 147 |
memcpy(out, lines[i].start, lines[i].length); |
| 148 |
out += lines[i].length; |
| 149 |
} |
| 150 |
*out = '\0'; |
| 151 |
return (size_t)(out - output); |
| 152 |
} |
| 153 |
|
| 154 |
size_t |
| 155 |
shuffle_lines(struct text_line *lines, size_t length, char *output) |
| 156 |
{ |
| 157 |
/* Shuffle lines in-place using Fisher–Yates algorithm. |
| 158 |
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */ |
| 159 |
|
| 160 |
assert(length < RAND_MAX); |
| 161 |
char *out = output; |
| 162 |
if (length > 1) { |
| 163 |
/* 1. Set the current i to the last line. |
| 164 |
* 2. Take a random line j before the current line i. |
| 165 |
* 3. Swap the lines i and j. |
| 166 |
* 4. Append line i to the output. |
| 167 |
* 5. If i is the first line, stop. Else decrease i and go to 2). |
| 168 |
*/ |
| 169 |
for (size_t i = length - 1; i > 0; i--) { |
| 170 |
/* Swap current line with random line before it */ |
| 171 |
size_t j = rand() % (i+1); |
| 172 |
struct text_line tmp = lines[j]; |
| 173 |
lines[j] = lines[i]; |
| 174 |
lines[i] = tmp; |
| 175 |
/* Append current line */ |
| 176 |
memcpy(out, lines[i].start, lines[i].length); |
| 177 |
out += lines[i].length; |
| 178 |
/* Ensure line ends with newline */ |
| 179 |
if (out[-1] != '\n') { |
| 180 |
out[0] = '\n'; |
| 181 |
out++; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
return (size_t)(out - output); |
| 186 |
} |