-
Notifications
You must be signed in to change notification settings - Fork 4
New string concept #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
af8eff6
7db585e
4ef0fb0
96a7161
e50c38b
e1412d4
d204826
76bd06a
5c5dbf5
5c10a83
cee59bd
7742b4e
4aa63aa
985df94
c1e0622
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,30 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#pragma once | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "string.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <cppcore/Common/Hash.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <cppcore/CPPCoreCommon.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <malloc.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace cppcore { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct Allocator { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline T *alloc(size_t size, size_t alignment) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return (T*) _aligned_malloc(size, alignment); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void dealloc(T *ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_aligned_free(ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline static size_t countChars(const T *ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nullptr != ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return (::strlen(ptr)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @class TStringBase | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @ingroup CPPCore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -57,135 +38,122 @@ template <class T> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TStringBase { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @brief The default class constructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TStringBase() noexcept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TStringBase() noexcept = default; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @brief The class constructor with a pointer showing to the data buffer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @param pPtr [in] The data buffer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TStringBase(const T *pPtr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TStringBase(const T *pPtr, size_t size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @brief The class destructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
~TStringBase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void set(const T *ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void set(const T *ptr, size_t size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void reset(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t size() const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t capacity() const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const T *c_str() const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @brief Helper method to copy data into the string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @param base [inout] The string data to copy in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// @param pPtr [in] The data source. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
static void copyFrom(TStringBase<T> &base, const T *pPtr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
static void copyFrom(TStringBase<T> &base, const T *pPtr, size_t size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool operator == (const TStringBase<T> &rhs) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool operator != (const TStringBase<T> &rhs) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T *m_pStringBuffer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t m_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t m_capacity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Allocator<T> mAllocator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
static constexpr size_t InitSize = 256; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T mBuffer[InitSize] = {'\0'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T *mStringBuffer{nullptr}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t mSize{0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t mCapacity{256}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HashId mHashId{0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringBase<T>::TStringBase() noexcept : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_pStringBuffer(nullptr), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_size(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_capacity(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mAllocator() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
copyFrom(*this, pPtr, size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mHashId = THash<HashId>::toHash(pPtr, mSize); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringBase<T>::TStringBase(const T *pPtr) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_pStringBuffer(nullptr), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_size(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_capacity(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mAllocator() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
copyFrom(*this, pPtr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringBase<T>::~TStringBase() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringBase<T>::~TStringBase() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (m_pStringBuffer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mAllocator.dealloc(m_pStringBuffer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
m_pStringBuffer = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void TStringBase<T>::set(const T *ptr, size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void reset(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nullptr != ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
copyFrom(*this, ptr, size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void TStringBase<T>::set(const T *ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mAllocator.dealloc(m_pStringBuffer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nullptr != ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
copyFrom(*this, ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void TStringBase<T>::reset() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mSize = 0u; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void TStringBase<T>::copyFrom(TStringBase<T> &base, const T *ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nullptr != ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.m_size = Allocator<T>::countChars(ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (base.m_size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.m_capacity = base.m_size + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.m_pStringBuffer = base.mAllocator.alloc(base.m_capacity, 16); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#ifdef CPPCORE_WINDOWS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
::strncpy_s(base.m_pStringBuffer, base.m_capacity, ptr, base.m_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
::strncpy(base.m_pStringBuffer, ptr, base.m_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.m_pStringBuffer[base.m_size] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline size_t TStringBase<T>::size() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mSize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline bool TStringBase<T>::operator==( const TStringBase<T> &rhs ) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (rhs.m_size != m_size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline size_t TStringBase<T>::capacity() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mCapacity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (size_t i = 0; i < m_size; ++i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (rhs.m_pStringBuffer[i] != m_pStringBuffer[i]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline const T *TStringBase<T>::c_str() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (mStringBuffer != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mStringBuffer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mBuffer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline bool TStringBase<T>::operator!=(const TStringBase<T> &rhs) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return !(*this == rhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void TStringBase<T>::clear() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (mStringBuffer != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
delete [] mStringBuffer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mStringBuffer = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mCapacity = InitSize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mSize = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class TCharType> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TStringView { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TStringView(TCharType *ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
~TStringView(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t size() const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TCharType *data() const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TCharType *mPtr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t mLen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class TCharType> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringView<TCharType>::TStringView(TCharType *ptr) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mPtr(ptr), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mLen(0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nullptr != mPtr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mLen = strlen(ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void TStringBase<T>::copyFrom(TStringBase<T> &base, const T *ptr, size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (ptr != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T *targetPtr = base.mBuffer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (size > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (size > base.mCapacity) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.mStringBuffer = new T[size]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.mCapacity = size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
targetPtr = base.mStringBuffer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
memcpy(targetPtr, ptr, size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
base.mSize = size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address unsafe memory operations and missing null termination. The
inline void TStringBase<T>::copyFrom(TStringBase<T> &base, const T *ptr, size_t size) {
+ static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable");
if (ptr != nullptr) {
T *targetPtr = base.mBuffer;
if (size > 0) {
- if (size > base.mCapacity) {
- base.mStringBuffer = new T[size];
- base.mCapacity = size;
+ size_t requiredCapacity = size + 1; // +1 for null terminator
+ if (requiredCapacity > base.mCapacity) {
+ base.mStringBuffer = new T[requiredCapacity];
+ base.mCapacity = requiredCapacity;
targetPtr = base.mStringBuffer;
}
memcpy(targetPtr, ptr, size);
+ targetPtr[size] = T{}; // Null termination
base.mSize = size;
}
}
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class TCharType> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TStringView<TCharType>::~TStringView() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline bool TStringBase<T>::operator == (const TStringBase<T> &rhs) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (rhs.mSize != mSize) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class TCharType> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline size_t TStringView<TCharType>::size() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mLen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mHashId == rhs.mHashId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class TCharType> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline TCharType *TStringView<TCharType>::data() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline bool TStringBase<T>::operator != (const TStringBase<T> &rhs) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return !(*this == rhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} // namespace cppcore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/*----------------------------------------------------------------------------------------------- | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014-2025 Kim Kulling | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
-----------------------------------------------------------------------------------------------*/ | ||
#pragma once | ||
|
||
#include <cppcore/CPPCoreCommon.h> | ||
|
||
namespace cppcore { | ||
|
||
//------------------------------------------------------------------------------------------------- | ||
/// @class TStringView | ||
/// @ingroup CPPCore | ||
/// | ||
/// @brief | ||
//------------------------------------------------------------------------------------------------- | ||
template <class T> | ||
class TStringView { | ||
public: | ||
using const_iterator = const T*; | ||
|
||
TStringView(const T *ptr, size_t len); | ||
~TStringView() = default; | ||
size_t size() const; | ||
T *data() const; | ||
bool isEmpty() const; | ||
const_iterator begin() const; | ||
const_iterator end() const; | ||
|
||
private: | ||
const T *mPtr; | ||
size_t mLen; | ||
}; | ||
|
||
template <class T> | ||
inline TStringView<T>::TStringView(const T *ptr, size_t len) : | ||
mPtr(ptr), | ||
mLen(len) { | ||
// empty | ||
} | ||
|
||
template <class T> | ||
inline size_t TStringView<T>::size() const { | ||
return mLen; | ||
} | ||
|
||
template <class T> | ||
inline T *TStringView<T>::data() const { | ||
return mPtr; | ||
} | ||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
template <class T> | ||
inline bool TStringView<T>::isEmpty() const { | ||
return mLen == 0; | ||
} | ||
|
||
template <class T> | ||
inline typename TStringView<T>::const_iterator TStringView<T>::begin() const { | ||
if (isEmpty()) { | ||
return end(); | ||
} | ||
return mPtr; | ||
} | ||
|
||
template <class T> | ||
inline typename TStringView<T>::const_iterator TStringView<T>::end() const { | ||
return mPtr + mLen; | ||
} | ||
|
||
} // namespace cppcore |
Uh oh!
There was an error while loading. Please reload this page.