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 95b445d commit 647e726
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Test/Sample.AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void example_binding(const vu::Socket::Endpoint& endpoint)
{
vu::Buffer data(KiB);
client.recv(data);
printf("client %d recv `%s`\n", client.get_remote_sai().sin_port, data.to_string_A().c_str());
printf("client %d recv `%s`\n", client.get_remote_sai().sin_port, data.to_string_A()->c_str());
});

server.bind(endpoint);
Expand Down
24 changes: 12 additions & 12 deletions Test/Sample.Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,22 @@ DEF_SAMPLE(Misc)
std::string s = "0123456789";
vu::Buffer slicer(s.data(), s.size());

std::cout << slicer(0, 0).to_string_A() << std::endl;
std::cout << slicer(9, 9).to_string_A() << std::endl;
std::cout << slicer(0, 9).to_string_A() << std::endl;
//std::cout << slicer(0, 0)->to_string_A()->c_str() << std::endl;
//std::cout << slicer(9, 9)->to_string_A()->c_str() << std::endl;
std::cout << slicer(0, 9)->to_string_A()->c_str() << std::endl;

std::cout << slicer(3, 7).to_string_A() << std::endl;
std::cout << slicer(3, 7)->to_string_A()->c_str() << std::endl;

std::cout << slicer(-7, -2).to_string_A() << std::endl;
std::cout << slicer(2, -2).to_string_A() << std::endl;
std::cout << slicer(-7, -2)->to_string_A()->c_str() << std::endl;
std::cout << slicer(2, -2)->to_string_A()->c_str() << std::endl;

std::cout << slicer(0, -8).to_string_A() << std::endl;
std::cout << slicer(0, -9).to_string_A() << std::endl;
std::cout << slicer(0, -10).to_string_A() << std::endl;
std::cout << slicer(0, -8)->to_string_A()->c_str() << std::endl;
std::cout << slicer(0, -9)->to_string_A()->c_str() << std::endl;
//std::cout << slicer(0, -10)->to_string_A()->c_str() << std::endl;

std::cout << slicer(-8, 10).to_string_A() << std::endl;
std::cout << slicer(-9, 10).to_string_A() << std::endl;
std::cout << slicer(-10, 10).to_string_A() << std::endl;
std::cout << slicer(-8, 10)->to_string_A()->c_str() << std::endl;
std::cout << slicer(-9, 10)->to_string_A()->c_str() << std::endl;
std::cout << slicer(-10, 10)->to_string_A()->c_str() << std::endl;

std::tcout << vu::undecorate_cpp_symbol(ts("?func1@a@@AAEXH@Z")) << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion Test/Sample.Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DEF_SAMPLE(Socket)

// extract response header & body

const std::string first_response = response.to_string_A();
const std::string first_response = response.to_string_A()->c_str();
const std::string http_header_sep = std::crlf;
const std::string http_header_end = http_header_sep + http_header_sep;

Expand Down
1 change: 1 addition & 0 deletions Test/Text.Encoding/ansi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is an ansi text
Binary file added Test/Text.Encoding/utf-16-be-bom.txt
Binary file not shown.
Binary file added Test/Text.Encoding/utf-16-be.txt
Binary file not shown.
Binary file added Test/Text.Encoding/utf-16-le-bom.txt
Binary file not shown.
Binary file added Test/Text.Encoding/utf-16-le.txt
Binary file not shown.
1 change: 1 addition & 0 deletions Test/Text.Encoding/utf-8-bom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
đây là văn bản utf-8 bom
1 change: 1 addition & 0 deletions Test/Text.Encoding/utf-8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
đây là văn bản utf-8
18 changes: 9 additions & 9 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ class Buffer
bool operator==(const Buffer& right) const;
bool operator!=(const Buffer& right) const;
byte& operator[](const size_t offset);
Buffer operator()(int begin, int end) const;
std::unique_ptr<Buffer> operator()(int begin, int end) const;

byte* bytes() const;
void* pointer() const;
Expand All @@ -1044,14 +1044,14 @@ class Buffer
bool replace(const Buffer& right);
bool match(const void* ptr, const size_t size) const;
size_t find(const void* ptr, const size_t size) const;
Buffer till(const void* ptr, const size_t size) const;
Buffer slice(int begin, int end) const;
std::unique_ptr<Buffer> till(const void* ptr, const size_t size) const;
std::unique_ptr<Buffer> slice(int begin, int end) const;

