From 071ca79b73d2af43eb3a89ceef7414e089d7853e Mon Sep 17 00:00:00 2001 From: HapiFive <1143910315@qq.com> Date: Mon, 29 Jan 2024 00:02:21 +0800 Subject: [PATCH] feat: basic implementation of iString and comparative testing code. --- iVerse/include/string/iString.h | 64 ++++++++++++++++++++++++++++++++- iVerse/tests/iVerseTest.cpp.cpp | 46 ++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/iVerse/include/string/iString.h b/iVerse/include/string/iString.h index 0b7bbf2..41765c5 100644 --- a/iVerse/include/string/iString.h +++ b/iVerse/include/string/iString.h @@ -17,7 +17,11 @@ ****/ #pragma once +#include +#include +#include #include +#include namespace MiracleForest::inline i { @@ -25,8 +29,66 @@ namespace string { class iString { + private: + Ptr _data; + size_t _length; + size_t _begin; + size_t _capacity; + std::allocator _alloc; + + public: + iString(CPtr data) { _create((CPtr)data); } + iString(CRef data) { _create((CPtr)data.c_str(), data.length()); } + iString(CPtr data) { _create(data); } + iString(CPtr data) + { + auto tempStr = std::wstring_convert>().to_bytes(data); + _create((CPtr)tempStr.c_str(), tempStr.length()); + } + ~iString() { _alloc.deallocate(_data, _capacity); } + operator std::string() const { return operator CPtr(); } + operator CPtr() const { return (CPtr)(_data + _begin); } + public: + size_t length() const { return _length; } + private: + void _create(CPtr 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 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 \ No newline at end of file +} // namespace MiracleForest::inline i + +namespace std +{ +template<> +struct hash +{ + size_t operator()(MiracleForest::CRef key) + { + MiracleForest::uint seed = 131; + MiracleForest::uint hash = 0; + MiracleForest::CPtr str = key; + while (*str) { hash = hash * seed + (*str++); } + return (hash & 0x7FFFFFFF); + } +}; +} // namespace std \ No newline at end of file diff --git a/iVerse/tests/iVerseTest.cpp.cpp b/iVerse/tests/iVerseTest.cpp.cpp index eb6ac12..8922bed 100644 --- a/iVerse/tests/iVerseTest.cpp.cpp +++ b/iVerse/tests/iVerseTest.cpp.cpp @@ -1,4 +1,46 @@ -#include +#include "string/iString.h" +#include +#include +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(end - start); -int main() { return 0; } \ No newline at end of file + // 输出结果 + 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(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; +} \ No newline at end of file