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 8 #include "catch_string_manip.h" 9 10 #include <algorithm> 11 #include <ostream> 12 #include <cstring> 13 #include <cctype> 14 15 namespace Catch { 16 startsWith(std::string const & s,std::string const & prefix)17 bool startsWith( std::string const& s, std::string const& prefix ) { 18 return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); 19 } startsWith(std::string const & s,char prefix)20 bool startsWith( std::string const& s, char prefix ) { 21 return !s.empty() && s[0] == prefix; 22 } endsWith(std::string const & s,std::string const & suffix)23 bool endsWith( std::string const& s, std::string const& suffix ) { 24 return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); 25 } endsWith(std::string const & s,char suffix)26 bool endsWith( std::string const& s, char suffix ) { 27 return !s.empty() && s[s.size()-1] == suffix; 28 } contains(std::string const & s,std::string const & infix)29 bool contains( std::string const& s, std::string const& infix ) { 30 return s.find( infix ) != std::string::npos; 31 } toLowerCh(char c)32 char toLowerCh(char c) { 33 return static_cast<char>( std::tolower( c ) ); 34 } toLowerInPlace(std::string & s)35 void toLowerInPlace( std::string& s ) { 36 std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); 37 } toLower(std::string const & s)38 std::string toLower( std::string const& s ) { 39 std::string lc = s; 40 toLowerInPlace( lc ); 41 return lc; 42 } trim(std::string const & str)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 48 return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); 49 } 50 replaceInPlace(std::string & str,std::string const & replaceThis,std::string const & withThis)51 bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { 52 bool replaced = false; 53 std::size_t i = str.find( replaceThis ); 54 while( i != std::string::npos ) { 55 replaced = true; 56 str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); 57 if( i < str.size()-withThis.size() ) 58 i = str.find( replaceThis, i+withThis.size() ); 59 else 60 i = std::string::npos; 61 } 62 return replaced; 63 } 64 pluralise(std::size_t count,std::string const & label)65 pluralise::pluralise( std::size_t count, std::string const& label ) 66 : m_count( count ), 67 m_label( label ) 68 {} 69 operator <<(std::ostream & os,pluralise const & pluraliser)70 std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { 71 os << pluraliser.m_count << ' ' << pluraliser.m_label; 72 if( pluraliser.m_count != 1 ) 73 os << 's'; 74 return os; 75 } 76 77 } 78