Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Sep 14, 2023
1 parent 647e726 commit dc76007
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 410 deletions.
77 changes: 2 additions & 75 deletions Test/Sample.Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,6 @@ DEF_SAMPLE(Misc)
// vu::msg_box(vu::get_console_window(), ts("I'm %s. I'm %d years old."), ts("Vic P"), 26);
// vu::msg_debug(ts("I'm %s. I'm %d years old."), ts("Vic P"), 26);

std::tcout << vu::lower_string(ts("I Love You")) << std::endl;
std::tcout << vu::upper_string(ts("I Love You")) << std::endl;

std::tcout << vu::trim_string(ts(" THIS IS A TRIM STRING ")) << std::endl;

std::tcout << vu::get_last_error() << std::endl;

std::vector<std::tstring> l;

l.clear();
l = vu::split_string(ts("THIS IS A SPLIT STRING"), ts(" "));
for (auto e : l) std::tcout << e << ts("|");
std::tcout << std::endl;

l.clear();
l = vu::multi_string_to_list(ts("THIS\0IS\0A\0MULTI\0STRING\0\0"));
for (auto& e : l) std::tcout << e << ts("|");
std::tcout << std::endl;

std::tcout << vu::date_time_to_string(time(NULL)) << std::endl;

std::cout << vu::to_string_A(L"THIS IS A WIDE STRING") << std::endl;
std::wcout << vu::to_string_W("THIS IS AN ANSI STRING") << std::endl;

std::tcout << ts("Environment `PATH`") << std::endl;
std::tstring envValue = vu::get_env(ts("PATH"));
auto env = vu::split_string(envValue, ts(";"));
for (auto e : env) {
std::tcout << '\t' << e << std::endl;
}

std::tcout << vu::replace_string(ts("Written in C++ and for C++"), ts("C++"), ts("Cpp")) << std::endl;

std::tcout << vu::starts_with(ts("Written in C++ and for C++"), ts("C++")) << std::endl;
std::tcout << vu::starts_with(ts("Written in C++ and for C++"), ts("Written")) << std::endl;

std::tcout << vu::ends_with(ts("Written in C++ and for C++"), ts("C++")) << std::endl;
std::tcout << vu::ends_with(ts("Written in C++ and for C++"), ts("Written")) << std::endl;

std::tcout << vu::contains_string(ts("Written in C++ and for C++"), ts("c++"), false) << std::endl;
std::tcout << vu::contains_string(ts("Written in C++ and for C++"), ts("c++"), true) << std::endl;

std::tcout << vu::compare_string(ts("C++"), ts("c++"), false) << std::endl;
std::tcout << vu::compare_string(ts("C++"), ts("c++"), true) << std::endl;

std::vector<vu::ulong> pids;
pids.clear();

Expand All @@ -76,38 +31,10 @@ DEF_SAMPLE(Misc)
// vu::inject_dll(pids.back(), ts("path\\to\\32-bit-dll"), true);
// vu::inject_dll(pids.back(), ts("path\\to\\64-bit-dll"), true);

static std::wstring LES[] = { // List Encoding Short
L"ANSI/UTF-8", L"UTF-8 BOM",
L"Unicode", L"Unicode BE",
L"Unicode BOM", L"Unicode BE BOM",
L"UTF-32 LE BOM", L"UTF-32 BE BOM"
};

static std::wstring LEL[] = { // List Encoding Long
L"ANSI/UTF-8", L"UTF-8 BOM",
L"UTF-16 Little Endian", L"UTF-16 Big Endian",
L"UTF-16 Little Endian BOM", L"UTF-16 Big Endian BOM",
L"UTF-32 Little Endian BOM", L"UTF-32 Big Endian BOM"
};

