| 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 | 20757188 | ConstStringBase::ConstStringBase() {} | |
| 22 | |||
| 23 | 41514376 | ConstStringBase::~ConstStringBase() {} | |
| 24 | |||
| 25 | 90410 | ConstStringBase::SizeType ConstStringBase::length() const { | |
| 26 | 90410 | SizeType length = 0; | |
| 27 | 90410 | SizeType capacity = this->getCapacity(); | |
| 28 | 90410 | SizeType maxLength = this->maxLength(capacity); | |
| 29 |
1/2✓ Branch 0 taken 90410 times.
✗ Branch 1 not taken.
|
90410 | if (capacity > 0) { |
| 30 | 90410 | length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), capacity)); | |
| 31 | } | ||
| 32 | 90410 | FW_ASSERT(length <= maxLength, static_cast<FwAssertArgType>(length), static_cast<FwAssertArgType>(maxLength)); | |
| 33 | 90410 | return length; | |
| 34 | } | ||
| 35 | |||
| 36 | 42659 | ConstStringBase::SizeType ConstStringBase::maxLength() const { | |
| 37 | 42659 | const SizeType capacity = this->getCapacity(); | |
| 38 | 42659 | return maxLength(capacity); | |
| 39 | } | ||
| 40 | |||
| 41 | 133069 | ConstStringBase::SizeType ConstStringBase::maxLength(SizeType capacity) const { | |
| 42 |
2/2✓ Branch 0 taken 133068 times.
✓ Branch 1 taken 1 times.
|
133069 | 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 | 13921 | bool ConstStringBase::operator==(const ConstStringBase& other) const { | |
| 54 | 13921 | SizeType len = this->length(); | |
| 55 |
2/2✓ Branch 7 taken 515 times.
✓ Branch 8 taken 13406 times.
|
13921 | if (len != other.length()) { |
| 56 | 515 | return false; | |
| 57 | } else { | ||
| 58 | 13406 | return this->operator==(other.toChar()); | |
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | 14189 | bool ConstStringBase::operator==(const CHAR* other) const { | |
| 63 | 14189 | const CHAR* const us = this->toChar(); | |
| 64 |
3/4✓ Branch 0 taken 14189 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 14180 times.
|
14189 | if ((us == nullptr) or (other == nullptr)) { |
| 65 | 9 | return false; | |
| 66 | } | ||
| 67 | |||
| 68 | 14180 | const SizeType capacity = this->getCapacity(); | |
| 69 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 14180 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14180 times.
|
14180 | const size_t result = static_cast<size_t>(strncmp(us, other, static_cast<size_t>(capacity))); |
| 70 | 14180 | return (result == 0); | |
| 71 | } | ||
| 72 | |||
| 73 | 6239 | bool ConstStringBase::operator!=(const ConstStringBase& other) const { | |
| 74 | 6239 | return !operator==(other); | |
| 75 | } | ||
| 76 | |||
| 77 | 420 | bool ConstStringBase::operator!=(const CHAR* other) const { | |
| 78 | 420 | return !operator==(other); | |
| 79 | } | ||
| 80 | |||
| 81 | 2012 | SerializeStatus ConstStringBase::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const { | |
| 82 | 2012 | return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), this->length()); | |
| 83 | } | ||
| 84 | |||
| 85 | 41700 | SerializeStatus ConstStringBase::serializeTo(SerialBufferBase& buffer, SizeType maxLength, Fw::Endianness mode) const { | |
| 86 |
1/1✓ Branch 8 taken 41700 times.
|
41700 | const FwSizeType len = std::min(maxLength, this->length()); |
| 87 | // Serialize length and then bytes | ||
| 88 | 41700 | 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 |