string_helpers.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <cxxomfort/backports.hpp>
  2. #include <cxxomfort/library/type_name.hpp>
  3. #include <iostream>
  4. #include <iomanip>
  5. template <typename StringType, typename PrefixType>
  6. void test_prefix_print(StringType const& str, PrefixType prefix) {
  7. using cxxomfort::string::starts_with;
  8. using cxxomfort::type_name;
  9. //std::cout<< "prefix test with str:"<< type_name<StringType>()<< " prefix:"<< type_name<PrefixType>()<< std::endl;
  10. std::cout << '\'' << str
  11. << "' starts with '" << prefix << "': "
  12. << starts_with(str,prefix) << '\n';
  13. }
  14. template <typename StringType, typename SuffixType>
  15. void test_suffix_print(StringType const& str, SuffixType suffix) {
  16. using cxxomfort::string::ends_with;
  17. using cxxomfort::type_name;
  18. //std::cout<< "prefix test with str:"<< type_name<StringType>()<< " prefix:"<< type_name<SuffixType>()<< std::endl;
  19. std::cout << '\'' << str
  20. << "' ends with '" << suffix << "': "
  21. << ends_with(str,suffix) << '\n';
  22. }
  23. int main() {
  24. using namespace std;
  25. srand(time(0));
  26. std::cout<< std::ios_base::boolalpha;
  27. cout<< "-- Testing substr \n"<< endl;
  28. std::string abecedary = "The quick brown fox jumped over the lazy dog";
  29. const size_t ah = abecedary.size() / 2;
  30. for (unsigned i=0; i < abecedary.size(); i+=2) {
  31. size_t j= 1+ (rand() % ah);
  32. cout<< "substr at "<< setw(2)<< i<< " size "<< j<< " : ";
  33. cout<< "["<< cxxomfort::string::substr(abecedary, i, j)<< "]"<< endl;
  34. }
  35. cout<< endl;
  36. cout<< "-- Testing starts_with, ends_with \n"<< endl;
  37. using cxxomfort::fix::string_helpers::s;
  38. using cxxomfort::fix::string_helpers::sv;
  39. std::string helloWorld = "hello world";
  40. cout<< "std::string: "<< endl;
  41. test_prefix_print(helloWorld, sv("hello"));
  42. test_prefix_print(helloWorld, sv("goodbye"));
  43. test_prefix_print(helloWorld, 'h');
  44. test_prefix_print(helloWorld, 'x');
  45. cout<< endl;
  46. test_suffix_print(helloWorld, sv("world"));
  47. test_suffix_print(helloWorld, sv("goodbye"));
  48. test_suffix_print(helloWorld, 'd');
  49. test_suffix_print(helloWorld, 'x');
  50. cout<< endl;
  51. std::string_view byeWorld = "Bye world!";
  52. cout<< "std::string_view: "<< endl;
  53. test_prefix_print(byeWorld, sv("Bye"));
  54. test_prefix_print(byeWorld, sv("Hai"));
  55. test_prefix_print(byeWorld, 'B');
  56. test_prefix_print(byeWorld, 'C');
  57. cout<< endl;
  58. test_suffix_print(byeWorld, sv("!"));
  59. test_suffix_print(byeWorld, sv("?"));
  60. //size_t pos= helloWorld.find(string("hello"));
  61. //cout<< pos<< endl;
  62. }