Add new constructor and setter for Fw::String#4617
Add new constructor and setter for Fw::String#4617valdaarhun wants to merge 2 commits intonasa:develfrom
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.
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
|
GitHub Actions has been having server issues; closing and reopening to re-trigger CI |
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