aboutsummaryrefslogtreecommitdiff
path: root/include/cru
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-10-17 10:01:30 +0800
committerYuqian Yang <crupest@crupest.life>2025-10-17 10:01:30 +0800
commitfaf77949e19dc0d01f75bf8abb783eda70328048 (patch)
tree0448c642573157d79a984ab19b3396210e91acca /include/cru
parent9e4419826b3e23c63567591701a2834a837da98e (diff)
downloadcru-faf77949e19dc0d01f75bf8abb783eda70328048.tar.gz
cru-faf77949e19dc0d01f75bf8abb783eda70328048.tar.bz2
cru-faf77949e19dc0d01f75bf8abb783eda70328048.zip
Platform base no String.
Diffstat (limited to 'include/cru')
-rw-r--r--include/cru/base/StringUtil.h21
-rw-r--r--include/cru/platform/Color.h21
-rw-r--r--include/cru/platform/GraphicsBase.h49
3 files changed, 43 insertions, 48 deletions
diff --git a/include/cru/base/StringUtil.h b/include/cru/base/StringUtil.h
index d79c0c23..0ce3802f 100644
--- a/include/cru/base/StringUtil.h
+++ b/include/cru/base/StringUtil.h
@@ -3,8 +3,8 @@
#include "Bitmask.h"
#include <compare>
+#include <format>
#include <functional>
-#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
@@ -30,6 +30,25 @@ struct SplitOptions {
std::vector<std::string> Split(std::string_view str, std::string_view sep,
SplitOption options = {});
+
+template <typename T>
+struct ImplementFormatterByToString {
+ template <class ParseContext>
+ constexpr ParseContext::iterator parse(ParseContext& ctx) const {
+ auto iter = ctx.begin();
+ if (*iter != '}') {
+ throw std::format_error(
+ "ImplementFormatterByToString does not accept format args.");
+ }
+ return iter;
+ }
+
+ template <class FmtContext>
+ FmtContext::iterator format(const T& object, FmtContext& ctx) const {
+ return std::ranges::copy(object.ToString(), ctx.out()).out;
+ }
+};
+
} // namespace string
using CodePoint = std::int32_t;
diff --git a/include/cru/platform/Color.h b/include/cru/platform/Color.h
index 54308bbd..53252cc4 100644
--- a/include/cru/platform/Color.h
+++ b/include/cru/platform/Color.h
@@ -1,13 +1,11 @@
#pragma once
#include "Base.h"
-#include "cru/base/Base.h"
-#include "cru/base/String.h"
+#include <cru/base/Base.h>
#include <cstdint>
-#include <format>
#include <optional>
-#include <unordered_map>
+#include <string_view>
namespace cru::platform {
struct CRU_PLATFORM_API Color {
@@ -38,14 +36,14 @@ struct CRU_PLATFORM_API Color {
float GetFloatBlue() const { return static_cast<float>(blue) / 255.f; }
float GetFloatAlpha() const { return static_cast<float>(alpha) / 255.f; }
- String ToString() const;
+ std::string ToString() const;
std::uint8_t red;
std::uint8_t green;
std::uint8_t blue;
std::uint8_t alpha;
- static std::optional<Color> Parse(StringView string,
+ static std::optional<Color> Parse(std::string_view string,
bool parse_predefined_color = true);
};
@@ -251,16 +249,7 @@ struct std::hash<cru::platform::Color> {
};
namespace cru::platform {
-namespace details {
-extern const std::unordered_map<StringView, Color> predefined_name_color_map;
-} // namespace details
-
-std::optional<Color> GetPredefinedColorByName(StringView name);
-
-inline String ToString(const Color& color) {
- return String::FromUtf8(std::format("rgba({}, {}, {}, {})", color.red,
- color.green, color.blue, color.alpha));
-}
+std::optional<Color> GetPredefinedColorByName(std::string_view name);
struct CRU_PLATFORM_API HslColor {
HslColor() = default;
diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h
index 79e539a3..23a95865 100644
--- a/include/cru/platform/GraphicsBase.h
+++ b/include/cru/platform/GraphicsBase.h
@@ -1,9 +1,8 @@
#pragma once
#include "Base.h"
-#include "cru/base/Format.h"
-#include "cru/base/Range.h"
-#include "cru/base/String.h"
+#include <cru/base/Range.h>
+#include <cru/base/StringUtil.h>
#include <format>
#include <limits>
@@ -24,6 +23,10 @@ struct Point final {
constexpr Point Negate() const { return Point(-x, -y); }
+ std::string ToString() const {
+ return std::format("Point(x: {}, y: {})", x, y);
+ }
+
float x = 0;
float y = 0;
};
@@ -40,14 +43,6 @@ constexpr bool operator==(const Point& left, const Point& right) {
return left.x == right.x && left.y == right.y;
}
-inline std::string ToUtf8String(const Point& point) {
- return std::format("Point(x: {}, y: {})", point.x, point.y);
-}
-
-inline String ToString(const Point& point) {
- return String::FromUtf8(ToUtf8String(point));
-}
-
struct Size final {
static CRU_PLATFORM_API const Size kMax;
static CRU_PLATFORM_API const Size kZero;
@@ -63,6 +58,10 @@ struct Size final {
std::numeric_limits<float>::max()};
}
+ std::string ToString() const {
+ return std::format("Size(width: {}, height: {})", width, height);
+ }
+
float width = 0;
float height = 0;
};
@@ -81,14 +80,6 @@ constexpr bool operator==(const Size& left, const Size& right) {
return left.width == right.width && left.height == right.height;
}
-inline std::string ToUtf8String(const Size& size) {
- return std::format("Size(width: {}, height: {})", size.width, size.height);
-}
-
-inline String ToString(const Size& size) {
- return String::FromUtf8(ToUtf8String(size));
-}
-
struct Thickness final {
constexpr Thickness() : Thickness(0) {}
@@ -230,6 +221,11 @@ struct Rect final {
return result;
}
+ std::string ToString() const {
+ return std::format("Rect(left: {}, top: {}, width: {}, height: {})", left,
+ top, width, height);
+ }
+
float left = 0.0f;
float top = 0.0f;
float width = 0.0f;
@@ -241,15 +237,6 @@ constexpr bool operator==(const Rect& left, const Rect& right) {
left.width == right.width && left.height == right.height;
}
-inline std::string ToUtf8String(const Rect& rect) {
- return std::format("Rect(left: {}, top: {}, width: {}, height: {})",
- rect.left, rect.top, rect.width, rect.height);
-}
-
-inline String ToString(const Rect& rect) {
- return String::FromUtf8(ToUtf8String(rect));
-}
-
struct RoundedRect final {
constexpr RoundedRect() = default;
constexpr RoundedRect(const Rect& rect, const float radius_x,
@@ -295,12 +282,12 @@ using TextRange = Range;
template <>
struct std::formatter<cru::platform::Point, char>
- : cru::ImplementFormatterByToUtf8String<cru::platform::Point> {};
+ : cru::string::ImplementFormatterByToString<cru::platform::Point> {};
template <>
struct std::formatter<cru::platform::Size, char>
- : cru::ImplementFormatterByToUtf8String<cru::platform::Size> {};
+ : cru::string::ImplementFormatterByToString<cru::platform::Size> {};
template <>
struct std::formatter<cru::platform::Rect, char>
- : cru::ImplementFormatterByToUtf8String<cru::platform::Rect> {};
+ : cru::string::ImplementFormatterByToString<cru::platform::Rect> {};