[ox] Cleanup

This commit is contained in:
Gary Talent 2024-04-18 19:23:32 -05:00
parent 2c0e02277c
commit a20d7fd923
27 changed files with 174 additions and 150 deletions

View File

@ -27,7 +27,7 @@ ClArgs::ClArgs(int argc, const char **args) noexcept {
m_bools[arg] = false; m_bools[arg] = false;
} }
m_strings[arg] = val; m_strings[arg] = val;
if (auto r = ox_atoi(val.c_str()); r.error == 0) { if (auto r = ox::atoi(val.c_str()); r.error == 0) {
m_ints[arg] = r.value; m_ints[arg] = r.value;
} }
++i; ++i;

View File

@ -13,7 +13,7 @@
namespace ox { namespace ox {
Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcept { Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcept {
const auto s1End = ox_strchr(buff, ';', buffLen); const auto s1End = ox::strchr(buff, ';', buffLen);
if (!s1End) { if (!s1End) {
return OxError(1, "Could not read Claw header"); return OxError(1, "Could not read Claw header");
} }
@ -22,7 +22,7 @@ Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcep
buff += s1Size + 1; buff += s1Size + 1;
buffLen -= s1Size + 1; buffLen -= s1Size + 1;
const auto s2End = ox_strchr(buff, ';', buffLen); const auto s2End = ox::strchr(buff, ';', buffLen);
if (!s2End) { if (!s2End) {
return OxError(2, "Could not read Claw header"); return OxError(2, "Could not read Claw header");
} }
@ -31,7 +31,7 @@ Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcep
buff += s2Size + 1; buff += s2Size + 1;
buffLen -= s2Size + 1; buffLen -= s2Size + 1;
const auto s3End = ox_strchr(buff, ';', buffLen); const auto s3End = ox::strchr(buff, ';', buffLen);
if (!s3End) { if (!s3End) {
return OxError(3, "Could not read Claw header"); return OxError(3, "Could not read Claw header");
} }
@ -49,7 +49,7 @@ Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcep
return OxError(4, "Claw format does not match any supported format/version combo"); return OxError(4, "Claw format does not match any supported format/version combo");
} }
hdr.typeName = typeName; hdr.typeName = typeName;
if (auto r = ox_atoi(versionStr.c_str()); r.error == 0) { if (auto r = ox::atoi(versionStr.c_str()); r.error == 0) {
hdr.typeVersion = r.value; hdr.typeVersion = r.value;
} }
hdr.data = buff; hdr.data = buff;

View File

@ -88,7 +88,7 @@ ox::Error writeClawHeader(Writer_c auto &writer, const T *t, ClawFormat fmt) noe
oxReturnError(writer.put(';')); oxReturnError(writer.put(';'));
const auto tn = detail::getTypeVersion(t); const auto tn = detail::getTypeVersion(t);
if (tn > -1) { if (tn > -1) {
oxReturnError(ox::itoa(tn, writer)); oxReturnError(ox::writeItoa(tn, writer));
} }
oxReturnError(writer.put(';')); oxReturnError(writer.put(';'));
return {}; return {};

View File

@ -48,7 +48,7 @@ struct OX_PACKED DirectoryEntry {
auto d = data(); auto d = data();
if (d.valid()) { if (d.valid()) {
d->inode = inode; d->inode = inode;
ox_strncpy(d->name, name, ox::min(bufferSize, static_cast<InodeId_t>(MaxFileNameLength))); ox::strncpy(d->name, name, ox::min(bufferSize, static_cast<InodeId_t>(MaxFileNameLength)));
return OxError(0); return OxError(0);
} }
return OxError(1); return OxError(1);

View File

@ -46,7 +46,7 @@ FileAddress &FileAddress::operator=(const FileAddress &other) noexcept {
case FileAddressType::Path: case FileAddressType::Path:
{ {
if (other.m_data.path) { if (other.m_data.path) {
auto strSize = ox_strlen(other.m_data.path) + 1; auto strSize = ox::strlen(other.m_data.path) + 1;
m_data.path = new char[strSize]; m_data.path = new char[strSize];
ox_memcpy(m_data.path, other.m_data.path, strSize); ox_memcpy(m_data.path, other.m_data.path, strSize);
} else { } else {

View File

@ -186,7 +186,7 @@ Error PassThroughFS::writeFileInode(uint64_t, const void*, uint64_t, FileType) n
} }
std::string_view PassThroughFS::stripSlash(StringView path) noexcept { std::string_view PassThroughFS::stripSlash(StringView path) noexcept {
const auto pathLen = ox_strlen(path); const auto pathLen = ox::strlen(path);
for (auto i = 0u; i < pathLen && path[0] == '/'; i++) { for (auto i = 0u; i < pathLen && path[0] == '/'; i++) {
path = substr(path, 1); path = substr(path, 1);
} }

View File

@ -19,7 +19,7 @@ PathIterator::PathIterator(const char *path, std::size_t maxSize, std::size_t it
m_iterator = iterator; m_iterator = iterator;
} }
PathIterator::PathIterator(const char *path): PathIterator(path, ox_strlen(path)) { PathIterator::PathIterator(const char *path): PathIterator(path, ox::strlen(path)) {
} }
PathIterator::PathIterator(CRStringView path): PathIterator(path.data(), path.bytes()) { PathIterator::PathIterator(CRStringView path): PathIterator(path.data(), path.bytes()) {
@ -29,10 +29,10 @@ PathIterator::PathIterator(CRStringView path): PathIterator(path.data(), path.by
* @return 0 if no error * @return 0 if no error
*/ */
Error PathIterator::dirPath(char *out, std::size_t outSize) { Error PathIterator::dirPath(char *out, std::size_t outSize) {
const auto idx = ox_lastIndexOf(m_path, '/', m_maxSize); const auto idx = ox::lastIndexOf(m_path, '/', m_maxSize);
const auto size = static_cast<std::size_t>(idx + 1); const auto size = static_cast<std::size_t>(idx + 1);
if (idx >= 0 && size < outSize) { if (idx >= 0 && size < outSize) {
ox_memcpy(out, m_path, size); ox::memcpy(out, m_path, size);
out[size] = 0; out[size] = 0;
return OxError(0); return OxError(0);
} else { } else {
@ -44,12 +44,12 @@ Error PathIterator::dirPath(char *out, std::size_t outSize) {
* @return 0 if no error * @return 0 if no error
*/ */
Error PathIterator::fileName(char *out, std::size_t outSize) { Error PathIterator::fileName(char *out, std::size_t outSize) {
auto idx = ox_lastIndexOf(m_path, '/', m_maxSize); auto idx = ox::lastIndexOf(m_path, '/', m_maxSize);
if (idx >= 0) { if (idx >= 0) {
idx++; // pass up the preceding / idx++; // pass up the preceding /
std::size_t fileNameSize = static_cast<size_t>(ox_strlen(&m_path[idx])); std::size_t fileNameSize = static_cast<size_t>(ox::strlen(&m_path[idx]));
if (fileNameSize < outSize) { if (fileNameSize < outSize) {
ox_memcpy(out, &m_path[idx], fileNameSize); ox::memcpy(out, &m_path[idx], fileNameSize);
out[fileNameSize] = 0; out[fileNameSize] = 0;
return OxError(0); return OxError(0);
} else { } else {
@ -67,8 +67,8 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
oxTracef("ox.fs.PathIterator.get", "m_iterator ({}) >= m_maxSize ({})", m_iterator, m_maxSize); oxTracef("ox.fs.PathIterator.get", "m_iterator ({}) >= m_maxSize ({})", m_iterator, m_maxSize);
return OxError(1); return OxError(1);
} }
if (!ox_strlen(&m_path[m_iterator])) { if (!ox::strlen(&m_path[m_iterator])) {
oxTrace("ox.fs.PathIterator.get", "!ox_strlen(&m_path[m_iterator])"); oxTrace("ox.fs.PathIterator.get", "!ox::strlen(&m_path[m_iterator])");
return OxError(1); return OxError(1);
} }
auto start = m_iterator; auto start = m_iterator;
@ -76,10 +76,10 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
start++; start++;
} }
// end is at the next / // end is at the next /
const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start);
// correct end if it is invalid, which happens if there is no next / // correct end if it is invalid, which happens if there is no next /
if (!substr) { if (!substr) {
substr = ox_strchr(&m_path[start], 0, m_maxSize - start); substr = ox::strchr(&m_path[start], 0, m_maxSize - start);
} }
const auto end = static_cast<size_t>(substr - m_path); const auto end = static_cast<size_t>(substr - m_path);
size = end - start; size = end - start;
@ -87,7 +87,7 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
if (size >= pathOutSize || size == 0) { if (size >= pathOutSize || size == 0) {
return OxError(1); return OxError(1);
} }
ox_memcpy(pathOut, &m_path[start], size); ox::memcpy(pathOut, &m_path[start], size);
// truncate trailing / // truncate trailing /
if (size && pathOut[size - 1] == '/') { if (size && pathOut[size - 1] == '/') {
size--; size--;
@ -100,17 +100,17 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
Error PathIterator::next(char *pathOut, std::size_t pathOutSize) { Error PathIterator::next(char *pathOut, std::size_t pathOutSize) {
std::size_t size = 0; std::size_t size = 0;
auto retval = OxError(1); auto retval = OxError(1);
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { if (m_iterator < m_maxSize && ox::strlen(&m_path[m_iterator])) {
retval = OxError(0); retval = OxError(0);
if (m_path[m_iterator] == '/') { if (m_path[m_iterator] == '/') {
m_iterator++; m_iterator++;
} }
const auto start = m_iterator; const auto start = m_iterator;
// end is at the next / // end is at the next /
const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start);
// correct end if it is invalid, which happens if there is no next / // correct end if it is invalid, which happens if there is no next /
if (!substr) { if (!substr) {
substr = ox_strchr(&m_path[start], 0, m_maxSize - start); substr = ox::strchr(&m_path[start], 0, m_maxSize - start);
} }
const auto end = static_cast<size_t>(substr - m_path); const auto end = static_cast<size_t>(substr - m_path);
size = end - start; size = end - start;
@ -118,7 +118,7 @@ Error PathIterator::next(char *pathOut, std::size_t pathOutSize) {
if (size >= pathOutSize) { if (size >= pathOutSize) {
return OxError(1); return OxError(1);
} }
ox_memcpy(pathOut, &m_path[start], size); ox::memcpy(pathOut, &m_path[start], size);
} }
// truncate trailing / // truncate trailing /
if (size && pathOut[size - 1] == '/') { if (size && pathOut[size - 1] == '/') {
@ -147,17 +147,17 @@ Result<std::size_t> PathIterator::nextSize() const {
std::size_t size = 0; std::size_t size = 0;
auto retval = OxError(1); auto retval = OxError(1);
auto it = m_iterator; auto it = m_iterator;
if (it < m_maxSize && ox_strlen(&m_path[it])) { if (it < m_maxSize && ox::strlen(&m_path[it])) {
retval = OxError(0); retval = OxError(0);
if (m_path[it] == '/') { if (m_path[it] == '/') {
it++; it++;
} }
const auto start = it; const auto start = it;
// end is at the next / // end is at the next /
const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start);
// correct end if it is invalid, which happens if there is no next / // correct end if it is invalid, which happens if there is no next /
if (!substr) { if (!substr) {
substr = ox_strchr(&m_path[start], 0, m_maxSize - start); substr = ox::strchr(&m_path[start], 0, m_maxSize - start);
} }
const auto end = static_cast<std::size_t>(substr - m_path); const auto end = static_cast<std::size_t>(substr - m_path);
size = end - start; size = end - start;
@ -168,16 +168,16 @@ Result<std::size_t> PathIterator::nextSize() const {
bool PathIterator::hasNext() const { bool PathIterator::hasNext() const {
std::size_t size = 0; std::size_t size = 0;
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { if (m_iterator < m_maxSize && ox::strlen(&m_path[m_iterator])) {
std::size_t start = m_iterator; std::size_t start = m_iterator;
if (m_path[start] == '/') { if (m_path[start] == '/') {
start++; start++;
} }
// end is at the next / // end is at the next /
const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start);
// correct end if it is invalid, which happens if there is no next / // correct end if it is invalid, which happens if there is no next /
if (!substr) { if (!substr) {
substr = ox_strchr(&m_path[start], 0, m_maxSize - start); substr = ox::strchr(&m_path[start], 0, m_maxSize - start);
} }
const auto end = static_cast<std::size_t>(substr - m_path); const auto end = static_cast<std::size_t>(substr - m_path);
size = end - start; size = end - start;
@ -192,16 +192,16 @@ bool PathIterator::valid() const {
PathIterator PathIterator::next() const { PathIterator PathIterator::next() const {
std::size_t size = 0; std::size_t size = 0;
auto iterator = m_iterator; auto iterator = m_iterator;
if (iterator < m_maxSize && ox_strlen(&m_path[iterator])) { if (iterator < m_maxSize && ox::strlen(&m_path[iterator])) {
if (m_path[iterator] == '/') { if (m_path[iterator] == '/') {
iterator++; iterator++;
} }
const auto start = iterator; const auto start = iterator;
// end is at the next / // end is at the next /
const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start);
// correct end if it is invalid, which happens if there is no next / // correct end if it is invalid, which happens if there is no next /
if (!substr) { if (!substr) {
substr = ox_strchr(&m_path[start], 0, m_maxSize - start); substr = ox::strchr(&m_path[start], 0, m_maxSize - start);
} }
const auto end = static_cast<std::size_t>(substr - m_path); const auto end = static_cast<std::size_t>(substr - m_path);
size = end - start; size = end - start;

View File

@ -61,9 +61,9 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("/usr/share/charset.gbag"); auto const path = ox::String("/usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next");
return OxError(0); return OxError(0);
} }
}, },
@ -73,8 +73,8 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("/usr/share/"); auto const path = ox::String("/usr/share/");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
return OxError(0); return OxError(0);
} }
}, },
@ -84,7 +84,7 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("/"); auto const path = ox::String("/");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "\0") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "\0") == 0, "PathIterator shows wrong next");
return OxError(0); return OxError(0);
} }
}, },
@ -94,9 +94,9 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("usr/share/charset.gbag"); auto const path = ox::String("usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next");
return OxError(0); return OxError(0);
} }
}, },
@ -106,8 +106,8 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("usr/share/"); auto const path = ox::String("usr/share/");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
return OxError(0); return OxError(0);
} }
}, },
@ -117,7 +117,7 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("/usr/share/charset.gbag"); auto const path = ox::String("/usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.dirPath(buff, path.len()) == 0 && ox_strcmp(buff, "/usr/share/") == 0, "PathIterator shows incorrect dir path"); oxAssert(it.dirPath(buff, path.len()) == 0 && ox::strcmp(buff, "/usr/share/") == 0, "PathIterator shows incorrect dir path");
return OxError(0); return OxError(0);
} }
}, },
@ -127,7 +127,7 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
auto const path = ox::String("/usr/share/charset.gbag"); auto const path = ox::String("/usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len()); ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1)); auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.fileName(buff, path.len()) == 0 && ox_strcmp(buff, "charset.gbag") == 0, "PathIterator shows incorrect file name"); oxAssert(it.fileName(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows incorrect file name");
return OxError(0); return OxError(0);
} }
}, },
@ -135,7 +135,7 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
"PathIterator::hasNext", "PathIterator::hasNext",
[](ox::StringView) { [](ox::StringView) {
const auto path = "/file1"; const auto path = "/file1";
ox::PathIterator it(path, ox_strlen(path)); ox::PathIterator it(path, ox::strlen(path));
oxAssert(it.hasNext(), "PathIterator shows incorrect hasNext"); oxAssert(it.hasNext(), "PathIterator shows incorrect hasNext");
oxAssert(!it.next().hasNext(), "PathIterator shows incorrect hasNext"); oxAssert(!it.next().hasNext(), "PathIterator shows incorrect hasNext");
return OxError(0); return OxError(0);
@ -171,9 +171,9 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
[](ox::StringView) { [](ox::StringView) {
constexpr auto buffLen = 5000; constexpr auto buffLen = 5000;
constexpr auto str1 = "Hello, World!"; constexpr auto str1 = "Hello, World!";
constexpr auto str1Len = ox_strlen(str1) + 1; constexpr auto str1Len = ox::strlen(str1) + 1;
constexpr auto str2 = "Hello, Moon!"; constexpr auto str2 = "Hello, Moon!";
constexpr auto str2Len = ox_strlen(str2) + 1; constexpr auto str2Len = ox::strlen(str2) + 1;
auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer<uint32_t, ox::FileStoreItem<uint32_t>>(buffLen); auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer<uint32_t, ox::FileStoreItem<uint32_t>>(buffLen);
oxAssert(ox::FileStore32::format(list, buffLen), "FileStore::format failed."); oxAssert(ox::FileStore32::format(list, buffLen), "FileStore::format failed.");
ox::FileStore32 fileStore(list, buffLen); ox::FileStore32 fileStore(list, buffLen);

View File

@ -214,7 +214,7 @@ template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::fieldCString(const char*, const char *const*val, std::size_t) noexcept { constexpr Error MetalClawWriter<Writer>::fieldCString(const char*, const char *const*val, std::size_t) noexcept {
bool fieldSet = false; bool fieldSet = false;
if (!m_unionIdx.has_value() || *m_unionIdx == m_field) { if (!m_unionIdx.has_value() || *m_unionIdx == m_field) {
const auto strLen = *val ? ox_strlen(*val) : 0; const auto strLen = *val ? ox::strlen(*val) : 0;
// write the length // write the length
const auto strLenBuff = mc::encodeInteger(strLen); const auto strLenBuff = mc::encodeInteger(strLen);
oxReturnError(m_writer.write(reinterpret_cast<const char*>(strLenBuff.data), strLenBuff.length)); oxReturnError(m_writer.write(reinterpret_cast<const char*>(strLenBuff.data), strLenBuff.length));

View File

@ -15,7 +15,7 @@ namespace ox {
OrganicClawReader::OrganicClawReader(const uint8_t *buff, std::size_t buffSize) { OrganicClawReader::OrganicClawReader(const uint8_t *buff, std::size_t buffSize) {
auto json = reinterpret_cast<const char*>(buff); auto json = reinterpret_cast<const char*>(buff);
auto jsonLen = ox_strnlen(json, buffSize); auto jsonLen = ox::strnlen(json, buffSize);
Json::CharReaderBuilder parserBuilder; Json::CharReaderBuilder parserBuilder;
auto parser = std::unique_ptr<Json::CharReader>(parserBuilder.newCharReader()); auto parser = std::unique_ptr<Json::CharReader>(parserBuilder.newCharReader());
if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) { if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) {
@ -198,7 +198,7 @@ Error OrganicClawReader::fieldCString(const char *key, char *val, std::size_t bu
if (strSize >= buffLen) { if (strSize >= buffLen) {
err = OxError(2, "String size exceeds capacity of destination"); err = OxError(2, "String size exceeds capacity of destination");
} else { } else {
ox_memcpy(data, begin, static_cast<std::size_t>(strSize)); ox::memcpy(data, begin, static_cast<std::size_t>(strSize));
data[strSize] = 0; data[strSize] = 0;
} }
} else { } else {
@ -224,7 +224,7 @@ Error OrganicClawReader::fieldCString(const char *key, char **val) noexcept {
const auto strSize = static_cast<std::size_t>(end - begin); const auto strSize = static_cast<std::size_t>(end - begin);
safeDelete(*val); safeDelete(*val);
*val = new char[strSize + 1]; *val = new char[strSize + 1];
ox_memcpy(data, begin, static_cast<std::size_t>(strSize)); ox::memcpy(data, begin, static_cast<std::size_t>(strSize));
data[strSize] = 0; data[strSize] = 0;
} else { } else {
err = OxError(1, "Type mismatch"); err = OxError(1, "Type mismatch");
@ -242,7 +242,7 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b
if (jv.empty()) { if (jv.empty()) {
auto data = val; auto data = val;
if (data) { if (data) {
data[0] = 0; data[0] = nullptr;
} }
} else if (jv.isString()) { } else if (jv.isString()) {
jv.getString(&begin, &end); jv.getString(&begin, &end);
@ -252,8 +252,8 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b
safeDelete(*val); safeDelete(*val);
*val = new char[strSize + 1]; *val = new char[strSize + 1];
} }
ox_memcpy(data, begin, static_cast<std::size_t>(strSize)); ox::memcpy(data, begin, static_cast<std::size_t>(strSize));
data[strSize] = 0; data[strSize] = nullptr;
} else { } else {
err = OxError(1, "Type mismatch"); err = OxError(1, "Type mismatch");
} }

View File

@ -274,7 +274,7 @@ Result<T> readOC(const char *json, std::size_t jsonLen) noexcept {
template<typename T> template<typename T>
Result<T> readOC(const char *json) noexcept { Result<T> readOC(const char *json) noexcept {
return readOC<T>(json, ox_strlen(json)); return readOC<T>(json, ox::strlen(json));
} }
template<typename T> template<typename T>

View File

@ -27,7 +27,7 @@ Error OrganicClawWriter::fieldCString(const char *key, const char *const*val, in
} }
Error OrganicClawWriter::fieldCString(const char *key, const char *const*val) noexcept { Error OrganicClawWriter::fieldCString(const char *key, const char *const*val) noexcept {
return fieldCString(key, const_cast<const char**>(val), static_cast<int>(ox_strlen(val))); return fieldCString(key, const_cast<const char**>(val), static_cast<int>(ox::strlen(val)));
} }
Error OrganicClawWriter::field(const char *key, const UUID *uuid) noexcept { Error OrganicClawWriter::field(const char *key, const UUID *uuid) noexcept {

View File

@ -105,6 +105,7 @@ install(
heapmgr.hpp heapmgr.hpp
iterator.hpp iterator.hpp
math.hpp math.hpp
maybeview.hpp
memops.hpp memops.hpp
memory.hpp memory.hpp
new.hpp new.hpp

View File

@ -131,7 +131,7 @@ class BaseStringView {
constexpr explicit BaseStringView(std::nullptr_t) noexcept {} constexpr explicit BaseStringView(std::nullptr_t) noexcept {}
constexpr explicit BaseStringView(const char *str) noexcept: m_str(str), m_len(str ? ox_strlen(str) : 0) {} constexpr explicit BaseStringView(const char *str) noexcept: m_str(str), m_len(str ? ox::strlen(str) : 0) {}
constexpr explicit BaseStringView(const char *str, std::size_t len) noexcept: m_str(str), m_len(len) {} constexpr explicit BaseStringView(const char *str, std::size_t len) noexcept: m_str(str), m_len(len) {}

View File

@ -109,7 +109,7 @@ constexpr BString<size>::BString(const char *str) noexcept: m_buff{{0}} {
template<std::size_t size> template<std::size_t size>
constexpr BString<size> &BString<size>::operator=(Integer_c auto i) noexcept { constexpr BString<size> &BString<size>::operator=(Integer_c auto i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox::itoa(i, str);
return this->operator=(str); return this->operator=(str);
} }
@ -127,7 +127,7 @@ constexpr BString<size> &BString<size>::operator=(ox::CRStringView str) noexcept
template<std::size_t size> template<std::size_t size>
constexpr BString<size> &BString<size>::operator=(const char *str) noexcept { constexpr BString<size> &BString<size>::operator=(const char *str) noexcept {
std::size_t strLen = ox_strlen(str) + 1; std::size_t strLen = ox::strlen(str) + 1;
if (cap() < strLen) { if (cap() < strLen) {
strLen = cap(); strLen = cap();
} }
@ -144,7 +144,7 @@ constexpr BString<size> &BString<size>::operator=(char *str) noexcept {
template<std::size_t size> template<std::size_t size>
constexpr BString<size> &BString<size>::operator+=(const char *str) noexcept { constexpr BString<size> &BString<size>::operator+=(const char *str) noexcept {
std::size_t strLen = ox_strlen(str) + 1; std::size_t strLen = ox::strlen(str) + 1;
oxIgnoreError(append(str, strLen)); oxIgnoreError(append(str, strLen));
return *this; return *this;
} }
@ -157,7 +157,7 @@ constexpr BString<size> &BString<size>::operator+=(char *str) noexcept {
template<std::size_t size> template<std::size_t size>
constexpr BString<size> &BString<size>::operator+=(Integer_c auto i) noexcept { constexpr BString<size> &BString<size>::operator+=(Integer_c auto i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox::itoa(i, str);
return this->operator+=(str); return this->operator+=(str);
} }
@ -171,7 +171,7 @@ constexpr BString<size> &BString<size>::operator+=(StringView s) noexcept {
template<std::size_t size> template<std::size_t size>
constexpr BString<size> BString<size>::operator+(const char *str) const noexcept { constexpr BString<size> BString<size>::operator+(const char *str) const noexcept {
auto out = *this; auto out = *this;
std::size_t strLen = ox_strlen(str) + 1; std::size_t strLen = ox::strlen(str) + 1;
oxIgnoreError(out.append(str, strLen)); oxIgnoreError(out.append(str, strLen));
return out; return out;
} }
@ -184,7 +184,7 @@ constexpr BString<size> BString<size>::operator+(char *str) const noexcept {
template<std::size_t size> template<std::size_t size>
constexpr BString<size> BString<size>::operator+(Integer_c auto i) const noexcept { constexpr BString<size> BString<size>::operator+(Integer_c auto i) const noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox::itoa(i, str);
return this->operator+(str); return this->operator+(str);
} }
@ -226,7 +226,7 @@ constexpr Error BString<buffLen>::append(const char *str, std::size_t strLen) no
strLen = cap() - currentLen; strLen = cap() - currentLen;
err = OxError(1, "Insufficient space for full string"); err = OxError(1, "Insufficient space for full string");
} }
ox_strncpy(m_buff + currentLen, str, strLen); ox::strncpy(m_buff + currentLen, str, strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
m_buff[currentLen + strLen] = 0; m_buff[currentLen + strLen] = 0;
return err; return err;

View File

@ -11,8 +11,10 @@
#include "types.hpp" #include "types.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
namespace ox {
template<typename T1, typename T2> template<typename T1, typename T2>
constexpr T1 ox_strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept { constexpr T1 strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept {
using T1Type = typename ox::remove_reference<decltype(dest[0])>::type; using T1Type = typename ox::remove_reference<decltype(dest[0])>::type;
std::size_t i = 0; std::size_t i = 0;
while (i < maxLen && src[i]) { while (i < maxLen && src[i]) {
@ -25,7 +27,7 @@ constexpr T1 ox_strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept {
} }
[[nodiscard]] [[nodiscard]]
constexpr auto ox_strnlen(const char *str1, std::size_t maxLen) noexcept { constexpr auto strnlen(const char *str1, std::size_t maxLen) noexcept {
std::size_t len = 0; std::size_t len = 0;
for (; len < maxLen && str1[len]; len++); for (; len < maxLen && str1[len]; len++);
return len; return len;
@ -33,7 +35,7 @@ constexpr auto ox_strnlen(const char *str1, std::size_t maxLen) noexcept {
template<typename T> template<typename T>
[[nodiscard]] [[nodiscard]]
constexpr auto ox_strlen(T const&str1) noexcept { constexpr auto strlen(T const&str1) noexcept {
std::size_t len = 0; std::size_t len = 0;
for (; str1[len]; len++); for (; str1[len]; len++);
return len; return len;
@ -41,7 +43,7 @@ constexpr auto ox_strlen(T const&str1) noexcept {
template<typename T1, typename T2> template<typename T1, typename T2>
[[nodiscard]] [[nodiscard]]
constexpr int ox_strcmp(const T1 &str1, const T2 &str2) noexcept { constexpr int strcmp(const T1 &str1, const T2 &str2) noexcept {
auto retval = 0; auto retval = 0;
auto i = 0u; auto i = 0u;
while (str1[i] || str2[i]) { while (str1[i] || str2[i]) {
@ -59,7 +61,7 @@ constexpr int ox_strcmp(const T1 &str1, const T2 &str2) noexcept {
template<typename T1, typename T2> template<typename T1, typename T2>
[[nodiscard]] [[nodiscard]]
constexpr int ox_strncmp(T1 const&str1, T2 const&str2, const std::size_t maxLen) noexcept { constexpr int strncmp(T1 const&str1, T2 const&str2, const std::size_t maxLen) noexcept {
auto retval = 0; auto retval = 0;
std::size_t i = 0; std::size_t i = 0;
while (i < maxLen && (str1[i] || str2[i])) { while (i < maxLen && (str1[i] || str2[i])) {
@ -76,7 +78,7 @@ constexpr int ox_strncmp(T1 const&str1, T2 const&str2, const std::size_t maxLen)
} }
[[nodiscard]] [[nodiscard]]
constexpr const char *ox_strchr(const char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { constexpr const char *strchr(const char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept {
for (std::size_t i = 0; i <= maxLen; i++) { for (std::size_t i = 0; i <= maxLen; i++) {
if (str[i] == character) { if (str[i] == character) {
return &str[i]; return &str[i];
@ -88,7 +90,7 @@ constexpr const char *ox_strchr(const char *str, int character, std::size_t maxL
} }
[[nodiscard]] [[nodiscard]]
constexpr char *ox_strchr(char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { constexpr char *strchr(char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept {
for (std::size_t i = 0; i < maxLen; i++) { for (std::size_t i = 0; i < maxLen; i++) {
if (str[i] == character) { if (str[i] == character) {
return &str[i]; return &str[i];
@ -100,7 +102,7 @@ constexpr char *ox_strchr(char *str, int character, std::size_t maxLen = 0xFFFFF
} }
[[nodiscard]] [[nodiscard]]
constexpr int ox_lastIndexOf(const auto &str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { constexpr int lastIndexOf(const auto &str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept {
int retval = -1; int retval = -1;
for (std::size_t i = 0; i < maxLen && str[i]; i++) { for (std::size_t i = 0; i < maxLen && str[i]; i++) {
if (str[i] == character) { if (str[i] == character) {
@ -111,7 +113,7 @@ constexpr int ox_lastIndexOf(const auto &str, int character, std::size_t maxLen
} }
template<typename Integer, typename T> template<typename Integer, typename T>
constexpr T ox_itoa(Integer v, T str) noexcept { constexpr T itoa(Integer v, T str) noexcept {
if (v) { if (v) {
ox::ResizedInt_t<Integer, 64> mod = 1000000000000000000; ox::ResizedInt_t<Integer, 64> mod = 1000000000000000000;
ox::ResizedInt_t<Integer, 64> val = v; ox::ResizedInt_t<Integer, 64> val = v;
@ -143,3 +145,5 @@ constexpr T ox_itoa(Integer v, T str) noexcept {
} }
return str; return str;
} }
}

View File

@ -81,7 +81,7 @@ class FmtArg {
if constexpr(is_bool_v<T>) { if constexpr(is_bool_v<T>) {
return v ? "true" : "false"; return v ? "true" : "false";
} else if constexpr(is_integer_v<T>) { } else if constexpr(is_integer_v<T>) {
return ox_itoa(v, dataStr); return ox::itoa(v, dataStr);
} else { } else {
return toStringView(v); return toStringView(v);
} }
@ -126,11 +126,11 @@ struct FmtSegment {
unsigned length = 0; unsigned length = 0;
constexpr bool operator==(const FmtSegment &o) const noexcept { constexpr bool operator==(const FmtSegment &o) const noexcept {
return length == o.length && ox_strncmp(str, o.str, length) == 0; return length == o.length && ox::strncmp(str, o.str, length) == 0;
} }
constexpr bool operator!=(const FmtSegment &o) const noexcept { constexpr bool operator!=(const FmtSegment &o) const noexcept {
return length != o.length || ox_strncmp(str, o.str, length) != 0; return length != o.length || ox::strncmp(str, o.str, length) != 0;
} }
}; };

View File

@ -45,16 +45,16 @@ class HashMap {
constexpr HashMap &operator=(HashMap &&other) noexcept; constexpr HashMap &operator=(HashMap &&other) noexcept;
constexpr T &operator[](MaybeSV_t<K> const&key); constexpr T &operator[](MaybeView_t<K> const&key);
constexpr Result<T*> at(MaybeSV_t<K> const&key) noexcept; constexpr Result<T*> at(MaybeView_t<K> const&key) noexcept;
constexpr Result<const T*> at(MaybeSV_t<K> const&key) const noexcept; constexpr Result<const T*> at(MaybeView_t<K> const&key) const noexcept;
constexpr void erase(MaybeSV_t<K> const&key); constexpr void erase(MaybeView_t<K> const&key);
[[nodiscard]] [[nodiscard]]
constexpr bool contains(MaybeSV_t<K> const&key) const noexcept; constexpr bool contains(MaybeView_t<K> const&key) const noexcept;
[[nodiscard]] [[nodiscard]]
constexpr std::size_t size() const noexcept; constexpr std::size_t size() const noexcept;
@ -134,7 +134,7 @@ constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) noexcep
} }
template<typename K, typename T> template<typename K, typename T>
constexpr T &HashMap<K, T>::operator[](MaybeSV_t<K> const&k) { constexpr T &HashMap<K, T>::operator[](MaybeView_t<K> const&k) {
auto &p = access(m_pairs, k); auto &p = access(m_pairs, k);
if (p == nullptr) { if (p == nullptr) {
if (static_cast<double>(m_pairs.size()) * 0.7 < if (static_cast<double>(m_pairs.size()) * 0.7 <
@ -149,7 +149,7 @@ constexpr T &HashMap<K, T>::operator[](MaybeSV_t<K> const&k) {
} }
template<typename K, typename T> template<typename K, typename T>
constexpr Result<T*> HashMap<K, T>::at(MaybeSV_t<K> const&k) noexcept { constexpr Result<T*> HashMap<K, T>::at(MaybeView_t<K> const&k) noexcept {
auto p = access(m_pairs, k); auto p = access(m_pairs, k);
if (!p) { if (!p) {
return {nullptr, OxError(1, "value not found for given key")}; return {nullptr, OxError(1, "value not found for given key")};
@ -158,7 +158,7 @@ constexpr Result<T*> HashMap<K, T>::at(MaybeSV_t<K> const&k) noexcept {
} }
template<typename K, typename T> template<typename K, typename T>
constexpr Result<const T*> HashMap<K, T>::at(MaybeSV_t<K> const&k) const noexcept { constexpr Result<const T*> HashMap<K, T>::at(MaybeView_t<K> const&k) const noexcept {
auto p = access(m_pairs, k); auto p = access(m_pairs, k);
if (!p) { if (!p) {
return {nullptr, OxError(1, "value not found for given key")}; return {nullptr, OxError(1, "value not found for given key")};
@ -167,7 +167,7 @@ constexpr Result<const T*> HashMap<K, T>::at(MaybeSV_t<K> const&k) const noexcep
} }
template<typename K, typename T> template<typename K, typename T>
constexpr void HashMap<K, T>::erase(MaybeSV_t<K> const&k) { constexpr void HashMap<K, T>::erase(MaybeView_t<K> const&k) {
if (!contains(k)) { if (!contains(k)) {
return; return;
} }
@ -185,7 +185,7 @@ constexpr void HashMap<K, T>::erase(MaybeSV_t<K> const&k) {
} }
template<typename K, typename T> template<typename K, typename T>
constexpr bool HashMap<K, T>::contains(MaybeSV_t<K> const&k) const noexcept { constexpr bool HashMap<K, T>::contains(MaybeView_t<K> const&k) const noexcept {
return access(m_pairs, k) != nullptr; return access(m_pairs, k) != nullptr;
} }

26
deps/ox/src/ox/std/maybeview.hpp vendored Normal file
View File

@ -0,0 +1,26 @@
/*
* Copyright 2015 - 2022 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "typetraits.hpp"
namespace ox {
// Maybe StringView. If T is a string type, MaybeType::type/MaybeView_t is a
// StringView. This avoids creating unnecessary Strings when taking a
// StringView or C string as a function argument.
template<typename T, bool isStr = isOxString_v<T>>
struct MaybeView {
using type = T;
};
template<typename T>
using MaybeView_t = typename MaybeView<T>::type;
}

View File

@ -28,6 +28,7 @@
#include "heapmgr.hpp" #include "heapmgr.hpp"
#include "iterator.hpp" #include "iterator.hpp"
#include "math.hpp" #include "math.hpp"
#include "maybeview.hpp"
#include "memops.hpp" #include "memops.hpp"
#include "memory.hpp" #include "memory.hpp"
#include "new.hpp" #include "new.hpp"

View File

@ -315,7 +315,7 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(int64_t i) noexcept { constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(int64_t i) noexcept {
ox::Array<char, 65> str{}; ox::Array<char, 65> str{};
ox_itoa(i, str.data()); ox::itoa(i, str.data());
set(str.data()); set(str.data());
return *this; return *this;
} }
@ -323,7 +323,7 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(uint64_t i) noexcept { constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(uint64_t i) noexcept {
ox::Array<char, 65> str{}; ox::Array<char, 65> str{};
ox_itoa(i, str.data()); ox::itoa(i, str.data());
set(str.data()); set(str.data());
return *this; return *this;
} }
@ -352,7 +352,7 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator+=(const char *str) noexcept { constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator+=(const char *str) noexcept {
std::size_t strLen = ox_strlen(str); std::size_t strLen = ox::strlen(str);
oxIgnoreError(append(str, strLen)); oxIgnoreError(append(str, strLen));
return *this; return *this;
} }
@ -371,7 +371,7 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator+=(Integer_c auto i) noexcept { constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator+=(Integer_c auto i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox::itoa(i, str);
return this->operator+=(str); return this->operator+=(str);
} }
@ -390,7 +390,7 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(const char *str) const noexcept { constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(const char *str) const noexcept {
const std::size_t strLen = ox_strlen(str); const std::size_t strLen = ox::strlen(str);
const auto currentLen = len(); const auto currentLen = len();
BasicString<SmallStringSize_v> cpy(currentLen + strLen); BasicString<SmallStringSize_v> cpy(currentLen + strLen);
cpy.m_buff.resize(m_buff.size() + strLen); cpy.m_buff.resize(m_buff.size() + strLen);
@ -415,7 +415,7 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operato
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(Integer_c auto i) const noexcept { constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(Integer_c auto i) const noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox::itoa(i, str);
return *this + str; return *this + str;
} }
@ -470,22 +470,22 @@ constexpr bool BasicString<SmallStringSize_v>::operator!=(OxString_c auto const&
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::operator<(BasicString const&other) const noexcept { constexpr bool BasicString<SmallStringSize_v>::operator<(BasicString const&other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) < 0; return ox::strcmp(c_str(), other.c_str()) < 0;
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::operator>(BasicString const&other) const noexcept { constexpr bool BasicString<SmallStringSize_v>::operator>(BasicString const&other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) > 0; return ox::strcmp(c_str(), other.c_str()) > 0;
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::operator<=(BasicString const&other) const noexcept { constexpr bool BasicString<SmallStringSize_v>::operator<=(BasicString const&other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) < 1; return ox::strcmp(c_str(), other.c_str()) < 1;
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::operator>=(BasicString const&other) const noexcept { constexpr bool BasicString<SmallStringSize_v>::operator>=(BasicString const&other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) > -1; return ox::strcmp(c_str(), other.c_str()) > -1;
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
@ -548,7 +548,7 @@ constexpr void BasicString<SmallStringSize_v>::set(CRStringView str) noexcept {
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr void BasicString<SmallStringSize_v>::set(const char8_t *str) noexcept { constexpr void BasicString<SmallStringSize_v>::set(const char8_t *str) noexcept {
std::size_t strBytes = ox_strlen(str) + 1; std::size_t strBytes = ox::strlen(str) + 1;
m_buff.resize(strBytes); m_buff.resize(strBytes);
memcpy(m_buff.data(), str, strBytes); memcpy(m_buff.data(), str, strBytes);
*m_buff.back().value = 0; *m_buff.back().value = 0;

View File

@ -14,6 +14,7 @@
#include "basestringview.hpp" #include "basestringview.hpp"
#include "cstrops.hpp" #include "cstrops.hpp"
#include "maybeview.hpp"
#include "writer.hpp" #include "writer.hpp"
namespace ox { namespace ox {
@ -63,7 +64,7 @@ constexpr auto operator==(CRStringView s1, CRStringView s2) noexcept {
if (s2.len() != s1.len()) { if (s2.len() != s1.len()) {
return false; return false;
} }
return ox_strncmp(s1.data(), s2.data(), s1.len()) == 0; return ox::strncmp(s1.data(), s2.data(), s1.len()) == 0;
} }
constexpr auto operator<=>(CRStringView s1, CRStringView s2) noexcept { constexpr auto operator<=>(CRStringView s1, CRStringView s2) noexcept {
@ -97,24 +98,12 @@ constexpr auto toStdStringView(CRStringView sv) noexcept {
#endif #endif
// Maybe StringView. If T is a string type, MaybeType::type/MaybeSV_t is a
// StringView. This avoids creating unnecessary Strings when taking a
// StringView or C string as a function argument.
template<typename T, bool isStr = isOxString_v<T>>
struct MaybeSV {
using type = T;
};
template<typename T> template<typename T>
struct MaybeSV<T, true> { struct MaybeView<T, true> {
using type = ox::StringView; using type = ox::StringView;
}; };
template<typename T>
using MaybeSV_t = typename MaybeSV<T>::type;
constexpr ox::Result<int> atoi(ox::CRStringView str) noexcept {
}
constexpr ox::Result<int> ox_atoi(ox::CRStringView str) noexcept {
int total = 0; int total = 0;
int multiplier = 1; int multiplier = 1;
for (auto i = static_cast<int64_t>(str.len()) - 1; i != -1; --i) { for (auto i = static_cast<int64_t>(str.len()) - 1; i != -1; --i) {
@ -129,3 +118,6 @@ constexpr ox::Result<int> ox_atoi(ox::CRStringView str) noexcept {
return total; return total;
} }
}

View File

@ -8,26 +8,26 @@
#include "strops.hpp" #include "strops.hpp"
static_assert(ox_strcmp("asdf", "hijk") < 0, "asdf < hijk"); static_assert(ox::strcmp("asdf", "hijk") < 0, "asdf < hijk");
static_assert(ox_strcmp("hijk", "asdf") > 0, "hijk > asdf"); static_assert(ox::strcmp("hijk", "asdf") > 0, "hijk > asdf");
static_assert(ox_strcmp("resize", "read") > 0, "resize > read"); static_assert(ox::strcmp("resize", "read") > 0, "resize > read");
static_assert(ox_strcmp("read", "resize") < 0, "read < resize"); static_assert(ox::strcmp("read", "resize") < 0, "read < resize");
static_assert(ox_strcmp("resize", "resize") == 0, "resize == resize"); static_assert(ox::strcmp("resize", "resize") == 0, "resize == resize");
static_assert(ox_strcmp("", "") == 0, "\"\" == \"\""); static_assert(ox::strcmp("", "") == 0, "\"\" == \"\"");
static_assert([] { static_assert([] {
auto testStr = "asdf"; auto testStr = "asdf";
return ox_strchr(testStr, 0, 4) == &testStr[4]; return ox::strchr(testStr, 0, 4) == &testStr[4];
}(), "ox_strchr 0"); }(), "ox::strchr 0");
static_assert([] { static_assert([] {
int retval = 0; int retval = 0;
auto testStr = "aaaa"; auto testStr = "aaaa";
// test the const and non-const versions of ox_lastIndexOf // test the const and non-const versions of ox::lastIndexOf
retval |= !(ox_lastIndexOf(const_cast<char*>(testStr), 'a', ox_strlen(testStr)) == 3); retval |= !(ox::lastIndexOf(const_cast<char*>(testStr), 'a', ox::strlen(testStr)) == 3);
retval |= !(ox_lastIndexOf(testStr, 'a', ox_strlen(testStr)) == 3); retval |= !(ox::lastIndexOf(testStr, 'a', ox::strlen(testStr)) == 3);
return retval == 0; return retval == 0;
}(), "ox_lastIndexOf aaaa a"); }(), "ox::lastIndexOf aaaa a");
#ifndef OX_USE_STDLIB #ifndef OX_USE_STDLIB

View File

@ -37,7 +37,7 @@ constexpr ox::StringView substr(Str const&str, std::size_t start, std::size_t en
} }
template<typename Integer> template<typename Integer>
constexpr ox::Error itoa(Integer v, ox::Writer_c auto &writer) noexcept { constexpr ox::Error writeItoa(Integer v, ox::Writer_c auto &writer) noexcept {
if (v) { if (v) {
ox::ResizedInt_t<Integer, 64> mod = 1000000000000000000; ox::ResizedInt_t<Integer, 64> mod = 1000000000000000000;
ox::ResizedInt_t<Integer, 64> val = v; ox::ResizedInt_t<Integer, 64> val = v;
@ -71,13 +71,13 @@ constexpr ox::Error itoa(Integer v, ox::Writer_c auto &writer) noexcept {
[[nodiscard]] [[nodiscard]]
constexpr bool beginsWith(CRStringView base, CRStringView beginning) noexcept { constexpr bool beginsWith(CRStringView base, CRStringView beginning) noexcept {
const auto beginningLen = ox::min(beginning.len(), base.len()); const auto beginningLen = ox::min(beginning.len(), base.len());
return base.len() >= beginning.len() && ox_strncmp(base.data(), beginning, beginningLen) == 0; return base.len() >= beginning.len() && ox::strncmp(base.data(), beginning, beginningLen) == 0;
} }
[[nodiscard]] [[nodiscard]]
constexpr bool endsWith(CRStringView base, CRStringView ending) noexcept { constexpr bool endsWith(CRStringView base, CRStringView ending) noexcept {
const auto endingLen = ending.len(); const auto endingLen = ending.len();
return base.len() >= endingLen && ox_strcmp(base.data() + (base.len() - endingLen), ending) == 0; return base.len() >= endingLen && ox::strcmp(base.data() + (base.len() - endingLen), ending) == 0;
} }
constexpr std::size_t find(CRStringView str, char search) noexcept { constexpr std::size_t find(CRStringView str, char search) noexcept {

View File

@ -30,13 +30,13 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
[]() { []() {
ox::Array<char, 10> buff; ox::Array<char, 10> buff;
ox::CharBuffWriter bw(buff); ox::CharBuffWriter bw(buff);
oxAssert(ox::itoa(5, bw), "ox::itoa returned Error"); oxAssert(ox::writeItoa(5, bw), "ox::writeItoa returned Error");
oxExpect(ox::StringView(buff.data()), ox::StringView("5")); oxExpect(ox::StringView(buff.data()), ox::StringView("5"));
oxReturnError(bw.seekp(0)); oxReturnError(bw.seekp(0));
oxAssert(ox::itoa(50, bw), "ox::itoa returned Error"); oxAssert(ox::writeItoa(50, bw), "ox::writeItoa returned Error");
oxExpect(ox::StringView(buff.data()), ox::StringView("50")); oxExpect(ox::StringView(buff.data()), ox::StringView("50"));
oxReturnError(bw.seekp(0)); oxReturnError(bw.seekp(0));
oxAssert(ox::itoa(500, bw), "ox::itoa returned Error"); oxAssert(ox::writeItoa(500, bw), "ox::writeItoa returned Error");
oxExpect(ox::StringView(buff.data()), ox::StringView("500")); oxExpect(ox::StringView(buff.data()), ox::StringView("500"));
return ox::Error{}; return ox::Error{};
} }

View File

@ -42,7 +42,7 @@ enum LogChan {
template<LogChan chan> template<LogChan chan>
static void log(ox::CRStringView str) { static void log(ox::CRStringView str) {
const auto sz = ox::min<std::size_t>(0x100, str.bytes()); const auto sz = ox::min<std::size_t>(0x100, str.bytes());
ox_strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz); ox::strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz);
REG_MGBA_DEBUG_FLAGS = chan | 0x100; REG_MGBA_DEBUG_FLAGS = chan | 0x100;
} }
@ -69,30 +69,30 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
std::cout << std::setw(53) << std::left << ch << "| "; std::cout << std::setw(53) << std::left << ch << "| ";
std::cout << std::setw(65) << std::left << msg << '|'; std::cout << std::setw(65) << std::left << msg << '|';
std::cout << " " << file << ':' << line << "\n"; std::cout << " " << file << ':' << line << "\n";
} else if (ox_strcmp(ch, "debug") == 0 || ox_strcmp(ch, "info") == 0) { } else if (ox::strcmp(ch, "debug") == 0 || ox::strcmp(ch, "info") == 0) {
printf("%s\n", msg); printf("%s\n", msg);
fflush(stdout); fflush(stdout);
} else if (ox_strcmp(ch, "stdout") == 0) { } else if (ox::strcmp(ch, "stdout") == 0) {
printf("%s", msg); printf("%s", msg);
fflush(stdout); fflush(stdout);
} else if (ox_strcmp(ch, "stderr") == 0) { } else if (ox::strcmp(ch, "stderr") == 0) {
printf("%s", msg); printf("%s", msg);
fflush(stdout); fflush(stdout);
} else if (ox_strcmp(ch, "error") == 0) { } else if (ox::strcmp(ch, "error") == 0) {
//std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n'; //std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg); fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg);
fflush(stderr); fflush(stderr);
} }
#else #else
if (ox_strcmp(ch, "info") == 0) { if (ox::strcmp(ch, "info") == 0) {
infoLog(msg); infoLog(msg);
} else if (ox_strcmp(ch, "debug") == 0) { } else if (ox::strcmp(ch, "debug") == 0) {
debugLog(msg); debugLog(msg);
} else if (ox_strcmp(ch, "stdout") == 0) { } else if (ox::strcmp(ch, "stdout") == 0) {
infoLog(msg); infoLog(msg);
} else if (ox_strcmp(ch, "stderr") == 0) { } else if (ox::strcmp(ch, "stderr") == 0) {
errorLog(msg); errorLog(msg);
} else if (ox_strcmp(ch, "error") == 0) { } else if (ox::strcmp(ch, "error") == 0) {
errorLog(msg); errorLog(msg);
} }
#endif #endif

View File

@ -263,12 +263,12 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
} }
[[nodiscard]] [[nodiscard]]
constexpr bool contains(MaybeSV_t<T> const&) const noexcept(useNoexcept); constexpr bool contains(MaybeView_t<T> const&) const noexcept(useNoexcept);
constexpr iterator<T&, T*, false> insert( constexpr iterator<T&, T*, false> insert(
std::size_t pos, std::size_t cnt, MaybeSV_t<T> const&val) noexcept(useNoexcept); std::size_t pos, std::size_t cnt, MaybeView_t<T> const&val) noexcept(useNoexcept);
constexpr iterator<T&, T*, false> insert(std::size_t pos, MaybeSV_t<T> const&val) noexcept(useNoexcept); constexpr iterator<T&, T*, false> insert(std::size_t pos, MaybeView_t<T> const&val) noexcept(useNoexcept);
template<typename... Args> template<typename... Args>
constexpr iterator<T&, T*, false> emplace(std::size_t pos, Args&&... args) noexcept(useNoexcept); constexpr iterator<T&, T*, false> emplace(std::size_t pos, Args&&... args) noexcept(useNoexcept);
@ -278,7 +278,7 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
constexpr void push_back(T &&item) noexcept(useNoexcept); constexpr void push_back(T &&item) noexcept(useNoexcept);
constexpr void push_back(MaybeSV_t<T> const&item) noexcept(useNoexcept); constexpr void push_back(MaybeView_t<T> const&item) noexcept(useNoexcept);
constexpr void pop_back() noexcept(useNoexcept); constexpr void pop_back() noexcept(useNoexcept);
@ -516,7 +516,7 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::resize(std::size_t size) n
} }
template<typename T, std::size_t SmallVectorSize, typename Allocator> template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(MaybeSV_t<T> const&v) const noexcept(useNoexcept) { constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(MaybeView_t<T> const&v) const noexcept(useNoexcept) {
for (std::size_t i = 0; i < m_size; i++) { for (std::size_t i = 0; i < m_size; i++) {
if (m_items[i] == v) { if (m_items[i] == v) {
return true; return true;
@ -528,7 +528,7 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(MaybeSV_t<T> cons
template<typename T, std::size_t SmallVectorSize, typename Allocator> template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false> constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false>
Vector<T, SmallVectorSize, Allocator>::insert( Vector<T, SmallVectorSize, Allocator>::insert(
std::size_t pos, std::size_t cnt, MaybeSV_t<T> const&val) noexcept(useNoexcept) { std::size_t pos, std::size_t cnt, MaybeView_t<T> const&val) noexcept(useNoexcept) {
if (m_size + cnt > m_cap) { if (m_size + cnt > m_cap) {
reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt); reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt);
if (pos < m_size) { if (pos < m_size) {
@ -556,7 +556,7 @@ Vector<T, SmallVectorSize, Allocator>::insert(
template<typename T, std::size_t SmallVectorSize, typename Allocator> template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false> constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, MaybeSV_t<T> const&val) noexcept(useNoexcept) { Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, MaybeView_t<T> const&val) noexcept(useNoexcept) {
if (m_size == m_cap) { if (m_size == m_cap) {
reserveInsert(m_cap ? m_cap * 2 : initialCap, pos); reserveInsert(m_cap ? m_cap * 2 : initialCap, pos);
if (pos < m_size) { if (pos < m_size) {
@ -622,7 +622,7 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::push_back(T &&item) noexce
} }
template<typename T, std::size_t SmallVectorSize, typename Allocator> template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::push_back(MaybeSV_t<T> const&item) noexcept(useNoexcept) { constexpr void Vector<T, SmallVectorSize, Allocator>::push_back(MaybeView_t<T> const&item) noexcept(useNoexcept) {
if (m_size == m_cap) { if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialCap); reserve(m_cap ? m_cap * 2 : initialCap);
} }