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. --- source/fud_string.cpp | 14 ++++++++++++++ source/libfud.cpp | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) (limited to 'source') 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