diff --git a/README.md b/README.md index 24874352..f5e61c2c 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ The header-only part of the Lol Engine framework. | `` | simple message logging | ● `lol::msg::info("hello\n");`
● `lol::msg::debug("%d %d\n", x, y);`
● also `lol::msg::error`, `lol::msg::warn` | | `` | threading and timing | ● `lol::thread`
● `lol::queue` (thread-safe FIFO queue)
● `lol::timer` (high precision timer) | | `` | unit test framework | | -| `` | various utilities: environment variables, string formatting, std::map and std::vector extensions… | | +| `` | various utilities: environment variables, std::map and std::vector extensions… | | ## Text utilities diff --git a/include/lol/private/base/string.h b/include/lol/private/base/string.h index bba41136..07924eef 100644 --- a/include/lol/private/base/string.h +++ b/include/lol/private/base/string.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010–2023 Sam Hocevar +// Copyright © 2010–2024 Sam Hocevar // © 2013–2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -21,11 +21,11 @@ #include "../features.h" +#include // std::format #include // std::vector #include // std::basic_string #include // std::transform #include // std::back_inserter -#include // va_list #include // size_t namespace lol @@ -47,7 +47,7 @@ std::vector> split(std::basic_string const &s, return ret; } -// Split a string along multiple separator +// Split a string along multiple separator characters template std::vector> split(std::basic_string const &s, std::basic_string const &seps) @@ -148,45 +148,4 @@ std::basic_string toupper(T const *s) return toupper(std::basic_string(s)); } -// Format a string, printf-style -template -std::basic_string vformat(char const *fmt, va_list ap) -{ - va_list ap2; -#if defined va_copy || !defined _MSC_VER - // Visual Studio 2010 does not support va_copy. - va_copy(ap2, ap); -#else - ap2 = ap; -#endif - - // vsnprintf() tells us how many characters we need, not counting - // the terminating null character. - size_t needed = vsnprintf(nullptr, 0, fmt, ap2); - -#if defined va_copy || !defined _MSC_VER - // do not call va_end() if va_copy() wasn't called. - va_end(ap2); -#endif - - std::string ret; - ret.resize(needed); - vsnprintf(&ret[0], needed + 1, fmt, ap); - - return ret; -} - -// XXX: we cheat by setting the argument time to char instead of T, because -// I found no other way to use the printf attribute. -template -std::basic_string lol_attr_printf_format(1, 2) format(char const *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - std::basic_string ret = vformat(fmt, ap); - va_end(ap); - return ret; -} - } // namespace lol - diff --git a/include/lol/private/features.h b/include/lol/private/features.h index a2ecd8ba..64704f7d 100644 --- a/include/lol/private/features.h +++ b/include/lol/private/features.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2020 Sam Hocevar +// Copyright © 2010–2024 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -42,13 +42,3 @@ # endif #endif - - -// Define some attribute macros - -#ifdef __GNUC__ -# define lol_attr_printf_format(n, p) __attribute__((format(printf, n, p))) -#else -# define lol_attr_printf_format(n, p) -#endif - diff --git a/include/lol/private/math/matrix.ipp b/include/lol/private/math/matrix.ipp index 01f31d6e..c60dc993 100644 --- a/include/lol/private/math/matrix.ipp +++ b/include/lol/private/math/matrix.ipp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2020 Sam Hocevar +// Copyright © 2010–2024 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -15,6 +15,7 @@ #include #include // std::tan #include // std::max +#include // std::format namespace lol { @@ -24,8 +25,8 @@ inline std::string mat2::tostring() const { mat2 const &p = *this; - return format("[ %6.6f %6.6f\n", p[0][0], p[1][0]) + - format(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); + return std::format("[ {:6.6f} {:6.6f}\n", p[0][0], p[1][0]) + + std::format(" {:6.6f} {:6.6f} ]\n", p[0][1], p[1][1]); } template<> @@ -33,9 +34,9 @@ inline std::string mat3::tostring() const { mat3 const &p = *this; - return format("[ %6.6f %6.6f %6.6f\n", p[0][0], p[1][0], p[2][0]) + - format(" %6.6f %6.6f %6.6f\n", p[0][1], p[1][1], p[2][1]) + - format(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); + return std::format("[ {:6.6f} {:6.6f} {:6.6f}\n", p[0][0], p[1][0], p[2][0]) + + std::format(" {:6.6f} {:6.6f} {:6.6f}\n", p[0][1], p[1][1], p[2][1]) + + std::format(" {:6.6f} {:6.6f} {:6.6f} ]\n", p[0][2], p[1][2], p[2][2]); } template<> @@ -43,14 +44,14 @@ inline std::string mat4::tostring() const { mat4 const &p = *this; - return format("[ %6.6f %6.6f %6.6f %6.6f\n", - p[0][0], p[1][0], p[2][0], p[3][0]) + - format(" %6.6f %6.6f %6.6f %6.6f\n", - p[0][1], p[1][1], p[2][1], p[3][1]) + - format(" %6.6f %6.6f %6.6f %6.6f\n", - p[0][2], p[1][2], p[2][2], p[3][2]) + - format(" %6.6f %6.6f %6.6f %6.6f ]\n", - p[0][3], p[1][3], p[2][3], p[3][3]); + return std::format("[ {:6.6f} {:6.6f} {:6.6f} {:6.6f}\n", + p[0][0], p[1][0], p[2][0], p[3][0]) + + std::format(" {:6.6f} {:6.6f} {:6.6f} {:6.6f}\n", + p[0][1], p[1][1], p[2][1], p[3][1]) + + std::format(" {:6.6f} {:6.6f} {:6.6f} {:6.6f}\n", + p[0][2], p[1][2], p[2][2], p[3][2]) + + std::format(" {:6.6f} {:6.6f} {:6.6f} {:6.6f} ]\n", + p[0][3], p[1][3], p[2][3], p[3][3]); } template diff --git a/include/lol/private/math/transform.ipp b/include/lol/private/math/transform.ipp index 800470f6..3e0fe862 100644 --- a/include/lol/private/math/transform.ipp +++ b/include/lol/private/math/transform.ipp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2020 Sam Hocevar +// Copyright © 2010–2024 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,7 +12,8 @@ #pragma once -#include // std::cos, std::sin +#include // std::cos, std::sin +#include // std::format namespace lol { @@ -20,13 +21,13 @@ namespace lol template<> inline std::string cmplx::tostring() const { - return format("[ %6.6f %6.6f ]", x, y); + return std::format("[ {:6.6f} {:6.6f} ]", x, y); } template<> inline std::string quat::tostring() const { - return format("[ %6.6f %6.6f %6.6f %6.6f ]", w, x, y, z); + return std::format("[ {:6.6f} {:6.6f} {:6.6f} {:6.6f} ]", w, x, y, z); } @@ -291,5 +292,4 @@ DEFINE_GENERIC_EULER_CONVERSIONS(z, y, x) #undef DEFINE_GENERIC_EULER_CONVERSIONS #undef DEFINE_GENERIC_EULER_CONVERSIONS_INNER -} /* namespace lol */ - +} // namespace lol diff --git a/include/lol/private/math/vector.ipp b/include/lol/private/math/vector.ipp index 32aafe44..0644d420 100644 --- a/include/lol/private/math/vector.ipp +++ b/include/lol/private/math/vector.ipp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2020 Sam Hocevar +// Copyright © 2010–2024 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,7 +12,7 @@ #pragma once -#include "../base/string.h" // lol::format +#include // std::format namespace lol { @@ -20,38 +20,37 @@ namespace lol template<> inline std::string vec2::tostring() const { - return format("[ %6.6f %6.6f ]", x, y); + return std::format("[ {:6.6f} {:6.6f} ]", x, y); } template<> inline std::string ivec2::tostring() const { - return format("[ %i %i ]", x, y); + return std::format("[ {} {} ]", x, y); } template<> inline std::string vec3::tostring() const { - return format("[ %6.6f %6.6f %6.6f ]", x, y, z); + return std::format("[ {:6.6f} {:6.6f} {:6.6f} ]", x, y, z); } template<> inline std::string ivec3::tostring() const { - return format("[ %i %i %i ]", x, y, z); + return std::format("[ {} {} {} ]", x, y, z); } template<> inline std::string vec4::tostring() const { - return format("[ %6.6f %6.6f %6.6f %6.6f ]", x, y, z, w); + return std::format("[ {:6.6f} {:6.6f} {:6.6f} {:6.6f} ]", x, y, z, w); } template<> inline std::string ivec4::tostring() const { - return format("[ %i %i %i %i ]", x, y, z, w); + return std::format("[ {} {} {} {} ]", x, y, z, w); } -} /* namespace lol */ - +} // namespace lol