Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parasoft fixes on base64.cpp files #58

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions include/up-cpp/uri/serializer/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
#ifndef IP_ADDRESS_H_
#define IP_ADDRESS_H_

#include <iostream>
#include <iostream>
namespace uprotocol::uri {

/**
* @brief
* IpAddress holds the string and byte representaion.
*/
class IpAddress {
class IpAddress final {

public:
/**
* @brief
* The type of address used for Micro URI.
*/
enum class AddressType : uint8_t {
Expand All @@ -46,68 +48,90 @@ class IpAddress {
};

/**
* @brief
* Constructor with IP address in string format.
* @param ipAddressString : IP address in string format.
*/
explicit IpAddress(std::string_view ipString) : ipString_(ipString) {
IpAddress(std::string_view ipAddressString) : ipString_(ipAddressString) {
toBytes();
}

/**
* @brief
* Constructor with IP address in byte format.
* @param ipAddressBytes : IP address in byte format.
* @param ipAddressType : Type of IP address.
*/
IpAddress(std::vector<uint8_t> const& ipBytes, AddressType type) : type_(type) , ipBytes_(ipBytes) {
IpAddress(std::vector<uint8_t> const& ipAddressBytes, AddressType ipAddressType) : type_(ipAddressType) , ipBytes_(ipAddressBytes) {
toString();

}

/**
* @brief
* Get the type of IP address.
* @return AddressType : Type of IP address.
*/
auto getType() const { return type_; }
AddressType getType() const noexcept { return type_; }

/**
* @brief
* Get the string format of IP address.
* @return std::string : IP address in string format.
*/
auto getString() const -> std::string { return ipString_; }
std::string getString() const noexcept {
return ipString_;
};

/**
* @brief
* Get the byte format of IP address.
* @return std::vector<uint8_t> : IP address in byte format.
*/
auto getBytes() const { return ipBytes_; }

/**
* Number of bytes in IPv4 address.
*/
static constexpr uint8_t IpV4AddressBytes = 4;
/**
* Number of bytes in IPv6 address.
*/
static constexpr uint8_t IpV6AddressBytes = 16;
std::vector<uint8_t> getBytes() const { return ipBytes_; }

private:
/**
* @brief
* Updates the byte format of IP address and type, from the string format.
*/
void toBytes();

/**
* @brief
* Updates the string format of IP address.
*/
void toString();

/**
* @brief
* Number of bytes in IPv4 address.
*/
static constexpr uint8_t IpV4AddressBytes = 4U;
/**
* @brief
* Number of bytes in IPv6 address.
*/
static constexpr uint8_t IpV6AddressBytes = 16U;

/**
* @brief
* Type of the IP addess.
*/
AddressType type_ = AddressType::Invalid;
/**
* @brief
* IP address in byte format.
*/
std::vector<uint8_t> ipBytes_{};

/**
* @brief
* IP address in string format.
*/
std::string ipString_;


}; // class IpAddress

} // namespace uprotocol::uri
Expand Down
96 changes: 60 additions & 36 deletions include/up-cpp/uri/serializer/LongUriSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,162 +36,186 @@
namespace uprotocol::uri {

/**
* @brief
* UUri Serializer that serializes a UUri to a string (long format) per
* https://github.com/eclipse-uprotocol/uprotocol-spec/blob/main/basics/uri.adoc
*/
class LongUriSerializer {
class LongUriSerializer final {
public:
/**
* @brief
* Support for serializing UUri objects into their String format.
* @param uUri UUri object to be serialized to the String format.
* @param uri UUri object to be serialized to the String format.
* @return Returns the String format of the supplied UUri that can be used as a sink or a source
* in a uProtocol publish communication.
*/
static auto serialize(const v1::UUri& uri) -> std::string;
static std::string serialize(const v1::UUri& uri);

/**
* @brief
* Deserialize a String into a UUri object.
* @param uProtocolUri A long format uProtocol URI.
* @param protocol_uri A long format uProtocol URI.
* @return Returns an UUri data object.
*/
static auto deserialize(std::string const& protocol_uri) -> v1::UUri;
static v1::UUri deserialize(std::string const& protocol_uri);

/**
* @brief
* Create the resource part of the Uri from a resource object.
* @param uResource Resource representing a resource or an RPC method.
* @param resource Resource representing a resource or an RPC method.
* @return Returns the String representation of the Resource in the uProtocol URI.
*/
[[nodiscard]] static auto buildResourcePartOfUri(const v1::UResource& resource) -> std::string;
[[nodiscard]] static std::string buildResourcePartOfUri(const v1::UResource& resource);

/**
* @brief
* Create the service part of the uProtocol URI from an software entity object.
* @param use Software Entity representing a service or an application.
* @return Returns the String representation of the Software Entity in the uProtocol URI.
*/
[[nodiscard]] static auto buildSoftwareEntityPartOfUri(const v1::UEntity& use) -> std::string;
[[nodiscard]] static std::string buildSoftwareEntityPartOfUri(const v1::UEntity& use);

/**
* @brief
* Create authority part of the URI from the given UAuthority object.
* @param uAuthority UAuthority object of the UUri.
* @param u_auth UAuthority object of the UUri.
* @return Returns the string representation of Authority.
*/
[[nodiscard]] static auto buildAuthorityPartOfUri(const v1::UAuthority& u_authority) -> std::string;
[[nodiscard]] static std::string buildAuthorityPartOfUri(const v1::UAuthority& u_auth);

/**
* Static factory method for creating a UResource using a string that contains
* name + instance + message.
* @param resourceString String that contains the UResource information.
* @return Returns a UResource object.
*/

private:
/**
* @brief
* Default constructor.
*/
LongUriSerializer() = default;

/**
* @brief
* Trim from start of a string (in place).
* @param s String to be trimmed.
*/
static inline auto ltrim(std::string& s) ->void {
s.erase(s.begin(),
std::find_if( s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
static inline void ltrim(std::string& s) {
std::ignore = s.erase(s.cbegin(),
std::find_if( s.cbegin(), s.cend(), [](unsigned char ch) -> bool {
return !std::isspace(ch);
}));
}

/**
* @brief
* Trim from end of a string (in place)
* @param s String to be trimmed.
*/
static inline auto rtrim(std::string& s) -> void {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(),
s.end());
static inline void rtrim(std::string& s) {
std::ignore = s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) -> bool {
return !std::isspace(ch);
}).base(), s.cend());
}

/**
* @brief
* Trim start and end of string (in place)
* @param s String to be trimmed.
*/
static inline auto trim(std::string& s) -> void {
static inline void trim(std::string& s) {
rtrim(s);
ltrim(s);
}

/**
* @brief
* Trim from start (copying)
* @param s String to be trimmed.
* @return Returns the trimmed string.
*/
[[nodiscard]] static inline auto ltrim_copy(std::string s) -> std::string {
[[nodiscard]] static inline std::string ltrim_copy(std::string& s) {
ltrim(s);
return s;
}

/**
* @brief
* Trim from end (copying)
* @param s String to be trimmed.
* @return Returns the trimmed string.
*/
[[nodiscard]] static inline auto rtrim_copy(std::string s) -> std::string {
[[nodiscard]] static inline std::string rtrim_copy(std::string& s) {
rtrim(s);
return s;
}

/**
* @brief
* Trim from both ends (copying)
* @param s String to be trimmed.
* @return Returns the trimmed string.
*/
[[nodiscard]] static inline auto trim_copy(std::string s) -> std::string {
[[nodiscard]] static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}

/**
* @brief
* Splits this string around matches of the given string delimiter.
* Trailing empty strings are therefore not included in the resulting array.
* @param str String to be split.
* @param delimiter Delimiter to split the string.
* @return Returns a vector of strings.
*/
[[nodiscard]] static auto split(std::string str,
const std::string_view& delimiter) -> std::vector<std::string>;
[[nodiscard]] static std::vector<std::string> split(std::string str,
std::string_view const &delimiter);

[[nodiscard]] static auto parseUResource(const std::string& resource_string) -> v1::UResource;
/**
* @brief parse uresource
* @param resource_string uresource string to be parsed
* @return uresource object
*/
[[nodiscard]] static v1::UResource parseUResource(std::string const &resource_string);

/**
* @brief
* Static factory method for creating a UEntity using a string that contains
* name and version.
* @param entity String that contains the UEntity information.
* @param version String that contains the UEntity version.
* @return Returns a UEntity object.
*/
[[nodiscard]] static auto parseUEntity(const std::string &entity, const std::string &version) -> v1::UEntity;
[[nodiscard]] static v1::UEntity parseUEntity( std::string const &entity, std::string const &version);

/**
* @brief
* Static factory method for creating a UUri using a vector of strings that contains
* the Local UUri information.
* @param uri_parts Vector of strings that contains the Local UUri information.
* @return Returns a v1::UUri object.
*/
[[nodiscard]] static auto parseLocalUUri(const std::vector<std::string>& uri_parts) -> v1::UUri;
[[nodiscard]] static v1::UUri parseLocalUUri(std::vector<std::string> const &uri_parts);

/**
* @brief
* Static factory method for creating a UUri using a vector of strings that contains
* the Remote UUri information.
* @param uriParts Vector of strings that contains the Remote UUri information.
* @param uri_parts Vector of strings that contains the Remote UUri information.
* @return Returns a UUri object.
*/
[[nodiscard]] static auto parseRemoteUUri(const std::vector<std::string>& uri_parts) -> v1::UUri;
[[nodiscard]] static v1::UUri parseRemoteUUri(std::vector<std::string> const &uri_parts);
/**
* @brief
* Verify if the given URI string is local
* @param uri
* @return
* @return
*/
[[nodiscard]] static auto inline isLocality(const std::string_view &uri) -> bool;
[[nodiscard]] inline static bool isLocality(std::string_view const &uri) noexcept;
/**
* @brief
* find the first not empty value in the vector and return location
* @param uri_parts
* @return
*/
[[nodiscard]] static auto inline getFirstNotEmpty(const std::vector<std::string> &uri_parts) -> uint64_t ;
[[nodiscard]] inline static uint64_t getFirstNotEmpty(std::vector<std::string> const &uri_parts);
}; // class LongUriSerializer

} // namespace uprotocol::uri
Expand Down
Loading