Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/string.hpp
3  *
4  * Some string helper functions
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2015 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
13 #pragma once
14 #ifndef THRILL_COMMON_STRING_HEADER
15 #define THRILL_COMMON_STRING_HEADER
16 
17 #include <tlx/define.hpp>
18 #include <tlx/unused.hpp>
19 
20 #include <array>
21 #include <cstdarg>
22 #include <cstdlib>
23 #include <limits>
24 #include <random>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 namespace thrill {
30 namespace common {
31 
32 /*!
33  * Helper for using sprintf to format into std::string and also to_string
34  * converters.
35  *
36  * \param max_size maximum length of output string, longer ones are truncated.
37  * \param fmt printf format and additional parameters
38  */
39 template <typename String = std::string>
40 String str_snprintf(size_t max_size, const char* fmt, ...)
42 
43 template <typename String>
44 String str_snprintf(size_t max_size, const char* fmt, ...) {
45  // allocate buffer on stack
46  char* s = static_cast<char*>(alloca(max_size));
47 
48  va_list args;
49  va_start(args, fmt);
50 
51  const int len = std::vsnprintf(s, max_size, fmt, args);
52 
53  va_end(args);
54 
55  return String(s, s + len);
56 }
57 
58 /*!
59  * Helper for using sprintf to format into std::string and also to_string
60  * converters.
61  *
62  * \param fmt printf format and additional parameters
63  */
64 template <typename String = std::string>
65 String str_sprintf(const char* fmt, ...)
66 TLX_ATTRIBUTE_FORMAT_PRINTF(1, 2);
67 
68 template <typename String>
69 String str_sprintf(const char* fmt, ...) {
70  // allocate buffer on stack
71  char* s = static_cast<char*>(alloca(256));
72 
73  va_list args;
74  va_start(args, fmt);
75 
76  int len = std::vsnprintf(s, 256, fmt, args); // NOLINT
77 
78  if (len >= 256) {
79  // try again.
80  s = static_cast<char*>(alloca(len + 1));
81 
82  len = std::vsnprintf(s, len + 1, fmt, args);
83  }
84 
85  va_end(args);
86 
87  return String(s, s + len);
88 }
89 
90 //! Use ostream to output any type as string. You generally DO NOT want to use
91 //! this, instead create a larger ostringstream.
92 template <typename Type>
93 static inline
94 std::string to_str(const Type& t) {
95  std::ostringstream oss;
96  oss << t;
97  return oss.str();
98 }
99 
100 /*!
101  * Template transformation function which uses std::istringstream to parse any
102  * istreamable type from a std::string. Returns true only if the whole string
103  * was parsed.
104  */
105 template <typename Type>
106 static inline
107 bool from_str(const std::string& str, Type& outval) {
108  std::istringstream is(str);
109  is >> outval;
110  return is.eof();
111 }
112 
113 /******************************************************************************/
114 //! Number parsing helpers, wraps strto{f,d,ld,l,ul,ll,ull}() via type switch.
115 
116 template <typename T>
117 T from_cstr(const char* nptr, char** endptr = nullptr, int base = 10);
118 
119 /*----------------------------------------------------------------------------*/
120 // specializations for floating point types
121 
122 // float
123 template <>
124 inline
125 float from_cstr<float>(const char* nptr, char** endptr, int) {
126  return std::strtof(nptr, endptr);
127 }
128 
129 // double
130 template <>
131 inline
132 double from_cstr<double>(const char* nptr, char** endptr, int) {
133  return std::strtod(nptr, endptr);
134 }
135 
136 // long double
137 template <>
138 inline
139 long double from_cstr<long double>(const char* nptr, char** endptr, int) {
140  return std::strtold(nptr, endptr);
141 }
142 
143 /*----------------------------------------------------------------------------*/
144 // specializations for integral types
145 
146 // long
147 template <>
148 inline
149 long from_cstr<long>(const char* nptr, char** endptr, int base) {
150  return std::strtol(nptr, endptr, base);
151 }
152 // unsigned long
153 template <>
154 inline
156  const char* nptr, char** endptr, int base) {
157  return std::strtoul(nptr, endptr, base);
158 }
159 
160 // long long
161 template <>
162 inline
163 long long from_cstr<long long>(const char* nptr, char** endptr, int base) {
164  return std::strtoll(nptr, endptr, base);
165 }
166 // unsigned long long
167 template <>
168 inline
169 unsigned long long from_cstr<unsigned long long>(
170  const char* nptr, char** endptr, int base) {
171  return std::strtoull(nptr, endptr, base);
172 }
173 
174 /******************************************************************************/
175 // Split and Join
176 
177 //! Logging helper to print arrays as [a1,a2,a3,...]
178 template <typename T, size_t N>
179 static std::string VecToStr(const std::array<T, N>& data) {
180  std::ostringstream oss;
181  oss << '[';
182  for (typename std::array<T, N>::const_iterator it = data.begin();
183  it != data.end(); ++it)
184  {
185  if (it != data.begin()) oss << ',';
186  oss << *it;
187  }
188  oss << ']';
189  return oss.str();
190 }
191 
192 //! Logging helper to print vectors as [a1,a2,a3,...]
193 template <typename T>
194 static std::string VecToStr(const std::vector<T>& data) {
195  std::ostringstream oss;
196  oss << '[';
197  for (typename std::vector<T>::const_iterator it = data.begin();
198  it != data.end(); ++it)
199  {
200  if (it != data.begin()) oss << ',';
201  oss << *it;
202  }
203  oss << ']';
204  return oss.str();
205 }
206 
207 /*!
208  * Generate a random string of given length. The set of available
209  * bytes/characters is given as the second argument. Each byte is equally
210  * probable. Uses the pseudo-random number generator from stdlib; take care to
211  * seed it using srand() before calling this function.
212  *
213  * \param size length of result
214  * \param rng Random number generator to use, e.g. std::default_random_engine.
215  * \param letters character set to choose from
216  * \return random string of given length
217  */
218 template <typename RandomEngine = std::default_random_engine>
219 static inline std::string
220 RandomString(std::string::size_type size, RandomEngine rng,
221  const std::string& letters
222  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") {
223  std::string out;
224  out.resize(size);
225 
226  std::uniform_int_distribution<size_t> distribution(0, letters.size() - 1);
227 
228  for (size_t i = 0; i < size; ++i)
229  out[i] = letters[distribution(rng)];
230 
231  return out;
232 }
233 
234 } // namespace common
235 } // namespace thrill
236 
237 #endif // !THRILL_COMMON_STRING_HEADER
238 
239 /******************************************************************************/
float from_cstr< float >(const char *nptr, char **endptr, int)
Definition: string.hpp:125
double T
unsigned long long from_cstr< unsigned long long >(const char *nptr, char **endptr, int base)
Definition: string.hpp:169
Type
VFS object type.
Definition: file_io.hpp:52
long long from_cstr< long long >(const char *nptr, char **endptr, int base)
Definition: string.hpp:163
T from_cstr(const char *nptr, char **endptr=nullptr, int base=10)
Number parsing helpers, wraps strto{f,d,ld,l,ul,ll,ull}() via type switch.
String str_sprintf(const char *fmt,...) TLX_ATTRIBUTE_FORMAT_PRINTF(1
Helper for using sprintf to format into std::string and also to_string converters.
long from_cstr< long >(const char *nptr, char **endptr, int base)
Definition: string.hpp:149
#define TLX_ATTRIBUTE_FORMAT_PRINTF(X, Y)
static std::string VecToStr(const std::array< T, N > &data)
Logging helper to print arrays as [a1,a2,a3,...].
Definition: string.hpp:179
double from_cstr< double >(const char *nptr, char **endptr, int)
Definition: string.hpp:132
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
unsigned long from_cstr< unsigned long >(const char *nptr, char **endptr, int base)
Definition: string.hpp:155
static std::string to_str(const Type &t)
Definition: string.hpp:94
static std::string RandomString(std::string::size_type size, RandomEngine rng, const std::string &letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
Generate a random string of given length.
Definition: string.hpp:220
static bool from_str(const std::string &str, Type &outval)
Template transformation function which uses std::istringstream to parse any istreamable type from a s...
Definition: string.hpp:107
long double from_cstr< long double >(const char *nptr, char **endptr, int)
Definition: string.hpp:139
String str_snprintf(size_t max_size, const char *fmt,...) TLX_ATTRIBUTE_FORMAT_PRINTF(2
Helper for using sprintf to format into std::string and also to_string converters.