bool append(const void* ptr, const size_t size);
bool append(const Buffer& right);

std::string to_string_A() const;
std::wstring to_string_W() const;
std::unique_ptr<std::string> to_string_A() const;
std::unique_ptr<std::wstring> to_string_W() const;

bool save_to_file(const std::string& file_path);
bool save_to_file(const std::wstring& file_path);
Expand Down Expand Up @@ -1718,9 +1718,9 @@ class FileSystemA : public FileSystemX
fs_share fs_flags = fs_share::FS_ALLACCESS,
fs_attribute fa_flags = fs_attribute::FA_NORMAL);

std::unique_ptr<std::string> vuapi read_as_string();
std::unique_ptr<std::string> vuapi read_as_text();

static std::unique_ptr<std::string> vuapi quick_read_as_string(const std::string& file_path);
static std::unique_ptr<std::string> vuapi quick_read_as_text(const std::string& file_path);

static std::unique_ptr<Buffer> quick_read_as_buffer(const std::string& file_path);

Expand Down Expand Up @@ -1749,9 +1749,9 @@ class FileSystemW : public FileSystemX
fs_share fs_flags = fs_share::FS_ALLACCESS,
fs_attribute fa_flags = fs_attribute::FA_NORMAL);

std::unique_ptr<std::wstring> vuapi read_as_string();
std::unique_ptr<std::wstring> vuapi read_as_text();

static std::unique_ptr<std::wstring>vuapi quick_read_as_string(const std::wstring& file_path);
static std::unique_ptr<std::wstring>vuapi quick_read_as_text(const std::wstring& file_path);

static std::unique_ptr<Buffer> vuapi quick_read_as_buffer(const std::wstring& file_path);

Expand Down
12 changes: 6 additions & 6 deletions src/details/filesys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ bool vuapi FileSystemA::initialize(
return true;
}

std::unique_ptr<std::string> vuapi FileSystemA::read_as_string()
std::unique_ptr<std::string> vuapi FileSystemA::read_as_text()
{
auto buffer = this->read_as_buffer();
if (buffer == nullptr)
Expand Down Expand Up @@ -254,10 +254,10 @@ std::unique_ptr<std::string> vuapi FileSystemA::read_as_string()
return result;
}

std::unique_ptr<std::string> vuapi FileSystemA::quick_read_as_string(const std::string& file_path)
std::unique_ptr<std::string> vuapi FileSystemA::quick_read_as_text(const std::string& file_path)
{
FileSystemA file(file_path, vu::fs_mode::FM_OPENEXISTING, fs_generic::FG_READ, fs_share::FS_READ);
return file.read_as_string();
return file.read_as_text();
}

std::unique_ptr<Buffer> FileSystemA::quick_read_as_buffer(const std::string& file_path)
Expand Down Expand Up @@ -368,7 +368,7 @@ bool vuapi FileSystemW::initialize(
return true;
}

std::unique_ptr<std::wstring> vuapi FileSystemW::read_as_string()
std::unique_ptr<std::wstring> vuapi FileSystemW::read_as_text()
{
auto buffer = this->read_as_buffer();
if (buffer == nullptr)
Expand Down Expand Up @@ -410,10 +410,10 @@ std::unique_ptr<std::wstring> vuapi FileSystemW::read_as_string()
return result;
}

std::unique_ptr<std::wstring> vuapi FileSystemW::quick_read_as_string(const std::wstring& file_path)
std::unique_ptr<std::wstring> vuapi FileSystemW::quick_read_as_text(const std::wstring& file_path)
{
FileSystemW file(file_path, vu::fs_mode::FM_OPENEXISTING, fs_generic::FG_READ, fs_share::FS_READ);
return file.read_as_string();
return file.read_as_text();
}

std::unique_ptr<Buffer> FileSystemW::quick_read_as_buffer(const std::wstring& file_path)
Expand Down
61 changes: 42 additions & 19 deletions src/details/mbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ byte& Buffer::operator[](const size_t offset)
return static_cast<byte*>(m_ptr)[offset];
}

Buffer Buffer::operator()(int begin, int end) const
std::unique_ptr<Buffer> Buffer::operator()(int begin, int end) const
{
return this->slice(begin, end);
}
Expand Down Expand Up @@ -113,26 +113,24 @@ bool Buffer::match(const void* ptr, const size_t size) const
return this->find(ptr, size) != -1;
}

