From 14ef2b445adcec770defe1abf83af9d22ccf39d8 Mon Sep 17 00:00:00 2001 From: Ekdohibs Date: Tue, 31 May 2016 17:30:11 +0200 Subject: Add colored text (not only colored chat). Add documentation, move files to a proper place and avoid memory leaks. Make it work with most kind of texts, and allow backgrounds too. --- src/util/string.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/util/string.h') diff --git a/src/util/string.h b/src/util/string.h index 40ef3e4d3..c77c5a6f9 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -519,6 +519,38 @@ std::basic_string unescape_enriched(const std::basic_string &s) return output; } +template +std::vector > split(const std::basic_string &s, T delim) +{ + std::vector > tokens; + + std::basic_string current; + bool last_was_escape = false; + for (size_t i = 0; i < s.length(); i++) { + T si = s[i]; + if (last_was_escape) { + current += '\\'; + current += si; + last_was_escape = false; + } else { + if (si == delim) { + tokens.push_back(current); + current = std::basic_string(); + last_was_escape = false; + } else if (si == '\\') { + last_was_escape = true; + } else { + current += si; + last_was_escape = false; + } + } + } + //push last element + tokens.push_back(current); + + return tokens; +} + /** * Checks that all characters in \p to_check are a decimal digits. * -- cgit v1.2.3 From 27aff22a9b68044d3ea51db731597834336effa3 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 11 Jun 2016 03:23:53 -0400 Subject: Random misc. warning fixes and cleanups - Fix unused c_sand member warning in Valleys Mapgen - Fix some code style - Make some std::string params const ref --- src/mapgen_valleys.h | 1 - src/util/string.cpp | 19 +++++++++++-------- src/util/string.h | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) (limited to 'src/util/string.h') diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index a7369ea92..faaffa905 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -125,7 +125,6 @@ private: Noise *noise_valley_profile; content_t c_lava_source; - content_t c_sand; float terrainLevelAtPoint(s16 x, s16 z); diff --git a/src/util/string.cpp b/src/util/string.cpp index 2c4143c76..94064ef93 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -314,7 +314,7 @@ std::string wide_to_narrow(const std::wstring &wcs) #endif -std::string urlencode(std::string str) +std::string urlencode(const std::string &str) { // Encodes non-unreserved URI characters by a percent sign // followed by two hex digits. See RFC 3986, section 2.3. @@ -322,17 +322,18 @@ std::string urlencode(std::string str) std::ostringstream oss(std::ios::binary); for (u32 i = 0; i < str.size(); i++) { unsigned char c = str[i]; - if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') + if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') { oss << c; - else + } else { oss << "%" << url_hex_chars[(c & 0xf0) >> 4] << url_hex_chars[c & 0x0f]; + } } return oss.str(); } -std::string urldecode(std::string str) +std::string urldecode(const std::string &str) { // Inverse of urlencode std::ostringstream oss(std::ios::binary); @@ -343,18 +344,20 @@ std::string urldecode(std::string str) hex_digit_decode(str[i+2], lowvalue)) { oss << (char) ((highvalue << 4) | lowvalue); i += 2; - } - else + } else { oss << str[i]; + } } return oss.str(); } u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask) { - u32 result = 0, mask = 0; + u32 result = 0; + u32 mask = 0; char *s = &str[0]; - char *flagstr, *strpos = NULL; + char *flagstr; + char *strpos = NULL; while ((flagstr = strtok_r(s, ",", &strpos))) { s = NULL; diff --git a/src/util/string.h b/src/util/string.h index c77c5a6f9..8f4ef4711 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -77,8 +77,8 @@ wchar_t *narrow_to_wide_c(const char *str); std::wstring narrow_to_wide(const std::string &mbs); std::string wide_to_narrow(const std::wstring &wcs); -std::string urlencode(std::string str); -std::string urldecode(std::string str); +std::string urlencode(const std::string &str); +std::string urldecode(const std::string &str); u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask); std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask); size_t mystrlcpy(char *dst, const char *src, size_t size); -- cgit v1.2.3 From b11720af459d44a553cb5d23ef776a632fb30a65 Mon Sep 17 00:00:00 2001 From: Rogier-5 Date: Thu, 11 Aug 2016 19:22:40 +0200 Subject: Use the standard to_string() functions for C++11 (#4279) If compiling according to a C++ version before C++11, then define std::to_string ourselves. Add a to_wstring version as well As std::to_string() for floating point types uses %.6f as floating point format converter, instead of %G, it needs special care. To preserve ftos() behavior (which is expected to use the %G format converter), it no longer uses to_string(). --- src/client.cpp | 2 +- src/util/string.h | 47 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'src/util/string.h') diff --git a/src/client.cpp b/src/client.cpp index 4ffcec6ba..483b22caa 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -820,7 +820,7 @@ void Client::initLocalMapSaving(const Address &address, const std::string world_path = porting::path_user + DIR_DELIM + "worlds" + DIR_DELIM + "server_" - + hostname + "_" + to_string(address.getPort()); + + hostname + "_" + std::to_string(address.getPort()); fs::CreateAllDirs(world_path); diff --git a/src/util/string.h b/src/util/string.h index 8f4ef4711..724543a36 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include #define STRINGIFY(x) #x @@ -350,23 +351,57 @@ inline T from_string(const std::string &str) /// Returns a 64-bit signed value represented by the string \p str (decimal). inline s64 stoi64(const std::string &str) { return from_string(str); } -// TODO: Replace with C++11 std::to_string. +#if __cplusplus < 201103L +namespace std { /// Returns a string representing the value \p val. template -inline std::string to_string(T val) +inline string to_string(T val) { - std::ostringstream oss; + ostringstream oss; oss << val; return oss.str(); } +#define DEFINE_STD_TOSTRING_FLOATINGPOINT(T) \ + template <> \ + inline string to_string(T val) \ + { \ + ostringstream oss; \ + oss << std::fixed \ + << std::setprecision(6) \ + << val; \ + return oss.str(); \ + } +DEFINE_STD_TOSTRING_FLOATINGPOINT(float) +DEFINE_STD_TOSTRING_FLOATINGPOINT(double) +DEFINE_STD_TOSTRING_FLOATINGPOINT(long double) + +#undef DEFINE_STD_TOSTRING_FLOATINGPOINT + +/// Returns a wide string representing the value \p val +template +inline wstring to_wstring(T val) +{ + return utf8_to_wide(to_string(val)); +} +} +#endif /// Returns a string representing the decimal value of the 32-bit value \p i. -inline std::string itos(s32 i) { return to_string(i); } +inline std::string itos(s32 i) { return std::to_string(i); } /// Returns a string representing the decimal value of the 64-bit value \p i. -inline std::string i64tos(s64 i) { return to_string(i); } +inline std::string i64tos(s64 i) { return std::to_string(i); } + +// std::to_string uses the '%.6f' conversion, which is inconsistent with +// std::ostream::operator<<() and impractical too. ftos() uses the +// more generic and std::ostream::operator<<()-compatible '%G' format. /// Returns a string representing the decimal value of the float value \p f. -inline std::string ftos(float f) { return to_string(f); } +inline std::string ftos(float f) +{ + std::ostringstream oss; + oss << f; + return oss.str(); +} /** -- cgit v1.2.3 From 667975fe3adee935a3f4d2b1a421a295771c664d Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 6 Oct 2016 08:48:20 +0200 Subject: Use more unordered_maps to improve performance in c++11 builds --- src/client.cpp | 9 +++------ src/client.h | 6 +++--- src/clientobject.cpp | 10 ++++------ src/clientobject.h | 3 ++- src/content_cao.cpp | 2 +- src/network/clientpackethandler.cpp | 4 +--- src/script/lua_api/l_mapgen.cpp | 4 ++-- src/server.cpp | 8 ++++---- src/util/string.h | 3 ++- 9 files changed, 22 insertions(+), 27 deletions(-) (limited to 'src/util/string.h') diff --git a/src/client.cpp b/src/client.cpp index a599e21dc..63653998a 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -623,10 +623,8 @@ void Client::step(float dtime) Update positions of sounds attached to objects */ { - for(std::map::iterator - i = m_sounds_to_objects.begin(); - i != m_sounds_to_objects.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_sounds_to_objects.begin(); + i != m_sounds_to_objects.end(); ++i) { int client_id = i->first; u16 object_id = i->second; ClientActiveObject *cao = m_env.getActiveObject(object_id); @@ -645,8 +643,7 @@ void Client::step(float dtime) m_removed_sounds_check_timer = 0; // Find removed sounds and clear references to them std::vector removed_server_ids; - for(std::map::iterator - i = m_sounds_server_to_client.begin(); + for(UNORDERED_MAP::iterator i = m_sounds_server_to_client.begin(); i != m_sounds_server_to_client.end();) { s32 server_id = i->first; int client_id = i->second; diff --git a/src/client.h b/src/client.h index fb479068f..a71c1dcbc 100644 --- a/src/client.h +++ b/src/client.h @@ -663,11 +663,11 @@ private: // Sounds float m_removed_sounds_check_timer; // Mapping from server sound ids to our sound ids - std::map m_sounds_server_to_client; + UNORDERED_MAP m_sounds_server_to_client; // And the other way! - std::map m_sounds_client_to_server; + UNORDERED_MAP m_sounds_client_to_server; // And relations to objects - std::map m_sounds_to_objects; + UNORDERED_MAP m_sounds_to_objects; // Privileges UNORDERED_SET m_privileges; diff --git a/src/clientobject.cpp b/src/clientobject.cpp index a11757ea6..ff3f47187 100644 --- a/src/clientobject.cpp +++ b/src/clientobject.cpp @@ -43,12 +43,11 @@ ClientActiveObject* ClientActiveObject::create(ActiveObjectType type, IGameDef *gamedef, ClientEnvironment *env) { // Find factory function - std::map::iterator n; - n = m_types.find(type); + UNORDERED_MAP::iterator n = m_types.find(type); if(n == m_types.end()) { // If factory is not found, just return. - warningstream<<"ClientActiveObject: No factory for type=" - <<(int)type< +#include "util/cpp11_container.h" /* @@ -103,7 +104,7 @@ protected: ClientEnvironment *m_env; private: // Used for creating objects based on type - static std::map m_types; + static UNORDERED_MAP m_types; }; struct DistanceSortedActiveObject diff --git a/src/content_cao.cpp b/src/content_cao.cpp index a141690f6..33dae6822 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -51,7 +51,7 @@ struct ToolCapabilities; #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" -std::map ClientActiveObject::m_types; +UNORDERED_MAP ClientActiveObject::m_types; SmoothTranslator::SmoothTranslator(): vect_old(0,0,0), diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 48c573da5..6d42edd7d 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -812,9 +812,7 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt) *pkt >> server_id; - std::map::iterator i = - m_sounds_server_to_client.find(server_id); - + UNORDERED_MAP::iterator i = m_sounds_server_to_client.find(server_id); if (i != m_sounds_server_to_client.end()) { int client_id = i->second; m_sound->stopSound(client_id); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index a176f4f52..da8e71cdc 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -244,7 +244,7 @@ bool read_schematic_def(lua_State *L, int index, schem->schemdata = new MapNode[numnodes]; size_t names_base = names->size(); - std::map name_id_map; + UNORDERED_MAP name_id_map; u32 i = 0; for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) { @@ -266,7 +266,7 @@ bool read_schematic_def(lua_State *L, int index, u8 param2 = getintfield_default(L, -1, "param2", 0); //// Find or add new nodename-to-ID mapping - std::map::iterator it = name_id_map.find(name); + UNORDERED_MAP::iterator it = name_id_map.find(name); content_t name_index; if (it != name_id_map.end()) { name_index = it->second; diff --git a/src/server.cpp b/src/server.cpp index a8494f76e..e9983ba11 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -794,7 +794,7 @@ void Server::AsyncRunStep(bool initial_step) // Key = object id // Value = data sent by object - std::map* > buffered_messages; + UNORDERED_MAP* > buffered_messages; // Get active object messages from environment for(;;) { @@ -803,7 +803,7 @@ void Server::AsyncRunStep(bool initial_step) break; std::vector* message_list = NULL; - std::map* >::iterator n; + UNORDERED_MAP* >::iterator n; n = buffered_messages.find(aom.id); if (n == buffered_messages.end()) { message_list = new std::vector; @@ -824,7 +824,7 @@ void Server::AsyncRunStep(bool initial_step) std::string reliable_data; std::string unreliable_data; // Go through all objects in message buffer - for (std::map* >::iterator + for (UNORDERED_MAP* >::iterator j = buffered_messages.begin(); j != buffered_messages.end(); ++j) { // If object is not known by client, skip it @@ -868,7 +868,7 @@ void Server::AsyncRunStep(bool initial_step) m_clients.unlock(); // Clear buffered_messages - for(std::map* >::iterator + for(UNORDERED_MAP* >::iterator i = buffered_messages.begin(); i != buffered_messages.end(); ++i) { delete i->second; diff --git a/src/util/string.h b/src/util/string.h index 724543a36..572c37150 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define UTIL_STRING_HEADER #include "irrlichttypes_bloated.h" +#include "cpp11_container.h" #include #include #include @@ -54,7 +55,7 @@ with this program; if not, write to the Free Software Foundation, Inc., (((unsigned char)(x) < 0xe0) ? 2 : \ (((unsigned char)(x) < 0xf0) ? 3 : 4)) -typedef std::map StringMap; +typedef UNORDERED_MAP StringMap; struct FlagDesc { const char *name; -- cgit v1.2.3