1 /* 2 * Created by Phil on 27/11/2013. 3 * Copyright 2013 Two Blue Cubes Ltd. All rights reserved. 4 * 5 * Distributed under the Boost Software License, Version 1.0. (See accompanying 6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 */ 8 #ifndef TWOBLUECUBES_CATCH_COMMON_HPP_INCLUDED 9 #define TWOBLUECUBES_CATCH_COMMON_HPP_INCLUDED 10 11 #include "catch_common.h" 12 13 #include <cstring> 14 #include <cctype> 15 16 namespace Catch { 17 startsWith(std::string const & s,std::string const & prefix)18 bool startsWith( std::string const& s, std::string const& prefix ) { 19 return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); 20 } startsWith(std::string const & s,char prefix)21 bool startsWith( std::string const& s, char prefix ) { 22 return !s.empty() && s[0] == prefix; 23 } endsWith(std::string const & s,std::string const & suffix)24 bool endsWith( std::string const& s, std::string const& suffix ) { 25 return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); 26 } endsWith(std::string const & s,char suffix)27 bool endsWith( std::string const& s, char suffix ) { 28 return !s.empty() && s[s.size()-1] == suffix; 29 } contains(std::string const & s,std::string const & infix)30 bool contains( std::string const& s, std::string const& infix ) { 31 return s.find( infix ) != std::string::npos; 32 } toLowerCh(char c)33 char toLowerCh(char c) { 34 return static_cast<char>( std::tolower( c ) ); 35 } toLowerInPlace(std::string & s)36 void toLowerInPlace( std::string& s ) { 37 std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); 38 } toLower(std::string const & s)39 std::string toLower( std::string const& s ) { 40 std::string lc = s; 41 toLowerInPlace( lc ); 42 return lc; 43 } trim(std::string const & str)44 std::string trim( std::string const& str ) { 45 static char const* whitespaceChars = "\n\r\t "; 46 std::string::size_type start = str.find_first_not_of( whitespaceChars ); 47 std::string::size_type end = str.find_last_not_of( whitespaceChars ); 48 49 return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); 50 } 51 replaceInPlace(std::string & str,std::string const & replaceThis,std::string const & withThis)52 bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { 53 bool replaced = false; 54 std::size_t i = str.find( replaceThis ); 55 while( i != std::string::npos ) { 56 replaced = true; 57 str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); 58 if( i < str.size()-withThis.size() ) 59 i = str.find( replaceThis, i+withThis.size() ); 60 else 61 i = std::string::npos; 62 } 63 return replaced; 64 } 65 pluralise(std::size_t count,std::string const & label)66 pluralise::pluralise( std::size_t count, std::string const& label ) 67 : m_count( count ), 68 m_label( label ) 69 {} 70 operator <<(std::ostream & os,pluralise const & pluraliser)71 std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { 72 os << pluraliser.m_count << ' ' << pluraliser.m_label; 73 if( pluraliser.m_count != 1 ) 74 os << 's'; 75 return os; 76 } 77 SourceLineInfo()78 SourceLineInfo::SourceLineInfo() : file(""), line( 0 ){} SourceLineInfo(char const * _file,std::size_t _line)79 SourceLineInfo::SourceLineInfo( char const* _file, std::size_t _line ) 80 : file( _file ), 81 line( _line ) 82 {} empty() const83 bool SourceLineInfo::empty() const { 84 return file[0] == '\0'; 85 } operator ==(SourceLineInfo const & other) const86 bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const { 87 return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0); 88 } operator <(SourceLineInfo const & other) const89 bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const { 90 return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0)); 91 } 92 seedRng(IConfig const & config)93 void seedRng( IConfig const& config ) { 94 if( config.rngSeed() != 0 ) 95 std::srand( config.rngSeed() ); 96 } rngSeed()97 unsigned int rngSeed() { 98 return getCurrentContext().getConfig()->rngSeed(); 99 } 100 operator <<(std::ostream & os,SourceLineInfo const & info)101 std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { 102 #ifndef __GNUG__ 103 os << info.file << '(' << info.line << ')'; 104 #else 105 os << info.file << ':' << info.line; 106 #endif 107 return os; 108 } 109 throwLogicError(std::string const & message,SourceLineInfo const & locationInfo)110 void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ) { 111 std::ostringstream oss; 112 oss << locationInfo << ": Internal Catch error: '" << message << '\''; 113 if( alwaysTrue() ) 114 throw std::logic_error( oss.str() ); 115 } 116 } 117 118 #endif // TWOBLUECUBES_CATCH_COMMON_HPP_INCLUDED 119 120