Skip to content

Conversation

ianbrault
Copy link
Collaborator

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

Change Description

Provides a new Fw::StaticString type which is a string backed by a const char* string literal. This mirrors the design of Fw::ExternalString but this type can be initialized with a const char* instead of Fw::ExternalString which requires a mutable char* pointer.

Rationale

This was devised during work to optimize string-handling critical sections and reduce data copying. We needed a string type that could wrap a string literal without requiring internal storage or copying. Fw::ExternalString nearly fit the bill but could not be initialized with constant string literals. This new type fills that gap.

Testing/Review Recommendations

Use in place of any Fw::StringBase type. Example usage:

const char* FOO = "fprime_test_string";
// ...
Fw::StaticString foo(FOO, Fw::StringUtils::string_length(FOO, FW_LOG_STRING_MAX_SIZE));
this->log_ACTIVITY_LO_Foo(foo);

Future Work

Only marginally-related here but wanted to note that, for a string type like this which should have a constant length, I was not able to figure out a way to override the length function for use in serializing. Instead, the serialization code is forced to use StringBase::length which uses Fw::StringUtils::string_length and it becomes an O(n) operation when it could be O(1).

AI Usage (see policy)

N/A

StaticString(const char* bufferPtr, //!< The buffer pointer
StringBase::SizeType bufferSize //!< The buffer size
)
: StringBase(), m_bufferPtr(bufferPtr), m_bufferSize(bufferSize + 1) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make clear in a comment that this class cannot be used in all cases where a StringBase can be used. This is because StringBase will sometimes cast the result of toChar() to a mutable pointer, and the caller may have provided a string that cannot safely be modified.

@LeStarch LeStarch requested a review from bocchino October 7, 2025 17:12
@LeStarch
Copy link
Collaborator

LeStarch commented Oct 7, 2025

@bocchino, will you review? If needed, we can discuss at the CCB on Friday.

Copy link
Collaborator

@bocchino bocchino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot initialize an object of type Fw::StringBase with an object of type const char*. The design of Fw::StringBase assumes that the underlying string buffer is mutable.

We needed a string type that could wrap a string literal without requiring internal storage or copying.

If there is a need for this, then I recommend the following:

  1. Factor the read-only operations of Fw::StringBase into a new abstract superclass Fw::ConstStringBase.
  2. Make a Fw::ConstExternalString that inherits from Fw::ConstStringBase and can be initialized with a const char*.

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.

4 participants