A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
char.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TEXT_CHAR_H_
2 #define GENESIS_UTILS_TEXT_CHAR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cctype>
35 #include <iostream>
36 #include <string>
37 #include <sstream>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Chars Functions
44 // =================================================================================================
45 
49 inline bool char_is_digit (const char c)
50 {
51  return ('0' <= c) && (c <= '9');
52 }
53 
57 inline bool char_is_sign (const char c)
58 {
59  return ('+' == c) || ('-' == c);
60 }
61 
65 inline bool char_is_number_part( char const c )
66 {
67  return char_is_digit( c ) || char_is_sign( c ) || c == '.' || c == 'e' || c == 'E';
68 }
69 
73 inline bool char_match_ci(const char c1, const char c2)
74 {
75  return std::tolower(c1) == std::tolower(c2);
76 }
77 
81 inline char to_lower_ascii( char c )
82 {
83  return ( 'A' <= c && c <= 'Z' ) ? c + 0x20 : c;
84 }
85 
89 inline char to_upper_ascii( char c )
90 {
91  return ( 'a' <= c && c <= 'z' ) ? c - 0x20 : c;
92 }
93 
104 inline std::string char_to_hex( char c, bool full = false )
105 {
106  std::stringstream ss;
107  if( full ) {
108  ss << "'" << std::string( 1, c ) << "' (0x" << std::hex << static_cast<int>( c ) << ")";
109  } else {
110  ss << std::hex << static_cast<int>( c );
111  }
112  return ss.str();
113 }
114 
115 } // namespace utils
116 } // namespace genesis
117 
118 #endif // include guard
char to_lower_ascii(char c)
Return the lower case of a given char, ascii-only.
Definition: char.hpp:81
bool char_is_number_part(char const c)
Return whether a char is valid in a number, i.e., a digit, a dot, plus, minus, or 'e'/'E'...
Definition: char.hpp:65
std::string char_to_hex(char c, bool full=false)
Return the hex representation of a char.
Definition: char.hpp:104
char to_upper_ascii(char c)
Return the upper case of a given char, ascii-only.
Definition: char.hpp:89
bool char_match_ci(const char c1, const char c2)
Return whether two chars are the same, case insensitive.
Definition: char.hpp:73
bool char_is_digit(const char c)
Return whether a char is a digit (0-9).
Definition: char.hpp:49
bool char_is_sign(const char c)
Return whether a char is a sign (+-).
Definition: char.hpp:57