vu::FileSystem::iterate(ts("path\\to\\example"), ts("*.txt"), [](const vu::FSObject& fso) -> bool
vu::FileSystem::iterate(ts(".\\Text.Encoding"), ts("*.txt"), [](const vu::FSObject& fso) -> bool
{
auto file_path = fso.directory + fso.name;
auto data = vu::FileSystem::quick_read_as_buffer(file_path);

auto result = vu::detect_text_encoding(data->pointer(), data->size());
auto es = result == vu::text_encoding::TE_UNKNOWN ? L"Unknown" : LES[int(result)];
auto el = result == vu::text_encoding::TE_UNKNOWN ? L"Unknown" : LEL[int(result)];

std::wcout
<< std::left
<< std::setw(15) << es
<< " | "
<< std::setw(25) << el
<< " | "
<< fso.name.c_str()
<< std::endl;

std::wcout << file_path << std::endl;
return true;
});

Expand Down
96 changes: 96 additions & 0 deletions Test/Sample.String.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#include "Sample.h"

DEF_SAMPLE(String)
{
std::tcout << vu::lower_string(ts("I Love You")) << std::endl;
std::tcout << vu::upper_string(ts("I Love You")) << std::endl;

std::tcout << vu::trim_string(ts(" THIS IS A TRIM STRING ")) << std::endl;

std::tcout << vu::get_last_error() << std::endl;

std::vector<std::tstring> l;

l.clear();
l = vu::split_string(ts("THIS IS A SPLIT STRING"), ts(" "));
for (auto e : l) std::tcout << e << ts("|");
std::tcout << std::endl;

l.clear();
l = vu::multi_string_to_list(ts("THIS\0IS\0A\0MULTI\0STRING\0\0"));
for (auto& e : l) std::tcout << e << ts("|");
std::tcout << std::endl;

std::tcout << vu::date_time_to_string(time(NULL)) << std::endl;

std::cout << vu::to_string_A(L"THIS IS A WIDE STRING") << std::endl;
std::wcout << vu::to_string_W("THIS IS AN ANSI STRING") << std::endl;

std::tcout << ts("Environment `PATH`") << std::endl;
std::tstring var_list = vu::get_env(ts("PATH"));
auto env = vu::split_string(var_list, ts(";"));
for (auto e : env)
{
std::tcout << '\t' << e << std::endl;
}

std::tcout << vu::replace_string(ts("Written in C++ and for C++"), ts("C++"), ts("Cpp")) << std::endl;

std::tcout << vu::starts_with(ts("Written in C++ and for C++"), ts("C++")) << std::endl;
std::tcout << vu::starts_with(ts("Written in C++ and for C++"), ts("Written")) << std::endl;

std::tcout << vu::ends_with(ts("Written in C++ and for C++"), ts("C++")) << std::endl;
std::tcout << vu::ends_with(ts("Written in C++ and for C++"), ts("Written")) << std::endl;

std::tcout << vu::contains_string(ts("Written in C++ and for C++"), ts("c++"), false) << std::endl;
std::tcout << vu::contains_string(ts("Written in C++ and for C++"), ts("c++"), true) << std::endl;

std::tcout << vu::compare_string(ts("C++"), ts("c++"), false) << std::endl;
std::tcout << vu::compare_string(ts("C++"), ts("c++"), true) << std::endl;

struct
{
vu::text_encoding encoding;
std::string file_name;
} list_text_encodings[] =
{
{ vu::text_encoding::TE_UTF8, "ansi.txt" },
{ vu::text_encoding::TE_UTF8_BOM, "utf-8-bom.txt" },
//{ vu::text_encoding::TE_UTF16_LE, "utf-16-le.txt" },
//{ vu::text_encoding::TE_UTF16_BE, "utf-16-be.txt" },
{ vu::text_encoding::TE_UTF16_LE_BOM, "utf-16-le-bom.txt" },
{ vu::text_encoding::TE_UTF16_BE_BOM, "utf-16-be-bom.txt" },
//{ vu::text_encoding::TE_UTF32_LE_BOM, "utf-32-le-bom.txt" },
//{ vu::text_encoding::TE_UTF32_BE_BOM, "utf-32-be-bom.txt" },
};

for (auto& e : list_text_encodings)
{
vu::PathA path = "Text.Encoding";
path.join(e.file_name).finalize();
auto file_path = path.as_string();
std::cout << file_path << std::endl;

auto data = vu::read_file_binary_A(file_path);
auto text_encoding = vu::detect_text_encoding(data->data(), data->size());
assert(text_encoding == e.encoding);

{
auto temp = vu::FileSystemA::quick_read_as_text(file_path);
auto text = *temp; // vu::to_string_W(*temp.get(), true);
assert(!text.empty());
std::cout << std::tab << text << std::endl;
}

{
auto temp = vu::FileSystemW::quick_read_as_text(vu::to_string_W(file_path));
auto text = vu::to_string_A(*temp, true);
assert(!text.empty());
std::cout << std::tab << text << std::endl;
}
}

return vu::VU_OK;
}
Loading

0 comments on commit dc76007

Please sign in to comment.