Buffer Buffer::till(const void* ptr, const size_t size) const
std::unique_ptr<Buffer> Buffer::till(const void* ptr, const size_t size) const
{
Buffer result;

size_t offset = this->find(ptr, size);
if (offset > 0)
if (offset == 0)
{
result.create(m_ptr, offset);
return nullptr;
}

std::unique_ptr<Buffer> result(new Buffer);
result->create(m_ptr, offset);
return result;
}

Buffer Buffer::slice(int begin, int end) const
std::unique_ptr<Buffer> Buffer::slice(int begin, int end) const
{
Buffer result;

if (m_ptr == nullptr || m_size == 0)
{
return result;
return nullptr;
}

if (begin < 0)
Expand All @@ -147,17 +145,18 @@ Buffer Buffer::slice(int begin, int end) const

if (begin < 0 || end < 0 || begin > int(m_size) || end > int(m_size) || begin > end)
{
return result;
return nullptr;
}

int size = end - begin;

if (size <= 0 || size > int(m_size))
{
return result;
return nullptr;
}

result.create(this->bytes() + begin, size);
std::unique_ptr<Buffer> result(new Buffer);
result->create(this->bytes() + begin, size);

return result;
}
Expand Down Expand Up @@ -295,14 +294,28 @@ bool Buffer::append(const Buffer& right)
return this->append(right.pointer(), right.size());
}

std::string Buffer::to_string_A() const
std::unique_ptr<std::string> Buffer::to_string_A() const
{
return std::string(reinterpret_cast<const char*>(m_ptr), m_size / sizeof(char));
std::unique_ptr<std::string> result(new std::string);
if (m_ptr == nullptr || m_size == 0)
{
return result;
}

result.reset(new std::string(reinterpret_cast<const char*>(m_ptr), m_size / sizeof(char)));
return result;
}

std::wstring Buffer::to_string_W() const
std::unique_ptr<std::wstring> Buffer::to_string_W() const
{
return std::wstring(reinterpret_cast<const wchar*>(m_ptr), m_size / sizeof(wchar));
std::unique_ptr<std::wstring> result(new std::wstring);
if (m_ptr == nullptr || m_size == 0)
{
return result;
}

result.reset(new std::wstring(reinterpret_cast<const wchar*>(m_ptr), m_size / sizeof(wchar)));
return result;
}

bool Buffer::save_to_file(const std::string& file_path)
Expand All @@ -323,8 +336,18 @@ bool Buffer::save_to_file(const std::string& file_path)

bool Buffer::save_to_file(const std::wstring& file_path)
{
const auto s = vu::to_string_A(file_path);
return this->save_to_file(s);
if (file_path.empty())
{
return false;
}

bool result = true;

FileSystemW file(file_path, fs_mode::FM_CREATEALWAY);
result &= file.write(m_ptr, ulong(m_size));
result &= file.close();

return result;
}

} // namespace vu
34 changes: 19 additions & 15 deletions src/details/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,26 @@ text_encoding vuapi detect_text_encoding(const void* data, const size_t size)
return text_encoding::TE_UTF16_BE_BOM;
}

/* UTF-16 LE */
if ((p[0] >= SCHAR_MIN && p[0] <= SCHAR_MAX) && p[1] == 0x00)
/* Without BOM */
if (p[0] >= SCHAR_MIN && p[0] <= SCHAR_MAX)
{
return text_encoding::TE_UTF16_LE;
}

/* UTF-16 BE */
if ((p[1] >= SCHAR_MIN && p[1] <= SCHAR_MAX) && p[0] == 0x00)
{
return text_encoding::TE_UTF16_BE;
}

/* UTF-8 */
if ((p[0] >= SCHAR_MIN && p[0] <= SCHAR_MAX) && (p[1] >= SCHAR_MIN && p[1] <= SCHAR_MAX))
{
return text_encoding::TE_UTF8;
// /* UTF-16 LE */
// ...
// {
// return text_encoding::TE_UTF16_LE;
// }

// /* UTF-16 BE */
// ...
// {
// return text_encoding::TE_UTF16_BE;
// }

/* UTF-8 */
if (p[1] >= SCHAR_MIN && p[1] <= SCHAR_MAX)
{
return text_encoding::TE_UTF8;
}
}
}
catch (int)
Expand Down

0 comments on commit 647e726

Please sign in to comment.