Skip to content

Commit

Permalink
handle situation where a delimiter cannot be found in input, and we m…
Browse files Browse the repository at this point in the history
…ust use the one supplied on the command line
  • Loading branch information
ColumPaget committed Feb 21, 2018
1 parent 48b7bc9 commit 35fcdd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
26 changes: 11 additions & 15 deletions ccut.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Copyright (c) 2015 Colum Paget <[email protected]>
//UTF-8 chars always have the top bit (128) set
#define isUTF8ch(ch) (((ch) & 128) ? 1 : 0)

char *Version="2.10";
char *Version="2.11";


char *Delim=NULL, *OutputDelim=NULL, *OutPath=NULL, *FieldSpec=NULL;
Expand Down Expand Up @@ -517,7 +517,13 @@ return(fcount);




void OutputDelimiter(char FoundDelim)
{
if (Flags & FLAG_REPLACE_DELIM) fputs(OutputDelim, stdout);
else if (Flags & FLAG_DELIMSTR) fputs(Delim, stdout);
else if (FoundDelim !=0) fputc(FoundDelim, stdout);
else if (StrValid(Delim)) fputc(*Delim, stdout);
}


void OutputField(const char *start, const char *end, int DelimFlags)
Expand Down Expand Up @@ -570,12 +576,7 @@ if (start)
else
{
//if we're outputing fields in reverse order than we copy a delimiter to the start
if (DelimFlags & DELIM_PREFIX)
{
if (Flags & FLAG_REPLACE_DELIM) fputs(OutputDelim, stdout);
else if (Flags & FLAG_DELIMSTR) fputs(Delim, stdout);
else fputc(delim, stdout);
}
if (DelimFlags & DELIM_PREFIX) OutputDelimiter(delim);

//messy calcluation. We returned the string including delimiter. We can output this by writing end-start bytes
//but this clips the delimiter off. If we have a string rather than a character as the delimiter though, then it
Expand All @@ -589,16 +590,11 @@ if (Flags & FLAG_DELIMSTR)
else fwrite(start,ptr-start,1,stdout);

//if we're outputing fields in normal order than we copy a delimiter to the end
if (DelimFlags & DELIM_POSTFIX)
{
if (Flags & FLAG_REPLACE_DELIM) fputs(OutputDelim, stdout);
else if (Flags & FLAG_DELIMSTR) fputs(Delim, stdout);
else fputc(delim, stdout);
}
if (DelimFlags & DELIM_POSTFIX) OutputDelimiter(delim);
}
}
//Use the cached 'last delim' if the field doesn't exist
else if (DelimFlags) fputs(OutputDelim, stdout);
else if (DelimFlags) OutputDelimiter(0);

OutputNo++;
}
Expand Down
8 changes: 4 additions & 4 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ TestCut "-Q -d ',' -f 3,6" tests/cut.5 "field3,field6" "Cut honoring quotes but
TestCut "-d , -j -f 3" tests/cut.3 "field3" "Cut combining runs of the same delimiter"
TestCut "-d ,;: -j -f 6" tests/cut.3 "field6" "Cut combining runs of different delimiters"
TestCut "-D ,, -f 2" tests/cut.3 ",field3,field4" "Cut using a string as a delimiter rather than a single char"
TestCut "-Q -d ',' -f 2,3,20" tests/cut.5 "field2,field3," "Cut with a non-existent field at the end"
TestCut "-Q -d ',' -f 2,20,3" tests/cut.5 "field2,,field3" "Cut with a non-existent field in the middle"
TestCut "-Q -d ',' -f 20,2,3" tests/cut.5 ",field2,field3" "Cut with a non-existent field at the start"
TestCut "-Q -d ',' -f 2,3,10-15" tests/cut.5 "field2,field3,,,,,," "Cut with a range of non-existent fields"
TestCut "-Q -d , -f 2,3,20" tests/cut.5 "field2,field3," "Cut with a non-existent field at the end"
TestCut "-Q -d , -f 2,20,3" tests/cut.5 "field2,,field3" "Cut with a non-existent field in the middle"
TestCut "-Q -d , -f 20,2,3" tests/cut.5 ",field2,field3" "Cut with a non-existent field at the start"
TestCut "-Q -d , -f 2,3,10-15" tests/cut.5 "field2,field3,,,,,," "Cut with a range of non-existent fields"

TestCut "-c 80-95 --utf8" tests/utf8.txt "Congress‘ infras" "Cut UTF-8 input"

Expand Down

0 comments on commit 35fcdd9

Please sign in to comment.