/* Comme is_any_of(a,b,c), mais avec des strncmp. if (strncmp(a,b,len_b)==0 || strncmp(a,c,len_c)==0 ...){ deviens cosnt char *pt; if (is_any_of_part(a,pt,b,c)){ C'est juste bon pour les chaines L'utilisation des CONST_STR n'est pas concluante. Plus lent. */ #include #include #include #include #include #include "getnow.h" using namespace std; enum class NONEED_T{noneed}; #define NONEED NONEED_T::noneed static constexpr unsigned length(const char *s) { return *s == '\0' ? 0 : 1+length(s+1); } struct CONST_STR{ const char *pt; unsigned len; constexpr CONST_STR(const char *s) :pt(s),len(length(s)){ } }; inline bool is_start_any_of (const char *a, const char *&pt, const CONST_STR &word) { bool ret = false; //printf ("compare %u %s\n",word.len,word.pt); if (strncmp(a,word.pt,word.len)==0){ ret = true; pt = a+word.len; } return ret; } inline bool is_start_any_of (const char *a, const char *&pt, const char *b) { bool ret = false; auto len = strlen(b); if (strncmp(a,b,len)==0){ ret = true; pt = a+len; } return ret; } inline bool is_start_any_of (const std::string &a, const char *&pt, const char *b) { return is_start_any_of(a.c_str(),pt,b); } inline bool is_start_any_of (const char *a, NONEED_T, const char *b) { bool ret = false; auto len = strlen(b); if (strncmp(a,b,len)==0){ ret = true; printf ("pt==NONEED\n"); } return ret; } inline bool is_start_any_of (const std::string &a, NONEED_T no, const char *b) { return is_start_any_of(a.c_str(),no,b); } template bool is_start_any_of(T t, const char *&pt, const char *t1, Ts ... ts){ return is_start_any_of(t,pt,t1) || is_start_any_of(t,pt,ts...); } template bool is_start_any_of(T t, const char *&pt, const T1 &&t1, Ts ... ts){ return is_start_any_of(t,pt,t1) || is_start_any_of(t,pt,ts...); } #if 0 template bool is_start_any_of(T t, const char *&pt, const CONST_STR &&t1, Ts ... ts){ return is_start_any_of(t,pt,t1) || is_start_any_of(t,pt,ts...); } #endif template bool is_start_any_of(T t, NONEED_T no, T1 t1, Ts ... ts){ return is_start_any_of(t,no,t1) || is_start_any_of(t,no,ts...); } template bool is_start_any_of(const std::string &t, const char *&pt, T1 t1, Ts ... ts){ return is_start_any_of(t.c_str(),pt,t1,ts...); } template bool is_start_any_of(const std::string &t, NONEED_T no, T1 t1, Ts ... ts){ return is_start_any_of(t.c_str(),no,t1,ts...); } static unsigned test_old(const char *s, unsigned nbrep) { unsigned count = 0; for (unsigned i=0; i void test(const char *title, unsigned nbrep, const char *s, T t){ long long start = getnow(); unsigned count = t(s,nbrep); long long end = getnow(); showtime (title,start,end); printf ("%s count = %u\n",s,count); } int main (int argc, char *argv[]) { glocal int ret = -1; glocal const char *line = NULL; glocal unsigned nbrep = 1; glocal.ret = (argc,argv); setproginfo ("","0.0","..."); setarg ('l',"line","Ligne à comparer",glocal.line,true); setarg ('n',"nbrep","Nombre de repetition",glocal.nbrep,false); int ret = -1; const char *pt; #if 1 if (is_start_any_of (glocal.line,pt,CONST_STR("allo"),"le","monde","et","toi","comment")){ printf ("CONST_STR Trouve pt=%s\n",pt); }else{ printf ("CONST_STR Trouve pas\n"); } #endif if (is_start_any_of (glocal.line,pt,"allo","le","monde","et","toi","comment")){ printf ("Trouve pt=%s\n",pt); }else{ printf ("Trouve pas\n"); } if (is_start_any_of (glocal.line,NONEED,"allo","le","monde","et","toi","comment")){ printf ("Trouve\n"); }else{ printf ("Trouve pas\n"); } if (is_start_any_of (glocal.line,NONEED,"allo")){ printf ("1 argument Trouve\n"); }else{ printf ("1 argument Trouve pas\n"); } test ("old là",glocal.nbrep,glocal.line,test_old); test ("new là",glocal.nbrep,glocal.line,test_new); test ("new2 là",glocal.nbrep,glocal.line,test_new2); test ("newc là",glocal.nbrep,glocal.line,test_new_const); test ("old pas là",glocal.nbrep,"pâs la",test_old); test ("new pas là",glocal.nbrep,"pas la",test_new); return ret; return glocal.ret; }