| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2019 - 2025 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #include "tools.h" |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <cassert> |
| 28 | #include <cctype> |
| 29 | #include <cstdlib> |
| 30 | #include <cstring> |
| 31 | #include <limits> |
| 32 | #include <system_error> |
| 33 | |
| 34 | #include <zconf.h> |
| 35 | #include <zlib.h> |
| 36 | |
| 37 | std::string StringTrim( std::string str ) |
| 38 | { |
| 39 | if ( str.empty() ) { |
| 40 | return str; |
| 41 | } |
| 42 | |
| 43 | // left |
| 44 | std::string::iterator iter = str.begin(); |
| 45 | while ( iter != str.end() && std::isspace( static_cast<unsigned char>( *iter ) ) ) { |
| 46 | ++iter; |
| 47 | } |
| 48 | |
| 49 | if ( iter == str.end() ) { |
| 50 | // Do not erase anything if we reached the end of the string. Just immediately return an empty string. |
| 51 | return {}; |
| 52 | } |
| 53 | |
| 54 | if ( iter != str.begin() ) |
| 55 | str.erase( str.begin(), iter ); |
| 56 | |
| 57 | // right |
| 58 | iter = str.end() - 1; |
| 59 | while ( iter != str.begin() && std::isspace( static_cast<unsigned char>( *iter ) ) ) { |
| 60 | --iter; |
| 61 | } |
| 62 | |
| 63 | if ( iter != str.end() - 1 ) { |
| 64 | str.erase( iter + 1, str.end() ); |
| 65 | } |
| 66 | |
| 67 | return str; |
| 68 | } |
| 69 | |
| 70 | std::string StringLower( std::string str ) |
| 71 | { |
| 72 | std::transform( str.begin(), str.end(), str.begin(), []( const unsigned char c ) { return static_cast<char>( std::tolower( c ) ); } ); |
| 73 | return str; |
| 74 | } |
| 75 | |
| 76 | std::string StringUpper( std::string str ) |
| 77 | { |
| 78 | std::transform( str.begin(), str.end(), str.begin(), []( const unsigned char c ) { return static_cast<char>( std::toupper( c ) ); } ); |
| 79 | return str; |
| 80 | } |
| 81 | |
| 82 | void StringReplace( std::string & dst, const char * pred, const std::string_view src ) |
| 83 | { |
| 84 | size_t pos; |
| 85 | |
| 86 | while ( std::string::npos != ( pos = dst.find( pred ) ) ) { |
| 87 | dst.replace( pos, std::strlen( pred ), src ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | std::vector<std::string> StringSplit( const std::string_view str, const char sep ) |
| 92 | { |
| 93 | std::vector<std::string> result; |
| 94 | |
| 95 | size_t startPos = 0; |
| 96 | |
| 97 | for ( size_t sepPos = str.find( sep ); sepPos != std::string::npos; sepPos = str.find( sep, startPos ) ) { |
| 98 | assert( startPos < str.size() && sepPos < str.size() ); |
| 99 | |
| 100 | result.emplace_back( str.begin() + startPos, str.begin() + sepPos ); |
| 101 | |
| 102 | startPos = sepPos + 1; |
| 103 | } |
| 104 | |
| 105 | assert( startPos <= str.size() ); |
| 106 | |
| 107 | result.emplace_back( str.begin() + startPos, str.end() ); |
| 108 | |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | int Sign( const int i ) |
| 113 | { |
| 114 | if ( i < 0 ) { |
| 115 | return -1; |
| 116 | } |
| 117 | if ( i > 0 ) { |
| 118 | return 1; |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | namespace fheroes2 |
| 124 | { |
| 125 | uint32_t calculateCRC32( const uint8_t * data, const size_t length ) |
| 126 | { |
| 127 | if ( length > std::numeric_limits<uInt>::max() ) { |
| 128 | throw std:: |
| 129 | system_error( std::make_error_code( std::errc::value_too_large ), |
| 130 | "Too large `length` provided to `calculateCRC32`. Must be no larger than `std::numeric_limits<uInt>::max()` (usually `(1 << 32) - 1`)." ); |
| 131 | } |
| 132 | |
| 133 | return static_cast<uint32_t>( crc32( 0, data, static_cast<uInt>( length ) ) ); |
| 134 | } |
| 135 | |
| 136 | void replaceStringEnding( std::string & output, const char * originalEnding, const char * correctedEnding ) |
| 137 | { |
| 138 | assert( originalEnding != nullptr && correctedEnding != nullptr ); |
| 139 | |
| 140 | const size_t originalEndingSize = strlen( originalEnding ); |
| 141 | const size_t correctedEndingSize = strlen( correctedEnding ); |
| 142 | if ( output.size() < originalEndingSize ) { |
| 143 | // The original string is smaller than the ending. |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | if ( memcmp( output.data() + output.size() - originalEndingSize, originalEnding, originalEndingSize ) != 0 ) { |
| 148 | // The string does not have the required ending. |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | output.replace( output.size() - originalEndingSize, originalEndingSize, correctedEnding, correctedEndingSize ); |
| 153 | } |
| 154 | |
| 155 | std::string abbreviateNumber( const int num ) |
| 156 | { |
| 157 | if ( std::abs( num ) >= 1000000 ) { |
| 158 | return std::to_string( num / 1000000 ) + 'M'; |
| 159 | } |
| 160 | |
| 161 | if ( std::abs( num ) >= 1000 ) { |
| 162 | return std::to_string( num / 1000 ) + 'K'; |
| 163 | } |
| 164 | |
| 165 | return std::to_string( num ); |
| 166 | } |
| 167 | |
| 168 | void appendModifierToString( std::string & str, const int mod ) |
| 169 | { |
| 170 | if ( mod < 0 ) { |
| 171 | // The minus sign is already present |
| 172 | str.append( " " ); |
| 173 | } |
| 174 | else if ( mod > 0 ) { |
| 175 | str.append( " +" ); |
| 176 | } |
| 177 | |
| 178 | str.append( std::to_string( mod ) ); |
| 179 | } |
| 180 | } |