Compare commits

...

3 Commits

Author SHA1 Message Date
95a69b72b5 [ox/std] Fix String::c_str to always retrun a valid C str
All checks were successful
Build / build (push) Successful in 2m36s
2024-05-29 20:51:15 -05:00
e4c3866017 [nostalgia/core] Fix subsheet resize not to read garbage 2024-05-29 02:16:07 -05:00
67cf3ae837 [ox/std] Ensure ox::String always has at least a null terminator 2024-05-29 02:15:30 -05:00
3 changed files with 19 additions and 34 deletions

View File

@ -13,6 +13,7 @@ namespace ox {
template class BasicString<8>; template class BasicString<8>;
static_assert(StringView("Write") != String("")); static_assert(StringView("Write") != String(""));
static_assert(ox::strcmp(String{}.c_str(), "\0") == 0);
static_assert(String("Write") != StringView("")); static_assert(String("Write") != StringView(""));
static_assert(String("Write") == StringView("Write")); static_assert(String("Write") == StringView("Write"));
static_assert(String(StringView("Write")) == StringView("Write")); static_assert(String(StringView("Write")) == StringView("Write"));

View File

@ -193,6 +193,7 @@ class BasicString {
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept; constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
constexpr void resize(size_t sz) noexcept { constexpr void resize(size_t sz) noexcept {
++sz;
m_buff.resize(sz); m_buff.resize(sz);
m_buff[sz - 1] = 0; m_buff[sz - 1] = 0;
} }
@ -249,38 +250,19 @@ class BasicString {
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString() noexcept { constexpr BasicString<SmallStringSize_v>::BasicString() noexcept {
if (!m_buff.empty()) { m_buff.resize(1);
m_buff[0] = 0;
} else {
m_buff.push_back(0);
}
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) { constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) {}
// GCC complains if you don't do this pretty unnecessary size check
if (!m_buff.empty()) {
m_buff[0] = 0;
}
}
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str) noexcept { constexpr BasicString<SmallStringSize_v>::BasicString(const char *str) noexcept {
if (!m_buff.empty()) {
m_buff[0] = 0;
} else {
m_buff.push_back(0);
}
set(str); set(str);
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char8_t *str) noexcept { constexpr BasicString<SmallStringSize_v>::BasicString(const char8_t *str) noexcept {
if (!m_buff.empty()) {
m_buff[0] = 0;
} else {
m_buff.push_back(0);
}
set(str); set(str);
} }
@ -288,21 +270,15 @@ template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size_t size) noexcept { constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size_t size) noexcept {
m_buff.resize(size + 1); m_buff.resize(size + 1);
ox::listcpy(m_buff.data(), str, size); ox::listcpy(m_buff.data(), str, size);
m_buff[size] = 0;
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(StringLiteral const&str) noexcept: constexpr BasicString<SmallStringSize_v>::BasicString(StringLiteral const&str) noexcept:
BasicString(StringView{str.data(), str.bytes()}) { BasicString(StringView{str.data(), str.len()}) {
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept { constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept {
if (m_buff.empty()) {
m_buff.push_back(0);
} else {
m_buff[0] = 0;
}
set(str); set(str);
} }
@ -313,6 +289,8 @@ constexpr BasicString<SmallStringSize_v>::BasicString(const BasicString &other)
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(BasicString &&other) noexcept: m_buff(std::move(other.m_buff)) { constexpr BasicString<SmallStringSize_v>::BasicString(BasicString &&other) noexcept: m_buff(std::move(other.m_buff)) {
other.m_buff.resize(1);
other.m_buff[0] = 0;
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
@ -358,6 +336,8 @@ template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(BasicString &&src) noexcept { constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(BasicString &&src) noexcept {
if (this != &src) { if (this != &src) {
m_buff = std::move(src.m_buff); m_buff = std::move(src.m_buff);
src.m_buff.resize(1);
src.m_buff[0] = 0;
} }
return *this; return *this;
} }

View File

@ -134,16 +134,20 @@ void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, ox::Point const&pt, uint8_t
} }
static ox::Error setPixelCount(ox::Vector<uint8_t> &pixels, int8_t pBpp, std::size_t cnt) noexcept { static ox::Error setPixelCount(ox::Vector<uint8_t> &pixels, int8_t pBpp, std::size_t cnt) noexcept {
size_t sz{};
switch (pBpp) { switch (pBpp) {
case 4: case 4:
pixels.resize(cnt / 2); sz = cnt / 2;
return OxError(0); break;
case 8: case 8:
pixels.resize(cnt); sz = cnt;
return OxError(0); break;
default: default:
return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount"); return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount");
} }
pixels.reserve(sz);
pixels.resize(sz);
return {};
} }
ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept { ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept {
@ -158,8 +162,8 @@ unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept {
ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz) noexcept { ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz) noexcept {
ox::Vector<uint8_t> out; ox::Vector<uint8_t> out;
oxReturnError(setPixelCount(out, pBpp, static_cast<size_t>(sz.width * sz.height) * PixelsPerTile)); oxReturnError(setPixelCount(out, pBpp, static_cast<size_t>(sz.width * sz.height) * PixelsPerTile));
auto const w = sz.width * TileWidth; auto const w = ss.columns * TileWidth;
auto const h = sz.height * TileHeight; auto const h = ss.rows * TileHeight;
for (auto x = 0; x < w; ++x) { for (auto x = 0; x < w; ++x) {
for (auto y = 0; y < h; ++y) { for (auto y = 0; y < h; ++y) {
auto const palIdx = getPixel(ss, pBpp, {x, y}); auto const palIdx = getPixel(ss, pBpp, {x, y});