Compare commits

..

No commits in common. "18bb50626d38c1dca2e01e3a8127eb36f5c23e97" and "e30ebce4c046ea2d610ae867cb6feaea5740f891" have entirely different histories.

7 changed files with 81 additions and 48 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) {
std::ignore = out.append(elements[i].out);
out += elements[i].out;
const auto &s = fmtSegments.segments[i + 1];
std::ignore = out.append(s.str);
std::ignore = out.append(s.str, s.length);
}
return out;
}

View File

@ -8,7 +8,7 @@
#pragma once
#include "array.hpp"
#include "ignore.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:
ox::Array<char, StrCap + 1> m_buff;
char m_buff[StrCap + 1];
size_t m_size{};
public:
@ -38,6 +38,19 @@ 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;
@ -53,8 +66,6 @@ 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;
@ -101,9 +112,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 {
ox::Array<char, 65> str{};
ox::itoa(i, str.data());
return this->operator=(str.data());
char str[65] = {};
ox::itoa(i, str);
return this->operator=(str);
}
template<std::size_t size>
@ -113,7 +124,7 @@ constexpr IString<size> &IString<size>::operator=(ox::CRStringView str) noexcept
strLen = cap();
}
m_size = strLen;
ox::listcpy(m_buff.data(), str.data(), strLen);
ox::listcpy(m_buff, str.data(), strLen);
// make sure last element is a null terminator
m_buff[strLen] = 0;
return *this;
@ -126,7 +137,7 @@ constexpr IString<size> &IString<size>::operator=(const char *str) noexcept {
strLen = cap();
}
m_size = strLen;
ox::listcpy(m_buff.data(), str, strLen);
ox::listcpy(m_buff, str, strLen);
// make sure last element is a null terminator
m_buff[cap()] = 0;
return *this;
@ -137,6 +148,52 @@ 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;
@ -169,28 +226,13 @@ 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.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);
ox::strncpy(m_buff + currentLen, str, strLen);
// make sure last element is a null terminator
m_buff[currentLen + strLen] = 0;
return err;
@ -198,17 +240,17 @@ constexpr Error IString<StrCap>::append(ox::StringView str) noexcept {
template<std::size_t StrCap>
constexpr const char *IString<StrCap>::data() const noexcept {
return static_cast<const char*>(m_buff.data());
return static_cast<const char*>(m_buff);
}
template<std::size_t StrCap>
constexpr char *IString<StrCap>::data() noexcept {
return static_cast<char*>(m_buff.data());
return static_cast<char*>(m_buff);
}
template<std::size_t StrCap>
constexpr const char *IString<StrCap>::c_str() const noexcept {
return static_cast<const char*>(m_buff.data());
return static_cast<const char*>(m_buff);
}
@ -216,7 +258,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++) {
auto const b = static_cast<uint8_t>(m_buff[i]);
uint8_t 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,10 +178,6 @@ 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,7 +6,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ox/std/def.hpp"
#undef NDEBUG
#include <map>
@ -70,10 +69,10 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
"BString",
[]() {
ox::IString<5> s;
oxReturnError(s.append("A"));
oxReturnError(s.append("B"));
oxReturnError(s.append("9"));
oxReturnError(s.append("C"));
s += "A";
s += "B";
s += 9;
s += "C";
oxAssert(s == "AB9C", "BString append broken");
s = "asdf";
oxAssert(s == "asdf", "String assign broken");

View File

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

View File

@ -32,7 +32,8 @@ 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);
auto const serr = ox::sfmt<ox::IString<23>>("Error code: {}", static_cast<int64_t>(err));
ox::IString<23> serr = "Error code: ";
serr += 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,8 +8,6 @@
#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 {
@ -442,9 +440,7 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept {
ImGui::PushID(static_cast<int>(i));
// Column: color idx
ImGui::TableNextColumn();
ox::IString<8> label;
ox::CharBuffWriter w(label.data(), label.bytes());
std::ignore = ox::writeItoa(i + 1, w);
auto const label = ox::IString<8>() + (i + 1);
auto const rowSelected = i == m_view.palIdx();
if (ImGui::Selectable(label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) {
m_view.setPalIdx(i);