diff --git a/LSString.sln b/LSString.sln new file mode 100644 index 0000000..4a005a0 --- /dev/null +++ b/LSString.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31624.102 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LSString", "LSString\LSString.vcxproj", "{5FE98B77-3558-4384-BDAF-82AC8B69B752}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Debug|x64.ActiveCfg = Debug|x64 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Debug|x64.Build.0 = Debug|x64 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Debug|x86.ActiveCfg = Debug|Win32 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Debug|x86.Build.0 = Debug|Win32 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Release|x64.ActiveCfg = Release|x64 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Release|x64.Build.0 = Release|x64 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Release|x86.ActiveCfg = Release|Win32 + {5FE98B77-3558-4384-BDAF-82AC8B69B752}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CDE2A97D-BF32-4748-9A47-B8D0E10D3A9D} + EndGlobalSection +EndGlobal diff --git a/LSString/LSString.cpp b/LSString/LSString.cpp new file mode 100644 index 0000000..c8db3ca --- /dev/null +++ b/LSString/LSString.cpp @@ -0,0 +1,229 @@ +#include "LSString.h" +#include + +//----------------------------------------------------------------------------------------------------------------------------- +LSString::LSString(const char* pData) + : m_uiLength(StringLength(pData)) + , m_uiCapacity(StringLength(pData)) +{ + if (!pData) return; + + m_pData = new char[m_uiLength + 1]; + memset(m_pData, 0, m_uiLength + 1); + memcpy(m_pData, pData, m_uiLength); +} + +//----------------------------------------------------------------------------------------------------------------------------- +LSString::LSString(const std::string& sRhs) + : m_pData(new char[sRhs.length()]) + , m_uiLength(sRhs.length()) + , m_uiCapacity(sRhs.length()) +{ +} + +//----------------------------------------------------------------------------------------------------------------------------- +LSString::LSString(const LSString& sRhs) + : m_uiLength(sRhs.m_uiLength) + , m_pData(new char[sRhs.m_uiLength + 1]) + , m_uiCapacity(sRhs.m_uiCapacity) +{ + memset(m_pData + m_uiLength, 0, 1); + memcpy(m_pData, sRhs.m_pData, m_uiLength); +} + +//----------------------------------------------------------------------------------------------------------------------------- +LSString::~LSString() +{ + DELETE_ARRAY(m_pData); +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::Insert(const LSString& sRhs, size_t uiPos) +{ + Insert(sRhs.m_pData, uiPos); +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::Insert(const std::string& sRhs, size_t uiPos) +{ + Insert(sRhs.c_str(), uiPos); +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::Insert(const char* pData, size_t uiPos) +{ + if (!pData) return; + + size_t uilength = StringLength(pData); + size_t uiNewLength = m_uiLength + uilength; + + while (uiNewLength > m_uiCapacity) + { + IncreaseCapacity(uiNewLength); + } + + char* pLeft = m_pData + uiPos; + char* pRight = m_pData + uiPos + uilength; + + memcpy(pRight, m_pData + uiPos, m_uiLength - uiPos); + memcpy(pLeft, pData, uilength); + + m_uiLength = uiNewLength; +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::Append(const LSString& sRhs) +{ + Insert(sRhs, sRhs.Length()); +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::Append(const std::string& sRhs) +{ + Insert(sRhs, sRhs.length()); +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::Append(const char* pData) +{ + Insert(pData, StringLength(pData)); +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::Remove(const LSString& sRhs) +{ + return RemoveAt(Find(sRhs), sRhs.m_uiLength); +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::Remove(const std::string& sRhs) +{ + return RemoveAt(Find(sRhs), sRhs.length()); +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::Remove(const char* pData) +{ + return RemoveAt(Find(pData), StringLength(pData)); +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::RemoveAt(size_t uiPos, size_t uiSize) +{ + if (uiPos > m_uiLength) return false; + + m_uiLength = m_uiLength - uiSize; + char* pLast = m_pData + uiPos + uiSize; + memcpy(m_pData + uiPos, pLast, uiSize); + memset(pLast, 0, uiSize); + + return true; +} + +//----------------------------------------------------------------------------------------------------------------------------- +size_t LSString::StringLength(const char* pChar) const +{ + if (!pChar) return 0; + int iRet = 0; + while (*pChar != 0x0) + { + pChar++; + iRet++; + } + return iRet; +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::Contains(const LSString& sRhs) const +{ + return Contains(sRhs.m_pData); +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::Contains(const std::string& sRhs) const +{ + return Contains(sRhs.c_str()); +} + +//----------------------------------------------------------------------------------------------------------------------------- +bool LSString::Contains(const char* pData) const +{ + int iRet = 1; + size_t uiLength = StringLength(pData); + size_t uiOffset = 0; + + while (iRet != 0) + { + if (uiLength > m_uiLength || uiOffset > m_uiLength) break; + iRet = memcmp(m_pData + uiOffset, pData, uiLength); + uiOffset++; + } + + return iRet == 0; +} + +//----------------------------------------------------------------------------------------------------------------------------- +size_t LSString::Find(const LSString& sRhs, size_t uiOffset) const +{ + return Find(sRhs.m_pData, uiOffset); +} + +//----------------------------------------------------------------------------------------------------------------------------- +size_t LSString::Find(const std::string& sRhs, size_t uiOffset) const +{ + return Find(sRhs.c_str(), uiOffset); +} + +//----------------------------------------------------------------------------------------------------------------------------- +size_t LSString::Find(const char* pData, size_t uiOffset) const +{ + size_t uiLength = StringLength(pData); + size_t uiCurrentOffset = uiOffset; + + for (int iRet = 1; iRet != 0; uiCurrentOffset++) + { + if (uiLength > m_uiLength || uiCurrentOffset > m_uiLength) + { + uiCurrentOffset = -1; + break; + } + iRet = memcmp(m_pData + uiCurrentOffset, pData, uiLength); + } + + return uiCurrentOffset; +} + +//----------------------------------------------------------------------------------------------------------------------------- +char LSString::At(size_t uiPos) const +{ + return *(m_pData + uiPos); +} + +//----------------------------------------------------------------------------------------------------------------------------- +size_t LSString::Length() const +{ + return m_uiLength; +} + +//----------------------------------------------------------------------------------------------------------------------------- +std::string LSString::ToStdString() const +{ + return std::string(m_pData); +} + +//----------------------------------------------------------------------------------------------------------------------------- +const char* LSString::ToCChar() const +{ + return m_pData; +} + +//----------------------------------------------------------------------------------------------------------------------------- +void LSString::IncreaseCapacity(size_t uiNewLength) +{ + m_uiCapacity = (m_uiCapacity + uiNewLength) * c_uiCapacityIncreaseFactor; + char* pNew = new char[m_uiCapacity + 1]; + memset(pNew, 0, m_uiCapacity + 1); + memcpy(pNew, m_pData, m_uiLength); + DELETE_ARRAY(m_pData); + m_pData = pNew; +} \ No newline at end of file diff --git a/LSString/LSString.h b/LSString/LSString.h new file mode 100644 index 0000000..8617564 --- /dev/null +++ b/LSString/LSString.h @@ -0,0 +1,88 @@ +#pragma once +#include + +class LSString +{ +private: +#define DELETE_POINTER(Pointer) \ +if (Pointer != nullptr)\ +{\ + delete Pointer;\ + Pointer = nullptr;\ +} + +#define DELETE_ARRAY(Array) \ +if (Array != nullptr)\ +{\ + delete[] Array;\ + Array = nullptr;\ +} + +public: + // - Constructor + LSString() = default; + + LSString(const char* pData); + + LSString(const LSString& sRhs); + + LSString(const std::string& sRhs); + + LSString& operator=(const LSString& sRhs) = delete; + + // - Destructor + ~LSString(); + + // - Alloc + void Insert(const LSString& sRhs, size_t uiPos); + + void Insert(const std::string& sRhs, size_t uiPos); + + void Insert(const char* pData, size_t uiPos); + + void Append(const LSString& sRhs); + + void Append(const std::string& sRhs); + + void Append(const char* pData); + + bool Remove(const LSString& sRhs); + + bool Remove(const std::string& sRhs); + + bool Remove(const char* pData); + + bool RemoveAt(size_t uiPos, size_t uiSize); + + // - Utility + size_t StringLength(const char* pData) const; + + bool Contains(const LSString& sRhs) const; + + bool Contains(const std::string& sRhs) const; + + bool Contains(const char* pData) const; + + size_t Find(const LSString& sRhs, size_t uiOffset = 0) const; + + size_t Find(const std::string& sRhs, size_t uiOffset = 0) const; + + size_t Find(const char* pData, size_t uiOffset = 0) const; + + char At(size_t uiPos) const; + + size_t Length() const; + + std::string ToStdString() const; + + const char* ToCChar() const; + +private: + + void IncreaseCapacity(size_t uiNewLength = 0); + + char* m_pData = nullptr; + size_t m_uiLength = 0; + size_t m_uiCapacity = 0; + const float c_uiCapacityIncreaseFactor = 1.5; +}; \ No newline at end of file diff --git a/LSString/LSString.vcxproj b/LSString/LSString.vcxproj new file mode 100644 index 0000000..d70572a --- /dev/null +++ b/LSString/LSString.vcxproj @@ -0,0 +1,166 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {5FE98B77-3558-4384-BDAF-82AC8B69B752} + Win32Proj + String + 10.0.18362.0 + LSString + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LSString/LSString.vcxproj.user b/LSString/LSString.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/LSString/LSString.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/LSString/Timer.cpp b/LSString/Timer.cpp new file mode 100644 index 0000000..14eac06 --- /dev/null +++ b/LSString/Timer.cpp @@ -0,0 +1,20 @@ +#include "Timer.h" +#include + +//----------------------------------------------------------------------------------------------------------------------------- +Timer::Timer() +{ + m_StartTimePoint = std::chrono::high_resolution_clock::now(); +} + +//----------------------------------------------------------------------------------------------------------------------------- +Timer::~Timer() +{ + auto endTimePoint = std::chrono::high_resolution_clock::now(); + + auto start = std::chrono::time_point_cast(m_StartTimePoint).time_since_epoch().count(); + auto end = std::chrono::time_point_cast(endTimePoint).time_since_epoch().count(); + + auto duration = end - start; + std::cout << "Duration in Microseconds: " << duration << " (" << duration * 0.001 << " ms)" << std::endl; +} \ No newline at end of file diff --git a/LSString/Timer.h b/LSString/Timer.h new file mode 100644 index 0000000..d82e9b5 --- /dev/null +++ b/LSString/Timer.h @@ -0,0 +1,12 @@ +#pragma once +#include + +class Timer +{ +public: + Timer(void); + + ~Timer(void); +private: + std::chrono::time_point m_StartTimePoint; +}; \ No newline at end of file diff --git a/LSString/main.cpp b/LSString/main.cpp new file mode 100644 index 0000000..761998c --- /dev/null +++ b/LSString/main.cpp @@ -0,0 +1,26 @@ +#include +#include "LSString.h" +#include "Timer.h" + +#define RUNS 50U +#define PAYLOAD "187" + +int main() +{ + printf("LSString:\n"); + printf("Number of runs: %d:\n", (int)RUNS); + Timer ttt; + for (size_t zRun = 1; zRun <= RUNS; zRun++) + { + printf("Run: %2d: ", (int)zRun); + + Timer tt; + LSString s("361"); + + for (size_t z = 0; z < RUNS * 187; z++) + { + s.Append(PAYLOAD); + } + } + return 0; +} \ No newline at end of file