-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
142 lines (122 loc) · 3.3 KB
/
utils.h
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <bitset>
#define isOfTypeT( obj, T ) ( dynamic_cast<T*>( obj ) != nullptr ) ? true : false
namespace util
{
template<typename T>
void removeByBackSwap( std::vector<T>& v,
std::size_t index )
{
typename std::vector<T>::iterator pback = v.back();
std::swap( v[index], pback );
v.pop_back();
}
template<typename T>
void removeByBackSwap( std::vector<T>& v,
const T& element )
{
v.erase( std::find( v.begin(),
v.end(),
&element ) );
}
template<typename T, class Alloc>
void shrinkCapacity( std::vector<T,Alloc>& v )
{
std::vector<T,Alloc>( v.begin(),
v.end() ).swap( v );
}
template<typename Container>
void printContainer( const Container& c,
const char* delimiter = " " )
{
std::copy( c.begin(),
c.end(),
std::ostream_iterator<Container::value_type>( std::cout, delimiter ) );
}
// convert ptr/ref to container iterator
template<template<class, class> typename Container, typename T, typename Alloc>
typename Container<T, Alloc>::Iter ptrToContainerIter( const Container<T, Alloc>& v,
void* pItem )
{
return std::find_if( v.begin(),
v.end(),
[&pItem] ( const T& p )
{
return pItem == *p;
});
}
//===================================================
// \function tokenizeQuotedString
// \brief converts a string input into a vector of strings
// separation into vector element "tokens" is based on spaces or quotes '
// \date 2021/01/12 12:54
std::vector<std::string> tokenizeQuotedString( const std::string& input );
//===================================================
// \function s2ws
// \brief convert from strings/chars to wide strings/wchar_ts
// or std::wstring( s.begin(), s.end() );
// \date 2020/12/30 20:38
std::wstring s2ws( const std::string& narrow );
//===================================================
// \function ws2s
// \brief convert wide strings/wchar_ts to strings/chars
// \date 2020/12/30 20:38
std::string ws2s( const std::wstring& wide );
namespace
{
template<class Iter>
void splitString_impl( const std::string& s,
const std::string& delim,
Iter out )
{
if ( delim.empty() )
{
*out++ = s;
}
else
{
size_t a = 0;
size_t b = s.find( delim );
for ( ; b != std::string::npos;
a = b + delim.length(),
b = s.find( delim, a ) )
{
*out++ = std::move( s.substr( a,
b - a ) );
}
*out++ = std::move( s.substr( a,
s.length() - a ) );
}
}
}
std::vector<std::string> splitString( const std::string& s, const std::string& delim );
bool stringContains( std::string_view haystack, std::string_view needle );
std::string& capitalizeFirstLetter( std::string& str );
std::string&& capitalizeFirstLetter( std::string&& str );
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
void printBinary( T val )
{
std::bitset<32> bin{val};
std::cout << bin;
}
std::uintptr_t pointerToInt( void* p );
void* intToPointer( uintptr_t i );
void* addPointers( void* p1, void* p2 );
std::string operator+( const std::string_view& sv1, const std::string_view& sv2 );
// print a comma every 3 decimal places
template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
std::string getNumberString( T num )
{
std::stringstream ss;
ss.imbue( std::locale{""} );
ss << std::fixed
<< num;
return ss.str();
}
}//util