PLCnext API Documentation 25.0.2.69
String.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7#include "Arp/Base/Core/Detail/BasicString.hxx"
8#include "Arp/Base/Core/Detail/BasicString.inl"
10
11#define ARP_BUILD_STRING_IMPL_INLINE
12
13#ifdef ARP_BUILD_STRING_IMPL_INLINE
14 #define ARP_STRING_INLINE inline
15 #define ARP_STRING_EXPORT inline
16 #define ARP_STRING_EXPORT_DECL
17#else
18 #define ARP_STRING_INLINE
19 #define ARP_STRING_EXPORT ARP_EXPORT
20 #define ARP_STRING_EXPORT_DECL ARP_EXPORT
21#endif
22
23namespace Arp { namespace Base { namespace Core
24{
25
38class ARP_BASE_CORE_EXPORT String
39{
40public: // usings
41 using CharType = char;
42 using Allocator = std::allocator<CharType>;
43 using AllocatorTraits = std::allocator_traits<Allocator>;
46 using StdString = BaseString::StdString;
49 using Iterator = typename BaseString::iterator;
50 using ConstIterator = typename BaseString::const_iterator;
53 using ThreeWayCompareResult = decltype(StdString() <=> StdString());
54 using Tokens = std::vector<String>;
55 using Bytes = std::vector<byte>;
56
57public: // container policy usings
60 using size_type = typename BaseString::size_type;
64 using pointer = typename AllocatorTraits::pointer;
65 using const_pointer = typename AllocatorTraits::const_pointer;
66 using iterator = typename BaseString::iterator;
67 using const_iterator = typename BaseString::const_iterator;
70
71public: // construction/destruction
72 String(void);
73 String(const String& arg);
74 String(String&& arg)noexcept;
75 String& operator=(const String& arg);
76 String& operator=(String&& arg)noexcept;
77 ~String(void);
78
79 String(const BaseString& arg);
80 String(BaseString&& arg);
81 String(const StdString& arg);
82 String(StdString&& arg);
83 String(const String& arg, size_type offset, size_type count = NPos);
84 String(const CharType* pChars, size_type count);
85 String(const CharType* pChars);
86 String(const Char8uType* pChars);
87 String(size_type count, CharType c);
88 String(std::initializer_list<CharType> arg);
89 String(Iterator first, Iterator last);
91 explicit String(const Bytes& bytes);
92
93public: // static properties
94 static const size_type NPos = size_type(-1);
95 static const String Empty;
96 static const String NewLine;
97
98public: // implicit cast operators (Arp extension)
99 operator const CharType*(void)const;
100 operator const StdString& (void)const;
101
102public: // assignment operator
103 String& operator=(std::initializer_list<CharType> right);
104 String& operator=(const CharType* right);
105 String& operator=(CharType c);
106
107public: // append operator
108 String& operator+=(std::initializer_list<CharType> right);
109 String& operator+=(const String& right);
110 String& operator+=(const CharType* right);
111 String& operator+=(CharType c);
112
113public: // assign operations
114 String& Assign(const String& arg);
115 String& Assign(String&& arg);
116 String& Assign(const StdString& arg);
117 String& Assign(StdString&& arg);
118 String& Assign(std::initializer_list<CharType> arg);
119 String& Assign(const String& arg, size_type offset, size_type count = NPos);
120
121 String& Assign(const CharType* pChars, size_type count);
122 String& Assign(const CharType* pChars);
123 String& Assign(size_type count, CharType c);
124 String& Assign(Iterator first, Iterator last);
125 String& Assign(ConstIterator first, ConstIterator last);
126 String& Assign(const_pointer first, const_pointer last);
127
128public: // append operations
129 String& Append(std::initializer_list<CharType> arg);
130 String& Append(const String& arg);
131 String& Append(const String& arg, size_type offset, size_type count = NPos);
132 String& Append(const CharType* pChars, size_type count);
133 String& Append(const CharType* pChars);
134 String& Append(size_type count, CharType c);
135 String& Append(Iterator first, Iterator last);
138
139public: // insert operations
140 String& Insert(size_type offset, const String& arg);
141 String& Insert(size_type offset, const String& arg, size_type argOffset, size_type count = NPos);
142 String& Insert(size_type offset, const CharType* pChars, size_type count);
143 String& Insert(size_type offset, const CharType* pChars);
144 String& Insert(size_type offset, size_type count, CharType c);
145 Iterator Insert(ConstIterator where, CharType c);
146 Iterator Insert(ConstIterator where, size_type count, CharType c);
147 Iterator Insert(ConstIterator where, Iterator first, Iterator last);
148 Iterator Insert(ConstIterator where, ConstIterator first, ConstIterator last);
149 Iterator Insert(ConstIterator where, const_pointer first, const_pointer last);
150 Iterator Insert(ConstIterator where, std::initializer_list<CharType> chars);
151
152public: // erase operations
153 String& Erase(size_type offset = 0);
154 String& Erase(size_type offset, size_type count);
155 Iterator Erase(ConstIterator where);
156 Iterator Erase(ConstIterator first, ConstIterator last);
157
158public: // replace operation
159 String& Replace(ConstIterator first, ConstIterator last, std::initializer_list<CharType> chars);
160 String& Replace(size_type offset, size_type length, const String& arg);
161 String& Replace(size_type offset, size_type length, const String& arg, size_type offsetArg, size_type count = NPos);
162 String& Replace(size_type offset, size_type length, const CharType* pChars, size_type count);
163 String& Replace(size_type offset, size_type length, const CharType* pChars);
164 String& Replace(size_type offset, size_type length, size_type count, CharType c);
165 String& Replace(ConstIterator first, ConstIterator last, const String& arg);
166 String& Replace(ConstIterator first, ConstIterator last, const CharType* pChars, size_type count);
167 String& Replace(ConstIterator first, ConstIterator last, const CharType* pChars);
168 String& Replace(ConstIterator first, ConstIterator last, size_type count, CharType c);
169 String& Replace(ConstIterator first, ConstIterator last, Iterator first2, Iterator last2);
170 String& Replace(ConstIterator first, ConstIterator last, ConstIterator first2, ConstIterator last2);
171 String& Replace(ConstIterator first, ConstIterator last, const_pointer first2, const_pointer last2);
172 String& Replace(ConstIterator first, ConstIterator last, pointer first2, pointer last2);
173
174public: // support of range based for loops
175 Iterator begin(void);
176 ConstIterator begin(void)const;
177 Iterator end(void);
178 ConstIterator end(void)const;
179
180public: // container operations
181 void ShrinkToFit(void);
182 void PushBack(CharType c);
183 void PopBack(void);
184 Iterator Begin(void);
185 ConstIterator Begin(void)const;
186 Iterator End(void);
187 ConstIterator End(void)const;
188 ReverseIterator ReverseBegin(void);
189 ConstReverseIterator ReverseBegin(void)const;
190 ReverseIterator ReverseEnd(void);
191 ConstReverseIterator ReverseEnd(void)const;
192 ConstIterator ConstBegin(void)const;
193 ConstIterator ConstEnd(void)const;
194 ConstReverseIterator ConstReverseBegin(void)const;
195 ConstReverseIterator ConstReverseEnd(void)const;
196 Reference Front(void);
197 ConstReference Front(void)const;
198 Reference Back(void);
199 ConstReference Back(void)const;
200 Reference At(size_type offset);
201 ConstReference At(size_type offset)const;
202 Reference operator[](size_type offset);
203 ConstReference operator[](size_type offset)const;
204
205public: // Size/Length operations
206 size_type Length(void)const;
207 size_type Size(void)const;
208 size_type MaxSize(void)const;
209 size_type Capacity(void)const;
210 bool IsEmpty(void)const;
211 void Resize(size_type newSize);
212 void Resize(size_type newSize, CharType c);
213 void Reserve(size_type newCapacity = 0);
214 void Clear(void);
215
216public: // Find operations
217 size_type Find(const String& pattern, size_type offset = 0)const;
218 size_type Find(const CharType* pChars, size_type offset, size_type count)const;
219 size_type Find(const CharType* pChars, size_type offset = 0)const;
220 size_type Find(CharType c, size_type offset = 0)const;
221 size_type ReverseFind(const String& pattern, size_type offset = NPos)const;
222 size_type ReverseFind(const CharType* pChars, size_type offset, size_type count)const;
223 size_type ReverseFind(const CharType* pChars, size_type offset = NPos)const;
224 size_type ReverseFind(CharType c, size_type offset = NPos)const;
225 size_type FindFirstOf(const String& chars, size_type offset = 0)const;
226 size_type FindFirstOf(const CharType* pChars, size_type offset, size_type count)const;
227 size_type FindFirstOf(const CharType* pChars, size_type offset = 0)const;
228 size_type FindFirstOf(CharType c, size_type offset = 0)const;
229 size_type FindLastOf(const String& chars, size_type offset = NPos)const;
230 size_type FindLastOf(const CharType* pChars, size_type offset, size_type count)const;
231 size_type FindLastOf(const CharType* pChars, size_type offset = NPos)const;
232 size_type FindLastOf(CharType c, size_type offset = NPos)const;
233 size_type FindFirstNotOf(const String& chars, size_type offset = 0)const;
234 size_type FindFirstNotOf(const CharType* pChars, size_type offset, size_type count)const;
235 size_type FindFirstNotOf(const CharType* pChars, size_type offset = 0)const;
236 size_type FindFirstNotOf(CharType c, size_type offset = 0)const;
237 size_type FindLastNotOf(const String& chars, size_type offset = NPos)const;
238 size_type FindLastNotOf(const CharType* pChars, size_type offset, size_type count)const;
239 size_type FindLastNotOf(const CharType* pChars, size_type offset = NPos)const;
240 size_type FindLastNotOf(CharType c, size_type offset = NPos)const;
241
242public: // StartsWith/EndsWith operations
243 bool StartsWith(const CharType* pChars)const;
244 bool StartsWith(const String& pattern)const;
245 bool EndsWith(const CharType* pChars)const;
246 bool EndsWith(const String& pattern)const;
247
248public: // Compare operations
249 int Compare(const String& other)const;
250 int Compare(size_type offset, size_type count, const String& other)const;
251 int Compare(size_type offset, size_type count, const String& other, size_type offsetOther, size_type countOther = NPos)const;
252 int Compare(const CharType* pOther)const;
253 int Compare(size_type offset, size_type count, const CharType* pOther)const;
254 int Compare(size_type offset, size_type count, const CharType* pOther, size_type countOther)const;
255
256public: // Misc operations
257 Allocator GetAllocator(void)const;
258 const CharType* CStr(void)const;
259 String Substr(size_type offset = 0, size_type count = NPos)const;
260 void Swap(String& other);
261
262public: // misc operations (Arp extension)
263 Bytes ToBytes(void)const;
264 String& ReplaceAll(const String& pattern, const String& replacement);
265 const StdString& GetStdString(void)const;
266 StdString& GetStdString(void);
267
268public: // Arp Trim operations (Arp extension)
269 String TrimLeft(void)const;
270 String TrimLeft(CharType trim)const;
271 String TrimRight(void)const;
272 String TrimRight(CharType trim)const;
273 String Trim(void)const;
274 String Trim(CharType trim)const;
275
276public: // Arp Split operations (Arp extension)
277 Tokens Split(CharType delimiter, bool trimTokens = true, bool removeEmptyTokens = true)const;
278 Tokens Split(std::span<const CharType> delimiters, bool trimTokens = true, bool removeEmptyTokens = true)const;
279 Tokens SplitByWord(const String& delimiter, bool trimTokens = true, bool removeEmptyTokens = true)const;
280
281public: // Arp Format operations (Arp extension)
282 template<typename... Args> static String Format(const String& format, const Args& ... args);
283 template<typename... Args> static String Format(const char* format, const Args& ... args);
284 template<typename... Args> static String FormatPrintf(const char* format, const Args& ... args);
285
286public: // friend operators
287 friend ARP_STRING_EXPORT_DECL String operator+(const String& left, const String& right);
288 friend ARP_STRING_EXPORT_DECL String operator+(const String& left, const String::CharType* right);
289 friend ARP_STRING_EXPORT_DECL String operator+(const String& left, String::CharType right);
290 friend ARP_STRING_EXPORT_DECL String operator+(const String::CharType* left, const String& right);
291 friend ARP_STRING_EXPORT_DECL String operator+(String::CharType left, const String& right);
292 friend ARP_STRING_EXPORT_DECL String operator+(String&& left, String&& right);
293 friend ARP_STRING_EXPORT_DECL String operator+(String&& left, const String& right);
294 friend ARP_STRING_EXPORT_DECL String operator+(String&& left, const String::CharType* right);
295 friend ARP_STRING_EXPORT_DECL String operator+(String&& left, String::CharType right);
296 friend ARP_STRING_EXPORT_DECL String operator+(const String& left, String&& right);
297 friend ARP_STRING_EXPORT_DECL String operator+(const String::CharType* left, String&& right);
298 friend ARP_STRING_EXPORT_DECL String operator+(String::CharType left, String&& right);
299
300public: // deprecated operations
301 ARP_DEPRECATED("Use String::GetStdString() instead")
302 const StdString& GetBaseString(void)const;
303 ARP_DEPRECATED("Use String::StartsWith() instead")
304 bool StartWith(const CharType* pChars)const;
305 ARP_DEPRECATED("Use String::StartsWith() instead")
306 bool StartWith(const String& pattern)const;
307 ARP_DEPRECATED("Use String::EndsWith() instead")
308 bool EndWith(const CharType* pChars)const;
309 ARP_DEPRECATED("Use String::EndsWith() instead")
310 bool EndWith(const String& pattern)const;
311
312private: //methods
313 StdString& GetImpl(void);
314 const StdString& GetImpl(void)const;
315
316private: // fields
317 BaseString baseString;
318};
319
321// declarations of global operators and functions of class String
322
323ARP_STRING_EXPORT_DECL std::ostream& operator<<(std::ostream& os, const String& right);
324ARP_STRING_EXPORT_DECL std::istream& operator>>(std::istream& is, String& right);
325
326ARP_STRING_EXPORT_DECL String operator+(const String& left, const String& right);
327ARP_STRING_EXPORT_DECL String operator+(const String::CharType* left, const String& right);
328ARP_STRING_EXPORT_DECL String operator+(String::CharType left, const String& right);
329ARP_STRING_EXPORT_DECL String operator+(const String& left, const String::CharType* right);
330ARP_STRING_EXPORT_DECL String operator+(const String& left, const String::CharType right);
331ARP_STRING_EXPORT_DECL String operator+(const String& left, String&& right);
332ARP_STRING_EXPORT_DECL String operator+(String&& left, const String& right);
333ARP_STRING_EXPORT_DECL String operator+(String&& left, String&& right);
334ARP_STRING_EXPORT_DECL String operator+(const String::CharType* left, String&& right);
335ARP_STRING_EXPORT_DECL String operator+(String::CharType left, String&& right);
336ARP_STRING_EXPORT_DECL String operator+(String&& left, const String::CharType* right);
337ARP_STRING_EXPORT_DECL String operator+(String&& left, const String::CharType right);
338
339ARP_STRING_EXPORT_DECL bool operator==(const String& left, const String& right);
340ARP_STRING_EXPORT_DECL bool operator==(const String::CharType* left, const String& right);
341ARP_STRING_EXPORT_DECL bool operator==(const String& left, const String::CharType* right);
342ARP_STRING_EXPORT_DECL bool operator!=(const String::CharType* left, const String& right);
343ARP_STRING_EXPORT_DECL bool operator!=(const String& left, const String::CharType* right);
344ARP_STRING_EXPORT_DECL bool operator< (const String::CharType* left, const String& right);
345ARP_STRING_EXPORT_DECL bool operator< (const String& left, const String::CharType* right);
346ARP_STRING_EXPORT_DECL bool operator> (const String::CharType* left, const String& right);
347ARP_STRING_EXPORT_DECL bool operator> (const String& left, const String::CharType* right);
348ARP_STRING_EXPORT_DECL bool operator<=(const String::CharType* left, const String& right);
349ARP_STRING_EXPORT_DECL bool operator<=(const String& left, const String::CharType* right);
350ARP_STRING_EXPORT_DECL bool operator>=(const String::CharType* left, const String& right);
351ARP_STRING_EXPORT_DECL bool operator>=(const String& left, const String::CharType* right);
352ARP_STRING_EXPORT_DECL String::ThreeWayCompareResult operator<=>(const String& left, const String& right);
353
354ARP_STRING_EXPORT_DECL void swap(String& left, String& right)noexcept;
355
356}}} // end of namespace Arp::Base::Core
357
358// include of inline implementations
359#include "Arp/Base/Core/Detail/String.inl"
360
361#ifdef ARP_BUILD_STRING_IMPL_INLINE
363#endif // def ARP_BUILD_STRING_IMPL_INLINE
typename StdString::difference_type difference_type
The difference type of this type.
Definition: BasicString.hxx:56
typename StdString::reverse_iterator reverse_iterator
The reverse iterator type of this type.
Definition: BasicString.hxx:63
typename StdString::const_reverse_iterator const_reverse_iterator
The const reverse iterator type of this type.
Definition: BasicString.hxx:64
typename StdString::const_reference const_reference
The const reference type of this type.
Definition: BasicString.hxx:58
typename StdString::reference reference
The reference type of this type.
Definition: BasicString.hxx:57
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
typename BaseString::const_iterator const_iterator
The const iterator type of this type.
Definition: String.hpp:67
String(String &&arg) noexcept
This move constructor moves the as argument passed string to this string.
typename AllocatorTraits::pointer pointer
The pointer type of this type.
Definition: String.hpp:64
typename BaseString::difference_type difference_type
The difference type of this type.
Definition: String.hpp:61
Allocator allocator_type
The allocator type of this type.
Definition: String.hpp:58
decltype(StdString()<=> StdString()) ThreeWayCompareResult
Used by operator<=>
Definition: String.hpp:53
typename AllocatorTraits::const_pointer const_pointer
The const pointer type of this type.
Definition: String.hpp:65
String & operator=(String &&arg) noexcept
This move assignment operator moves the right-hand-side operand to this string.
typename BaseString::const_reference const_reference
The const reference type of this type.
Definition: String.hpp:63
typename BaseString::const_iterator ConstIterator
The const iterator type of this type.
Definition: String.hpp:50
typename BaseString::const_reference ConstReference
The const reference type of this type.
Definition: String.hpp:48
typename BaseString::iterator iterator
The iterator type of this type.
Definition: String.hpp:66
typename BaseString::iterator Iterator
The iterator type of this type.
Definition: String.hpp:49
typename BaseString::reverse_iterator reverse_iterator
The reverse iterator type of this type.
Definition: String.hpp:68
std::allocator_traits< Allocator > AllocatorTraits
The allocator traits type.
Definition: String.hpp:43
BaseString::StdString StdString
The type of the basic std string.
Definition: String.hpp:46
std::allocator< CharType > Allocator
The characters allocator type.
Definition: String.hpp:42
typename BaseString::const_reverse_iterator const_reverse_iterator
The const reverse iterator type of this type.
Definition: String.hpp:69
std::vector< String > Tokens
Used by initializing operations.
Definition: String.hpp:54
CharType value_type
The char type of this type.
Definition: String.hpp:59
typename BaseString::reference reference
The reference type of this type.
Definition: String.hpp:62
String(void)
The default constructor constructs an empty string instance.
String(const String &arg)
The copy constructor copies the as argument passed string deeply.
typename BaseString::reference Reference
The reference type of this type.
Definition: String.hpp:47
Arp::Base::Core::char8u Char8uType
The UTF8 character type.
Definition: String.hpp:44
typename BaseString::size_type size_type
The size type of this type.
Definition: String.hpp:60
~String(void)
The destructor deallocates all memory of this instance.
typename BaseString::reverse_iterator ReverseIterator
The reverse iterator type of this type.
Definition: String.hpp:51
typename BaseString::const_reverse_iterator ConstReverseIterator
Used by Split() operations.
Definition: String.hpp:52
String & operator=(const String &arg)
This assignment operator copies the right-hand-side operand to this string.
char CharType
The character type.
Definition: String.hpp:41
char8_t char8u
The Arp UTF8 character type of 1 byte size.
Definition: PrimitiveTypes.hpp:47
@ Empty
No sink assigned to session yet.
@ Append
Opens the file if it exists and seeks to the end. Otherwise, creates a new file.
Root namespace for the PLCnext API
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38
Namespace of the C++ standard library