iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
string.hpp
1// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// SPDX-License-Identifier: Apache-2.0
17#ifndef IOX_HOOFS_CXX_STRING_HPP
18#define IOX_HOOFS_CXX_STRING_HPP
19
20#include "iceoryx_hoofs/cxx/type_traits.hpp"
21#include "iceoryx_hoofs/internal/cxx/string_internal.hpp"
22#include "optional.hpp"
23
24#include <algorithm>
25#include <cstdint>
26#include <cstring>
27#include <iostream>
28
29namespace iox
30{
31namespace cxx
32{
43template <typename T1, typename T2>
44typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
45 && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
46 string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
47concatenate(const T1& t1, const T2& t2) noexcept;
48
59template <typename T1, typename T2, typename... Targs>
60typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
61 && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
62 string<internal::SumCapa<T1, T2, Targs...>::value>>::type
63concatenate(const T1& t1, const T2& t2, const Targs&... targs) noexcept;
64
71template <typename T1, typename T2>
72typename std::enable_if<(internal::IsCharArray<T1>::value && internal::IsCxxString<T2>::value)
73 || (internal::IsCxxString<T1>::value && internal::IsCharArray<T2>::value)
74 || (internal::IsCxxString<T1>::value && internal::IsCxxString<T2>::value),
75 string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
76operator+(const T1& t1, const T2& t2) noexcept;
77
81{
82 explicit TruncateToCapacity_t() = default;
83};
84constexpr TruncateToCapacity_t TruncateToCapacity{};
85
88template <uint64_t Capacity>
89class string
90{
91 static_assert(Capacity > 0U, "The capacity of the fixed string must be greater than 0!");
92
93 public:
95 constexpr string() noexcept = default;
96
100 string(const string& other) noexcept;
101
105 string(string&& other) noexcept;
106
112 string& operator=(const string& rhs) noexcept;
113
119 string& operator=(string&& rhs) noexcept;
120
125 template <uint64_t N>
126 string(const string<N>& other) noexcept;
127
132 template <uint64_t N>
133 string(string<N>&& other) noexcept;
134
141 template <uint64_t N>
142 string& operator=(const string<N>& rhs) noexcept;
143
150 template <uint64_t N>
151 string& operator=(string<N>&& rhs) noexcept;
152
170 template <uint64_t N>
171 string(const char (&other)[N]) noexcept;
172
190 string(TruncateToCapacity_t, const char* const other) noexcept;
191
210 string(TruncateToCapacity_t, const std::string& other) noexcept;
211
230 string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept;
231
251 template <uint64_t N>
252 string& operator=(const char (&rhs)[N]) noexcept;
253
260 template <uint64_t N>
261 string& assign(const string<N>& str) noexcept;
262
285 template <uint64_t N>
286 string& assign(const char (&str)[N]) noexcept;
287
294 bool unsafe_assign(const char* const str) noexcept;
295
302 bool unsafe_assign(const std::string& str) noexcept;
303
311 template <uint64_t N>
312 int64_t compare(const string<N>& other) const noexcept;
313
319 template <uint64_t N>
320 bool operator==(const string<N>& rhs) const noexcept;
321
327 template <uint64_t N>
328 bool operator!=(const string<N>& rhs) const noexcept;
329
335 template <uint64_t N>
336 bool operator<(const string<N>& rhs) const noexcept;
337
343 template <uint64_t N>
344 bool operator<=(const string<N>& rhs) const noexcept;
345
351 template <uint64_t N>
352 bool operator>(const string<N>& rhs) const noexcept;
353
359 template <uint64_t N>
360 bool operator>=(const string<N>& rhs) const noexcept;
361
375 bool operator==(const char* const rhs) const noexcept;
376
390 bool operator!=(const char* const rhs) const noexcept;
391
395 const char* c_str() const noexcept;
396
400 constexpr uint64_t size() const noexcept;
401
405 static constexpr uint64_t capacity() noexcept;
406
410 constexpr bool empty() const noexcept;
411
415 operator std::string() const noexcept;
416
420 template <typename T>
421 string& operator+=(const T&) noexcept;
422
436 template <typename T>
437 typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, string&>::type
438 append(TruncateToCapacity_t, const T& t) noexcept;
439
446 template <typename T>
447 typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, bool>::type
448 unsafe_append(const T& t) noexcept;
449
459 iox::cxx::optional<string<Capacity>> substr(const uint64_t pos, const uint64_t count) const noexcept;
460
468 iox::cxx::optional<string<Capacity>> substr(const uint64_t pos = 0U) const noexcept;
469
478 template <typename T>
479 typename std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value
480 || internal::IsCxxString<T>::value,
481 iox::cxx::optional<uint64_t>>::type
482 find(const T& t, const uint64_t pos = 0U) const noexcept;
483
493 template <typename T>
494 typename std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value
495 || internal::IsCxxString<T>::value,
496 iox::cxx::optional<uint64_t>>::type
497 find_first_of(const T& t, const uint64_t pos = 0U) const noexcept;
498
507 template <typename T>
508 typename std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value
509 || internal::IsCxxString<T>::value,
510 iox::cxx::optional<uint64_t>>::type
511 find_last_of(const T& t, const uint64_t pos = Capacity) const noexcept;
512
513 template <uint64_t N>
514 friend class string;
515
516 template <typename T1, typename T2>
517 friend typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
518 && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
519 string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
520 concatenate(const T1& t1, const T2& t2) noexcept;
521
522 private:
529 template <uint64_t N>
530 string& copy(const string<N>& rhs) noexcept;
531
538 template <uint64_t N>
539 string& move(string<N>&& rhs) noexcept;
540
541 char m_rawstring[Capacity + 1U]{'\0'};
542 uint64_t m_rawstringSize{0U};
543};
544
551template <uint64_t Capacity>
552inline bool operator==(const std::string& lhs, const string<Capacity>& rhs) noexcept;
553
560template <uint64_t Capacity>
561inline bool operator==(const string<Capacity>& lhs, const std::string& rhs) noexcept;
562
569template <uint64_t Capacity>
570inline bool operator!=(const std::string& lhs, const string<Capacity>& rhs) noexcept;
571
578template <uint64_t Capacity>
579inline bool operator!=(const string<Capacity>& lhs, const std::string& rhs) noexcept;
580
590template <uint64_t Capacity>
591inline bool operator==(const char* const lhs, const string<Capacity>& rhs) noexcept;
592
602template <uint64_t Capacity>
603inline bool operator!=(const char* const lhs, const string<Capacity>& rhs) noexcept;
604
611template <uint64_t Capacity>
612inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str) noexcept;
613} // namespace cxx
614} // namespace iox
615#include "iceoryx_hoofs/internal/cxx/string.inl"
616
617#endif // IOX_HOOFS_CXX_STRING_HPP
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition optional.hpp:69
string implementation with some adjustments in the API, because we are not allowed to throw exception...
Definition string.hpp:90
iox::cxx::optional< string< Capacity > > substr(const uint64_t pos, const uint64_t count) const noexcept
creates a substring containing the characters from pos until count; if pos+count is greater than the ...
constexpr bool empty() const noexcept
returns if the string is empty or not
constexpr string() noexcept=default
creates an empty string with size 0
string & assign(const string< N > &str) noexcept
fixed string assignment with compile time check if capacity of str is less than or equal to this' cap...
std::enable_if< internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, bool >::type unsafe_append(const T &t) noexcept
appends a fixed string or string literal to the end of this. The appending fails if the sum of both s...
std::enable_if< std::is_same< T, std::string >::value||internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, iox::cxx::optional< uint64_t > >::type find_first_of(const T &t, const uint64_t pos=0U) const noexcept
finds the first occurence of a character equal to one of the characters of the given character sequen...
static constexpr uint64_t capacity() noexcept
returns the maximum number of characters that can be stored in the string
bool unsafe_assign(const char *const str) noexcept
assigns a cstring to string. The assignment fails if the cstring size is greater than the string capa...
std::enable_if< std::is_same< T, std::string >::value||internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, iox::cxx::optional< uint64_t > >::type find(const T &t, const uint64_t pos=0U) const noexcept
finds the first occurence of the given character sequence; returns the position of the first characte...
std::enable_if< internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, string & >::type append(TruncateToCapacity_t, const T &t) noexcept
appends a fixed string or string literal to the end of this. If this' capacity is too small for appen...
constexpr uint64_t size() const noexcept
returns the number of characters stored in the string
int64_t compare(const string< N > &other) const noexcept
compares two strings
std::enable_if< std::is_same< T, std::string >::value||internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, iox::cxx::optional< uint64_t > >::type find_last_of(const T &t, const uint64_t pos=Capacity) const noexcept
finds the last occurence of a character equal to one of the characters of the given character sequenc...
const char * c_str() const noexcept
returns a pointer to the char array of self
friend std::enable_if<(internal::IsCharArray< T1 >::value||internal::IsCxxString< T1 >::value)&&(internal::IsCharArray< T2 >::value||internal::IsCxxString< T2 >::value), string< internal::GetCapa< T1 >::capa+internal::GetCapa< T2 >::capa > >::type concatenate(const T1 &t1, const T2 &t2) noexcept
concatenates two fixed strings/string literals
building block to easily create free function for logging in a library context
Definition lockfree_queue.hpp:29
struct used to define a compile time variable which is used to distinguish between constructors with ...
Definition string.hpp:81