WIP: Graphics Overhaul #2

Draft
naifb wants to merge 18 commits from buffer_update into main
3 changed files with 145 additions and 33 deletions
Showing only changes of commit fd7d36e4be - Show all commits

View File

@ -1,4 +1,6 @@
#pragma once
#include <cstdlib>
#include <type_traits>
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
@ -9,20 +11,65 @@
namespace nb {
#ifdef _NB_TARGET_WINDOWS
const std::string NEWLINE("\r\n");
const std::string NEWLINE;
const std::wstring WNEWLINE;
#endif // _NB_TARGET_WINDOWS
#ifdef _NB_TARGET_LINUX
const std::string NEWLINE("\n");
const std::string NEWLINE;
const std::wstring WNEWLINE;
#endif // _NB_TARGET_LINUX
template<typename K, typename V, size_t N>
struct ConstexprMap {
template<typename Input>
constexpr ConstexprMap(Input inp) : _data{inp} {}
template<typename T>
std::enable_if_t<std::is_convertible_v<T, std::string>, std::wstring> str_to_wstr(const T& str) {
std::mbstate_t state = std::mbstate_t();
const char* cstr = std::string(str).c_str();
std::size_t wlen = 1 + std::mbsrtowcs(nullptr, &cstr, 0, &state);
wchar_t* c_wstr= new wchar_t[wlen];
std::mbsrtowcs(c_wstr, &cstr, wlen, &state);
std::wstring ret(c_wstr, wlen);
delete[] c_wstr;
return ret;
}
protected:
const std::array<std::pair<K, V>, N> _data;
};
template<typename T>
std::enable_if_t<std::is_convertible_v<T, std::wstring>, std::string> wstr_to_str(const T& wstr) {
const wchar_t* c_wstr = std::wstring(wstr).c_str();
std::size_t strlen = 1 + std::wcstombs(nullptr, c_wstr, 0);
char* c_str= new char[strlen];
std::wcstombs(c_str, c_wstr, strlen);
std::string ret(c_str, strlen);
delete[] c_str;
return ret;
}
template <typename T = std::string, typename A, typename B, typename C>
T find_and_replace(
const A& original,
const B& find,
const C& replace
) {
const T& original_t = T(original);
const T& find_t = T(find);
const T& replace_t = T(replace);
T ret = original_t;
std::size_t find_len = find_t.length();
std::size_t replace_len = replace_t.length();
std::size_t currpos = 0;
std::size_t lastpos = 0;
while(true) {
lastpos = currpos;
currpos = original_t.find(find_t, lastpos);
if (currpos == T::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace_t);
currpos += replace_len;
}
return ret;
}
/* template<typename T>
T swap_endian(const T& val) {
@ -37,7 +84,6 @@ T swap_endian(const T& val) {
return ret;
} */
std::string find_and_replace(const std::string& original, const std::string& find, const std::string& replace);
// using ByteVector = std::vector<uint8_t>;

View File

@ -1,23 +1,14 @@
#include <corecrt.h>
#include <stdint.h>
#include <string>
#include "Utils.hpp"
std::string nb::find_and_replace(const std::string& original, const std::string& find, const std::string& replace) {
std::string ret = original;
std::size_t find_len = find.length();
std::size_t replace_len = replace.length();
std::size_t currpos = 0;
std::size_t lastpos = 0;
while(true) {
lastpos = currpos;
currpos = original.find(find, lastpos);
if (currpos == std::string::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace);
currpos += replace_len;
}
return ret;
}
#ifdef _NB_TARGET_WINDOWS
const std::string NEWLINE("\r\n");
const std::wstring WNEWLINE(L"\r\n");
#endif // _NB_TARGET_WINDOWS
#ifdef _NB_TARGET_LINUX
const std::string NEWLINE("\n");
const std::wstring WNEWLINE(L"\n");
#endif // _NB_TARGET_LINUX

View File

@ -1,17 +1,20 @@
#include <iterator>
#define CODE_ERROR_LOCATIONS
#include "Utils.hpp"
#include <gtest/gtest.h>
#include <string>
#include "Utils.hpp"
namespace nb {
TEST(UtilsTest, Test) {
TEST(UtilsTest, TestFindAndReplace) {
ASSERT_STREQ(
find_and_replace("Jeff", "e", "efe").c_str(),
"Jefeff"
);
auto tmp = find_and_replace("Naif", "a", "afa");
std::string tmp = find_and_replace("Naif", "a", "afa");
ASSERT_STREQ(
find_and_replace(tmp, "i", "ifi").c_str(),
"Nafaifif"
@ -38,4 +41,76 @@ TEST(UtilsTest, Test) {
);
}
TEST(UtilsTest, TestFindAndReplaceWstr) {
ASSERT_STREQ(
find_and_replace<std::wstring>(L"Jeff", L"e", L"efe").c_str(),
L"Jefeff"
);
std::wstring tmp = find_and_replace<std::wstring>(L"Naif", L"a", L"afa");
ASSERT_STREQ(
find_and_replace<std::wstring>(tmp, L"i", L"ifi").c_str(),
L"Nafaifif"
);
tmp = find_and_replace<std::wstring>(L"aeiou", L"a", L"afa");
tmp = find_and_replace<std::wstring>(tmp, L"e", L"efe");
tmp = find_and_replace<std::wstring>(tmp, L"i", L"ifi");
tmp = find_and_replace<std::wstring>(tmp, L"o", L"ofo");
tmp = find_and_replace<std::wstring>(tmp, L"u", L"ufu");
ASSERT_STREQ(
tmp.c_str(),
L"afaefeifiofoufu"
);
tmp = find_and_replace<std::wstring>(tmp, L"afa", L"a");
tmp = find_and_replace<std::wstring>(tmp, L"efe", L"e");
tmp = find_and_replace<std::wstring>(tmp, L"ifi", L"i");
tmp = find_and_replace<std::wstring>(tmp, L"ofo", L"o");
tmp = find_and_replace<std::wstring>(tmp, L"ufu", L"u");
ASSERT_STREQ(
tmp.c_str(),
L"aeiou"
);
}
TEST(UtilsTest, TestWstrToStr) {
ASSERT_STREQ(
nb::wstr_to_str(L"Hi!").c_str(),
"Hi!"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif").c_str(),
"Naif"
);
ASSERT_STREQ(
nb::wstr_to_str(L"\r\r\r\n").c_str(),
"\r\r\r\n"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
"Naif\ttalks\r\ra\t\nlot"
);
}
TEST(UtilsTest, TestStrToWstr) {
ASSERT_STREQ(
nb::str_to_wstr("Hi!").c_str(),
L"Hi!"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif").c_str(),
L"Naif"
);
ASSERT_STREQ(
nb::str_to_wstr("\r\r\r\n").c_str(),
L"\r\r\r\n"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
L"Naif\ttalks\r\ra\t\nlot"
);
}
} // namespace nb