GCC Code Coverage Report


Directory: Fw/Types/
File: StringBase.cpp
Date: 2026-09-03 21:15:04
Exec Total Coverage
Lines: 49 53 92.5%
Functions: 12 13 92.3%
Branches: 9 14 64.3%

Line Branch Exec Source
1 /**
2 * \file StringBase.cpp
3 * \author T. Canham
4 * \brief Implements F Prime string base class
5 *
6 * \copyright
7 * Copyright 2009-2016, by the California Institute of Technology.
8 * ALL RIGHTS RESERVED. United States Government Sponsorship
9 * acknowledged.
10 *
11 */
12
13 #include <Fw/Types/Assert.hpp>
14 #include <Fw/Types/StringFormatStatusEnumAc.hpp>
15 #include <Fw/Types/StringType.hpp>
16 #include <Fw/Types/StringUtils.hpp>
17 #include <cstdarg>
18 #include <cstring>
19
20 // Check consistency of every constant in the Fw::FormatStatus enum with the FPP shadow enum
21 static_assert(static_cast<FwIndexType>(Fw::StringFormatStatus::NUM_CONSTANTS) == 5,
22 "Format status and FPP shadow enum have inconsistent number of values");
23 static_assert(static_cast<Fw::StringFormatStatus::T>(Fw::FormatStatus::SUCCESS) == Fw::StringFormatStatus::T::SUCCESS,
24 "Format status and FPP shadow enum do not match");
25 static_assert(static_cast<Fw::StringFormatStatus::T>(Fw::FormatStatus::OVERFLOWED) ==
26 Fw::StringFormatStatus::T::OVERFLOWED,
27 "Format status and FPP shadow enum do not match");
28 static_assert(static_cast<Fw::StringFormatStatus::T>(Fw::FormatStatus::INVALID_FORMAT_STRING) ==
29 Fw::StringFormatStatus::T::INVALID_FORMAT_STRING,
30 "Format status and FPP shadow enum do not match");
31 static_assert(static_cast<Fw::StringFormatStatus::T>(Fw::FormatStatus::SIZE_OVERFLOW) ==
32 Fw::StringFormatStatus::T::SIZE_OVERFLOW,
33 "Format status and FPP shadow enum do not match");
34 static_assert(static_cast<Fw::StringFormatStatus::T>(Fw::FormatStatus::OTHER_ERROR) ==
35 Fw::StringFormatStatus::T::OTHER_ERROR,
36 "Format status and FPP shadow enum do not match");
37
38 namespace Fw {
39
40 36 StringBase::StringBase() {}
41
42 72 StringBase::~StringBase() {}
43
44 2 const CHAR* StringBase::operator+=(const CHAR* src) {
45 2 this->appendBuff(src, static_cast<SizeType>(StringUtils::string_length(src, this->getCapacity())));
46 2 return this->toChar();
47 }
48
49 2 const StringBase& StringBase::operator+=(const ConstStringBase& src) {
50 2 this->appendBuff(src.toChar(), src.length());
51 2 return *this;
52 }
53
54 2 const StringBase& StringBase::operator+=(const StringBase& src) {
55 2 return StringBase::operator+=(static_cast<const ConstStringBase&>(src));
56 }
57
58 20 FormatStatus StringBase::format(const CHAR* formatString, ...) {
59 20 va_list args;
60 20 va_start(args, formatString);
61
1/1
✓ Branch 4 taken 20 times.
20 FormatStatus status = this->vformat(formatString, args);
62 20 va_end(args);
63 20 return status;
64 }
65
66 22 FormatStatus StringBase::vformat(const CHAR* formatString, va_list args) {
67 22 CHAR* us = const_cast<CHAR*>(this->toChar());
68 22 SizeType cap = this->getCapacity();
69 22 FW_ASSERT(us != nullptr);
70 // Needed until SizeType an FwSizeType are the same
71 static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<SizeType>::max(),
72 "String size type must fit into FwSizeType");
73 22 return Fw::stringFormat(us, static_cast<FwSizeType>(cap), formatString, args);
74 }
75
76 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
77 void StringBase::toString(StringBase& text) const {
78 text = *this;
79 }
80 #endif
81
82 15 StringBase& StringBase::operator=(const ConstStringBase& other) {
83
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 if (this != &other) {
84 15 (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other.toChar(), this->getCapacity());
85 }
86 15 return *this;
87 }
88
89 13 StringBase& StringBase::operator=(const StringBase& other) {
90 13 return StringBase::operator=(static_cast<const ConstStringBase&>(other));
91 }
92
93 // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should
94 // call the empty constructor and then call their own copy function
95 37 StringBase& StringBase::operator=(const CHAR* other) { // lgtm[cpp/rule-of-two]
96 37 (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other, this->getCapacity());
97 37 return *this;
98 }
99
100 4 void StringBase::appendBuff(const CHAR* buff, SizeType size) {
101 4 const SizeType capacity = this->getCapacity();
102 4 const SizeType length = this->length();
103 4 FW_ASSERT(capacity > length, static_cast<FwAssertArgType>(capacity), static_cast<FwAssertArgType>(length));
104 // Subtract 1 to leave space for null terminator
105 4 SizeType remaining = capacity - length - 1;
106
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (size < remaining) {
107 4 remaining = size;
108 }
109 4 FW_ASSERT(remaining < capacity, static_cast<FwAssertArgType>(remaining), static_cast<FwAssertArgType>(capacity));
110
2/4
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
4 (void)strncat(const_cast<CHAR*>(this->toChar()), buff, static_cast<size_t>(remaining));
111 4 }
112
113 2 SerializeStatus StringBase::deserializeFrom(SerialBufferBase& buffer, Fw::Endianness mode) {
114 // Get the max size of the deserialized string
115
1/1
✓ Branch 5 taken 2 times.
2 const SizeType maxSize = this->maxLength();
116 // Initial estimate of actual size is max size
117 // This estimate is refined when calling the deserialize function below
118 2 SizeType actualSize = maxSize;
119 // Public interface returns const char*, but implementation needs char*
120 // So use const_cast
121
1/1
✓ Branch 10 taken 2 times.
2 CHAR* raw = const_cast<CHAR*>(this->toChar());
122 // Deserialize length
123 // Fail if length exceeds max size (the initial value of actualSize)
124 // Otherwise deserialize length bytes and set actualSize to length
125
1/1
✓ Branch 7 taken 2 times.
2 SerializeStatus stat = buffer.deserializeTo(reinterpret_cast<U8*>(raw), actualSize, Serialization::INCLUDE_LENGTH);
126
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (stat == FW_SERIALIZE_OK) {
127 // Deserialization succeeded: null-terminate string at actual size
128 2 FW_ASSERT(actualSize <= maxSize, static_cast<FwAssertArgType>(actualSize),
129 static_cast<FwAssertArgType>(maxSize));
130 2 raw[actualSize] = 0;
131 } else {
132 // Deserialization failed: leave string unmodified, but ensure that it
133 // is null-terminated
134 raw[maxSize] = 0;
135 }
136 2 return stat;
137 }
138
139 } // namespace Fw
140