Skip to content

Conversation

@valdaarhun
Copy link
Contributor

Related Issue(s) #4572
Has Unit Tests (y/n) y
Documentation Included (y/n) n
Generative AI was used in this contribution (y/n) n

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

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);
Copy link
Contributor Author

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.

@thomas-bc thomas-bc requested a review from bocchino January 15, 2026 20:43
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

This use of parameter src has not been checked.
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

This use of parameter length has not been checked.
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

This use of parameter src has not been checked.

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

src uses the basic integral type char rather than a typedef with size and signedness.

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

src uses the basic integral type char rather than a typedef with size and signedness.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant