A toolkit for working with phylogenetic data.
v0.24.0
char.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2020 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
33 #include <array>
34 #include <cassert>
35 #include <cctype>
36 #include <iomanip>
37 #include <ios>
38 #include <iostream>
39 #include <sstream>
40 #include <stdexcept>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Chars Functions
47 // =================================================================================================
48 
56 static const std::array<std::string, 128> ascii_symbols_ = {{
57  "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
58  "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
59  " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
60  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
61  "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
62  "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_",
63  "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
64  "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "DEL"
65 }};
66 
67 std::string char_to_hex( char c, bool full )
68 {
69  // By conversion to unsigned char, we transform potential negative numbers to their mod 256
70  // equivalent. This helps to mitigate the irritation that is caused by allowing char to be
71  // signed or unsigned in the standard.
72  return char_to_hex( static_cast<unsigned char>( c ), full );
73 
74  // The following check is obsolte now, as we accept the whole byte range.
75 
76  // Check that we are in the valid ascii range. If not, outputting an ascii char does not make
77  // sense anyway. We cast to int here, because char can either be signed or unsigned, and hence
78  // different compilers will warn that one of the comparisons is always false. But we need both,
79  // because of compilers differ...
80  // if( static_cast<int>( c ) < 0 || static_cast<int>( c ) > 127 ) {
81  // throw std::runtime_error( "Invalid ASCII char " + std::to_string( static_cast<int>( c )));
82  // }
83 }
84 
85 std::string char_to_hex( unsigned char c, bool full )
86 {
87  std::stringstream ss;
88  if( full ) {
89  if( c < 128 ) {
90  if( std::isprint(c) ) {
91  assert( std::string(1, c) == ascii_symbols_[c] );
92  ss << "'" << std::string( 1, c ) << "'";
93  } else {
94  ss << ascii_symbols_[c];
95  }
96  } else {
97  ss << "non-ASCII char";
98  }
99 
100  ss << " (0x";
101  }
102  ss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>( c );
103  if( full ) {
104  ss << ")";
105  }
106  return ss.str();
107 }
108 
109 } // namespace utils
110 } // namespace genesis
std::string char_to_hex(char c, bool full)
Return the name and hex representation of a char.
Definition: char.cpp:67
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
static const std::array< std::string, 128 > ascii_symbols_
List of all ASCII symbols.
Definition: char.cpp:56