GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <functional>
4#include <iostream>
5#include <iterator>
6#include <locale>
7#include <sstream>
8#include <vector>
9
10namespace ga {
11
12// string utilities
13// ----------------------
14
15inline std::vector<std::string> splitString( const std::string& str, char delim = ' ' )
16{
17 std::vector<std::string> result;
18 std::stringstream ss( str );
19 std::string token;
20 while ( std::getline( ss, token, delim ) ) {
21 result.push_back( token );
22 }
23 return result;
24}
25
26inline std::string toLower( std::string str )
27{
28 std::transform( str.begin(), str.end(), str.begin(), []( char c ) { return std::tolower( c, std::locale() ); } );
29 return str;
30}
31
32inline std::string toUpper( std::string str )
33{
34 std::transform( str.begin(), str.end(), str.begin(), []( char c ) { return std::toupper( c, std::locale() ); } );
35
36 return str;
37}
38
39// Helper RAII class to auto cleanup / do something on early return
41{
42 scope_guard( const std::function<void()>& f )
43 : _f( f )
44 {
45 }
47 {
48 if ( _f )
49 _f();
50 }
51
52protected:
53 std::function<void()> _f;
54};
55
56} // namespace ga
Definition: color.h:9
std::string toLower(std::string str)
Definition: util.h:26
std::vector< std::string > splitString(const std::string &str, char delim=' ')
Definition: util.h:15
std::string toUpper(std::string str)
Definition: util.h:32
Definition: util.h:41
scope_guard(const std::function< void()> &f)
Definition: util.h:42
~scope_guard()
Definition: util.h:46
std::function< void()> _f
Definition: util.h:53