From 2f95c0aeccc80190cf6cf57bec8bad45496ddb0e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 11 Mar 2021 21:18:50 -0600 Subject: [PATCH] [ox/std] Addressing CLion suggestions --- deps/ox/src/ox/std/string.cpp | 26 ++++++++++---------- deps/ox/src/ox/std/string.hpp | 46 +++++++++++++++++++++-------------- deps/ox/src/ox/std/strops.hpp | 2 +- deps/ox/src/ox/std/trace.cpp | 2 +- deps/ox/src/ox/std/trace.hpp | 2 +- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index 9d5349c5..1ead6fb4 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -47,7 +47,7 @@ String::String(String &&other) noexcept { m_buff = ox::move(other.m_buff); } -const String &String::operator=(const char *str) noexcept { +String &String::operator=(const char *str) noexcept { std::size_t strLen = ox_strlen(str) + 1; m_buff.resize(strLen + 1); memcpy(m_buff.data(), str, strLen); @@ -56,26 +56,26 @@ const String &String::operator=(const char *str) noexcept { return *this; } -const String &String::operator=(char *str) noexcept { +String &String::operator=(char *str) noexcept { return *this = static_cast(str); } -const String &String::operator=(int64_t i) noexcept { +String &String::operator=(int64_t i) noexcept { char str[65] = {}; ox_itoa(i, str); return this->operator=(str); } -const String &String::operator=(const String &src) noexcept { +String &String::operator=(const String &src) noexcept { return *this = src.c_str(); } -const String &String::operator=(const String &&src) noexcept { +String &String::operator=(String &&src) noexcept { m_buff = ox::move(src.m_buff); return *this; } -const String &String::operator+=(const char *str) noexcept { +String &String::operator+=(const char *str) noexcept { std::size_t strLen = ox_strlen(str); auto currentLen = len(); m_buff.resize(m_buff.size() + strLen); @@ -85,21 +85,21 @@ const String &String::operator+=(const char *str) noexcept { return *this; } -const String &String::operator+=(char *str) noexcept { +String &String::operator+=(char *str) noexcept { return *this += static_cast(str); } -const String &String::operator+=(int64_t i) noexcept { +String &String::operator+=(int64_t i) noexcept { char str[65] = {}; ox_itoa(i, str); return this->operator+=(str); } -const String &String::operator+=(const String &src) noexcept { +String &String::operator+=(const String &src) noexcept { return *this += src.c_str(); } -const String String::operator+(const char *str) const noexcept { +String String::operator+(const char *str) const noexcept { std::size_t strLen = ox_strlen(str); auto currentLen = len(); String cpy(currentLen + strLen); @@ -111,17 +111,17 @@ const String String::operator+(const char *str) const noexcept { return cpy; } -const String String::operator+(char *str) const noexcept { +String String::operator+(char *str) const noexcept { return *this + static_cast(str); } -const String String::operator+(int64_t i) const noexcept { +String String::operator+(int64_t i) const noexcept { char str[65] = {}; ox_itoa(i, str); return *this + str; } -const String String::operator+(const String &src) const noexcept { +String String::operator+(const String &src) const noexcept { return *this + src.c_str(); } diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index ea456cf7..cf63298a 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -8,6 +8,10 @@ #pragma once +#if defined(OX_USE_STDLIB) +#include +#endif + #include "memops.hpp" #include "strops.hpp" #include "typetraits.hpp" @@ -22,7 +26,7 @@ class String { public: String() noexcept; - String(std::size_t cap) noexcept; + explicit String(std::size_t cap) noexcept; String(const char *str) noexcept; @@ -32,31 +36,31 @@ class String { String(String&&) noexcept; - const String &operator=(const char *str) noexcept; + String &operator=(const char *str) noexcept; - const String &operator=(char *str) noexcept; + String &operator=(char *str) noexcept; - const String &operator=(int64_t i) noexcept; + String &operator=(int64_t i) noexcept; - const String &operator=(const String &src) noexcept; + String &operator=(const String &src) noexcept; - const String &operator=(const String &&src) noexcept; + String &operator=(String &&src) noexcept; - const String &operator+=(const char *str) noexcept; + String &operator+=(const char *str) noexcept; - const String &operator+=(char *str) noexcept; + String &operator+=(char *str) noexcept; - const String &operator+=(int64_t i) noexcept; + String &operator+=(int64_t i) noexcept; - const String &operator+=(const String &src) noexcept; + String &operator+=(const String &src) noexcept; - const String operator+(const char *str) const noexcept; + String operator+(const char *str) const noexcept; - const String operator+(char *str) const noexcept; + String operator+(char *str) const noexcept; - const String operator+(int64_t i) const noexcept; + String operator+(int64_t i) const noexcept; - const String operator+(const String &src) const noexcept; + String operator+(const String &src) const noexcept; bool operator==(const String &other) const noexcept; @@ -66,19 +70,25 @@ class String { char &operator[](std::size_t i) noexcept; - char *data() noexcept; + [[nodiscard]] char *data() noexcept; - const char *c_str() const noexcept; + [[nodiscard]] const char *c_str() const noexcept; + +#ifdef OX_USE_STDLIB + [[nodiscard]] inline std::string toStdString() const { + return c_str(); + } +#endif /** * Returns the number of characters in this string. */ - std::size_t len() const noexcept; + [[nodiscard]] std::size_t len() const noexcept; /** * Returns the number of bytes used for this string. */ - std::size_t bytes() const noexcept; + [[nodiscard]] std::size_t bytes() const noexcept; }; diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index b78c111e..02c97c44 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -126,7 +126,7 @@ template return retval; } -[[nodiscard]] constexpr ox::Result ox_atoi(const char *str) noexcept { +constexpr ox::Result ox_atoi(const char *str) noexcept { int total = 0; int multiplier = 1; for (auto i = static_cast(ox_strlen(str)) - 1; i != -1; i--) { diff --git a/deps/ox/src/ox/std/trace.cpp b/deps/ox/src/ox/std/trace.cpp index d049b1af..08a0d245 100644 --- a/deps/ox/src/ox/std/trace.cpp +++ b/deps/ox/src/ox/std/trace.cpp @@ -10,7 +10,7 @@ namespace ox::trace { -void logError(const char *file, int line, Error err) { +void logError(const char *file, int line, const Error &err) { if (err) { TraceStream trc(file, line, "ox::error"); trc << "Error:" << err; diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index dccbe342..25859263 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -158,7 +158,7 @@ using TraceStream = OutStream; using TraceStream = NullStream; #endif -void logError(const char *file, int line, Error err); +void logError(const char *file, int line, const Error &err); void init();