Keyple Util C++ Library 2.3.0.5-SNAPSHOT
Reference Terminal Reader API for C++
KeypleStd.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#pragma once
14
15#include <memory>
16#include <iomanip>
17#include <iostream>
18#include <iterator>
19#include <map>
20#include <ostream>
21#include <regex>
22#include <set>
23#include <vector>
24
25namespace std {
26
27// when using a compiler that does not support C++17, define our own implementation of reinterpret_pointer_cast
28#if !defined(_WIN32) && !(defined(__APPLE__) && defined(__clang__)) && __cplusplus < 201703L
29template <typename To, typename From>
30inline std::shared_ptr<To> reinterpret_pointer_cast(std::shared_ptr<From> const & ptr) noexcept
31{
32 return std::shared_ptr<To>(ptr, reinterpret_cast<To *>(ptr.get()));
33}
34#endif
35
36inline std::ostream& operator<<(std::ostream& os, const uint8_t v)
37{
38 os << static_cast<int>(v)
39 << "(0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2)
40 << static_cast<int>(v) << ")";
41
42 return os;
43}
44
45inline std::ostream& operator<<(std::ostream& os, const std::vector<uint8_t>& v)
46{
47 for (int i = 0; i < static_cast<int>(v.size()); i++)
48 os << std::uppercase << std::hex << std::setfill('0') << std::setw(2)
49 << static_cast<int>(v[i]);
50
51 return os;
52}
53
54inline std::ostream& operator<<(std::ostream& os, const std::vector<int>& v)
55{
56 for (int i = 0; i < static_cast<int>(v.size()); i++) {
57 if (i != 0) {
58 os << ", ";
59 }
60
61 os << std::hex << std::setfill('0') << std::setw(8)
62 << static_cast<int>(v[i]);
63 }
64
65 return os;
66}
67
68inline std::ostream& operator<<(std::ostream& os, const std::vector<std::string>& v)
69{
70 os << "{";
71 for (auto it = v.begin(); it != v.end(); ++it)
72 {
73 if (it != v.begin())
74 os << ", ";
75 os << *it;
76 }
77 os << "}";
78
79 return os;
80}
81
82inline std::ostream& operator<<(std::ostream& os, const std::set<std::string>& s)
83{
84 os << "{";
85 for (auto it = s.begin(); it != s.end(); ++it)
86 {
87 if (it != s.begin())
88 os << ", ";
89 os << *it;
90 }
91 os << "}";
92
93 return os;
94}
95
96template<class A, class B>
97inline
98std::ostream& operator<<(std::ostream& os, const std::map<A, B>& s)
99{
100 os << "MAP: {";
101 for (auto it = s.begin(); it != s.end(); ++it)
102 {
103 if (it != s.begin())
104 os << ", ";
105 os << "{" << it->first << ", " << it->second << "}";
106 }
107 os << "}";
108
109 return os;
110}
111
112template<class A, class B>
113inline
114std::ostream& operator<<(std::ostream& os, const std::map<const A, B>& s)
115{
116 os << "MAP: {";
117 for (auto it = s.begin(); it != s.end(); ++it)
118 {
119 if (it != s.begin())
120 os << ", ";
121 os << "{" << it->first << ", " << it->second << "}";
122 }
123 os << "}";
124
125 return os;
126}
127
128template <typename out>
129inline void split(const std::string &s, const std::regex& re, out result)
130{
131 std::sregex_token_iterator d(s.begin(), s.end(), re, -1);
132 std::sregex_token_iterator end;
133
134 while (d != end) {
135 *result++ = *d++;
136 }
137}
138
139/* Trim from left */
140inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v")
141{
142 s.erase(0, s.find_first_not_of(t));
143
144 return s;
145}
146
147/* Trim from right */
148inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v")
149{
150 s.erase(s.find_last_not_of(t) + 1);
151
152 return s;
153}
154
155/* Trim from left & right */
156inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v")
157{
158 return ltrim(rtrim(s, t), t);
159}
160
161}
Definition: Any.h:504
std::string & ltrim(std::string &s, const char *t=" \t\n\r\f\v")
Definition: KeypleStd.h:140
std::shared_ptr< To > reinterpret_pointer_cast(std::shared_ptr< From > const &ptr) noexcept
Definition: KeypleStd.h:30
std::ostream & operator<<(std::ostream &os, const uint8_t v)
Definition: KeypleStd.h:36
std::string & trim(std::string &s, const char *t=" \t\n\r\f\v")
Definition: KeypleStd.h:156
std::string & rtrim(std::string &s, const char *t=" \t\n\r\f\v")
Definition: KeypleStd.h:148
void split(const std::string &s, const std::regex &re, out result)
Definition: KeypleStd.h:129