staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
hex_operations.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * File: hex_operations.hpp
19  * Author: alex
20  *
21  * Created on March 20, 2018, 5:15 PM
22  */
23 
24 #ifndef STATICLIB_IO_HEX_HPP
25 #define STATICLIB_IO_HEX_HPP
26 
27 #include <cctype>
28 #include <cstdint>
29 #include <array>
30 #include <string>
31 
37 
38 namespace staticlib {
39 namespace io {
40 
49 inline std::string hex_from_string(const std::string& plain) {
50  if (plain.empty()) return std::string();
51  auto src = array_source(plain.c_str(), plain.length());
52  auto sink = string_sink();
53  {
54  auto hsink = make_hex_sink(sink);
55  copy_all(src, hsink);
56  }
57  return std::move(sink.get_string());
58 }
59 
66 inline std::string string_to_hex(const std::string& plain) {
67  return hex_from_string(plain);
68 }
69 
76 inline std::string string_from_hex(const std::string& hexstr) {
77  if (hexstr.empty()) return std::string();
78  auto src = make_hex_source(array_source(hexstr.c_str(), hexstr.length()));
79  auto sink = string_sink();
80  copy_all(src, sink);
81  return std::move(sink.get_string());
82 }
83 
96 inline std::string format_hex_and_plain(const std::string& hexstr, const std::string& plain) {
97  if (hexstr.empty() || plain.empty()) return std::string();
98 
99  // prettify hex
100  auto pretty = std::string();
101  for (size_t i = 0; i < hexstr.length() - 1; i += 2 ) {
102  if (pretty.length() > 0) {
103  pretty.push_back(' ');
104  }
105  pretty.push_back(hexstr[i]);
106  pretty.push_back(hexstr[i + 1]);
107  }
108 
109  // cleanup plain
110  auto clean = std::string();
111  for (size_t i = 0; i < plain.length(); i++) {
112  char ch = plain[i];
113  if (ch < 32 || std::isspace(ch)) {
114  clean.push_back(' ');
115  } else {
116  clean.push_back(ch);
117  }
118  }
119 
120  // return formatted
121  return pretty + " [" + clean + "]";
122 }
123 
135 inline std::string format_hex(const std::string& hexstr) {
136  if (hexstr.empty()) return std::string();
137  auto plain = string_from_hex(hexstr);
138  return format_hex_and_plain(hexstr, plain);
139 }
140 
152 inline std::string format_plain_as_hex(const std::string& plain) {
153  if (plain.empty()) return std::string();
154  auto hexstr = string_to_hex(plain);
155  return format_hex_and_plain(hexstr, plain);
156 }
157 
158 } // namespace
159 }
160 
161 #endif /* STATICLIB_IO_HEX_HPP */
162