ko4life-net / ko

Open source development of the game Knight Online. This is a reversed engineered old version of the game aiming to replicate the nostalgic experience we all once had <3
MIT License
53 stars 21 forks source link

Replace sprintf usages with C++20's std::format #81

Open stevewgr opened 2 years ago

stevewgr commented 2 years ago

Description

Searching for instances of sprintf in code, you will see that there are some usages where we create a C char array in order to format it with sprintf and then copy it to std::string. This is wasteful and not intiutive, especially when sprintf function is vulnerable to buffer-overflow exploits. C++20's std::format comes built-in into the standard: https://en.cppreference.com/w/cpp/utility/format/format Adapted from the old and famous fmt library: https://github.com/fmtlib/fmt

Apart from making the code more elegant, it also wastes less resources and often increase in speed.

However note that the project currently uses C++17 and migrating from C++17 to C++20 brings more strict constructs to the code base. So first we need to safely upgrade to C++20 before we can tackle this ticket. Some WIP is done here: https://github.com/stevewgr/ko/tree/cpp20