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 #include "catch_stringref.h"
10 
11 #include <algorithm>
12 #include <ostream>
13 #include <cstring>
14 #include <cctype>
15 #include <vector>
16 
17 namespace Catch {
18 
19     namespace {
toLowerCh(char c)20         char toLowerCh(char c) {
21             return static_cast<char>( std::tolower( static_cast<unsigned char>(c) ) );
22         }
23     }
24 
startsWith(std::string const & s,std::string const & prefix)25     bool startsWith( std::string const& s, std::string const& prefix ) {
26         return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
27     }
startsWith(std::string const & s,char prefix)28     bool startsWith( std::string const& s, char prefix ) {
29         return !s.empty() && s[0] == prefix;
30     }
endsWith(std::string const & s,std::string const & suffix)31     bool endsWith( std::string const& s, std::string const& suffix ) {
32         return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
33     }
endsWith(std::string const & s,char suffix)34     bool endsWith( std::string const& s, char suffix ) {
35         return !s.empty() && s[s.size()-1] == suffix;
36     }
contains(std::string const & s,std::string const & infix)37     bool contains( std::string const& s, std::string const& infix ) {
38         return s.find( infix ) != std::string::npos;
39     }
toLowerInPlace(std::string & s)40     void toLowerInPlace( std::string& s ) {
41         std::transform( s.begin(), s.end(), s.begin(), toLowerCh );
42     }
toLower(std::string const & s)43     std::string toLower( std::string const& s ) {
44         std::string lc = s;
45         toLowerInPlace( lc );
46         return lc;
47     }
trim(std::string const & str)48     std::string trim( std::string const& str ) {
49         static char const* whitespaceChars = "\n\r\t ";
50         std::string::size_type start = str.find_first_not_of( whitespaceChars );
51         std::string::size_type end = str.find_last_not_of( whitespaceChars );
52 
53         return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string();
54     }
55 
trim(StringRef ref)56     StringRef trim(StringRef ref) {
57         const auto is_ws = [](char c) {
58             return c == ' ' || c == '\t' || c == '\n' || c == '\r';
59         };
60         size_t real_begin = 0;
61         while (real_begin < ref.size() && is_ws(ref[real_begin])) { ++real_begin; }
62         size_t real_end = ref.size();
63         while (real_end > real_begin && is_ws(ref[real_end - 1])) { --real_end; }
64 
65         return ref.substr(real_begin, real_end - real_begin);
66     }
67 
replaceInPlace(std::string & str,std::string const & replaceThis,std::string const & withThis)68     bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
69         bool replaced = false;
70         std::size_t i = str.find( replaceThis );
71         while( i != std::string::npos ) {
72             replaced = true;
73             str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
74             if( i < str.size()-withThis.size() )
75                 i = str.find( replaceThis, i+withThis.size() );
76             else
77                 i = std::string::npos;
78         }
79         return replaced;
80     }
81 
splitStringRef(StringRef str,char delimiter)82     std::vector<StringRef> splitStringRef( StringRef str, char delimiter ) {
83         std::vector<StringRef> subStrings;
84         std::size_t start = 0;
85         for(std::size_t pos = 0; pos < str.size(); ++pos ) {
86             if( str[pos] == delimiter ) {
87                 if( pos - start > 1 )
88                     subStrings.push_back( str.substr( start, pos-start ) );
89                 start = pos+1;
90             }
91         }
92         if( start < str.size() )
93             subStrings.push_back( str.substr( start, str.size()-start ) );
94         return subStrings;
95     }
96 
pluralise(std::size_t count,std::string const & label)97     pluralise::pluralise( std::size_t count, std::string const& label )
98     :   m_count( count ),
99         m_label( label )
100     {}
101 
operator <<(std::ostream & os,pluralise const & pluraliser)102     std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) {
103         os << pluraliser.m_count << ' ' << pluraliser.m_label;
104         if( pluraliser.m_count != 1 )
105             os << 's';
106         return os;
107     }
108 
109 }
110