diff --git a/CMakeLists.txt b/CMakeLists.txt index d1edd83e..e5ce481e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,6 +173,7 @@ set(INTERNAL_HEADERS ${HEADER_DIR}/internal/catch_stringbuilder.h ${HEADER_DIR}/internal/catch_stringdata.h ${HEADER_DIR}/internal/catch_stringref.h + ${HEADER_DIR}/internal/catch_string_manip.h ${HEADER_DIR}/internal/catch_suppress_warnings.h ${HEADER_DIR}/internal/catch_tag_alias.h ${HEADER_DIR}/internal/catch_tag_alias_registry.h @@ -220,6 +221,7 @@ set(IMPL_SOURCES ${HEADER_DIR}/internal/catch_stringbuilder.cpp ${HEADER_DIR}/internal/catch_stringdata.cpp ${HEADER_DIR}/internal/catch_stringref.cpp + ${HEADER_DIR}/internal/catch_string_manip.cpp ${HEADER_DIR}/internal/catch_tag_alias_registry.cpp ${HEADER_DIR}/internal/catch_test_case_info.cpp ${HEADER_DIR}/internal/catch_test_case_tracker.cpp diff --git a/include/internal/catch_commandline.cpp b/include/internal/catch_commandline.cpp index 087aa276..484b7740 100644 --- a/include/internal/catch_commandline.cpp +++ b/include/internal/catch_commandline.cpp @@ -9,6 +9,7 @@ #include "catch_commandline.hpp" #include "catch_common.h" +#include "catch_string_manip.h" #include #include diff --git a/include/internal/catch_common.cpp b/include/internal/catch_common.cpp index a35ef93a..d865cd52 100644 --- a/include/internal/catch_common.cpp +++ b/include/internal/catch_common.cpp @@ -11,70 +11,9 @@ #include "catch_interfaces_config.h" #include -#include namespace Catch { - bool startsWith( std::string const& s, std::string const& prefix ) { - return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); - } - bool startsWith( std::string const& s, char prefix ) { - return !s.empty() && s[0] == prefix; - } - bool endsWith( std::string const& s, std::string const& suffix ) { - return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); - } - bool endsWith( std::string const& s, char suffix ) { - return !s.empty() && s[s.size()-1] == suffix; - } - bool contains( std::string const& s, std::string const& infix ) { - return s.find( infix ) != std::string::npos; - } - char toLowerCh(char c) { - return static_cast( std::tolower( c ) ); - } - void toLowerInPlace( std::string& s ) { - std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); - } - std::string toLower( std::string const& s ) { - std::string lc = s; - toLowerInPlace( lc ); - return lc; - } - std::string trim( std::string const& str ) { - static char const* whitespaceChars = "\n\r\t "; - std::string::size_type start = str.find_first_not_of( whitespaceChars ); - std::string::size_type end = str.find_last_not_of( whitespaceChars ); - - return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); - } - - bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { - bool replaced = false; - std::size_t i = str.find( replaceThis ); - while( i != std::string::npos ) { - replaced = true; - str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); - if( i < str.size()-withThis.size() ) - i = str.find( replaceThis, i+withThis.size() ); - else - i = std::string::npos; - } - return replaced; - } - - pluralise::pluralise( std::size_t count, std::string const& label ) - : m_count( count ), - m_label( label ) - {} - - std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { - os << pluraliser.m_count << ' ' << pluraliser.m_label; - if( pluraliser.m_count != 1 ) - os << 's'; - return os; - } - SourceLineInfo::SourceLineInfo() noexcept : file(""), line( 0 ){} SourceLineInfo::SourceLineInfo( char const* _file, std::size_t _line ) noexcept : file( _file ), @@ -111,4 +50,8 @@ namespace Catch { bool alwaysTrue() { return true; } bool alwaysFalse() { return false; } + std::string StreamEndStop::operator+() const { + return std::string(); + } + } diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h index 3ef325e0..5c5de452 100644 --- a/include/internal/catch_common.h +++ b/include/internal/catch_common.h @@ -22,7 +22,6 @@ #define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr ) #include -#include #include namespace Catch { @@ -45,26 +44,6 @@ namespace Catch { virtual ~NonCopyable(); }; - - bool startsWith( std::string const& s, std::string const& prefix ); - bool startsWith( std::string const& s, char prefix ); - bool endsWith( std::string const& s, std::string const& suffix ); - bool endsWith( std::string const& s, char suffix ); - bool contains( std::string const& s, std::string const& infix ); - void toLowerInPlace( std::string& s ); - std::string toLower( std::string const& s ); - std::string trim( std::string const& str ); - bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); - - struct pluralise { - pluralise( std::size_t count, std::string const& label ); - - friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); - - std::size_t m_count; - std::string m_label; - }; - struct SourceLineInfo { SourceLineInfo() noexcept; @@ -98,9 +77,7 @@ namespace Catch { // as well as // >> stuff +StreamEndStop struct StreamEndStop { - std::string operator+() const { - return std::string(); - } + std::string operator+() const; }; template T const& operator + ( T const& value, StreamEndStop ) { diff --git a/include/internal/catch_list.cpp b/include/internal/catch_list.cpp index de58badb..36cd0545 100644 --- a/include/internal/catch_list.cpp +++ b/include/internal/catch_list.cpp @@ -12,11 +12,12 @@ #include "catch_interfaces_reporter.h" #include "catch_interfaces_testcase.h" -#include "internal/catch_text.h" +#include "catch_text.h" #include "catch_console_colour.hpp" #include "catch_test_spec_parser.hpp" #include "catch_tostring.h" +#include "catch_string_manip.h" #include #include diff --git a/include/internal/catch_matchers_string.cpp b/include/internal/catch_matchers_string.cpp index 207193fb..2f8ffdb8 100644 --- a/include/internal/catch_matchers_string.cpp +++ b/include/internal/catch_matchers_string.cpp @@ -7,6 +7,7 @@ */ #include "catch_matchers_string.h" +#include "catch_string_manip.h" namespace Catch { namespace Matchers { diff --git a/include/internal/catch_matchers_vector.h b/include/internal/catch_matchers_vector.h index 1f9e0a61..b176d4a3 100644 --- a/include/internal/catch_matchers_vector.h +++ b/include/internal/catch_matchers_vector.h @@ -10,6 +10,8 @@ #include "catch_matchers.hpp" +#include + namespace Catch { namespace Matchers { diff --git a/include/internal/catch_run_context.cpp b/include/internal/catch_run_context.cpp index 7ed79950..68f824fa 100644 --- a/include/internal/catch_run_context.cpp +++ b/include/internal/catch_run_context.cpp @@ -1,6 +1,7 @@ #include "catch_run_context.hpp" #include +#include namespace Catch { diff --git a/include/internal/catch_string_manip.cpp b/include/internal/catch_string_manip.cpp new file mode 100644 index 00000000..84762044 --- /dev/null +++ b/include/internal/catch_string_manip.cpp @@ -0,0 +1,77 @@ +/* + * Created by Martin on 25/07/2017. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ + +#include "catch_string_manip.h" + +#include +#include +#include +#include + +namespace Catch { + + bool startsWith( std::string const& s, std::string const& prefix ) { + return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); + } + bool startsWith( std::string const& s, char prefix ) { + return !s.empty() && s[0] == prefix; + } + bool endsWith( std::string const& s, std::string const& suffix ) { + return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); + } + bool endsWith( std::string const& s, char suffix ) { + return !s.empty() && s[s.size()-1] == suffix; + } + bool contains( std::string const& s, std::string const& infix ) { + return s.find( infix ) != std::string::npos; + } + char toLowerCh(char c) { + return static_cast( std::tolower( c ) ); + } + void toLowerInPlace( std::string& s ) { + std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); + } + std::string toLower( std::string const& s ) { + std::string lc = s; + toLowerInPlace( lc ); + return lc; + } + std::string trim( std::string const& str ) { + static char const* whitespaceChars = "\n\r\t "; + std::string::size_type start = str.find_first_not_of( whitespaceChars ); + std::string::size_type end = str.find_last_not_of( whitespaceChars ); + + return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); + } + + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { + bool replaced = false; + std::size_t i = str.find( replaceThis ); + while( i != std::string::npos ) { + replaced = true; + str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); + if( i < str.size()-withThis.size() ) + i = str.find( replaceThis, i+withThis.size() ); + else + i = std::string::npos; + } + return replaced; + } + + pluralise::pluralise( std::size_t count, std::string const& label ) + : m_count( count ), + m_label( label ) + {} + + std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { + os << pluraliser.m_count << ' ' << pluraliser.m_label; + if( pluraliser.m_count != 1 ) + os << 's'; + return os; + } + +} diff --git a/include/internal/catch_string_manip.h b/include/internal/catch_string_manip.h new file mode 100644 index 00000000..6292cd57 --- /dev/null +++ b/include/internal/catch_string_manip.h @@ -0,0 +1,36 @@ +/* + * Created by Martin on 25/07/2017. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_CATCH_STRING_MANIP_H_INCLUDED +#define TWOBLUECUBES_CATCH_STRING_MANIP_H_INCLUDED + +#include +#include + +namespace Catch { + + bool startsWith( std::string const& s, std::string const& prefix ); + bool startsWith( std::string const& s, char prefix ); + bool endsWith( std::string const& s, std::string const& suffix ); + bool endsWith( std::string const& s, char suffix ); + bool contains( std::string const& s, std::string const& infix ); + void toLowerInPlace( std::string& s ); + std::string toLower( std::string const& s ); + std::string trim( std::string const& str ); + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); + + struct pluralise { + pluralise( std::size_t count, std::string const& label ); + + friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); + + std::size_t m_count; + std::string m_label; + }; +} + +#endif // TWOBLUECUBES_CATCH_STRING_MANIP_H_INCLUDED + diff --git a/include/internal/catch_tag_alias_registry.cpp b/include/internal/catch_tag_alias_registry.cpp index d0ee2f7c..c0969445 100644 --- a/include/internal/catch_tag_alias_registry.cpp +++ b/include/internal/catch_tag_alias_registry.cpp @@ -10,6 +10,7 @@ #include "catch_console_colour.hpp" #include "catch_interfaces_registry_hub.h" #include "catch_stream.h" +#include "catch_string_manip.h" namespace Catch { diff --git a/include/internal/catch_test_case_info.cpp b/include/internal/catch_test_case_info.cpp index f75f5a3c..0e4ac1d2 100644 --- a/include/internal/catch_test_case_info.cpp +++ b/include/internal/catch_test_case_info.cpp @@ -10,6 +10,7 @@ #include "catch_test_case_info.h" #include "catch_interfaces_testcase.h" #include "catch_common.h" +#include "catch_string_manip.h" #include #include diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index 068ffe58..7008ae9f 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -13,6 +13,7 @@ #include "catch_test_spec.hpp" #include "catch_context.h" #include "catch_interfaces_config.h" +#include "catch_string_manip.h" #include #include diff --git a/include/internal/catch_test_spec.cpp b/include/internal/catch_test_spec.cpp index 878d8424..0809fc5b 100644 --- a/include/internal/catch_test_spec.cpp +++ b/include/internal/catch_test_spec.cpp @@ -6,6 +6,7 @@ */ #include "catch_test_spec.hpp" +#include "catch_string_manip.h" #include #include diff --git a/include/internal/catch_test_spec_parser.hpp b/include/internal/catch_test_spec_parser.hpp index 3e003685..497253d2 100644 --- a/include/internal/catch_test_spec_parser.hpp +++ b/include/internal/catch_test_spec_parser.hpp @@ -14,6 +14,7 @@ #endif #include "catch_test_spec.hpp" +#include "catch_string_manip.h" #include "catch_interfaces_tag_alias_registry.h" namespace Catch { diff --git a/include/internal/catch_wildcard_pattern.cpp b/include/internal/catch_wildcard_pattern.cpp index e99ff5f2..f33035dd 100644 --- a/include/internal/catch_wildcard_pattern.cpp +++ b/include/internal/catch_wildcard_pattern.cpp @@ -6,6 +6,7 @@ */ #include "catch_wildcard_pattern.hpp" +#include "catch_string_manip.h" namespace Catch { diff --git a/include/reporters/catch_reporter_bases.hpp b/include/reporters/catch_reporter_bases.hpp index ce1834d5..6fd40243 100644 --- a/include/reporters/catch_reporter_bases.hpp +++ b/include/reporters/catch_reporter_bases.hpp @@ -10,6 +10,7 @@ #include "../internal/catch_interfaces_reporter.h" +#include #include #include #include