Compare commits

...

4 Commits

Author SHA1 Message Date
18bb50626d [ox/std] Add String::append(StringView), cleanup
All checks were successful
Build / build (push) Successful in 2m30s
2024-05-03 00:07:03 -05:00
6a4b48221f [nostalgia,studio] Fixes for Ox changes 2024-05-03 00:00:05 -05:00
d2a3cfa72e [ox/std] Remove append operators from IString
This is because the append operators cannot report the failure that is possible with IString
2024-05-02 23:59:19 -05:00
7c4e2a6564 [ox/std] Cleanup IString 2024-05-02 23:33:39 -05:00
7 changed files with 48 additions and 81 deletions

View File

@ -195,9 +195,9 @@ constexpr StringType sfmt(StringView fmt, Args&&... args) noexcept {
std::ignore = out.append(firstSegment.str, firstSegment.length);
const detail::FmtArg elements[sizeof...(args)] = {args...};
for (size_t i = 0; i < fmtSegments.size - 1; ++i) {
out += elements[i].out;
std::ignore = out.append(elements[i].out);
const auto &s = fmtSegments.segments[i + 1];
std::ignore = out.append(s.str, s.length);
std::ignore = out.append(s.str);
}
return out;
}

View File

@ -8,7 +8,7 @@
#pragma once
#include "ignore.hpp"
#include "array.hpp"
#include "memops.hpp"
#include "ox/std/error.hpp"
#include "stringview.hpp"
@ -20,7 +20,7 @@ namespace ox {
template<std::size_t StrCap>
class IString {
private:
char m_buff[StrCap + 1];
ox::Array<char, StrCap + 1> m_buff;
size_t m_size{};
public:
@ -38,19 +38,6 @@ class IString {
constexpr IString &operator=(Integer_c auto i) noexcept;
constexpr IString &operator+=(const char *str) noexcept;
constexpr IString &operator+=(char *str) noexcept;
constexpr IString &operator+=(Integer_c auto i) noexcept;
constexpr IString &operator+=(StringView s) noexcept;
constexpr IString operator+(const char *str) const noexcept;
constexpr IString operator+(char *str) const noexcept;
constexpr IString operator+(Integer_c auto i) const noexcept;
constexpr bool operator==(const char *other) const noexcept;
@ -66,6 +53,8 @@ class IString {
constexpr Error append(const char *str, std::size_t strLen) noexcept;
constexpr Error append(ox::StringView str) noexcept;
[[nodiscard]]
constexpr const char *data() const noexcept;
@ -112,9 +101,9 @@ constexpr IString<size>::IString(const char *str) noexcept: m_buff{{0}} {
template<std::size_t size>
constexpr IString<size> &IString<size>::operator=(Integer_c auto i) noexcept {
char str[65] = {};
ox::itoa(i, str);
return this->operator=(str);
ox::Array<char, 65> str{};
ox::itoa(i, str.data());
return this->operator=(str.data());
}
template<std::size_t size>
@ -124,7 +113,7 @@ constexpr IString<size> &IString<size>::operator=(ox::CRStringView str) noexcept
strLen = cap();
}
m_size = strLen;
ox::listcpy(m_buff, str.data(), strLen);
ox::listcpy(m_buff.data(), str.data(), strLen);
// make sure last element is a null terminator
m_buff[strLen] = 0;
return *this;
@ -137,7 +126,7 @@ constexpr IString<size> &IString<size>::operator=(const char *str) noexcept {
strLen = cap();
}
m_size = strLen;
ox::listcpy(m_buff, str, strLen);
ox::listcpy(m_buff.data(), str, strLen);
// make sure last element is a null terminator
m_buff[cap()] = 0;
return *this;
@ -148,52 +137,6 @@ constexpr IString<size> &IString<size>::operator=(char *str) noexcept {
return *this = static_cast<const char*>(str);
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(const char *str) noexcept {
std::size_t strLen = ox::strlen(str) + 1;
std::ignore = append(str, strLen);
return *this;
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(char *str) noexcept {
return *this += static_cast<const char*>(str);
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(Integer_c auto i) noexcept {
char str[65] = {};
ox::itoa(i, str);
return this->operator+=(str);
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(StringView s) noexcept {
std::size_t strLen = s.bytes();
std::ignore = append(s.data(), strLen);
return *this;
}
template<std::size_t size>
constexpr IString<size> IString<size>::operator+(const char *str) const noexcept {
auto out = *this;
std::size_t strLen = ox::strlen(str) + 1;
std::ignore = out.append(str, strLen);
return out;
}
template<std::size_t size>
constexpr IString<size> IString<size>::operator+(char *str) const noexcept {
return *this + static_cast<const char*>(str);
}
template<std::size_t size>
constexpr IString<size> IString<size>::operator+(Integer_c auto i) const noexcept {
char str[65] = {};
ox::itoa(i, str);
return this->operator+(str);
}
template<std::size_t StrCap>
constexpr bool IString<StrCap>::operator==(const char *other) const noexcept {
return ox::StringView(*this) == other;
@ -226,13 +169,28 @@ constexpr char &IString<StrCap>::operator[](std::size_t i) noexcept {
template<std::size_t StrCap>
constexpr Error IString<StrCap>::append(const char *str, std::size_t strLen) noexcept {
Error err;
Error err{};
auto currentLen = len();
if (cap() < currentLen + strLen + 1) {
strLen = cap() - currentLen;
err = OxError(1, "Insufficient space for full string");
}
ox::strncpy(m_buff + currentLen, str, strLen);
ox::strncpy(m_buff.data() + currentLen, str, strLen);
// make sure last element is a null terminator
m_buff[currentLen + strLen] = 0;
return err;
}
template<std::size_t StrCap>
constexpr Error IString<StrCap>::append(ox::StringView str) noexcept {
auto strLen = str.len();
Error err{};
auto currentLen = len();
if (cap() < currentLen + strLen + 1) {
strLen = cap() - currentLen;
err = OxError(1, "Insufficient space for full string");
}
ox::strncpy(m_buff.data() + currentLen, str, strLen);
// make sure last element is a null terminator
m_buff[currentLen + strLen] = 0;
return err;
@ -240,17 +198,17 @@ constexpr Error IString<StrCap>::append(const char *str, std::size_t strLen) noe
template<std::size_t StrCap>
constexpr const char *IString<StrCap>::data() const noexcept {
return static_cast<const char*>(m_buff);
return static_cast<const char*>(m_buff.data());
}
template<std::size_t StrCap>
constexpr char *IString<StrCap>::data() noexcept {
return static_cast<char*>(m_buff);
return static_cast<char*>(m_buff.data());
}
template<std::size_t StrCap>
constexpr const char *IString<StrCap>::c_str() const noexcept {
return static_cast<const char*>(m_buff);
return static_cast<const char*>(m_buff.data());
}
@ -258,7 +216,7 @@ template<std::size_t StrCap>
constexpr std::size_t IString<StrCap>::len() const noexcept {
std::size_t length = 0;
for (std::size_t i = 0; i < StrCap; i++) {
uint8_t b = static_cast<uint8_t>(m_buff[i]);
auto const b = static_cast<uint8_t>(m_buff[i]);
if (b) {
const auto asciiChar = (b & 128) == 0;
const auto utf8Char = (b & (256 << 6)) == (256 << 6);

View File

@ -178,6 +178,10 @@ class BasicString {
return OxError(0);
}
constexpr Error append(ox::StringView sv) noexcept {
return append(sv.data(), sv.len());
}
[[nodiscard]]
constexpr BasicString substr(std::size_t pos) const noexcept;

View File

@ -6,6 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ox/std/def.hpp"
#undef NDEBUG
#include <map>
@ -69,10 +70,10 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
"BString",
[]() {
ox::IString<5> s;
s += "A";
s += "B";
s += 9;
s += "C";
oxReturnError(s.append("A"));
oxReturnError(s.append("B"));
oxReturnError(s.append("9"));
oxReturnError(s.append("C"));
oxAssert(s == "AB9C", "BString append broken");
s = "asdf";
oxAssert(s == "asdf", "String assign broken");

View File

@ -8,6 +8,7 @@
#pragma once
#include "ignore.hpp"
#include "istring.hpp"
#include "buffer.hpp"
#include "random.hpp"

View File

@ -32,8 +32,7 @@ void panic(const char *file, int line, const char *panicMsg, ox::Error const&err
std::ignore = initConsole(*ctx);
setBgStatus(*ctx, 0, true);
clearBg(*ctx, 0);
ox::IString<23> serr = "Error code: ";
serr += static_cast<int64_t>(err);
auto const serr = ox::sfmt<ox::IString<23>>("Error code: {}", static_cast<int64_t>(err));
puts(*ctx, 32 + 1, 1, "SADNESS...");
puts(*ctx, 32 + 1, 4, "UNEXPECTED STATE:");
puts(*ctx, 32 + 2, 6, panicMsg);

View File

@ -8,6 +8,8 @@
#include <ox/std/point.hpp>
#include <keel/media.hpp>
#include "ox/std/buffer.hpp"
#include "ox/std/cstrops.hpp"
#include "tilesheeteditor-imgui.hpp"
namespace nostalgia::core {
@ -440,7 +442,9 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept {
ImGui::PushID(static_cast<int>(i));
// Column: color idx
ImGui::TableNextColumn();
auto const label = ox::IString<8>() + (i + 1);
ox::IString<8> label;
ox::CharBuffWriter w(label.data(), label.bytes());
std::ignore = ox::writeItoa(i + 1, w);
auto const rowSelected = i == m_view.palIdx();
if (ImGui::Selectable(label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) {
m_view.setPalIdx(i);