summaryrefslogtreecommitdiff
path: root/src/libutil/StringUtils.cpp
blob: 640ce204c33adf62b2fb74866cdd8686bea7dbed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "util/StringUtils.hpp"

#include <algorithm>
#include <cctype>

using namespace std;

bool stringicasecmp( const string &s1, const string &s2 )
{
	string::const_iterator i1 = s1.begin( ), e1 = s1.end( ),
		i2 = s2.begin( ), e2 = s2.end( );
	
	while( i1 != e1 && i2 != e2 ) {
		if( toupper( *i1 ) != toupper( *i2 ) ) return false;
		i1++;
		i2++;
	}
	
	if( i1 == e1 && i2 == e2 ) return true;
	
	return false;
}

std::vector<std::string> split( const string &s, const string &delimiter, bool keepEmpty )
{
	vector<string> result; 

	if( delimiter.empty( ) ) { 
		result.push_back( s );
		return result;
	}

	string::const_iterator b = s.begin( ), e;
	while( true ) {
		e = search( b, s.end( ), delimiter.begin( ), delimiter.end( ) );
		string tmp( b, e );
		if( keepEmpty || !tmp.empty( ) ) {
			result.push_back( tmp );
		}
		if( e == s.end( ) ) {
			break;
		}
		b = e + delimiter.size( );
	}

	return result;
}

bool endswith( const string &s, const string &endstring )
{
	unsigned int pos = s.rfind( endstring );
	return pos != string::npos && pos + endstring.length( ) == s.length( );
}

bool startswith( const string &s, const string &startstring )
{
	unsigned int pos = s.find( startstring );
	return pos != string::npos;
}