Skip to content

Commit

Permalink
Increased functionality of '+'
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown authored and Unknown committed Oct 21, 2017
1 parent 5a449ba commit 808022e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
Binary file modified rgrep
Binary file not shown.
64 changes: 59 additions & 5 deletions rgrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ int firstOccur(char* line, char c);
int checkSpecial(char* pattern);
int sameChar(char* pattern);
int dotPlus(char* pattern);

int matchPatternDots(char* pattern,char* line);
int patternPlus(char* pattern,char* line);
/**
* You can use this recommended helper function
* Returns true if partial_line matches pattern, starting from
Expand Down Expand Up @@ -55,14 +56,20 @@ int rgrep_matches(char *line, char *pattern){

else if (dotPlus(pattern))
return 1;
else if (matchPatternDots(pattern, line))
return 1;
else
return 0;
case '\\' :
return 2;
return 0;
case '+' :
return 3;
if(n==1)
return 1;
if(patternPlus(pattern, line))
return 1;
return 0;
case '\?' :
return 4;
return 0;
default :
return 0;
}
Expand Down Expand Up @@ -134,6 +141,53 @@ int main(int argc, char **argv) {
return 0;
}

int patternPlus(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int p = firstOccur(pattern, '+');
int count = 0;
for(int j = 0 ; j < p - 1; j++){
if(line[j] == pattern[j])
count++;



}
//printf("count: %d, line: %s\n", count, line);
for(int i = 0; i< m; i++){
if(line[i] == pattern[p-1] && n <= 2){
return 1;
}
if(pattern[p-1] == line[i]){
count++;
break;
}
}


//printf("count: %d,n: %d, line: %s\n", count,n, line);
if(count == n-1)
return 1;
return 0;
}


//Checks for matchs to dot-pattern
int matchPatternDots(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int count = 0;
for(int i = 0; i< m; i++){
if(line[i] == '.' || pattern[i] == line[i]){
count++;
}
if(count == n)
return 1;
}
return 0;

}


//Checks for a plus after dots
int dotPlus(char* pattern){
Expand Down Expand Up @@ -181,7 +235,7 @@ int firstOccur(char * line, char c){
}
return 5000;
}
/*Checks for any special characters not proceeded by a \(escape char)
/*Checks for the first special character not proceeded by a \(escape char)
returns index of special char or returns 5000
. = any char
+ = preceeding will appear any # of times
Expand Down
41 changes: 1 addition & 40 deletions test.txt
Original file line number Diff line number Diff line change
@@ -1,42 +1,3 @@
Build test cases for all these in test.txt
Usage:
a+ Matches a, aa, aaaaa or really any number of a’s more than one
.+ Matches any non-empty String
\\+ Matches a string of \’s
a?b+ Matches ab, b, abbb or any amount of b following op:onal a.
\? A question mark must appear in the line
they?re Matches a line that contains either the string "theyre" or the string "there"
h.d..?n Matches lines that contain substrings like "hidden", "hidin", "hbdwen", "hadbn", etc.
cu\.?t Matches lines that either contain the substring "cut" or "cu.t"

aa
aah
aahed
aahing
aahs
aardwolf
aardwolves
aas
aasvogel
aasvogels
abaci
aback
abacus
abacuses
zyme
zymogens
zymologies
zymurgies
zymurgy
zzaabb
sF1xxO?
t.bO?T1
a2\W4pH
DTJg2gQ
qkp9H9M
TMLBIPV
Ih?Lutl
bB0hy1w
jYed9FK
qQqMfDl
.?as..\?
zymurgies

0 comments on commit 808022e

Please sign in to comment.