Skip to content

Commit

Permalink
feat: implement to inherit strings and provide methods for replacing …
Browse files Browse the repository at this point in the history
…and splitting strings.
  • Loading branch information
1143910315 committed Mar 12, 2024
1 parent b1da9db commit c677342
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 37 deletions.
108 changes: 72 additions & 36 deletions iVerse/include/string/iString.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
#pragma once

#include <aliases.h>
#include <codecvt>
#include <functional>
#include <iFamily.h>
#include <string>
#include <vector>

namespace MiracleForest::inline i::inline iVerse
{
namespace String
{
class iString
class iString : public std::string
{
private:
Ptr<u8char> mData;
Expand All @@ -37,46 +37,82 @@ namespace String
std::allocator<u8char> mAlloc;

public:
iString(CPtr<char> dat) { _create((CPtr<u8char>)dat); }
iString(CRef<std::string> dat) { _create((CPtr<u8char>)dat.c_str(), dat.length()); }
iString(CPtr<u8char> dat) { _create(dat); }
iString(CPtr<wchar> dat)
{
auto tempStr = std::wstring_convert<std::codecvt_utf16<wchar>>().to_bytes(dat);
_create((CPtr<u8char>)tempStr.c_str(), tempStr.length());
}
~iString() { mAlloc.deallocate(mData, mCapacity); }
operator std::string() const { return operator CPtr<char>(); }
operator CPtr<char>() const { return (CPtr<char>)(mData + mBegin); }
iString()
: std::string() {};
iString(const char* const _Ptr)
: std::string(_Ptr) {};
iString(const std::allocator<char>& _Al)
: std::string(_Al) {};
iString(std::string&& _Right)
: std::string(_Right) {};
iString(const std::string& _Right)
: std::string(_Right) {};
iString(std::initializer_list<char> _Ilist, const std::allocator<char>& _Al)
: std::string(_Ilist, _Al) {};
iString(std::string&& _Right, const std::allocator<char>& _Al)
: std::string(_Right, _Al) {};
iString(const char* const _Ptr, const std::allocator<char>& _Al)
: std::string(_Ptr, _Al) {};
iString(const std::string& _Right, const std::allocator<char>& _Al)
: std::string(_Right, _Al) {};
iString(const size_t _Count, const char _Ch)
: std::string(_Count, _Ch) {};
iString(const char* const _Ptr, const size_t _Count)
: std::string(_Ptr, _Count) {};
iString(const size_t _Count, const char _Ch, const std::allocator<char>& _Al)
: std::string(_Count, _Ch, _Al) {};
iString(const char* const _Ptr, const size_t _Count, const std::allocator<char>& _Al)
: std::string(_Ptr, _Count, _Al) {};
iString(const std::string& _Right, const size_t _Roff, const std::allocator<char>& _Al)
: std::string(_Right, _Roff, _Al) {};
iString(
const std::string& _Right,
const size_t _Roff,
const size_t _Count,
const std::allocator<char>& _Al
)
: std::string(_Right, _Roff, _Count, _Al) {};

public:
size_t length() const { return mLength; }

private:
void _create(CPtr<u8char> dat)
iString& replace(CRef<iString> oldString, CRef<iString> newString, size_t times = std::string::npos)
{
mLength = _calculateLength(dat);
mCapacity = 2 * mLength;
mData = mAlloc.allocate(mCapacity);
mBegin = mLength / 2;
memcpy(mData + mBegin, dat, mLength + 1);
size_t move = newString.length();
if (oldString.empty()) { move++; }
size_t pos = 0;
while ((pos = find(oldString.data(), pos)) != std::string::npos && times-- != 0)
{
basic_string::replace(pos, oldString.length(), newString.data());
pos += move;
}
return *this;
}

void _create(CPtr<u8char> dat, size_t length)
std::vector<iString> split(CRef<iString> delimiter, bool pushEmpty = false)
{
mCapacity = 2 * length;
mData = mAlloc.allocate(mCapacity);
mBegin = length / 2;
mLength = length;
memcpy(mData + mBegin, dat, length + 1);
}
std::vector<iString> result;
size_t start = 0;
size_t end = find(delimiter);

constexpr static std::size_t _calculateLength(CPtr<u8char> arg)
{
std::size_t length = 0;
while (*arg++) { length++; }
return length;
while (end != std::string::npos)
{
if (delimiter.empty() && start == end) { end++; }

// 提取子字符串(不包括分隔符)
std::string token = substr(start, end - start);
if (!token.empty() || pushEmpty) { result.push_back(token); }


// 跳过分隔符
start = end + delimiter.size();
end = find(delimiter, start);
}

// 添加最后一个子字符串(可能没有分隔符)
if (start < size()) { result.push_back(substr(start)); }

return result;
}

private:
};
} // namespace String
} // namespace MiracleForest::inline i::inline iVerse
Expand All @@ -89,7 +125,7 @@ struct hash<MiracleForest::String::iString>
size_t operator()(MiracleForest::CRef<MiracleForest::String::iString> key)
{
MiracleForest::uint hash = 0;
MiracleForest::CPtr<char> str = key;
MiracleForest::CPtr<char> str = key.data();
while (*str) { hash = hash * 131 + (*str++); }
return (hash & 0x7FFFFFFF);
}
Expand Down
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_rules("mode.debug", "mode.release", "mode.releasedbg")

add_repositories("MiF-repo https://github.com/MiracleForest/xmake-repo.git")

add_requires("iFamily master")
add_requires("iFamily 0.1.0")

target("iVerseTest")
set_kind("binary")
Expand Down

0 comments on commit c677342

Please sign in to comment.