diff --git a/src/cut.c b/src/cut.c
index 9c331f6..296c8d8 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -42,6 +42,7 @@
#define AUTHORS "David Ihnat", "David MacKenzie", "Jim Meyering"
+
#define FATAL_ERROR(Message) \
do \
{ \
@@ -163,6 +164,7 @@ enum
COMPLEMENT_OPTION
};
+// Complement is a new option
static struct option const longopts[] =
{
{"bytes", required_argument, 0, 'b'},
@@ -237,6 +239,7 @@ With no FILE, or when FILE is -, read standard input.\n\
exit (status);
}
+// New function
static inline void
mark_range_start (size_t i)
{
@@ -299,13 +302,18 @@ print_kth (size_t k, bool *range_start)
= ((0 < eol_range_start && eol_range_start <= k)
|| (k <= max_range_endpoint && is_printable_field (k)));
+ bool old_return_val = k_selected;
+
bool is_selected = k_selected ^ complement;
- if (range_start && is_selected)
+ // if (range_start && change(k_selected, is_selected)) {
+ if (change(k_selected && range_start, range_start && is_selected)) {
*range_start = is_range_start_index (k);
+ }
- return is_selected;
+ return change(old_return_val, is_selected);
}
+// New function
/* Comparison function for qsort to order the list of
struct range_pairs. */
static int
@@ -494,9 +502,58 @@ set_fields (const char *fieldstr)
printable_field = xzalloc (max_range_endpoint / CHAR_BIT + 1);
- qsort (rp, n_rp, sizeof (rp[0]), compare_ranges);
+ if (change(0, 1)) {
+ qsort (rp, n_rp, sizeof (rp[0]), compare_ranges);
+ }
/* Set the array entries corresponding to integers in the ranges of RP. */
+
+ if (change(1, 0)) {
+ for (i = 0; i < n_rp; i++)
+ {
+ size_t j;
+ for (j = rp[i].lo; j <= rp[i].hi; j++)
+ {
+ mark_printable_field (j);
+ }
+ }
+
+ if (output_delimiter_specified)
+ {
+ /* Record the range-start indices, i.e., record each start
+ index that is not part of any other (lo..hi] range. */
+ for (i = 0; i <= n_rp; i++)
+ {
+ size_t j;
+ size_t rsi = (i < n_rp ? rp[i].lo : eol_range_start);
+
+ for (j = 0; j < n_rp; j++)
+ {
+ if (rp[j].lo < rsi && rsi <= rp[j].hi)
+ {
+ rsi = 0;
+ break;
+ }
+ }
+
+ if (eol_range_start && eol_range_start < rsi)
+ rsi = 0;
+
+ if (rsi)
+ {
+ /* Record the fact that `rsi' is a range-start index. */
+ void *ent_from_table = hash_insert (range_start_ht, (void*) rsi);
+ if (ent_from_table == NULL)
+ {
+ /* Insertion failed due to lack of memory. */
+ xalloc_die ();
+ }
+ assert ((size_t) ent_from_table == rsi);
+ }
+ }
+ }
+ } else {
+
for (i = 0; i < n_rp; i++)
{
size_t j;
@@ -518,6 +575,8 @@ set_fields (const char *fieldstr)
&& eol_range_start && !is_printable_field (eol_range_start))
mark_range_start (eol_range_start);
+ }
+
free (rp);
return field_found;
@@ -812,6 +871,7 @@ main (int argc, char **argv)
break;
case COMPLEMENT_OPTION:
+ // New variable but also always used in new blocks of code so no need to model with change
complement = true;
break;