Thrill  0.1
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 //! Use ostream to output any type as string. You generally DO NOT want to use
33 //! this, instead create a larger ostringstream.
34 template <typename Type>
35 static inline
36 std::string to_str(const Type& t) {
37  std::ostringstream oss;
38  oss << t;
39  return oss.str();
40 }
41 
42 /*!
43  * Template transformation function which uses std::istringstream to parse any
44  * istreamable type from a std::string. Returns true only if the whole string
45  * was parsed.
46  */
47 template <typename Type>
48 static inline
49 bool from_str(const std::string& str, Type& outval) {
50  std::istringstream is(str);
51  is >> outval;
52  return is.eof();
53 }
54 
55 /******************************************************************************/
56 //! Number parsing helpers, wraps strto{f,d,ld,l,ul,ll,ull}() via type switch.
57 
58 template <typename T>
59 T from_cstr(const char* nptr, char** endptr = nullptr, int base = 10);
60 
61 /*----------------------------------------------------------------------------*/
62 // specializations for floating point types
63 
64 // float
65 template <>
66 inline
67 float from_cstr<float>(const char* nptr, char** endptr, int) {
68  return std::strtof(nptr, endptr);
69 }
70 
71 // double
72 template <>
73 inline
74 double from_cstr<double>(const char* nptr, char** endptr, int) {
75  return std::strtod(nptr, endptr);
76 }
77 
78 // long double
79 template <>
80 inline
81 long double from_cstr<long double>(const char* nptr, char** endptr, int) {
82  return std::strtold(nptr, endptr);
83 }
84 
85 /*----------------------------------------------------------------------------*/
86 // specializations for integral types
87 
88 // long
89 template <>
90 inline
91 long from_cstr<long>(const char* nptr, char** endptr, int base) {
92  return std::strtol(nptr, endptr, base);
93 }
94 // unsigned long
95 template <>
96 inline
98  const char* nptr, char** endptr, int base) {
99  return std::strtoul(nptr, endptr, base);
100 }
101 
102 // long long
103 template <>
104 inline
105 long long from_cstr<long long>(const char* nptr, char** endptr, int base) {
106  return std::strtoll(nptr, endptr, base);
107 }
108 // unsigned long long
109 template <>
110 inline
111 unsigned long long from_cstr<unsigned long long>(
112  const char* nptr, char** endptr, int base) {
113  return std::strtoull(nptr, endptr, base);
114 }
115 
116 /******************************************************************************/
117 // Split and Join
118 
119 //! Logging helper to print arrays as [a1,a2,a3,...]
120 template <typename T, size_t N>
121 static std::string VecToStr(const std::array<T, N>& data) {
122  std::ostringstream oss;
123  oss << '[';
124  for (typename std::array<T, N>::const_iterator it = data.begin();
125  it != data.end(); ++it)
126  {
127  if (it != data.begin()) oss << ',';
128  oss << *it;
129  }
130  oss << ']';
131  return oss.str();
132 }
133 
134 //! Logging helper to print vectors as [a1,a2,a3,...]
135 template <typename T>
136 static std::string VecToStr(const std::vector<T>& data) {
137  std::ostringstream oss;
138  oss << '[';
139  for (typename std::vector<T>::const_iterator it = data.begin();
140  it != data.end(); ++it)
141  {
142  if (it != data.begin()) oss << ',';
143  oss << *it;
144  }
145  oss << ']';
146  return oss.str();
147 }
148 
149 /*!
150  * Generate a random string of given length. The set of available
151  * bytes/characters is given as the second argument. Each byte is equally
152  * probable. Uses the pseudo-random number generator from stdlib; take care to
153  * seed it using srand() before calling this function.
154  *
155  * \param size length of result
156  * \param rng Random number generator to use, e.g. std::default_random_engine.
157  * \param letters character set to choose from
158  * \return random string of given length
159  */
160 template <typename RandomEngine = std::default_random_engine>
161 static inline std::string
162 RandomString(std::string::size_type size, RandomEngine rng,
163  const std::string& letters
164  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") {
165  std::string out;
166  out.resize(size);
167 
168  std::uniform_int_distribution<size_t> distribution(0, letters.size() - 1);
169 
170  for (size_t i = 0; i < size; ++i)
171  out[i] = letters[distribution(rng)];
172 
173  return out;
174 }
175 
176 } // namespace common
177 } // namespace thrill
178 
179 #endif // !THRILL_COMMON_STRING_HEADER
180 
181 /******************************************************************************/
float from_cstr< float >(const char *nptr, char **endptr, int)
Definition: string.hpp:67
double T
unsigned long long from_cstr< unsigned long long >(const char *nptr, char **endptr, int base)
Definition: string.hpp:111
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:105
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.
long from_cstr< long >(const char *nptr, char **endptr, int base)
Definition: string.hpp:91
static std::string VecToStr(const std::array< T, N > &data)
Logging helper to print arrays as [a1,a2,a3,...].
Definition: string.hpp:121
double from_cstr< double >(const char *nptr, char **endptr, int)
Definition: string.hpp:74
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:97
static std::string to_str(const Type &t)
Definition: string.hpp:36
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:162
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:49
long double from_cstr< long double >(const char *nptr, char **endptr, int)
Definition: string.hpp:81