Skip to content

Commit

Permalink
feat: basic implementation of iString and comparative testing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
1143910315 committed Jan 28, 2024
1 parent ac7b7c3 commit 071ca79
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
64 changes: 63 additions & 1 deletion iVerse/include/string/iString.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,78 @@
****/
#pragma once

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

namespace MiracleForest::inline i
{
namespace string
{
class iString
{
private:
Ptr<u8char> _data;
size_t _length;
size_t _begin;
size_t _capacity;
std::allocator<u8char> _alloc;

public:
iString(CPtr<char> data) { _create((CPtr<u8char>)data); }
iString(CRef<std::string> data) { _create((CPtr<u8char>)data.c_str(), data.length()); }
iString(CPtr<u8char> data) { _create(data); }
iString(CPtr<wchar> data)
{
auto tempStr = std::wstring_convert<std::codecvt_utf16<wchar>>().to_bytes(data);
_create((CPtr<u8char>)tempStr.c_str(), tempStr.length());
}
~iString() { _alloc.deallocate(_data, _capacity); }
operator std::string() const { return operator CPtr<char>(); }
operator CPtr<char>() const { return (CPtr<char>)(_data + _begin); }

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

private:
void _create(CPtr<u8char> originData)
{
for (_length = 0; *(originData + _length); _length++) {}
_capacity = 2 * _length;
_data = _alloc.allocate(_capacity);
_begin = _length - _length / 2;
for (size_t i = 0; i < _length; i++) { _data[_begin + i] = originData[i]; }
_data[_begin + _length] = '\0';
}
void _create(CPtr<u8char> originData, size_t originDataLength)
{
_capacity = 2 * originDataLength;
_data = _alloc.allocate(_capacity);
_begin = originDataLength - originDataLength / 2;
for (_length = 0; *(originData + _length); _length++)
{
_data[_begin + _length] = originData[_length];
}
_data[_begin + _length] = '\0';
}
};
} // namespace string
} // namespace MiracleForest::inline i
} // namespace MiracleForest::inline i

namespace std
{
template<>
struct hash<MiracleForest::string::iString>
{
size_t operator()(MiracleForest::CRef<MiracleForest::string::iString> key)
{
MiracleForest::uint seed = 131;
MiracleForest::uint hash = 0;
MiracleForest::CPtr<char> str = key;
while (*str) { hash = hash * seed + (*str++); }
return (hash & 0x7FFFFFFF);
}
};
} // namespace std
46 changes: 44 additions & 2 deletions iVerse/tests/iVerseTest.cpp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
#include <string/iString.h>
#include "string/iString.h"
#include <chrono>
#include <iostream>
using string = MiracleForest::string::iString;

void compare()
{
char c[] = "emmm,测试utf-8字符串999";
char* d = c + 5;
{
// 获取开始时间
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 10000000; i++) { std::string str(d); }
auto end = std::chrono::high_resolution_clock::now();
// 计算耗时,并将结果转换为毫秒
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

int main() { return 0; }
// 输出结果
std::cout << "标准库Function took " << duration.count() << " ms to execute." << std::endl;
}
{
// 获取开始时间
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 10000000; i++) { string str(d); }
auto end = std::chrono::high_resolution_clock::now();
// 计算耗时,并将结果转换为毫秒
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

// 输出结果
std::cout << "自定义字符串Function took " << duration.count() << " ms to execute." << std::endl;
}
}
int main()
{
system("chcp 65001");
char8_t c[] = u8"emmm,测试utf-8字符串999";
string str("emmm,测试utf-8字符串999");
// string str1(c);
std::cout << (const char*)c << std::endl;
std::cout << str << std::endl;
char any;
std::cout << "按下任意键开始测试" << std::endl;
std::cin >> any;
for (size_t i = 0; i < 10; i++) { compare(); }
return 0;
}

0 comments on commit 071ca79

Please sign in to comment.