| 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 | 20058837 | ConstStringBase::ConstStringBase() {} | |
| 22 | |||
| 23 | 40117674 | ConstStringBase::~ConstStringBase() {} | |
| 24 | |||
| 25 | 85190 | ConstStringBase::SizeType ConstStringBase::length() const { | |
| 26 | 85190 | SizeType length = 0; | |
| 27 | 85190 | SizeType capacity = this->getCapacity(); | |
| 28 | 85190 | SizeType maxLength = this->maxLength(capacity); | |
| 29 |
1/2✓ Branch 0 taken 85190 times.
✗ Branch 1 not taken.
|
85190 | if (capacity > 0) { |
| 30 | 85190 | length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), capacity)); | |
| 31 | } | ||
| 32 | 85190 | FW_ASSERT(length <= maxLength, static_cast<FwAssertArgType>(length), static_cast<FwAssertArgType>(maxLength)); | |
| 33 | 85190 | return length; | |
| 34 | } | ||
| 35 | |||
| 36 | 39930 | ConstStringBase::SizeType ConstStringBase::maxLength() const { | |
| 37 | 39930 | const SizeType capacity = this->getCapacity(); | |
| 38 | 39930 | return maxLength(capacity); | |
| 39 | } | ||
| 40 | |||
| 41 | 125120 | ConstStringBase::SizeType ConstStringBase::maxLength(SizeType capacity) const { | |
| 42 |
2/2✓ Branch 0 taken 125119 times.
✓ Branch 1 taken 1 times.
|
125120 | return capacity == 0 ? 0 : capacity - 1; |
| 43 | } | ||
| 44 | |||
| 45 | 21 | ConstStringBase::SizeType ConstStringBase::serializedSize() const { | |
| 46 | 21 | 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 | 13809 | bool ConstStringBase::operator==(const ConstStringBase& other) const { | |
| 54 | 13809 | SizeType len = this->length(); | |
| 55 |
2/2✓ Branch 7 taken 514 times.
✓ Branch 8 taken 13295 times.
|
13809 | if (len != other.length()) { |
| 56 | 514 | return false; | |
| 57 | } else { | ||
| 58 | 13295 | return this->operator==(other.toChar()); | |
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | 13393 | bool ConstStringBase::operator==(const CHAR* other) const { | |
| 63 | 13393 | const CHAR* const us = this->toChar(); | |
| 64 |
3/4✓ Branch 0 taken 13393 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 13385 times.
|
13393 | if ((us == nullptr) or (other == nullptr)) { |
| 65 | 8 | return false; | |
| 66 | } | ||
| 67 | |||
| 68 | 13385 | const SizeType capacity = this->getCapacity(); | |
| 69 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 13385 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13385 times.
|
13385 | const size_t result = static_cast<size_t>(strncmp(us, other, static_cast<size_t>(capacity))); |
| 70 | 13385 | return (result == 0); | |
| 71 | } | ||
| 72 | |||
| 73 | 6154 | bool ConstStringBase::operator!=(const ConstStringBase& other) const { | |
| 74 | 6154 | return !operator==(other); | |
| 75 | } | ||
| 76 | |||
| 77 | 77 | bool ConstStringBase::operator!=(const CHAR* other) const { | |
| 78 | 77 | return !operator==(other); | |
| 79 | } | ||
| 80 | |||
| 81 | 1033 | SerializeStatus ConstStringBase::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const { | |
| 82 | 1033 | return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), this->length()); | |
| 83 | } | ||
| 84 | |||
| 85 | 39721 | SerializeStatus ConstStringBase::serializeTo(SerialBufferBase& buffer, SizeType maxLength, Fw::Endianness mode) const { | |
| 86 |
1/1✓ Branch 8 taken 39721 times.
|
39721 | const FwSizeType len = std::min(maxLength, this->length()); |
| 87 | // Serialize length and then bytes | ||
| 88 | 39721 | 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 | 204 | std::ostream& operator<<(std::ostream& os, const ConstStringBase& str) { | |
| 99 | 204 | os << str.toChar(); | |
| 100 | 204 | 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 |