-
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
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe updates introduce a new Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Unit Test
participant TStringBase as TStringBase<char>
participant TStringView as TStringView<char>
Test->>TStringBase: Create default instance
Test->>TStringBase: set(ptr, size)
Test->>TStringBase: Check size(), c_str(), equality
Test->>TStringBase: reset()
Test->>TStringBase: Check size() == 0
Test->>TStringView: Create with ptr, len
Test->>TStringView: begin(), end()
Test->>TStringView: Iterate and check elements
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
include/cppcore/Common/TStringView.h (1)
33-34
: Add a description to the @brief tag.The documentation comment is missing a description. Please add a brief explanation of what
TStringView
does.Apply this diff to add documentation:
-/// @brief +/// @brief Provides a lightweight, non-owning view over a contiguous sequence of elements.include/cppcore/Common/TStringBase.h (1)
145-152
: Consider the risk of hash collisions in equality comparison.The equality operator relies solely on hash comparison after size check. While hash collisions are rare, they can occur. Consider adding actual data comparison for critical use cases or document this behavior.
For complete correctness, consider comparing the actual string data:
template <class T> inline bool TStringBase<T>::operator == (const TStringBase<T> &rhs) const { if (rhs.mSize != mSize) { return false; } - - return mHashId == rhs.mHashId; + if (mHashId != rhs.mHashId) { + return false; + } + + // Compare actual data to handle hash collisions + const T* lhsData = c_str(); + const T* rhsData = rhs.c_str(); + return memcmp(lhsData, rhsData, mSize * sizeof(T)) == 0;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
CMakeLists.txt
(2 hunks)include/cppcore/CPPCoreCommon.h
(2 hunks)include/cppcore/Common/Sort.h
(1 hunks)include/cppcore/Common/TStringBase.h
(2 hunks)include/cppcore/Common/TStringView.h
(1 hunks)include/cppcore/Memory/MemUtils.h
(0 hunks)test/common/TStringBaseTest.cpp
(1 hunks)test/common/TStringViewTest.cpp
(1 hunks)test/container/THashMapTest.cpp
(1 hunks)test/container/TQueueTest.cpp
(0 hunks)
💤 Files with no reviewable changes (2)
- test/container/TQueueTest.cpp
- include/cppcore/Memory/MemUtils.h
🔇 Additional comments (12)
include/cppcore/Common/Sort.h (1)
179-181
: Documentation spacing tweak LGTM
The alignment change after@tparam
improves readability; no functional impact.CMakeLists.txt (2)
79-79
: LGTM: Build system integration for new string view class.The addition of
TStringView.h
to the common source group properly integrates the new string view functionality into the build system.
141-142
: LGTM: Test integration for new string classes.The addition of test files for both
TStringView
andTStringBase
ensures proper test coverage for the new string functionality.test/container/THashMapTest.cpp (1)
33-34
: LGTM: Consistent formatting improvements.The changes consistently use brace initialization and improve spacing in macros, enhancing code style consistency without affecting functionality.
Also applies to: 42-42, 45-45, 47-49, 53-53, 58-58
include/cppcore/CPPCoreCommon.h (2)
48-61
: LGTM: Macro formatting improvements.The formatting improvements to DLL export/import macros and stack allocation macros enhance readability while maintaining functionality.
110-111
: LGTM: Well-designed hash identifier type.The introduction of
HashId
as auint64_t
alias provides a standardized type for hash identifiers across the codebase, supporting the new string functionality with consistent hash-based operations.test/common/TStringViewTest.cpp (2)
33-42
: LGTM: Well-structured construction test.The test correctly validates
TStringView
construction and iterator functionality by checking that the distance between begin and end iterators matches the expected size.
44-52
: LGTM: Comprehensive iteration test.The test thoroughly validates iterator functionality by comparing each character accessed through the
TStringView
with the original string data, ensuring correct element access behavior.test/common/TStringBaseTest.cpp (2)
33-38
: LGTM: Effective equality comparison test.The test correctly validates
TStringBase
construction and equality comparison by creating two strings with the same content through different methods and verifying they compare as equal.
40-49
: LGTM: Comprehensive functionality test.The test thoroughly validates the core
TStringBase
operations including default initialization, setting data, size/capacity queries, content verification, and reset functionality. The default capacity of 256 appears reasonable for a string implementation.include/cppcore/Common/TStringView.h (1)
83-86
: Verify handling of null pointer arithmetic.The
end()
method performs pointer arithmetic without checking ifmPtr
is null. While null + 0 is well-defined, null + non-zero is undefined behavior.Consider adding a check or documenting that the constructor should ensure
mPtr
is non-null whenmLen > 0
.include/cppcore/Common/TStringBase.h (1)
128-142
: ```shell
#!/bin/bashDisplay constructors, set, copyFrom, and c_str implementations
sed -n '1,300p' include/cppcore/Common/TStringBase.h
</details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 7
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
CMakeLists.txt
(6 hunks)include/cppcore/Common/TStringBase.h
(2 hunks)include/cppcore/Common/TStringView.h
(1 hunks)test/common/HashTest.cpp
(0 hunks)test/common/TOptionalTest.cpp
(1 hunks)test/common/TStringViewTest.cpp
(1 hunks)
💤 Files with no reviewable changes (1)
- test/common/HashTest.cpp
✅ Files skipped from review due to trivial changes (2)
- test/common/TOptionalTest.cpp
- include/cppcore/Common/TStringView.h
🚧 Files skipped from review as they are similar to previous changes (2)
- test/common/TStringViewTest.cpp
- CMakeLists.txt
🔇 Additional comments (4)
include/cppcore/Common/TStringBase.h (4)
104-106
: LGTM!The capacity method implementation is correct and straightforward.
109-115
: LGTM!The c_str() method correctly returns the appropriate buffer pointer based on whether dynamic allocation is used.
81-83
: LGTM!The destructor correctly delegates cleanup to the clear() method, following good RAII practices.
99-101
: LGTM!The size method implementation is correct and straightforward.
include/cppcore/Common/TStringBase.h
Outdated
inline void TStringBase<T>::clear() { | ||
if (mStringBuffer != nullptr) { | ||
delete [] mStringBuffer; | ||
mStringBuffer = nullptr; | ||
mCapacity = InitSize; | ||
} | ||
mSize = 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Reset hash ID when clearing string data.
The clear()
method should also reset the hash ID to maintain consistency.
inline void TStringBase<T>::clear() {
if (mStringBuffer != nullptr) {
delete [] mStringBuffer;
mStringBuffer = nullptr;
mCapacity = InitSize;
}
mSize = 0;
+ mHashId = 0;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
inline void TStringBase<T>::clear() { | |
if (mStringBuffer != nullptr) { | |
delete [] mStringBuffer; | |
mStringBuffer = nullptr; | |
mCapacity = InitSize; | |
} | |
mSize = 0; | |
} | |
inline void TStringBase<T>::clear() { | |
if (mStringBuffer != nullptr) { | |
delete [] mStringBuffer; | |
mStringBuffer = nullptr; | |
mCapacity = InitSize; | |
} | |
mSize = 0; | |
mHashId = 0; | |
} |
🤖 Prompt for AI Agents
In include/cppcore/Common/TStringBase.h around lines 118 to 125, the clear()
method deletes the string buffer and resets size and capacity but does not reset
the hash ID. Modify the clear() method to also reset the hash ID member variable
to its initial state to ensure consistency after clearing the string data.
include/cppcore/Common/TStringBase.h
Outdated
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; | ||
} | ||
memcpy(targetPtr, ptr, size); | ||
base.mSize = size; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Address unsafe memory operations and missing null termination.
The copyFrom
method has several critical issues:
- Using
memcpy
for potentially non-trivial types is unsafe - No null termination for C-style strings
- Missing size validation
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
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; | |
} | |
memcpy(targetPtr, ptr, size); | |
base.mSize = size; | |
} | |
} | |
} | |
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) { | |
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; | |
} | |
} | |
} |
🤖 Prompt for AI Agents
In include/cppcore/Common/TStringBase.h around lines 128 to 142, the copyFrom
method uses memcpy which is unsafe for non-trivial types, lacks null termination
for C-style strings, and does not validate the size properly. Replace memcpy
with element-wise copy or std::copy to safely handle non-trivial types, ensure
the copied string is null-terminated by adding a terminator if applicable, and
add checks to validate that size does not exceed capacity or other logical
constraints before copying.
inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) { | ||
copyFrom(*this, pPtr, size); | ||
mHashId = THash<HashId>::toHash(pPtr, mSize); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix hash calculation in constructor.
The hash is calculated using the input pointer pPtr
and mSize
before copyFrom
completes. This could result in incorrect hash values since mSize
may not be properly set yet, and the hash should be calculated from the stored data, not the input data.
inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) {
copyFrom(*this, pPtr, size);
- mHashId = THash<HashId>::toHash(pPtr, mSize);
+ mHashId = THash<HashId>::toHash(c_str(), mSize);
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) { | |
copyFrom(*this, pPtr, size); | |
mHashId = THash<HashId>::toHash(pPtr, mSize); | |
} | |
inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) { | |
copyFrom(*this, pPtr, size); | |
mHashId = THash<HashId>::toHash(c_str(), mSize); | |
} |
🤖 Prompt for AI Agents
In include/cppcore/Common/TStringBase.h around lines 75 to 78, the constructor
calculates the hash using pPtr and mSize before copyFrom sets mSize and copies
the data. To fix this, move the hash calculation to after the copyFrom call and
use the internal data pointer and mSize to compute the hash, ensuring the hash
reflects the stored data correctly.
…ng/cppcore into feature/refactor_string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (3)
include/cppcore/Common/TStringBase.h (3)
77-80
: Fix hash calculation in constructor.The hash is calculated using the input pointer
pPtr
instead of the stored data. This approach is inconsistent with other methods that use the internal data for hash calculation.Apply this diff to fix the hash calculation:
inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) { copyFrom(*this, pPtr, size); - mHashId = THash<HashId>::toHash(pPtr, mSize); + mHashId = THash<HashId>::toHash(c_str(), mSize); }
88-93
: Update hash after setting new data.The
set()
method copies new data but doesn't update the cached hash ID, which will cause equality comparisons to fail.Apply this diff to fix the hash update:
inline void TStringBase<T>::set(const T *ptr, size_t size) { reset(); if (nullptr != ptr) { copyFrom(*this, ptr, size); + mHashId = THash<HashId>::toHash(c_str(), mSize); } }
165-179
: Address unsafe memory operations and missing null termination.The
copyFrom
method has critical safety issues:
- Using
memcpy
for potentially non-trivial types- No null termination for C-style strings
- Missing size validation
Apply this diff to fix the safety issues:
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 * sizeof(T)); + targetPtr[size] = T{}; // Null termination base.mSize = size; } } }
🧹 Nitpick comments (2)
include/cppcore/Common/TStringBase.h (2)
143-152
: Complete the append() method implementation.The method is incomplete - it calculates the new length but doesn't implement the actual appending logic or handle capacity expansion.
The method needs to:
- Resize if necessary
- Copy the new data to the appropriate location
- Update mSize
- Recalculate hash ID
Would you like me to provide a complete implementation?
67-74
: Ensure hash consistency across all data-modifying operations.The class uses cached hash IDs for performance, but several data-modifying methods don't update the hash consistently. This architectural concern affects
resize()
,append()
, and potentially other future methods.Consider implementing a private
updateHash()
method that all data-modifying operations can call to ensure consistency:private: void updateHash() { mHashId = THash<HashId>::toHash(c_str(), mSize); }Then call this method after any data modification in
set()
,append()
,resize()
, etc.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
include/cppcore/Common/TStringBase.h
(2 hunks)
🧰 Additional context used
🪛 GitHub Actions: CMake
include/cppcore/Common/TStringBase.h
[error] 116-116: Compiler error: ‘size’ cannot be used as a function in member function 'void cppcore::TStringBase::resize(size_t)'.
[error] 122-122: Compiler error: ‘size’ cannot be used as a function in member function 'void cppcore::TStringBase::resize(size_t)'.
[error] 124-124: Syntax error: expected ‘;’ before ‘}’ token in member function 'void cppcore::TStringBase::resize(size_t)'.
🔇 Additional comments (2)
include/cppcore/Common/TStringBase.h (2)
155-162
: LGTM! Hash reset is properly handled.The method correctly deallocates the dynamic buffer and calls
reset()
which handles the hash ID reset, addressing previous concerns about hash consistency.
182-193
: LGTM! Hash collision handling is properly implemented.The equality operator correctly implements the recommended pattern with size check, hash comparison, and fallback to byte-wise comparison for hash collision cases.
inline void TStringBase<T>::resize(size_t size) { | ||
if (size <= mCapacity) { | ||
return; | ||
} | ||
|
||
for (size_t i = 0; i < m_size; ++i) { | ||
if (rhs.m_pStringBuffer[i] != m_pStringBuffer[i]) { | ||
return false; | ||
if (mStringBuffer == nullptr) { | ||
mStringBuffer = new T[size]; | ||
mCapacity = size; | ||
if (mSize > 0) { | ||
memcpy(mStringBuffer, mBuffer, size()); | ||
} | ||
} else { | ||
T *ptr = new T[size]; | ||
mCapacity = size; | ||
if (mSize > 0) { | ||
memcpy(ptr, mBuffer, size()); | ||
} | ||
mStringBuffer = ptr | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix compilation errors and logic issues in resize() method.
The method has several critical issues causing compilation failures:
- Lines 116, 122:
size()
should bemSize
- Line 124: Missing semicolon
- Logic error: Always copying from
mBuffer
instead of current data source - Memory leak: Not deleting old
mStringBuffer
before reallocation
Apply this diff to fix the issues:
inline void TStringBase<T>::resize(size_t size) {
if (size <= mCapacity) {
return;
}
if (mStringBuffer == nullptr) {
mStringBuffer = new T[size];
mCapacity = size;
if (mSize > 0) {
- memcpy(mStringBuffer, mBuffer, size());
+ memcpy(mStringBuffer, mBuffer, mSize * sizeof(T));
}
} else {
T *ptr = new T[size];
mCapacity = size;
if (mSize > 0) {
- memcmp(ptr, mBuffer, size());
+ memcpy(ptr, mStringBuffer, mSize * sizeof(T));
}
+ delete[] mStringBuffer;
- mStringBuffer = ptr
+ mStringBuffer = ptr;
}
}
Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 GitHub Actions: CMake
[error] 116-116: Compiler error: ‘size’ cannot be used as a function in member function 'void cppcore::TStringBase::resize(size_t)'.
[error] 122-122: Compiler error: ‘size’ cannot be used as a function in member function 'void cppcore::TStringBase::resize(size_t)'.
[error] 124-124: Syntax error: expected ‘;’ before ‘}’ token in member function 'void cppcore::TStringBase::resize(size_t)'.
🤖 Prompt for AI Agents
In include/cppcore/Common/TStringBase.h around lines 107 to 126, fix the
resize() method by replacing all incorrect calls to size() with mSize, add the
missing semicolon after assigning mStringBuffer, change the memcpy source from
mBuffer to mStringBuffer or the correct current data pointer, and ensure to
delete the old mStringBuffer before assigning a new allocation to prevent memory
leaks.
|
Summary by CodeRabbit
New Features
Refactor
Style
Tests
Chores