From 255fa256b106506e0c951f704314c5c633217468 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 25 Sep 2024 11:25:25 -0500 Subject: Further expansion of string api. --- include/fud_result.hpp | 2 +- include/fud_string.hpp | 9 ++++++++- include/libfud.hpp | 36 +++++++++++++++--------------------- source/fud_string.cpp | 14 ++++++++++++++ source/libfud.cpp | 21 ++++++++++++++++++++- 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/include/fud_result.hpp b/include/fud_result.hpp index 9c69800..e4e36cf 100644 --- a/include/fud_result.hpp +++ b/include/fud_result.hpp @@ -24,7 +24,7 @@ namespace fud { template -class Result { +class [[nodiscard]] Result { public: using ResultType = Result; static ResultType okay(const T& okay) diff --git a/include/fud_string.hpp b/include/fud_string.hpp index 5229f26..f7e4813 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -47,6 +47,11 @@ class String { return m_length; } + [[nodiscard]] constexpr bool empty() const + { + return m_length == 0; + } + [[nodiscard]] constexpr size_t size() const { return m_length + 1; @@ -89,6 +94,8 @@ class String { [[nodiscard]] FudStatus nullTerminate(); + [[nodiscard]] std::optional back(); + [[nodiscard]] constexpr size_t remainingLength() const { if (m_length >= m_capacity) { @@ -136,7 +143,7 @@ class StringView { } StringView(size_t strLen, const char* strData) : - m_length(strLen), // line break + m_length(strLen), // line break m_data{reinterpret_cast(strData)} // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) { } diff --git a/include/libfud.hpp b/include/libfud.hpp index ceb1b20..a0b2909 100644 --- a/include/libfud.hpp +++ b/include/libfud.hpp @@ -18,31 +18,25 @@ #ifndef LIBFUD_HPP #define LIBFUD_HPP -#include "fud_status.hpp" // IWYU pragma: export +#include "fud_status.hpp" +#include "fud_result.hpp" +#include "fud_string.hpp" -#include "fud_result.hpp" // IWYU pragma: export - -#include "fud_memory.hpp" // IWYU pragma: export - -#include "fud_assert.hpp" // IWYU pragma: export - -#include "fud_array.hpp" // IWYU pragma: export - -#include "fud_c_file.hpp" // IWYU pragma: export - -#include "fud_fud_type_traits.hpp" // IWYU pragma: export - -#include "fud_string.hpp" // IWYU pragma: export - -#include "fud_unique_array.hpp" // IWYU pragma: export +namespace fud { -#include "fud_utf8.hpp" // IWYU pragma: export +Result getEnv(const char* name); -#include "fud_utf8_iterator.hpp" // IWYU pragma: export +template +concept CStringRepr = requires(T a) +{ + { a.c_str() } -> std::convertible_to; +}; -namespace fud { - -void fud(); +template +Result getEnv(const T& name) +{ + return getEnv(name.c_str()); +} } diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 27496ff..57bf1e0 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -157,6 +157,20 @@ FudStatus String::nullTerminate() return FudStatus::StringInvalid; } +[[nodiscard]] std::optional String::back() +{ + if (!valid()) { + return std::nullopt; + } + + utf8 backChar = data()[m_length - 1]; + if (Ascii::valid(backChar)) { + return backChar; + } + + return std::nullopt; +} + std::optional String::pop() { if (m_length < 1) { diff --git a/source/libfud.cpp b/source/libfud.cpp index 834082e..e1dad1d 100644 --- a/source/libfud.cpp +++ b/source/libfud.cpp @@ -17,10 +17,29 @@ #include "libfud.hpp" +#include + namespace fud { -void fud() +Result getEnv(const char* name) { + using RetType = Result; + + if (name == nullptr) { + return RetType::error(FudStatus::NullPointer); + } + + const char* resultString = getenv(name); + if (resultString == nullptr) { + return RetType::error(FudStatus::NotFound); + } + + String envVar{resultString}; + if (!envVar.valid()) { + return RetType::error(FudStatus::Failure); + } + + return RetType::okay(std::move(envVar)); } } // namespace fud -- cgit v1.2.3