@@ -31,7 +31,7 @@ The header-only part of the Lol Engine framework. | |||
| `<lol/msg>` | simple message logging | ● `lol::msg::info("hello\n");`<br>● `lol::msg::debug("%d %d\n", x, y);`<br>● also `lol::msg::error`, `lol::msg::warn` | | |||
| `<lol/thread>` | threading and timing | ● `lol::thread`<br>● `lol::queue<int, 200>` (thread-safe FIFO queue)<br>● `lol::timer` (high precision timer) | | |||
| `<lol/unit_test>` | unit test framework | | | |||
| `<lol/utils>` | various utilities: environment variables, string formatting, std::map and std::vector extensions… | | | |||
| `<lol/utils>` | various utilities: environment variables, std::map and std::vector extensions… | | | |||
## Text utilities | |||
@@ -1,7 +1,7 @@ | |||
// | |||
// Lol Engine | |||
// | |||
// Copyright © 2010–2023 Sam Hocevar <sam@hocevar.net> | |||
// Copyright © 2010–2024 Sam Hocevar <sam@hocevar.net> | |||
// © 2013–2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com> | |||
// | |||
// Lol Engine is free software. It comes without any warranty, to | |||
@@ -21,11 +21,11 @@ | |||
#include "../features.h" | |||
#include <format> // std::format | |||
#include <vector> // std::vector | |||
#include <string> // std::basic_string | |||
#include <algorithm> // std::transform | |||
#include <iterator> // std::back_inserter | |||
#include <cstdarg> // va_list | |||
#include <cctype> // size_t | |||
namespace lol | |||
@@ -47,7 +47,7 @@ std::vector<std::basic_string<T>> split(std::basic_string<T> const &s, | |||
return ret; | |||
} | |||
// Split a string along multiple separator | |||
// Split a string along multiple separator characters | |||
template<typename T> | |||
std::vector<std::basic_string<T>> split(std::basic_string<T> const &s, | |||
std::basic_string<T> const &seps) | |||
@@ -148,45 +148,4 @@ std::basic_string<T> toupper(T const *s) | |||
return toupper(std::basic_string<T>(s)); | |||
} | |||
// Format a string, printf-style | |||
template<typename T = char> | |||
std::basic_string<T> 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<typename T = char> | |||
std::basic_string<T> lol_attr_printf_format(1, 2) format(char const *fmt, ...) | |||
{ | |||
va_list ap; | |||
va_start(ap, fmt); | |||
std::basic_string<T> ret = vformat(fmt, ap); | |||
va_end(ap); | |||
return ret; | |||
} | |||
} // namespace lol | |||
@@ -1,7 +1,7 @@ | |||
// | |||
// Lol Engine | |||
// | |||
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net> | |||
// Copyright © 2010–2024 Sam Hocevar <sam@hocevar.net> | |||
// | |||
// 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 | |||
@@ -1,7 +1,7 @@ | |||
// | |||
// Lol Engine | |||
// | |||
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net> | |||
// Copyright © 2010–2024 Sam Hocevar <sam@hocevar.net> | |||
// | |||
// 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 <cassert> | |||
#include <cmath> // std::tan | |||
#include <algorithm> // std::max | |||
#include <format> // 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<typename T> | |||
@@ -1,7 +1,7 @@ | |||
// | |||
// Lol Engine | |||
// | |||
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net> | |||
// Copyright © 2010–2024 Sam Hocevar <sam@hocevar.net> | |||
// | |||
// 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 <cmath> // std::cos, std::sin | |||
#include <cmath> // std::cos, std::sin | |||
#include <format> // 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 |
@@ -1,7 +1,7 @@ | |||
// | |||
// Lol Engine | |||
// | |||
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net> | |||
// Copyright © 2010–2024 Sam Hocevar <sam@hocevar.net> | |||
// | |||
// 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 <format> // 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 |