From 406e901bf76cc187705da58c58e1a153cc7b0f69 Mon Sep 17 00:00:00 2001 From: Cojocaru Alexandru Date: Mon, 10 Dec 2012 16:42:47 +0100 Subject: [PATCH] cut: split print_kth to avoid extra checks. src/cut.c (print_kth): Split it. Check *only* if a given field or byte is printable. --- src/cut.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/cut.c b/src/cut.c index de9320c..e7bd3bf 100644 --- a/src/cut.c +++ b/src/cut.c @@ -284,29 +284,25 @@ hash_compare_ints (void const *x, void const *y) return (x == y) ? true : false; } +/* Return nonzero if the K'th field or byte is the beginning + of a range. */ + static bool is_range_start_index (size_t i) { return hash_lookup (range_start_ht, (void *) i) ? true : false; } -/* Return nonzero if the K'th field or byte is printable. - When returning nonzero, if RANGE_START is non-NULL, - set *RANGE_START to true if K is the beginning of a range, and to - false otherwise. */ +/* Return nonzero if the K'th field or byte is printable. */ static bool -print_kth (size_t k, bool *range_start) +print_kth (size_t k) { bool k_selected = ((0 < eol_range_start && eol_range_start <= k) || (k <= max_range_endpoint && is_printable_field (k))); - bool is_selected = k_selected ^ complement; - if (range_start && is_selected) - *range_start = is_range_start_index (k); - - return is_selected; + return k_selected ^ complement; } /* Comparison function for qsort to order the list of @@ -571,11 +567,10 @@ cut_bytes (FILE *stream) } else { - bool range_start; - bool *rs = output_delimiter_specified ? &range_start : NULL; - if (print_kth (++byte_idx, rs)) + if (print_kth (++byte_idx)) { - if (rs && *rs && print_delimiter) + if (output_delimiter_specified && print_delimiter && + is_range_start_index (byte_idx)) { fwrite (output_delimiter_string, sizeof (char), output_delimiter_length, stdout); @@ -609,7 +604,7 @@ cut_fields (FILE *stream) and the first field has been selected, or if non-delimited lines must be suppressed and the first field has *not* been selected. That is because a non-delimited line has exactly one field. */ - buffer_first_field = (suppress_non_delimited ^ !print_kth (1, NULL)); + buffer_first_field = (suppress_non_delimited ^ !print_kth (1)); while (1) { @@ -650,7 +645,7 @@ cut_fields (FILE *stream) } continue; } - if (print_kth (1, NULL)) + if (print_kth (1)) { /* Print the field, but not the trailing delimiter. */ fwrite (field_1_buffer, sizeof (char), n_bytes - 1, stdout); @@ -661,7 +656,7 @@ cut_fields (FILE *stream) if (c != EOF) { - if (print_kth (field_idx, NULL)) + if (print_kth (field_idx)) { if (found_any_selected_field) { -- 1.8.0.1