string.h
Go to the documentation of this file.
1 
9 #pragma once
10 #include <cctype>
11 #include <algorithm>
12 #include <istream>
13 namespace illumina { namespace interop { namespace util
14 {
15 
16  namespace detail
17  {
25  inline char tolower(const char ch)
26  {
27  return static_cast<char>(::tolower(ch));
28  }
29  }
30  // replace, camel_to_space
31  //
39  inline bool replace(std::string& str, const std::string& from, const std::string& to)
40  {
41  const size_t start_pos = str.find(from);
42  if(start_pos == std::string::npos) return false;
43  str.replace(start_pos, from.length(), to);
44  return true;
45  }
46 
55  inline void camel_to(std::string& str, const std::string& sep=" ")
56  {
57  for(size_t i=1;i<str.length()-1;++i)
58  {
59  if(std::isupper(str[i]))
60  {
61  str.insert(i, sep);
62  ++i;
63  }
64  }
65  }
74  inline void camel_from(std::string& str, const char sep=' ')
75  {
76  for(size_t i=1;i<str.length()-1;)
77  {
78  if(str[i] == sep)
79  {
80  str.erase(str.begin() + i);
81  str[i] = static_cast<char>(::toupper(str[i]));
82  }
83  else ++i;
84  }
85  }
90  inline bool contains_ASCII_only (const std::string& input) {
91  for (size_t s = 0; s < input.size(); s++) {
92  if (static_cast<unsigned char>(input[s]) > 127) {
93  return false;
94  }
95  }
96  return true;
97  }
98  inline std::string to_lower(const std::string &input) {
99  std::string result(input);
100  std::transform(input.begin(), input.end(), result.begin(), detail::tolower);
101  return result;
102  }
103 
104  inline bool ends_with(const std::string &input, const std::string &ending, const bool is_case_insensitive=false) {
105  if (input.length() >= ending.length()) {
106  if (is_case_insensitive) {
107  std::string input_lower = illumina::interop::util::to_lower(input);
108  std::string ending_lower = illumina::interop::util::to_lower(ending);
109  return (input_lower.compare (input.length() - ending.length(), ending.length(), ending_lower) == 0);
110  }
111  return (input.compare (input.length() - ending.length(), ending.length(), ending) == 0);
112  } else {
113  return false;
114  }
115  }
117  bool operator()(const std::string &a, const std::string &b) const {
119  }
120  };
121 
122  //Function taken from https://gist.github.com/josephwb/df09e3a71679461fc104
123  inline std::istream& cross_platform_getline(std::istream &is, std::string& line) {
124  line.clear();
125  std::istream::sentry se(is, true);
126  std::streambuf* sb = is.rdbuf();
127  for (;;) {
128  int c = sb->sbumpc();
129  switch (c) {
130  case '\n':
131  return is;
132  case '\r':
133  if (sb->sgetc() == '\n') {
134  sb->sbumpc();
135  }
136  return is;
137  case EOF:
138  // Also handle the case when the last line has no line ending
139  if (line.empty()) {
140  is.setstate(std::ios::eofbit);
141  }
142  return is;
143  default:
144  line += (char)c;
145  }
146  }
147  }
148 
149 }}}
150 
151 
152 
std::istream & cross_platform_getline(std::istream &is, std::string &line)
Definition: string.h:123
Definition: enum_description.h:15
void camel_from(std::string &str, const char sep=' ')
Definition: string.h:74
void camel_to(std::string &str, const std::string &sep=" ")
Definition: string.h:55
bool ends_with(const std::string &input, const std::string &ending, const bool is_case_insensitive=false)
Definition: string.h:104
char tolower(const char ch)
Definition: string.h:25
bool contains_ASCII_only(const std::string &input)
Definition: string.h:90
bool replace(std::string &str, const std::string &from, const std::string &to)
Definition: string.h:39
bool operator()(const std::string &a, const std::string &b) const
Definition: string.h:117
std::string to_lower(const std::string &input)
Definition: string.h:98