| 1 |
|
| /* This code is provided by bliako and is heavily based on the |
| 2 |
|
| original main.cpp supplied with the C++ library |
| 3 |
|
| */ |
| 4 |
|
| |
| 5 |
|
| #include <iostream> |
| 6 |
|
| #include <fstream> |
| 7 |
|
| #include <string> |
| 8 |
|
| #include <cstdlib> |
| 9 |
|
| #include <cctype> |
| 10 |
|
| |
| 11 |
|
| #include <string.h> |
| 12 |
|
| |
| 13 |
|
| #include "harness.h" |
| 14 |
|
| #include "regxstring.h" |
| 15 |
|
| |
| 16 |
|
| #define DEBUG 1 |
| 17 |
|
| |
| 18 |
181 |
| static std::string Trim(std::string str){ |
| 19 |
|
| #if DEBUG==1 |
| 20 |
181 |
| std::cerr << "Trim: entering with '" << str << "'." << std::endl; |
| 21 |
|
| #endif |
| 22 |
181 |
| size_t i = 0,e = str.length(); |
| 23 |
181 |
▶ | for(;i < e && std::isspace(str[i]);++i); |
Branch: 3 branches
| Branch | True | False | Total |
| for(;i < e && std::isspace(str[i]);++i); |
181 |
0 |
181 |
| for(;i < e && std::isspace(str[i]);++i); |
0 |
181 |
181 |
| for(;i < e && std::isspace(str[i]);++i); |
0 |
181 |
181 |
|
| 24 |
181 |
| size_t j = e; |
| 25 |
181 |
▶ | for(;j > i && std::isspace(str[j - 1]);--j); |
Branch: 3 branches
| Branch | True | False | Total |
| for(;j > i && std::isspace(str[j - 1]);--j); |
181 |
0 |
181 |
| for(;j > i && std::isspace(str[j - 1]);--j); |
0 |
181 |
181 |
| for(;j > i && std::isspace(str[j - 1]);--j); |
0 |
181 |
181 |
|
| 26 |
|
| #if DEBUG==1 |
| 27 |
181 |
▶ | std::string ret = i < j ? str.substr(i,j - i) : ""; |
Branch: 5 branches
| Branch | True | False | Total |
| std::string ret = i < j ? str.substr(i,j - i) : ""; |
181 |
0 |
181 |
| std::string ret = i < j ? str.substr(i,j - i) : ""; |
181 |
0 |
181 |
| std::string ret = i < j ? str.substr(i,j - i) : ""; |
0 |
0 |
0 |
| std::string ret = i < j ? str.substr(i,j - i) : ""; |
0 |
181 |
181 |
| std::string ret = i < j ? str.substr(i,j - i) : ""; |
0 |
0 |
0 |
|
| 28 |
181 |
▶ | std::cerr << "Trim: leaving with '" << ret << "'." << std::endl; |
Branch: 4 branches
| Branch | True | False | Total |
| std::cerr << "Trim: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "Trim: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "Trim: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "Trim: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
|
| 29 |
181 |
| return ret; |
| 30 |
|
| #else |
| 31 |
|
| return (i < j ? str.substr(i,j - i) : ""); |
| 32 |
|
| #endif |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
181 |
| static std::string pre_handle( |
| 36 |
|
| const std::string & str |
| 37 |
|
| ){ |
| 38 |
|
| #if DEBUG==1 |
| 39 |
181 |
| std::cerr << "pre_handle: entering with '" << str << "'." << std::endl; |
| 40 |
|
| #endif |
| 41 |
181 |
▶ | std::string ret = Trim(str); |
Branch: 2 branches
| Branch | True | False | Total |
| std::string ret = Trim(str); |
181 |
0 |
181 |
| std::string ret = Trim(str); |
181 |
0 |
181 |
|
| 42 |
181 |
▶ | if(!ret.empty()){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!ret.empty()){ |
181 |
0 |
181 |
|
| 43 |
181 |
▶ | if(ret[0] != '^') |
Branch: 2 branches
| Branch | True | False | Total |
| if(ret[0] != '^') |
181 |
0 |
181 |
| if(ret[0] != '^') |
4 |
177 |
181 |
|
| 44 |
4 |
▶ | ret.insert(ret.begin(),'^'); |
Branch: 1 branch
| Branch | True | False | Total |
| ret.insert(ret.begin(),'^'); |
4 |
0 |
4 |
|
| 45 |
181 |
▶ | if(ret[ret.size() - 1] != '$') |
Branch: 2 branches
| Branch | True | False | Total |
| if(ret[ret.size() - 1] != '$') |
181 |
0 |
181 |
| if(ret[ret.size() - 1] != '$') |
181 |
0 |
181 |
|
| 46 |
181 |
▶ | ret.push_back('$'); |
Branch: 1 branch
| Branch | True | False | Total |
| ret.push_back('$'); |
181 |
0 |
181 |
|
| 47 |
|
| } |
| 48 |
|
| #if DEBUG==1 |
| 49 |
181 |
▶ | std::cerr << "pre_handle: leaving with '" << ret << "'." << std::endl; |
Branch: 4 branches
| Branch | True | False | Total |
| std::cerr << "pre_handle: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "pre_handle: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "pre_handle: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "pre_handle: leaving with '" << ret << "'." << std::endl; |
181 |
0 |
181 |
|
| 50 |
|
| #endif |
| 51 |
181 |
| return ret; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| /* Given the number of random strings to produce and a regex string |
| 55 |
|
| it will return the array of random strings (as char**). |
| 56 |
|
| Caller must free the returned array |
| 57 |
|
| */ |
| 58 |
181 |
| char ** regxstring_generate_random_strings_from_regex( |
| 59 |
|
| const char *regx,/* the regex */ |
| 60 |
|
| int N, /* number of strings to produce */ |
| 61 |
|
| int debug /* set to 1 for debugging */ |
| 62 |
|
| ){ |
| 63 |
|
| #if DEBUG==1 |
| 64 |
362 |
▶ | std::cerr << "regxstring_generate_random_strings_from_regex: entering with '" << std::string(regx) << "'." << std::endl; |
Branch: 5 branches
| Branch | True | False | Total |
| std::cerr << "regxstring_generate_random_strings_from_regex: entering with '" << std::string(regx) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: entering with '" << std::string(regx) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: entering with '" << std::string(regx) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: entering with '" << std::string(regx) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: entering with '" << std::string(regx) << "'." << std::endl; |
181 |
0 |
181 |
|
| 65 |
|
| #endif |
| 66 |
181 |
| CRegxString regxstr; |
| 67 |
181 |
▶ | regxstr.ParseRegx(pre_handle(std::string(regx)).c_str()); |
Branch: 3 branches
| Branch | True | False | Total |
| regxstr.ParseRegx(pre_handle(std::string(regx)).c_str()); |
181 |
0 |
181 |
| regxstr.ParseRegx(pre_handle(std::string(regx)).c_str()); |
181 |
0 |
181 |
| regxstr.ParseRegx(pre_handle(std::string(regx)).c_str()); |
181 |
0 |
181 |
|
| 68 |
|
| |
| 69 |
181 |
▶ | if(debug>0) regxstr.Debug(std::cerr); |
Branch: 2 branches
| Branch | True | False | Total |
| if(debug>0) regxstr.Debug(std::cerr); |
177 |
4 |
181 |
| if(debug>0) regxstr.Debug(std::cerr); |
177 |
0 |
177 |
|
| 70 |
|
| |
| 71 |
|
| char **ret, *astring; |
| 72 |
|
| |
| 73 |
181 |
▶ | if( (ret=(char **)malloc(N*sizeof(char *))) == NULL ){ std::cerr << "regxstring_generate_random_strings_from_regex() : error, failed to allocate memory for " << N << " random strings (to return)."; return (char **)NULL; } |
Branch: 4 branches
| Branch | True | False | Total |
| if( (ret=(char **)malloc(N*sizeof(char *))) == NULL ){ std::cerr << "regxstring_generate_random_strings_from_regex() : error, failed to allocate memory for " << N << " random strings (to return)."; return (char **)NULL; } |
0 |
181 |
181 |
| if( (ret=(char **)malloc(N*sizeof(char *))) == NULL ){ std::cerr << "regxstring_generate_random_strings_from_regex() : error, failed to allocate memory for " << N << " random strings (to return)."; return (char **)NULL; } |
0 |
0 |
0 |
| if( (ret=(char **)malloc(N*sizeof(char *))) == NULL ){ std::cerr << "regxstring_generate_random_strings_from_regex() : error, failed to allocate memory for " << N << " random strings (to return)."; return (char **)NULL; } |
0 |
0 |
0 |
| if( (ret=(char **)malloc(N*sizeof(char *))) == NULL ){ std::cerr << "regxstring_generate_random_strings_from_regex() : error, failed to allocate memory for " << N << " random strings (to return)."; return (char **)NULL; } |
0 |
0 |
0 |
|
| 74 |
|
| |
| 75 |
17788 |
▶ | for(int i = 0;i < N;++i){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i = 0;i < N;++i){ |
17607 |
181 |
17788 |
|
| 76 |
17607 |
▶ | const char *rs = regxstr.RandString(); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *rs = regxstr.RandString(); |
17607 |
0 |
17607 |
|
| 77 |
17607 |
▶ | if( rs != NULL ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( rs != NULL ){ |
17607 |
0 |
17607 |
|
| 78 |
17607 |
| ret[i] = strdup(rs); |
| 79 |
|
| } else { |
| 80 |
0 |
| ret[i] = (char *)malloc(sizeof(char)); |
| 81 |
0 |
| *(ret[i]) = 0; |
| 82 |
|
| } |
| 83 |
|
| } |
| 84 |
|
| #if DEBUG==1 |
| 85 |
362 |
▶ | std::cerr << "regxstring_generate_random_strings_from_regex: leaving with 1st string as '" << std::string(ret[0]) << "'." << std::endl; |
Branch: 5 branches
| Branch | True | False | Total |
| std::cerr << "regxstring_generate_random_strings_from_regex: leaving with 1st string as '" << std::string(ret[0]) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: leaving with 1st string as '" << std::string(ret[0]) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: leaving with 1st string as '" << std::string(ret[0]) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: leaving with 1st string as '" << std::string(ret[0]) << "'." << std::endl; |
181 |
0 |
181 |
| std::cerr << "regxstring_generate_random_strings_from_regex: leaving with 1st string as '" << std::string(ret[0]) << "'." << std::endl; |
181 |
0 |
181 |
|
| 86 |
|
| #endif |
| 87 |
181 |
| return ret; |
| 88 |
181 |
| } |