| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#pragma once |
| 25 |
|
| 26 |
#include <bitset> |
| 27 |
#include <cmath> |
| 28 |
#include <cstddef> |
| 29 |
#include <cstdint> |
| 30 |
#include <iomanip> |
| 31 |
#include <limits> |
| 32 |
#include <locale> |
| 33 |
#include <optional> |
| 34 |
#include <sstream> |
| 35 |
#include <string> |
| 36 |
#include <string_view> |
| 37 |
#include <type_traits> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> |
| 41 |
std::string GetHexString( const T value, const int width = 8 ) |
| 42 |
{ |
| 43 |
std::ostringstream stream; |
| 44 |
|
| 45 |
stream << "0x" << std::setw( width ) << std::setfill( '0' ) << std::hex << value; |
| 46 |
|
| 47 |
return stream.str(); |
| 48 |
} |
| 49 |
|
| 50 |
int Sign( const int i ); |
| 51 |
|
| 52 |
std::string StringTrim( std::string str ); |
| 53 |
|
| 54 |
std::string StringLower( std::string str ); |
| 55 |
std::string StringUpper( std::string str ); |
| 56 |
|
| 57 |
std::vector<std::string> StringSplit( const std::string_view str, const char sep ); |
| 58 |
|
| 59 |
void StringReplace( std::string & dst, const char * pred, const std::string_view src ); |
| 60 |
|
| 61 |
template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> |
| 62 |
void StringReplace( std::string & dst, const char * pred, const T value ) |
| 63 |
{ |
| 64 |
StringReplace( dst, pred, std::to_string( value ) ); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
constexpr int CountBits( uint32_t val ) |
| 69 |
{ |
| 70 |
int res = 0; |
| 71 |
|
| 72 |
|
| 73 |
while ( val ) { |
| 74 |
val &= ( val - 1 ); |
| 75 |
++res; |
| 76 |
} |
| 77 |
|
| 78 |
return res; |
| 79 |
} |
| 80 |
|
| 81 |
namespace fheroes2 |
| 82 |
{ |
| 83 |
uint32_t calculateCRC32( const uint8_t * data, const size_t length ); |
| 84 |
|
| 85 |
template <size_t N> |
| 86 |
std::bitset<N> makeBitsetFromVector( const std::vector<int> & vector ) |
| 87 |
{ |
| 88 |
std::bitset<N> result; |
| 89 |
for ( const int index : vector ) { |
| 90 |
result.set( index, true ); |
| 91 |
} |
| 92 |
return result; |
| 93 |
} |
| 94 |
|
| 95 |
void replaceStringEnding( std::string & output, const char * originalEnding, const char * correctedEnding ); |
| 96 |
|
| 97 |
std::string abbreviateNumber( const int num ); |
| 98 |
|
| 99 |
|
| 100 |
void appendModifierToString( std::string & str, const int mod ); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
template <typename CharType> |
| 105 |
bool compareStringsCaseInsensitively( const std::basic_string<CharType> & lhs, const std::basic_string<CharType> & rhs ) |
| 106 |
{ |
| 107 |
typename std::basic_string<CharType>::const_iterator li = lhs.begin(); |
| 108 |
typename std::basic_string<CharType>::const_iterator ri = rhs.begin(); |
| 109 |
|
| 110 |
const std::locale currentGlobalLocale; |
| 111 |
|
| 112 |
while ( li != lhs.end() && ri != rhs.end() ) { |
| 113 |
const CharType lc = std::tolower( *li, currentGlobalLocale ); |
| 114 |
const CharType rc = std::tolower( *ri, currentGlobalLocale ); |
| 115 |
|
| 116 |
if ( lc < rc ) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
if ( lc > rc ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
++li; |
| 124 |
++ri; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
return li == lhs.end() && ri != rhs.end(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
template <typename To, typename From, std::enable_if_t<std::is_integral_v<To> && std::is_integral_v<From>, bool> = true> |
| 134 |
constexpr std::optional<To> checkedCast( const From from ) |
| 135 |
{ |
| 136 |
|
| 137 |
if constexpr ( std::is_signed_v<From> == std::is_signed_v<To> ) { |
| 138 |
if ( from < std::numeric_limits<To>::min() || from > std::numeric_limits<To>::max() ) { |
| 139 |
return {}; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
else if constexpr ( std::is_signed_v<From> ) { |
| 144 |
if ( from < 0 ) { |
| 145 |
return {}; |
| 146 |
} |
| 147 |
|
| 148 |
const std::make_unsigned_t<From> unsignedFrom = from; |
| 149 |
if ( unsignedFrom > std::numeric_limits<To>::max() ) { |
| 150 |
return {}; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
else { |
| 155 |
constexpr To maxTo = std::numeric_limits<To>::max(); |
| 156 |
static_assert( maxTo >= 0 ); |
| 157 |
|
| 158 |
constexpr std::make_unsigned_t<To> unsignedMaxTo = maxTo; |
| 159 |
if ( from > unsignedMaxTo ) { |
| 160 |
return {}; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return static_cast<To>( from ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
template <typename To, typename From, |
| 170 |
std::enable_if_t<( std::is_integral_v<To> && std::numeric_limits<To>::radix == 2 && std ::is_floating_point_v<From> && std::numeric_limits<From>::is_iec559 |
| 171 |
&& std::numeric_limits<From>::radix == 2 && std::numeric_limits<To>::digits < std::numeric_limits<From>::max_exponent ), |
| 172 |
bool> |
| 173 |
= true> |
| 174 |
std::optional<To> checkedCast( const From from ) |
| 175 |
{ |
| 176 |
if ( !std::isfinite( from ) ) { |
| 177 |
return {}; |
| 178 |
} |
| 179 |
|
| 180 |
static_assert( std::numeric_limits<int8_t>::min() == -128 && std::numeric_limits<int8_t>::max() == 127, |
| 181 |
"The following logic will only work on platforms with two's complement signed integer representation" ); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if constexpr ( std::is_signed_v<To> ) { |
| 190 |
|
| 191 |
|
| 192 |
if ( from < std::ldexp( static_cast<From>( -1.0 ), std::numeric_limits<To>::digits ) |
| 193 |
|| from >= std::ldexp( static_cast<From>( 1.0 ), std::numeric_limits<To>::digits ) ) { |
| 194 |
return {}; |
| 195 |
} |
| 196 |
} |
| 197 |
else { |
| 198 |
|
| 199 |
|
| 200 |
if ( from < 0 || from >= std::ldexp( static_cast<From>( 1.0 ), std::numeric_limits<To>::digits ) ) { |
| 201 |
return {}; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return static_cast<To>( from ); |
| 206 |
} |
| 207 |
} |