From 71063bc858cd927e3622b511297e66b3e13f7453 Mon Sep 17 00:00:00 2001 From: Dylan Cali Date: Fri, 5 Sep 2014 04:42:02 -0500 Subject: numfmt: implement support for field ranges * src/numfmt.c: Replace field handling code with logic that understands field range specifiers. Instead of processing a single field and printing line prefix/suffix around it, process each field in the line checking whether it has been included for conversion. If so convert and print, otherwise just print the unaltered field. (extract_fields): Removed. (skip_fields): Removed. (process_line): Gutted and heavily reworked. (process_suffixed_number): FIELD is now passed as an arg instead of using a global. (parse_field_arg): New function that parses field range specifiers. (next_field): New function that returns pointers to the next field in a line. (process_field): New function that wraps the field conversion logic (include_field): New function that checks whether a field should be converted (compare_field): New function used for field value comparisons in a gl_list. (free_field): New function used for freeing field values in a gl_list. Global variable FIELD removed. New global variable all_fields indicates whether all fields should be processed. New global variable all_fields_after stores the first field of a N- style range. New global variable all_fields_before stores the last field of a -M style range. New global variable field_list stores explicitly specified fields to process (N N,M or N-M style specifiers). (usage): Document newly supported field range specifiers. * bootstrap.conf: Include xlist and linked-list modules. numfmt now uses the gl_linked_list implementation to store the field ranges. * tests/misc/numfmt.pl: Add tests for 'cut style' field ranges. Adjust existing tests as partial output can occur before an error Remove test for the 'invalid' field -5.. this is now a valid range. * gnulib: update to avoid compiler warnings in linked-list. * NEWS: Mention the new feature. --- tests/misc/numfmt.pl | 54 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'tests/misc') diff --git a/tests/misc/numfmt.pl b/tests/misc/numfmt.pl index e8640c0f7..630d18707 100755 --- a/tests/misc/numfmt.pl +++ b/tests/misc/numfmt.pl @@ -194,21 +194,16 @@ my @Tests = ['delim-3', '--delimiter=" " --from=auto "40M Foo"',{OUT=>'40000000 Foo'}], ['delim-4', '--delimiter=: --from=auto 40M:60M', {OUT=>'40000000:60M'}], ['delim-5', '-d: --field=2 --from=auto :40M:60M', {OUT=>':40000000:60M'}], - ['delim-6', '--delimiter=: --field 3 --from=auto 40M:60M', - {EXIT=>2}, - {ERR=>"$prog: input line is too short, no numbers found " . - "to convert in field 3\n"}], + ['delim-6', '-d: --field 3 --from=auto 40M:60M', {OUT=>"40M:60M"}], #Fields ['field-1', '--field A', {ERR => "$prog: invalid field value 'A'\n"}, {EXIT => '1'}], - ['field-1.1', '--field -5', - {ERR => "$prog: invalid field value '-5'\n"}, - {EXIT => '1'}], ['field-2', '--field 2 --from=auto "Hello 40M World 90G"', {OUT=>'Hello 40000000 World 90G'}], ['field-3', '--field 3 --from=auto "Hello 40M World 90G"', + {OUT=>"Hello 40M "}, {ERR=>"$prog: invalid number: 'World'\n"}, {EXIT => 2},], # Last field - no text after number @@ -223,10 +218,32 @@ my @Tests = {OUT=>"Hello:40000000:World:90G"}], # not enough fields - ['field-8', '--field 3 --to=si "Hello World"', - {EXIT=>2}, - {ERR=>"$prog: input line is too short, no numbers found " . - "to convert in field 3\n"}], + ['field-8', '--field 3 --to=si "Hello World"', {OUT=>"Hello World"}], + + # Multiple fields + ['field-range-1', '--field 2,4 --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1000 2.0K 3000 4.0K 5000"}], + + ['field-range-2', '--field 2-4 --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1000 2.0K 3.0K 4.0K 5000"}], + + ['field-range-3', '--field 1,2,3-5 --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}], + + ['field-range-4', '--field 1-5 --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}], + + ['field-range-5', '--field 1-3,5 --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1.0K 2.0K 3.0K 4000 5.0K"}], + + ['field-range-6', '--field 3- --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1000 2000 3.0K 4.0K 5.0K"}], + + ['field-range-7', '--field -3 --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1.0K 2.0K 3.0K 4000 5000"}], + + ['all-fields-1', '--field=- --to=si "1000 2000 3000 4000 5000"', + {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}], # Auto-consume white-space, setup auto-padding ['whitespace-1', '--to=si --field 2 "A 500 B"', {OUT=>"A 500 B"}], @@ -679,9 +696,6 @@ my @Tests = ['devdebug-11', '---debug --format "%\'-10f" 10000',{OUT=>"10000 "}, {ERR=>""}, {ERR_SUBST=>"s/.*//msg"}], - ['devdebug-12', '---debug --field 2 A',{OUT=>""}, - {ERR=>""}, {EXIT=>2}, - {ERR_SUBST=>"s/.*//msg"}], # Invalid parameters ['help-1', '--foobar', @@ -787,11 +801,6 @@ my @Tests = {ERR => "$prog: invalid number: 'World'\n"}, {OUT => "Hello 40M World 90G\n"}, {EXIT => 2}], - ['ign-err-6', '--invalid=fail --field 3 --to=si "Hello World"', - {ERR => "$prog: input line is too short, no numbers found " . - "to convert in field 3\n"}, - {OUT => "Hello World\n"}, - {EXIT => 2}], ['ign-err-7', '--invalid=fail --from=si "foo"', {ERR => "$prog: invalid number: 'foo'\n"}, {OUT => "foo\n"}, @@ -855,13 +864,6 @@ my @Tests = {OUT => "A 1000 x\nB Foo y\nC 2.8G z\n"}, {ERR => "$prog: invalid number: 'Foo'\n"}, {EXIT => 2}], - # one of the lines is too short - ['ign-err-m3.2', '--invalid=fail --field 2 --from=si --to=iec', - {IN_PIPE => "A 1K x\nB\nC 3G z\n"}, - {OUT => "A 1000 x\nB\nC 2.8G z\n"}, - {ERR => "$prog: input line is too short, no numbers found " . - "to convert in field 2\n"}, - {EXIT => 2}], ); my @Locale_Tests = -- cgit v1.2.3-70-g09d2