-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
123 lines (106 loc) · 2.16 KB
/
Copy pathutils.cpp
File metadata and controls
123 lines (106 loc) · 2.16 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "utils.h"
#include <locale>
#include <codecvt>
#include <sstream>
#include <iomanip>
#include <cctype>
namespace util
{
std::vector<std::string> tokenizeQuotedString( const std::string& input )
{
std::istringstream stream;
stream.str( input );
std::vector<std::string> tokens;
std::string token;
while ( stream >> std::quoted( token ) )
{
tokens.push_back( std::move( token ) );
}
return tokens;
}
std::wstring s2ws( const std::string& s )
{
try
{
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>{}
.from_bytes( s );
}
catch( std::range_error& e )
{
(void)e;
size_t length = s.length();
std::wstring result;
result.reserve( length );
for ( size_t i = 0; i < length; ++i )
{
result.push_back( s[i] & 0xFF );
}
return result;
}
}
std::string ws2s( const std::wstring& ws )
{
try
{
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>{}
.to_bytes( ws );
}
catch( std::range_error& e )
{
(void)e;
size_t length = ws.length();
std::string result;
result.reserve( length );
for ( size_t i = 0; i < length; ++i )
{
result.push_back( ws[i] & 0xFF );
}
return result;
}
}
std::vector<std::string> splitString( const std::string& s,
const std::string& delim )
{
std::vector<std::string> strings;
splitString_impl( s,
delim,
std::back_inserter( strings ) );
return strings;
}
bool stringContains( std::string_view haystack,
std::string_view needle )
{
return std::search( haystack.begin(),
haystack.end(),
needle.begin(),
needle.end() ) != haystack.end();
}
std::string& capitalizeFirstLetter( std::string& str )
{
str[0] = toupper( str[0] );
return str;
}
std::string&& capitalizeFirstLetter( std::string&& str )
{
str[0] = toupper( str[0] );
return std::move( str );
}
std::uintptr_t pointerToInt( void* p )
{
return reinterpret_cast<std::uintptr_t>( p );
}
void* intToPointer( uintptr_t i )
{
return reinterpret_cast<void*>( i );
}
void* addPointers( void* p1,
void* p2 )
{
return intToPointer( pointerToInt( p1 ) + pointerToInt( p2 ) );
}
std::string operator+( const std::string_view& sv1,
const std::string_view& sv2 )
{
return std::string{sv1} + std::string{sv2};
}
}//util