Split out helper functions on std::string from catch_common.h

This commit is contained in:
Martin Hořeňovský 2017-07-25 21:57:35 +02:00
parent 1e59ccee41
commit 1a96175bb2
17 changed files with 134 additions and 86 deletions

View File

@ -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

View File

@ -9,6 +9,7 @@
#include "catch_commandline.hpp"
#include "catch_common.h"
#include "catch_string_manip.h"
#include <fstream>
#include <ctime>

View File

@ -11,70 +11,9 @@
#include "catch_interfaces_config.h"
#include <cstring>
#include <cctype>
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<char>( 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();
}
}

View File

@ -22,7 +22,6 @@
#define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
#include <sstream>
#include <algorithm>
#include <exception>
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<typename T>
T const& operator + ( T const& value, StreamEndStop ) {

View File

@ -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 <limits>
#include <algorithm>

View File

@ -7,6 +7,7 @@
*/
#include "catch_matchers_string.h"
#include "catch_string_manip.h"
namespace Catch {
namespace Matchers {

View File

@ -10,6 +10,8 @@
#include "catch_matchers.hpp"
#include <algorithm>
namespace Catch {
namespace Matchers {

View File

@ -1,6 +1,7 @@
#include "catch_run_context.hpp"
#include <cassert>
#include <algorithm>
namespace Catch {

View File

@ -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 <algorithm>
#include <ostream>
#include <cstring>
#include <cctype>
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<char>( 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;
}
}

View File

@ -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 <string>
#include <iosfwd>
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

View File

@ -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 {

View File

@ -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 <cctype>
#include <exception>

View File

@ -13,6 +13,7 @@
#include "catch_test_spec.hpp"
#include "catch_context.h"
#include "catch_interfaces_config.h"
#include "catch_string_manip.h"
#include <vector>
#include <set>

View File

@ -6,6 +6,7 @@
*/
#include "catch_test_spec.hpp"
#include "catch_string_manip.h"
#include <string>
#include <vector>

View File

@ -14,6 +14,7 @@
#endif
#include "catch_test_spec.hpp"
#include "catch_string_manip.h"
#include "catch_interfaces_tag_alias_registry.h"
namespace Catch {

View File

@ -6,6 +6,7 @@
*/
#include "catch_wildcard_pattern.hpp"
#include "catch_string_manip.h"
namespace Catch {

View File

@ -10,6 +10,7 @@
#include "../internal/catch_interfaces_reporter.h"
#include <algorithm>
#include <cstring>
#include <cfloat>
#include <cstdio>