catch_string_manip.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Created by Martin on 25/07/2017.
  3. *
  4. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include "catch_string_manip.h"
  8. #include "catch_stringref.h"
  9. #include <algorithm>
  10. #include <ostream>
  11. #include <cstring>
  12. #include <cctype>
  13. #include <vector>
  14. namespace Catch {
  15. namespace {
  16. char toLowerCh(char c) {
  17. return static_cast<char>( std::tolower( static_cast<unsigned char>(c) ) );
  18. }
  19. }
  20. bool startsWith( std::string const& s, std::string const& prefix ) {
  21. return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
  22. }
  23. bool startsWith( std::string const& s, char prefix ) {
  24. return !s.empty() && s[0] == prefix;
  25. }
  26. bool endsWith( std::string const& s, std::string const& suffix ) {
  27. return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
  28. }
  29. bool endsWith( std::string const& s, char suffix ) {
  30. return !s.empty() && s[s.size()-1] == suffix;
  31. }
  32. bool contains( std::string const& s, std::string const& infix ) {
  33. return s.find( infix ) != std::string::npos;
  34. }
  35. void toLowerInPlace( std::string& s ) {
  36. std::transform( s.begin(), s.end(), s.begin(), toLowerCh );
  37. }
  38. std::string toLower( std::string const& s ) {
  39. std::string lc = s;
  40. toLowerInPlace( lc );
  41. return lc;
  42. }
  43. std::string trim( std::string const& str ) {
  44. static char const* whitespaceChars = "\n\r\t ";
  45. std::string::size_type start = str.find_first_not_of( whitespaceChars );
  46. std::string::size_type end = str.find_last_not_of( whitespaceChars );
  47. return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string();
  48. }
  49. StringRef trim(StringRef ref) {
  50. const auto is_ws = [](char c) {
  51. return c == ' ' || c == '\t' || c == '\n' || c == '\r';
  52. };
  53. size_t real_begin = 0;
  54. while (real_begin < ref.size() && is_ws(ref[real_begin])) { ++real_begin; }
  55. size_t real_end = ref.size();
  56. while (real_end > real_begin && is_ws(ref[real_end - 1])) { --real_end; }
  57. return ref.substr(real_begin, real_end - real_begin);
  58. }
  59. bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
  60. bool replaced = false;
  61. std::size_t i = str.find( replaceThis );
  62. while( i != std::string::npos ) {
  63. replaced = true;
  64. str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
  65. if( i < str.size()-withThis.size() )
  66. i = str.find( replaceThis, i+withThis.size() );
  67. else
  68. i = std::string::npos;
  69. }
  70. return replaced;
  71. }
  72. std::vector<StringRef> splitStringRef( StringRef str, char delimiter ) {
  73. std::vector<StringRef> subStrings;
  74. std::size_t start = 0;
  75. for(std::size_t pos = 0; pos < str.size(); ++pos ) {
  76. if( str[pos] == delimiter ) {
  77. if( pos - start > 1 )
  78. subStrings.push_back( str.substr( start, pos-start ) );
  79. start = pos+1;
  80. }
  81. }
  82. if( start < str.size() )
  83. subStrings.push_back( str.substr( start, str.size()-start ) );
  84. return subStrings;
  85. }
  86. pluralise::pluralise( std::size_t count, std::string const& label )
  87. : m_count( count ),
  88. m_label( label )
  89. {}
  90. std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) {
  91. os << pluraliser.m_count << ' ' << pluraliser.m_label;
  92. if( pluraliser.m_count != 1 )
  93. os << 's';
  94. return os;
  95. }
  96. }