• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17     namespace {
toLowerCh(char c)18         char toLowerCh(char c) {
19             return static_cast<char>( std::tolower( c ) );
20         }
21     }
22 
startsWith(std::string const & s,std::string const & prefix)23     bool startsWith( std::string const& s, std::string const& prefix ) {
24         return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
25     }
startsWith(std::string const & s,char prefix)26     bool startsWith( std::string const& s, char prefix ) {
27         return !s.empty() && s[0] == prefix;
28     }
endsWith(std::string const & s,std::string const & suffix)29     bool endsWith( std::string const& s, std::string const& suffix ) {
30         return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
31     }
endsWith(std::string const & s,char suffix)32     bool endsWith( std::string const& s, char suffix ) {
33         return !s.empty() && s[s.size()-1] == suffix;
34     }
contains(std::string const & s,std::string const & infix)35     bool contains( std::string const& s, std::string const& infix ) {
36         return s.find( infix ) != std::string::npos;
37     }
toLowerInPlace(std::string & s)38     void toLowerInPlace( std::string& s ) {
39         std::transform( s.begin(), s.end(), s.begin(), toLowerCh );
40     }
toLower(std::string const & s)41     std::string toLower( std::string const& s ) {
42         std::string lc = s;
43         toLowerInPlace( lc );
44         return lc;
45     }
trim(std::string const & str)46     std::string trim( std::string const& str ) {
47         static char const* whitespaceChars = "\n\r\t ";
48         std::string::size_type start = str.find_first_not_of( whitespaceChars );
49         std::string::size_type end = str.find_last_not_of( whitespaceChars );
50 
51         return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string();
52     }
53 
replaceInPlace(std::string & str,std::string const & replaceThis,std::string const & withThis)54     bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
55         bool replaced = false;
56         std::size_t i = str.find( replaceThis );
57         while( i != std::string::npos ) {
58             replaced = true;
59             str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
60             if( i < str.size()-withThis.size() )
61                 i = str.find( replaceThis, i+withThis.size() );
62             else
63                 i = std::string::npos;
64         }
65         return replaced;
66     }
67 
pluralise(std::size_t count,std::string const & label)68     pluralise::pluralise( std::size_t count, std::string const& label )
69     :   m_count( count ),
70         m_label( label )
71     {}
72 
operator <<(std::ostream & os,pluralise const & pluraliser)73     std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) {
74         os << pluraliser.m_count << ' ' << pluraliser.m_label;
75         if( pluraliser.m_count != 1 )
76             os << 's';
77         return os;
78     }
79 
80 }
81