summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/fud_result.hpp2
-rw-r--r--include/fud_string.hpp9
-rw-r--r--include/libfud.hpp36
-rw-r--r--source/fud_string.cpp14
-rw-r--r--source/libfud.cpp21
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<typename T, typename E>
-class Result {
+class [[nodiscard]] Result {
public:
using ResultType = Result<T, E>;
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<utf8> 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<const utf8*>(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<String, FudStatus> getEnv(const char* name);
-#include "fud_utf8_iterator.hpp" // IWYU pragma: export
+template<typename T>
+concept CStringRepr = requires(T a)
+{
+ { a.c_str() } -> std::convertible_to<const char*>;
+};
-namespace fud {
-
-void fud();
+template <CStringRepr T>
+Result<String, FudStatus> 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<utf8> String::back()
+{
+ if (!valid()) {
+ return std::nullopt;
+ }
+
+ utf8 backChar = data()[m_length - 1];
+ if (Ascii::valid(backChar)) {
+ return backChar;
+ }
+
+ return std::nullopt;
+}
+
std::optional<utf8> 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 <cstdlib>
+
namespace fud {
-void fud()
+Result<String, FudStatus> getEnv(const char* name)
{
+ using RetType = Result<String, FudStatus>;
+
+ 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