Keyple Util C++ Library 2.0.0
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#if !defined(_WIN32)
28template <typename To, typename From>
29inline std::shared_ptr<To> reinterpret_pointer_cast(std::shared_ptr<From> const & ptr) noexcept
30{
31 return std::shared_ptr<To>(ptr, reinterpret_cast<To *>(ptr.get()));
32}
33#endif
34
35inline std::ostream& operator<<(std::ostream& os, const uint8_t v)
36{
37 os << static_cast<int>(v)
38 << "(0x" << std::hex << std::setfill('0') << std::setw(2)
39 << static_cast<int>(v) << ")";
40
41 return os;
42}
43
44inline std::ostream& operator<<(std::ostream& os, const std::vector<uint8_t>& v)
45{
46 for (int i = 0; i < static_cast<int>(v.size()); i++)
47 os << std::hex << std::setfill('0') << std::setw(2)
48 << static_cast<int>(v[i]);
49
50 return os;
51}
52
53inline std::ostream& operator<<(std::ostream& os, const std::vector<int>& v)
54{
55 for (int i = 0; i < static_cast<int>(v.size()); i++)
56 os << std::hex << std::setfill('0') << std::setw(8)
57 << static_cast<int>(v[i]);
58
59 return os;
60}
61
62inline std::ostream& operator<<(std::ostream& os, const std::vector<std::string>& v)
63{
64 os << "{";
65 for (auto it = v.begin(); it != v.end(); ++it)
66 {
67 if (it != v.begin())
68 os << ", ";
69 os << *it;
70 }
71 os << "}";
72
73 return os;
74}
75
76inline std::ostream& operator<<(std::ostream& os, const std::set<std::string>& s)
77{
78 os << "{";
79 for (auto it = s.begin(); it != s.end(); ++it)
80 {
81 if (it != s.begin())
82 os << ", ";
83 os << *it;
84 }
85 os << "}";
86
87 return os;
88}
89
90template<class A, class B>
91inline
92std::ostream& operator<<(std::ostream& os, const std::map<A, B>& s)
93{
94 os << "MAP: {";
95 for (auto it = s.begin(); it != s.end(); ++it)
96 {
97 if (it != s.begin())
98 os << ", ";
99 os << "{" << it->first << ", " << it->second << "}";
100 }
101 os << "}";
102
103 return os;
104}
105
106template<class A, class B>
107inline
108std::ostream& operator<<(std::ostream& os, const std::map<const A, B>& s)
109{
110 os << "MAP: {";
111 for (auto it = s.begin(); it != s.end(); ++it)
112 {
113 if (it != s.begin())
114 os << ", ";
115 os << "{" << it->first << ", " << it->second << "}";
116 }
117 os << "}";
118
119 return os;
120}
121
122template <typename out>
123inline void split(const std::string &s, const std::regex& re, out result)
124{
125 std::sregex_token_iterator d(s.begin(), s.end(), re, -1);
126 std::sregex_token_iterator end;
127
128 while (d != end) {
129 *result++ = *d++;
130 }
131}
132
133/* Trim from left */
134inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v")
135{
136 s.erase(0, s.find_first_not_of(t));
137
138 return s;
139}
140
141/* Trim from right */
142inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v")
143{
144 s.erase(s.find_last_not_of(t) + 1);
145
146 return s;
147}
148
149/* Trim from left & right */
150inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v")
151{
152 return ltrim(rtrim(s, t), t);
153}
154
155}
Definition: KeypleStd.h:25
std::string & ltrim(std::string &s, const char *t=" \t\n\r\f\v")
Definition: KeypleStd.h:134
std::shared_ptr< To > reinterpret_pointer_cast(std::shared_ptr< From > const &ptr) noexcept
Definition: KeypleStd.h:29
std::ostream & operator<<(std::ostream &os, const uint8_t v)
Definition: KeypleStd.h:35
std::string & trim(std::string &s, const char *t=" \t\n\r\f\v")
Definition: KeypleStd.h:150
std::string & rtrim(std::string &s, const char *t=" \t\n\r\f\v")
Definition: KeypleStd.h:142
void split(const std::string &s, const std::regex &re, out result)
Definition: KeypleStd.h:123