0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
String.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
24 #ifndef HYPERTABLE_STRING_H
25 #define HYPERTABLE_STRING_H
26 
27 #include <string>
28 #include <sstream>
29 #include <limits>
30 
31 namespace Hypertable {
32 
44  typedef std::string String;
45 
47  typedef long unsigned int Lu;
48 
50  typedef long long unsigned int Llu;
51 
53  typedef long long int Lld;
54 
65  String format(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
66 
75  String format_number(int64_t n, int sep = ',');
76 
87  String format_bytes(size_t n, const void *buf, size_t len,
88  const char *trailer = "...");
89 
98  template <class SequenceT>
99  String format_list(const SequenceT &seq, const char *sep = ", ") {
100  typedef typename SequenceT::const_iterator Iterator;
101  Iterator it = seq.begin(), end = seq.end();
102  std::ostringstream out;
103  out << '[';
104 
105  if (it != end) {
106  out << *it;
107  ++it;
108  }
109  for (; it != end; ++it)
110  out << sep << *it;
111 
112  out << ']';
113  return out.str();
114  }
115 
129  inline bool strip_enclosing_quotes(const char *input, size_t input_len,
130  const char **output, size_t *output_len) {
131  if (input_len < 2 ||
132  !((*input == '\'' && input[input_len-1] == '\'') ||
133  (*input == '"' && input[input_len-1] == '"'))) {
134  *output = input;
135  *output_len = input_len;
136  return false;
137  }
138  *output = input + 1;
139  *output_len = input_len - 2;
140  return true;
141  }
142 
145  protected:
146  static const char DIGITS[];
147  };
148 
149  template<class T>
151  public:
152 
156  size_t size() const { return buf - s + BUFFER_SIZE - 1; }
157 
162  const char *data() const { return str; }
163 
168  const char *c_str() const {
169  return s;
170  }
171 
175  std::string str() const { return std::string(s, size()); }
176 
180  char* append_to(char* p) const {
181  memcpy(p, s, size());
182  return p + size();
183  }
184 
185  protected:
186 
188  buf[BUFFER_SIZE - 1] = '\0';
189  }
190  void format_unsigned(T value) {
191  s = buf + BUFFER_SIZE - 1;
192  while (value >= 100) {
193  // Integer division is slow so do it for a group of two digits instead
194  // of for every digit. The idea comes from the talk by Alexandrescu
195  // "Three Optimization Tips for C++". See speed-test for a comparison.
196  unsigned index = (value % 100) * 2;
197  value /= 100;
198  *--s = DIGITS[index + 1];
199  *--s = DIGITS[index];
200  }
201  if (value < 10) {
202  *--s = static_cast<char>('0' + value);
203  return;
204  }
205  unsigned index = static_cast<unsigned>(value * 2);
206  *--s = DIGITS[index + 1];
207  *--s = DIGITS[index];
208  }
209 
210  void format_signed(T value) {
211  if (value >= 0)
212  format_unsigned(value);
213  else {
214  format_unsigned(-value);
215  *--s = '-';
216  }
217  }
218 
219  private:
220  enum { BUFFER_SIZE = std::numeric_limits<T>::digits10 + 3 };
222  char* s;
223  };
224 
225  template<class T>
227  public:
228  explicit NumericSignedFormatter(T value) {
230  }
231  };
232 
233  template<class T>
235  public:
236  explicit NumericUnsignedFormatter(T value) {
238  }
239  };
240 
245 
250 
253 } // namespace Hypertable
254 
255 #endif // HYPERTABLE_STRING_H
NumericSignedFormatter< int8_t > Int8Formatter
Definition: String.h:246
NumericUnsignedFormatter< uint32_t > UInt32Formatter
Definition: String.h:243
String format_number(int64_t n, int sep)
Return decimal number string separated by a separator (default: comma) for every 3 digits...
Definition: String.cc:73
NumericSignedFormatter< int16_t > Int16Formatter
Definition: String.h:247
NumericSignedFormatter< int32_t > Int32Formatter
Definition: String.h:248
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
String format_list(const SequenceT &seq, const char *sep=", ")
Return a string presentation of a sequence.
Definition: String.h:99
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
const char * data() const
Returns a pointer to the output buffer content.
Definition: String.h:162
String format_bytes(size_t n, const void *buf, size_t len, const char *trailer)
Return first n bytes of buffer with an optional trailer if the size of the buffer exceeds n...
Definition: String.cc:103
The fast numeric formatters, very much inspired from http://cppformat.github.io/. ...
Definition: String.h:144
NumericUnsignedFormatter< uint16_t > UInt16Formatter
Definition: String.h:242
char buf[BUFFER_SIZE]
Definition: String.h:221
bool strip_enclosing_quotes(const char *input, size_t input_len, const char **output, size_t *output_len)
Strips enclosing quotes.
Definition: String.h:129
static const char DIGITS[]
Definition: String.h:146
void format_unsigned(T value)
Definition: String.h:190
std::string str() const
Returns the content of the output buffer as an std::string.
Definition: String.h:175
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
NumericSignedFormatter< int64_t > Int64Formatter
Definition: String.h:249
NumericUnsignedFormatter< uint64_t > UInt64Formatter
Definition: String.h:244
void format_signed(T value)
Definition: String.h:210
long unsigned int Lu
Shortcut for printf formats.
Definition: String.h:47
char * append_to(char *p) const
Appends the converted number to the buffer specified, returns the forwarded pointer.
Definition: String.h:180
size_t size() const
Returns the number of characters written to the output buffer.
Definition: String.h:156
#define __attribute__(x)
Definition: compat-c.h:63
const char * c_str() const
Returns a pointer to the output buffer content with terminating null character appended.
Definition: String.h:168
NumericUnsignedFormatter< uint8_t > UInt8Formatter
Definition: String.h:241