open62541pp 0.18.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
string_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdarg> // va_list
4#include <string>
5#include <string_view>
6
7#include "open62541pp/detail/open62541/common.h" // UA_String
8
9namespace opcua::detail {
10
11/// Convert std::string_view to UA_String (no copy)
12UA_String toNativeString(std::string_view src) noexcept;
13
14/// Allocate UA_String from std::string_view
15[[nodiscard]] UA_String allocNativeString(std::string_view src);
16
17/// Allocate const char* from std::string_view
18[[nodiscard]] char* allocCString(std::string_view src);
19
20void clear(const char* str) noexcept;
21
22/// Convert UA_String to std::string_view
23/// Can be marked noexcept: https://stackoverflow.com/a/62061549/9967707
24inline std::string_view toStringView(const UA_String& src) noexcept {
25 if (src.data == nullptr || src.length == 0U) {
26 return {};
27 }
28 return {(const char*)src.data, src.length}; // NOLINT
29}
30
31/// Convert UA_String to std::string
32inline std::string toString(const UA_String& src) {
33 return std::string(toStringView(src));
34}
35
36/// Convert format string with args to std::string
37std::string toString(const char* format, va_list args);
38
39// NOLINTBEGIN
40inline std::string toString(const char* format, ...) {
41 va_list args;
42 va_start(args, format);
43 std::string result = toString(format, args);
44 va_end(args);
45 return result;
46}
47
48// NOLINTEND
49
50} // namespace opcua::detail
static UA_LogCategory const char va_list args
va_end(args)
va_start(args, msg)