-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add new constructor and setter for Fw::String #4617
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: devel
Are you sure you want to change the base?
Conversation
Add a new Fw::String constructor and setter to copy the first n bytes of a string into Fw::String. The result is similar to the application of strncpy.
| String(const char* src) : StringBase() { *this = src; } | ||
|
|
||
| String(const char* src, FwSizeType length) : StringBase() { | ||
| setString(src, length); |
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.
I am not entirely sure if this is fine in terms of efficiency. The constructor calls the setter and there's an assert as well.
Also, should constructors/setters be added that accept String and ConstStringBase? This will make copies such as Fw::String copyStr5(copyStr4, 5); possible. This doesn't work at the moment.
| String(const char* src) : StringBase() { *this = src; } | ||
|
|
||
| String(const char* src, FwSizeType length) : StringBase() { | ||
| setString(src, length); |
Check warning
Code scanning / CodeQL
Unchecked function argument Warning
| String(const char* src) : StringBase() { *this = src; } | ||
|
|
||
| String(const char* src, FwSizeType length) : StringBase() { | ||
| setString(src, length); |
Check warning
Code scanning / CodeQL
Unchecked function argument Warning
| void setString(const char* src, FwSizeType length) { | ||
| // "length" non-null bytes should be followed by a null byte | ||
| FW_ASSERT(length < this->getCapacity()); | ||
| (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), src, length + 1); |
Check warning
Code scanning / CodeQL
Unchecked function argument Warning
|
|
||
| String(const char* src) : StringBase() { *this = src; } | ||
|
|
||
| String(const char* src, FwSizeType length) : StringBase() { |
Check notice
Code scanning / CodeQL
Use of basic integral type Note
|
|
||
| StringBase::SizeType getCapacity() const { return sizeof this->m_buf; } | ||
|
|
||
| void setString(const char* src, FwSizeType length) { |
Check notice
Code scanning / CodeQL
Use of basic integral type Note
Change Description
Add a new Fw::String constructor and setter to copy the first n bytes of a string into Fw::String.
Rationale
This could come in handy when writing to a field/buffer whose length is known beforehand.
Testing/Review Recommendations
Two test cases have been added to
Fw/Types/test/ut/TypesTest.cpp:StringTest.Future Work
NA
AI Usage (see policy)
NA