Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1394 update iox-eclipse-iceoryx#1394-fix-axivion-…
Browse files Browse the repository at this point in the history
…violation-for-string

Signed-off-by: shaik saifulla <[email protected]>
  • Loading branch information
saif-at-github committed Nov 9, 2022
1 parent 08d7d51 commit dbb9c7e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
59 changes: 31 additions & 28 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class string final
/// @endcode
// TruncateToCapacity_t is a compile time variable to distinguish between constructors
// NOLINTNEXTLINE(hicpp-named-parameter, readability-named-parameter)
string(TruncateToCapacity_t, const char* const other) noexcept(false);
string(TruncateToCapacity_t, const char* const other) noexcept;

/// @brief conversion constructor for std::string to string which truncates characters if the std::string size is
/// greater than the string capacity
Expand Down Expand Up @@ -275,7 +275,7 @@ class string final
/// @endcode
// TruncateToCapacity_t is a compile time variable to distinguish between constructors
// NOLINTNEXTLINE(hicpp-named-parameter, readability-named-parameter)
string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept(false);
string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept;

/// @brief assigns a char array to string with compile time check if the array size is less than or equal
/// to the string capacity
Expand Down Expand Up @@ -402,7 +402,9 @@ class string final
/// @brief converts the string to a std::string
///
/// @return a std::string with data equivalent to those stored in the string
// NOLINTNEXTLINE(hicpp-explicit-conversions) todo #1196 remove this conversion and implement toStdString method

// @todo iox-#260 remove this conversion and implement toStdString method
// NOLINTNEXTLINE(hicpp-explicit-conversions)
operator std::string() const noexcept;

/// @brief since there are two valid options for what should happen when appending a string larger than this'
Expand Down Expand Up @@ -570,6 +572,15 @@ class string final
uint64_t m_rawstringSize{0U};
};

/// @brief outputs the fixed string on stream
///
/// @param [in] stream is the output stream
/// @param [in] str is the fixed string
///
/// @return the stream output of the fixed string
template <uint64_t Capacity>
inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str) noexcept;

// AXIVION DISABLE STYLE AutosarC++19_03-A13.5.5: Comparison with std::string, char array or
// char is also intended
/// @brief checks if a lhs std::string, char array or char is equal to a rhs iox::cxx::string
Expand Down Expand Up @@ -625,65 +636,57 @@ IsStdStringOrCharArrayOrChar<T, bool> operator>(const T& lhs, const string<Capac
template <typename T, uint64_t Capacity>
IsStdStringOrCharArrayOrChar<T, bool> operator>=(const T& lhs, const string<Capacity>& rhs) noexcept;

/// @brief outputs the fixed string on stream
///
/// @param [in] stream is the output stream
/// @param [in] str is the fixed string
///
/// @return the stream output of the fixed string
template <uint64_t Capacity>
inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str) noexcept(false);

/// @brief checks if self is equal to rhs
/// @brief checks if lhs is equal to rhs
///
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with self
/// @param [in] lhs is the iox::cxx::string
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with lhs
///
/// @return true if both strings are equal, otherwise false
template <typename T, uint64_t Capacity>
IsStringOrCharArrayOrChar<T, bool> operator==(const string<Capacity>& lhs, const T& rhs) noexcept;

/// @brief checks if self is not equal to rhs
/// @brief checks if lhs is not equal to rhs
///
/// @param [in] lhs is the iox::cxx::string
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with self
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with lhs
///
/// @return true if both strings are not equal, otherwise false
template <typename T, uint64_t Capacity>
IsStringOrCharArrayOrChar<T, bool> operator!=(const string<Capacity>& lhs, const T& rhs) noexcept;

/// @brief checks if self is less than rhs, in lexicographical order
/// @brief checks if lhs is less than rhs, in lexicographical order
///
/// @param [in] lhs is the iox::cxx::string
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with self
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with lhs
///
/// @return true if self is less than rhs, otherwise false
/// @return true if lhs is less than rhs, otherwise false
template <typename T, uint64_t Capacity>
IsStringOrCharArrayOrChar<T, bool> operator<(const string<Capacity>& lhs, const T& rhs) noexcept;

/// @brief checks if self is less than or equal to rhs, in lexicographical order
/// @brief checks if lhs is less than or equal to rhs, in lexicographical order
///
/// @param [in] lhs is the iox::cxx::string
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with self
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with lhs
///
/// @return true if self is less than or equal to rhs, otherwise false
/// @return true if lhs is less than or equal to rhs, otherwise false
template <typename T, uint64_t Capacity>
IsStringOrCharArrayOrChar<T, bool> operator<=(const string<Capacity>& lhs, const T& rhs) noexcept;

/// @brief checks if self is greater than rhs, in lexicographical order
/// @brief checks if lhs is greater than rhs, in lexicographical order
///
/// @param [in] lhs is the iox::cxx::string
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with self
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with lhs
///
/// @return true if self is greater than rhs, otherwise false
/// @return true if lhs is greater than rhs, otherwise false
template <typename T, uint64_t Capacity>
IsStringOrCharArrayOrChar<T, bool> operator>(const string<Capacity>& lhs, const T& rhs) noexcept;

