GCC Code Coverage Report


Directory: Fw/Types/
File: ConstStringBase.cpp
Date: 2026-09-03 21:15:04
Exec Total Coverage
Lines: 34 48 70.8%
Functions: 10 16 62.5%
Branches: 10 16 62.5%

Line Branch Exec Source
1 /**
2 * \file ConstStringBase.cpp
3 * \brief Implements F Prime read-only string base class
4 *
5 * \copyright
6 * Copyright 2009-2016, by the California Institute of Technology.
7 * ALL RIGHTS RESERVED. United States Government Sponsorship
8 * acknowledged.
9 *
10 */
11
12 #include <Fw/Types/Assert.hpp>
13 #include <Fw/Types/StringType.hpp>
14 #include <Fw/Types/StringUtils.hpp>
15 #include <algorithm>
16 #include <cstdarg>
17 #include <cstring>
18
19 namespace Fw {
20
21 40 ConstStringBase::ConstStringBase() {}
22
23 80 ConstStringBase::~ConstStringBase() {}
24
25 39 ConstStringBase::SizeType ConstStringBase::length() const {
26 39 SizeType length = 0;
27 39 SizeType capacity = this->getCapacity();
28 39 SizeType maxLength = this->maxLength(capacity);
29
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (capacity > 0) {
30 39 length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), capacity));
31 }
32 39 FW_ASSERT(length <= maxLength, static_cast<FwAssertArgType>(length), static_cast<FwAssertArgType>(maxLength));
33 39 return length;
34 }
35
36 5 ConstStringBase::SizeType ConstStringBase::maxLength() const {
37 5 const SizeType capacity = this->getCapacity();
38 5 return maxLength(capacity);
39 }
40
41 44 ConstStringBase::SizeType ConstStringBase::maxLength(SizeType capacity) const {
42
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1 times.
44 return capacity == 0 ? 0 : capacity - 1;
43 }
44
45 ConstStringBase::SizeType ConstStringBase::serializedSize() const {
46 return static_cast<SizeType>(sizeof(FwSizeStoreType)) + this->length();
47 }
48
49 ConstStringBase::SizeType ConstStringBase::serializedTruncatedSize(FwSizeType maxLength) const {
50 return static_cast<SizeType>(sizeof(FwSizeStoreType)) + static_cast<SizeType>(std::min(this->length(), maxLength));
51 }
52
53 12 bool ConstStringBase::operator==(const ConstStringBase& other) const {
54 12 SizeType len = this->length();
55
2/2
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 10 times.
12 if (len != other.length()) {
56 2 return false;
57 } else {
58 10 return this->operator==(other.toChar());
59 }
60 }
61
62 26 bool ConstStringBase::operator==(const CHAR* other) const {
63 26 const CHAR* const us = this->toChar();
64
3/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 25 times.
26 if ((us == nullptr) or (other == nullptr)) {
65 1 return false;
66 }
67
68 25 const SizeType capacity = this->getCapacity();
69
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
25 const size_t result = static_cast<size_t>(strncmp(us, other, static_cast<size_t>(capacity)));
70 25 return (result == 0);
71 }
72
73 4 bool ConstStringBase::operator!=(const ConstStringBase& other) const {
74 4 return !operator==(other);
75 }
76
77 bool ConstStringBase::operator!=(const CHAR* other) const {
78 return !operator==(other);
79 }
80
81 2 SerializeStatus ConstStringBase::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const {
82 2 return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), this->length());
83 }
84
85 SerializeStatus ConstStringBase::serializeTo(SerialBufferBase& buffer, SizeType maxLength, Fw::Endianness mode) const {
86 const FwSizeType len = std::min(maxLength, this->length());
87 // Serialize length and then bytes
88 return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), len, Serialization::INCLUDE_LENGTH);
89 }
90
91 SerializeStatus ConstStringBase::deserializeFrom(SerialBufferBase& buffer, Fw::Endianness mode) {
92 // Cannot deserialize into a read-only string
93 // This should be overridden by derived classes
94 return SerializeStatus::FW_DESERIALIZE_IMMUTABLE;
95 }
96
97 #ifdef BUILD_UT
98 2 std::ostream& operator<<(std::ostream& os, const ConstStringBase& str) {
99 2 os << str.toChar();
100 2 return os;
101 }
102 #endif
103
104 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
105 void ConstStringBase::toString(StringBase& text) const {
106 text = *this;
107 }
108 #endif
109
110 } // namespace Fw
111