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