/// @brief checks if self is greater than or equal to rhs, in lexicographical order
/// @brief checks if lhs is greater than or equal to rhs, in lexicographical order
///
/// @param [in] lhs is the iox::cxx::string
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with self
/// @param [in] rhs is the iox::cxx::string, std::string, char array or char to compare with lhs
///
/// @return true if self is greater than or equal to rhs, otherwise false
/// @return true if lhs is greater than or equal to rhs, otherwise false
template <typename T, uint64_t Capacity>
IsStringOrCharArrayOrChar<T, bool> operator>=(const string<Capacity>& lhs, const T& rhs) noexcept;
// AXIVION ENABLE STYLE AutosarC++19_03-A13.5.5
Expand Down
20 changes: 12 additions & 8 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ inline string<Capacity>& string<Capacity>::operator=(string<N>&& rhs) noexcept

template <uint64_t Capacity>
template <uint64_t N>
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : C-array type usage is intentional
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays) cxx::string wraps char array
inline string<Capacity>::string(const char (&other)[N]) noexcept
{
Expand All @@ -106,7 +107,7 @@ inline string<Capacity>::string(const char (&other)[N]) noexcept

template <uint64_t Capacity>
// NOLINTNEXTLINE(hicpp-named-parameter, readability-named-parameter) justification in header
inline string<Capacity>::string(TruncateToCapacity_t, const char* const other) noexcept(false)
inline string<Capacity>::string(TruncateToCapacity_t, const char* const other) noexcept
: string(TruncateToCapacity, other, [&other]() -> uint64_t {
return (other != nullptr) ? strnlen(other, Capacity) : 0U;
}())
Expand All @@ -124,15 +125,16 @@ inline string<Capacity>::string(TruncateToCapacity_t, const std::string& other)
template <uint64_t Capacity>
// TruncateToCapacity_t is a compile time variable to distinguish between constructors
// NOLINTNEXTLINE(hicpp-named-parameter, readability-named-parameter)
inline string<Capacity>::string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept(false)
inline string<Capacity>::string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept
{
if (other == nullptr)
{
clear();
}
else if (Capacity < count)
{
// AXIVION DISABLE STYLE AutosarC++19_03-A16.0.1: conditional compilation is required for setting gcc diagnostics
// AXIVION DISABLE STYLE AutosarC++19_03-A16.0.1: conditional compilation is required for setting gcc diagnostics, since
// gcc 8 incorrectly warns here about out of bounds array access
#if (defined(__GNUC__) && (__GNUC__ == 8)) && (__GNUC_MINOR__ == 3)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
Expand All @@ -158,6 +160,7 @@ inline string<Capacity>::string(TruncateToCapacity_t, const char* const other, c

template <uint64_t Capacity>
template <uint64_t N>
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : C-array type usage is intentional
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays) cxx::string wraps char array
inline string<Capacity>& string<Capacity>::operator=(const char (&rhs)[N]) noexcept
{
Expand Down Expand Up @@ -195,6 +198,7 @@ inline string<Capacity>& string<Capacity>::assign(const string<N>& str) noexcept

template <uint64_t Capacity>
template <uint64_t N>
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : C-array type usage is intentional
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays) cxx::string wraps char array
inline string<Capacity>& string<Capacity>::assign(const char (&str)[N]) noexcept
{
Expand Down Expand Up @@ -333,10 +337,11 @@ inline string<Capacity>& string<Capacity>::move(string<N>&& rhs) noexcept
rhs.clear();
return *this;
}
// AXIVION DISABLE Style AutosarC++19_03-M5.17.1: Only need to support streaming output

// AXIVION Next Construct AutosarC++19_03-M5.17.1: This is not used as shift operator but as stream operator and does
// not require to implement '<<='
template <uint64_t Capacity>
inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str) noexcept(false)
inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str) noexcept
{
stream << str.c_str();
return stream;
Expand Down Expand Up @@ -375,9 +380,9 @@ concatenate(const T1& str1, const T2& str2, const Targs&... targs) noexcept
return concatenate(concatenate(str1, str2), targs...);
}

template <typename T1, typename T2>
// AXIVION Next Construct AutosarC++19_03-M17.0.3 : operator+ is defined within iox::cxx namespace which prevents easy
// misuse
template <typename T1, typename T2>
inline IsCxxStringAndCxxStringOrCharArrayOrChar<T1, T2, string<internal::SumCapa<T1, T2>::value>>
operator+(const T1& str1, const T2& str2) noexcept
{
Expand Down Expand Up @@ -578,7 +583,6 @@ inline constexpr char& string<Capacity>::at(const uint64_t pos) noexcept
template <uint64_t Capacity>
inline constexpr const char& string<Capacity>::at(const uint64_t pos) const noexcept
{
// AXIVION Next Construct AutosarC++19_03-M5.3.1 : operand of logical operator is non-bool, enhances readability
ExpectsWithMsg((pos < size()), "Out of bounds access!");
return m_rawstring[pos];
}
Expand Down Expand Up @@ -668,7 +672,7 @@ inline IsStringOrCharArrayOrChar<T, bool> operator>=(const string<Capacity>& lhs
{
return (lhs.compare(rhs) >= 0);
}
// AXIVION ENABLE Style AutosarC++19_03-M5.17.1
// AXIVION ENABLE Style AutosarC++19_03-A13.5.5
} // namespace cxx
} // namespace iox

Expand Down

0 comments on commit dbb9c7e

Please sign in to comment.