Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_string_manip.cpp
Go to the documentation of this file.
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
9#include "catch_stringref.h"
10
11#include <algorithm>
12#include <ostream>
13#include <cstring>
14#include <cctype>
15#include <vector>
16
17namespace Catch {
18
19 namespace {
20 char toLowerCh(char c) {
21 return static_cast<char>( std::tolower( c ) );
22 }
23 }
24
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 }
28 bool startsWith( std::string const& s, char prefix ) {
29 return !s.empty() && s[0] == prefix;
30 }
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 }
34 bool endsWith( std::string const& s, char suffix ) {
35 return !s.empty() && s[s.size()-1] == suffix;
36 }
37 bool contains( std::string const& s, std::string const& infix ) {
38 return s.find( infix ) != std::string::npos;
39 }
40 void toLowerInPlace( std::string& s ) {
41 std::transform( s.begin(), s.end(), s.begin(), toLowerCh );
42 }
43 std::string toLower( std::string const& s ) {
44 std::string lc = s;
45 toLowerInPlace( lc );
46 return lc;
47 }
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
56 bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
57 bool replaced = false;
58 std::size_t i = str.find( replaceThis );
59 while( i != std::string::npos ) {
60 replaced = true;
61 str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
62 if( i < str.size()-withThis.size() )
63 i = str.find( replaceThis, i+withThis.size() );
64 else
65 i = std::string::npos;
66 }
67 return replaced;
68 }
69
70 std::vector<StringRef> splitStringRef( StringRef str, char delimiter ) {
71 std::vector<StringRef> subStrings;
72 std::size_t start = 0;
73 for(std::size_t pos = 0; pos < str.size(); ++pos ) {
74 if( str[pos] == delimiter ) {
75 if( pos - start > 1 )
76 subStrings.push_back( str.substr( start, pos-start ) );
77 start = pos+1;
78 }
79 }
80 if( start < str.size() )
81 subStrings.push_back( str.substr( start, str.size()-start ) );
82 return subStrings;
83 }
84
85 pluralise::pluralise( std::size_t count, std::string const& label )
86 : m_count( count ),
87 m_label( label )
88 {}
89
90 std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) {
91 os << pluraliser.m_count << ' ' << pluraliser.m_label;
92 if( pluraliser.m_count != 1 )
93 os << 's';
94 return os;
95 }
96
97}
os_t os
int * count
void toLowerInPlace(std::string &s)
std::string trim(std::string const &str)
std::vector< StringRef > splitStringRef(StringRef str, char delimiter)
bool startsWith(std::string const &s, std::string const &prefix)
bool contains(std::string const &s, std::string const &infix)
auto operator<<(std::ostream &os, LazyExpression const &lazyExpr) -> std::ostream &
std::string toLower(std::string const &s)
bool endsWith(std::string const &s, std::string const &suffix)
bool replaceInPlace(std::string &str, std::string const &replaceThis, std::string const &withThis)
pluralise(std::size_t count, std::string const &label)
char * label
char * s