diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h index c6e4213d0a22fe200e227517a7dbbe4c2533ad65..af122ab1e00be193a8adfdb31f53cc0ef05c7029 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h @@ -151,6 +151,9 @@ MANTID_KERNEL_DLL size_t split_path(const std::string &path, std::vector<std::st /// Loads the entire contents of a text file into a string MANTID_KERNEL_DLL std::string loadFile(const std::string & filename); +/// checks if the candidate is the member of the group +MANTID_KERNEL_DLL int isMember(const std::vector<std::string> &group, const std::string &candidate); + } // NAMESPACE Strings } // NAMESPACE Kernel diff --git a/Code/Mantid/Framework/Kernel/src/Strings.cpp b/Code/Mantid/Framework/Kernel/src/Strings.cpp index 669a15afae027f9c06464503689c6085dac678d8..9815b0efb02be53a7ebbcf69618a654fb11200d7 100644 --- a/Code/Mantid/Framework/Kernel/src/Strings.cpp +++ b/Code/Mantid/Framework/Kernel/src/Strings.cpp @@ -962,6 +962,25 @@ size_t split_path(const std::string &path, std::vector<std::string> &path_compon path_components.resize(n_folders); return n_folders; } +/** function checks if the candidate is the member of the group + * @param group -- vector of string to check + * @param candidate -- the string which has to be checked against the group + + * @returns -- number of the candidate in the input vector of strings if the candidate belongs to the group + or -1 if it does not. + Returns the number of the first maching entry in the group if there are duplicated entries in the group +*/ +int isMember(const std::vector<std::string> &group, const std::string &candidate) +{ + int num(-1); + for(size_t i=0;i<group.size();i++){ + if(candidate.compare(group[i])==0){ + num = int(i); + return num; + } + } + return num; +} /// \cond TEMPLATE diff --git a/Code/Mantid/Framework/Kernel/test/StringsTest.h b/Code/Mantid/Framework/Kernel/test/StringsTest.h index fd06a39146615b108365795b4386ad4892011926..f04ef08b367070d57ec62f514c988ce975044362 100644 --- a/Code/Mantid/Framework/Kernel/test/StringsTest.h +++ b/Code/Mantid/Framework/Kernel/test/StringsTest.h @@ -216,6 +216,21 @@ public: } + void test_isMember() + { + std::vector<std::string> group(5,""); + group[0]="A"; + group[1]="A1"; + group[2]="B0"; + group[3]="C"; + + TS_ASSERT_EQUALS(1,isMember(group,"A1")); + TS_ASSERT_EQUALS(-1,isMember(group," ")); + TS_ASSERT_EQUALS(-1,isMember(group,"nothing")); + TS_ASSERT_EQUALS(0,isMember(group,"A")); + TS_ASSERT_EQUALS(3,isMember(group,"C")); + } + };