Imt.Base C++ API V4.1.1.0
Loading...
Searching...
No Matches
StringUtil.h
Go to the documentation of this file.
1// (c) IMT - Information Management Technology AG, CH-9470 Buchs, www.imt.ch.
2
3#ifndef IMT_BASE_LIB_STRINGS_STRINGUTIL_H
4#define IMT_BASE_LIB_STRINGS_STRINGUTIL_H
5
8
9#ifdef USE_STL
10
11namespace imt {
12namespace base {
13namespace lib {
14namespace strings {
15
21// AXIVION Next Construct AutosarC++19_03-M3.4.1: No definition in primary file to separate concerns.
22class StringUtil final : private ::imt::base::core::platform::StaticClass {
23
24public:
25
37 template<typename Type>
38 static inline bool stringToValue(std::wstring const& s, Type& returnValue, std::ios_base& (*const func)(std::ios_base&)) noexcept {
39 std::wistringstream iss {s};
40 // AXIVION Next Line AutosarC++19_03-A18.0.2
41 return !((iss >> func >> returnValue).fail());
42 }
43
53 template<typename Type>
54 static inline bool stringToValue(std::wstring const& s, Type& returnValue) noexcept {
55 std::wistringstream iss {s};
56 // AXIVION Next Line AutosarC++19_03-A18.0.2
57 return !((iss >> returnValue).fail());
58 }
59
70 template<typename Type>
71 static inline std::wstring valueToString(Type const& inputValue, std::ios_base& (*const func)(std::ios_base&)) noexcept {
72 std::wostringstream oss {};
73 oss << func << inputValue;
74 return oss.str();
75 }
76
85 template<typename Type>
86 static inline std::wstring valueToString(Type const& inputValue) noexcept {
87 std::wostringstream oss {};
88 oss << inputValue;
89 return oss.str();
90 }
91
100 static inline std::wstring valueToString(float32_t const inputValue, uint8_t const precision) {
101 std::wostringstream oss {};
102 oss.precision(static_cast<int32_t>(precision)); // AXIVION Line AutosarC++19_03-A0.1.2 AutosarC++19_03-M0.3.2: required Construct
103 oss << std::fixed;
104 oss << inputValue;
105 return oss.str();
106 }
107
116 static inline std::wstring valueToString(float64_t const inputValue, uint8_t const precision) {
117 std::wostringstream oss {};
118 oss.precision(static_cast<int32_t>(precision)); // AXIVION Line AutosarC++19_03-A0.1.2 AutosarC++19_03-M0.3.2: required Construct
119 oss << std::fixed;
120 oss << inputValue;
121 return oss.str();
122 }
123
128 static int8_t compareNoCase(std::wstring const& str1, std::wstring const& str2);
129
134 static bool equalsNoCase(std::wstring const& str1, std::wstring const& str2);
135
139 static void trim(std::wstring& str);
140
144 static void replace(std::wstring& str, std::wstring const& oldValue, std::wstring const& newValue);
145
149 static void removeBlanks(std::wstring& str);
150
155 static int32_t toInt(std::wstring const& str);
156
161 static uint32_t toUInt(std::wstring const& str);
162
167 static int32_t hexToInt(std::wstring const& str);
168
173 static float32_t toFloat(std::wstring const& str);
174
179 static float64_t toDouble(std::wstring const& str);
180
186 static bool toBool(std::wstring const& str);
187
191 static bool isDecLiteral(std::wstring const& str);
192
196 static bool isHexLiteral(std::wstring const& str);
197
201 static bool contains(std::wstring const& str, wchar_t const c);
202
206 static bool contains(std::wstring const& str, std::wstring const& chars);
207
208private:
209
210 static void trimLeft(std::wstring& str);
211 static void trimRight(std::wstring& str);
212};
213
214//Justification: Specialization of 8bit types, as these can not be converted by default
215
216// AXIVION Next Construct AutosarC++19_03-A14.8.2: Specialization of 8bit types, as these can not be converted by default
217// AXIVION Next Construct AutosarC++19_03-A8.4.8: Use output parameter for compatibility
218// AXIVION Next Construct AutosarC++19_03-A8.4.4: Use output parameter instead of return value for compatibility
219// AXIVION Next Construct AutosarC++19_03-A8.4.9: Is not an in-out parameter but only a output parameter so it is not read
220template<>
221inline bool StringUtil::stringToValue<uint8_t>(std::wstring const& s, uint8_t& returnValue, std::ios_base& (*const func)(std::ios_base&)) noexcept {
222 uint16_t value {0U};
223 bool const result {stringToValue<uint16_t>(s, value, func)};
224 returnValue = static_cast<uint8_t>(value);
225 return result;
226}
227
228// AXIVION Next Construct AutosarC++19_03-A14.8.2: Specialization of 8bit types, as these can not be converted by default
229// AXIVION Next Construct AutosarC++19_03-A8.4.8: Use output parameter for compatibility
230// AXIVION Next Construct AutosarC++19_03-A8.4.4: Use output parameter instead of return value for compatibility
231// AXIVION Next Construct AutosarC++19_03-A8.4.9: Is not an in-out parameter but only a output parameter so it is not read
232template<>
233inline bool StringUtil::stringToValue<int8_t>(std::wstring const& s, int8_t& returnValue, std::ios_base& (*const func)(std::ios_base&)) noexcept {
234 int16_t value {0};
235 bool const result {stringToValue<int16_t>(s, value, func)};
236 returnValue = static_cast<int8_t>(value);
237 return result;
238}
239
240// AXIVION Next Construct AutosarC++19_03-A14.8.2: Specialization of 8bit types, as these can not be converted by default
241// AXIVION Next Construct AutosarC++19_03-A8.4.8: Use output parameter for compatibility
242// AXIVION Next Construct AutosarC++19_03-A8.4.4: Use output parameter instead of return value for compatibility
243// AXIVION Next Construct AutosarC++19_03-A8.4.9: Is not an in-out parameter but only a output parameter so it is not read
244template<>
245inline bool StringUtil::stringToValue<uint8_t>(std::wstring const& s, uint8_t& returnValue) noexcept {
246 uint16_t value {0U};
247 bool const result {stringToValue<uint16_t>(s, value)};
248 returnValue = static_cast<uint8_t>(value);
249 return result;
250}
251
252// AXIVION Next Construct AutosarC++19_03-A14.8.2: Specialization of 8bit types, as these can not be converted by default
253// AXIVION Next Construct AutosarC++19_03-A8.4.8: Use output parameter for compatibility
254// AXIVION Next Construct AutosarC++19_03-A8.4.4: Use output parameter instead of return value for compatibility
255// AXIVION Next Construct AutosarC++19_03-A8.4.9: Is not an in-out parameter but only a output parameter so it is not read
256template<>
257inline bool StringUtil::stringToValue<int8_t>(std::wstring const& s, int8_t& returnValue) noexcept {
258 int16_t value {0};
259 bool const result {stringToValue<int16_t>(s, value)};
260 returnValue = static_cast<int8_t>(value);
261 return result;
262}
263
264// AXIVION Next Routine AutosarC++19_03-A16.0.1: Disable precompiler decision rule
265#if defined(__IAR_SYSTEMS_ICC__)
266// Due to the IAR compiler, some conversion functions must be specialized
267
268template<>
269inline std::wstring StringUtil::valueToString<uint8_t>(uint8_t const& inputValue, std::ios_base& (*const func)(std::ios_base&)) {
270 return valueToString<uint16_t>(inputValue, func);
271}
272
273template<>
274inline std::wstring StringUtil::valueToString<int8_t>(int8_t const& inputValue, std::ios_base& (*const func)(std::ios_base&)) {
275 return valueToString<int16_t>(inputValue, func);
276}
277
278template<>
279inline std::wstring StringUtil::valueToString<uint8_t>(uint8_t const& inputValue) {
280 return valueToString<uint16_t>(inputValue);
281}
282
283template<>
284inline std::wstring StringUtil::valueToString<int8_t>(int8_t const& inputValue) {
285 return valueToString<int16_t>(inputValue);
286}
287#endif //__IAR_SYSTEMS_ICC__
288
289} // namespace strings
290} // namespace lib
291} // namespace base
292} // namespace imt
293
294#endif // USE_STL
295#endif // IMT_BASE_LIB_STRINGS_STRINGUTIL_H
Base class for a static class that disables construction, copy, assignment and move of instances.
Definition StaticClass.h:48
This is a application specific file which is used to configure Imt.Base.Core.Math.
float float32_t
32 Bits float variable (float)
Definition stdfloat.h:56
double float64_t
64 Bits float variable (double)
Definition stdfloat.h:63
__int16 int16_t
Definition stdint.h:59
__int8 int8_t
Definition stdint.h:58
unsigned __int16 uint16_t
Definition stdint.h:63
__int32 int32_t
Definition stdint.h:60
unsigned __int32 uint32_t
Definition stdint.h:64
unsigned __int8 uint8_t
Definition stdint.h:62