string_buffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Extended std::string, which has left-associative append
  2. // operator (<<) and automatic conversion to c_str()
  3. //
  4. // Platform: ISO C++ 98/11
  5. // $Id$
  6. //
  7. // (c) __vic 2007
  8. #ifndef __VIC_STRING_BUFFER_H
  9. #define __VIC_STRING_BUFFER_H
  10. #include<__vic/defs.h>
  11. #include<__vic/to_text.h>
  12. #include<__vic/string_ref.h>
  13. #include<string>
  14. #include<cstring>
  15. namespace __vic {
  16. //////////////////////////////////////////////////////////////////////////////
  17. // Extended std::string, which has left-associative append
  18. // operator (<<) and automatic conversion to c_str()
  19. class string_buffer : public std::string
  20. {
  21. typedef std::string base;
  22. typedef
  23. #if __cplusplus < 201103L || \
  24. defined(__GLIBCXX__) && _GLIBCXX_USE_CXX11_ABI != 1
  25. iterator
  26. #else
  27. const_iterator
  28. #endif
  29. const_iterator11;
  30. public:
  31. string_buffer() __VIC_DEFAULT_CTR
  32. explicit string_buffer(size_type n) { base::reserve(n); }
  33. string_buffer(const char *st) : base(st ? st : "") {}
  34. #if __cpp_rvalue_references
  35. string_buffer(std::string st) : base(std::move(st)) {}
  36. #else
  37. string_buffer(const std::string &st) : base(st) {}
  38. #endif
  39. string_buffer(const std::string &st, size_type off,
  40. size_type n = npos) : base(st, off, n) {}
  41. string_buffer(string_ref sr) : base(sr.begin(), sr.size()) {}
  42. string_buffer(const char *st, size_type n) : base(st, n) {}
  43. string_buffer(const char *b, const char *e) : base(b, e - b) {}
  44. template<class InputIterator>
  45. string_buffer(InputIterator b, InputIterator e) : base(b, e) {}
  46. string_buffer &operator<<(const char *st) { return append(st); }
  47. string_buffer &operator<<(const std::string &st) { return append(st); }
  48. string_buffer &operator<<(string_ref sr) { return append(sr); }
  49. string_buffer &operator<<(char ch) { return *this += ch; }
  50. string_buffer &operator<<(long n) { to_text_append(n, *this); return *this; }
  51. string_buffer &operator<<(int n) { to_text_append(n, *this); return *this; }
  52. string_buffer &operator<<(short n) { to_text_append(n, *this); return *this; }
  53. string_buffer &operator<<(signed char n) { to_text_append(n, *this); return *this; }
  54. string_buffer &operator<<(unsigned long n) { to_text_append(n, *this); return *this; }
  55. string_buffer &operator<<(unsigned n) { to_text_append(n, *this); return *this; }
  56. string_buffer &operator<<(unsigned short n) { to_text_append(n, *this); return *this; }
  57. string_buffer &operator<<(unsigned char n) { to_text_append(n, *this); return *this; }
  58. #ifdef __VIC_LONGLONG
  59. string_buffer &operator<<(__VIC_LONGLONG n) { to_text_append(n, *this); return *this; }
  60. string_buffer &operator<<(unsigned __VIC_LONGLONG n) { to_text_append(n, *this); return *this; }
  61. #endif
  62. string_buffer &operator<<(long double n) { to_text_append(n, *this); return *this; }
  63. string_buffer &operator<<(double n) { to_text_append(n, *this); return *this; }
  64. string_buffer &operator<<(float n) { to_text_append(n, *this); return *this; }
  65. string_buffer &operator<<(bool f) { to_text_append(f, *this); return *this; }
  66. string_buffer &operator<<(const void *p) { to_text_append(p, *this); return *this; }
  67. operator const char *() const { return c_str(); }
  68. #if __cplusplus < 201103L
  69. // a missing container interface of std::string in C++98
  70. reference front() { return *begin(); }
  71. reference back() { return *rbegin(); }
  72. const_reference front() const { return *begin(); }
  73. const_reference back() const { return *rbegin(); }
  74. void pop_back() { base::erase(length() - 1); }
  75. #endif
  76. // mapping for std::string operations
  77. string_buffer &operator=(char ch) { base::operator=(ch); return *this; }
  78. string_buffer &operator=(const char *st) { return assign(st); }
  79. string_buffer &operator=(const std::string &st) { return assign(st); }
  80. string_buffer &operator=(string_ref sr) { return assign(sr); }
  81. string_buffer &operator+=(char ch) { base::operator+=(ch); return *this; }
  82. string_buffer &operator+=(const char *st) { return append(st); }
  83. string_buffer &operator+=(const std::string &st) { return append(st); }
  84. string_buffer &operator+=(string_ref sr) { return append(sr); }
  85. string_buffer &assign(const char *st) { base::assign(st ? st : ""); return *this; }
  86. string_buffer &assign(const char *st, size_type n) { if(st) base::assign(st,n) ; return *this; }
  87. string_buffer &assign(const std::string &st) { base::assign(st); return *this; }
  88. string_buffer &assign(const std::string &st, size_type off, size_type n = npos) { base::assign(st,off,n); return *this; }
  89. string_buffer &assign(size_type n, char ch) { base::assign(n,ch); return *this; }
  90. template<class InputIterator>
  91. string_buffer &assign(InputIterator ib, InputIterator ie) { base::assign(ib,ie); return *this; }
  92. string_buffer &assign(string_ref sr) { base::assign(sr.begin(),sr.size()); return *this; }
  93. string_buffer &append(const char *st) { if(st) base::append(st); return *this; }
  94. string_buffer &append(const char *st, size_type n) { if(st) base::append(st,n); return *this; }
  95. string_buffer &append(const std::string &st) { base::append(st); return *this; }
  96. string_buffer &append(const std::string &st, size_type off, size_type n = npos) { base::append(st,off,n); return *this; }
  97. string_buffer &append(size_type n, char ch) { base::append(n,ch); return *this; }
  98. template<class InputIterator>
  99. string_buffer &append(InputIterator b, InputIterator e) { base::append(b,e); return *this; }
  100. string_buffer &append(string_ref sr) { base::append(sr.begin(),sr.size()); return *this; }
  101. using base::insert;
  102. string_buffer &insert(size_type pos, const char *st) { if(st) base::insert(pos,st); return *this; }
  103. string_buffer &insert(size_type pos, const char *st, size_type n) { if(st) base::insert(pos,st,n); return *this; }
  104. string_buffer &insert(size_type pos, const std::string &st) { base::insert(pos,st); return *this; }
  105. string_buffer &insert(size_type pos, const std::string &st, size_type off, size_type n = npos) { base::insert(pos,st,off,n); return *this; }
  106. string_buffer &insert(size_type pos, size_type n, char ch) { base::insert(pos,n,ch); return *this; }
  107. using base::erase;
  108. string_buffer &erase(size_type pos=0, size_type n=npos) { base::erase(pos,n); return *this; }
  109. string_buffer &replace(size_type pos, size_type n, const char *st) { if(st) base::replace(pos,n,st); return *this; }
  110. string_buffer &replace(size_type pos, size_type n1, const char *st, size_type n2) { if(st) base::replace(pos,n1,st,n2); return *this; }
  111. string_buffer &replace(size_type pos, size_type n, const std::string &st) { base::replace(pos,n,st); return *this; }
  112. string_buffer &replace(size_type pos1, size_type n1, const std::string &st, size_type pos2, size_type n2) { base::replace(pos1,n1,st,pos2,n2); return *this; }
  113. string_buffer &replace(size_type pos, size_type n, size_type count, char ch) { base::replace(pos,n,count,ch); return *this; }
  114. string_buffer &replace(const_iterator11 b, const_iterator11 e, const char *st) { if(st) base::replace(b,e,st); return *this; }
  115. string_buffer &replace(const_iterator11 b, const_iterator11 e, const std::string &st) { base::replace(b,e,st); return *this; }
  116. string_buffer &replace(const_iterator11 b, const_iterator11 e, const char *st, size_type n) { if(st) base::replace(b,e,st,n); return *this; }
  117. string_buffer &replace(const_iterator11 b, const_iterator11 e, size_type n, char ch) { base::replace(b,e,n,ch); return *this; }
  118. template<class InputIterator>
  119. string_buffer &replace(const_iterator11 f, const_iterator11 l, InputIterator b, InputIterator e) { base::replace(f,l,b,e); return *this; }
  120. string_buffer &reserve(size_type n) { base::reserve(n); return *this; }
  121. string_buffer &clear() { base::clear(); return *this; }
  122. };
  123. //////////////////////////////////////////////////////////////////////////////
  124. //----------------------------------------------------------------------------
  125. inline string_buffer operator+(const string_buffer &s1, const std::string &s2)
  126. {
  127. return string_buffer(s1.length() + s2.length()) << s1 << s2;
  128. }
  129. inline string_buffer operator+(const std::string &s1, const string_buffer &s2)
  130. {
  131. return string_buffer(s1.length() + s2.length()) << s1 << s2;
  132. }
  133. inline string_buffer operator+(const string_buffer &s1, const string_buffer &s2)
  134. {
  135. return s1 + static_cast<const std::string &>(s2);
  136. }
  137. inline string_buffer operator+(const string_buffer &s1, const char *s2)
  138. {
  139. size_t n = s2 ? std::strlen(s2) : 0;
  140. return string_buffer(s1.length() + n).append(s1).append(s2, n);
  141. }
  142. inline string_buffer operator+(const char *s1, const string_buffer &s2)
  143. {
  144. size_t n = s1 ? std::strlen(s1) : 0;
  145. return string_buffer(n + s2.length()).append(s1, n).append(s2);
  146. }
  147. inline string_buffer operator+(const string_buffer &s, char ch)
  148. {
  149. return string_buffer(s.length() + 1) << s << ch;
  150. }
  151. inline string_buffer operator+(char ch, const string_buffer &s)
  152. {
  153. return string_buffer(s.length() + 1) << ch << s;
  154. }
  155. #if __cpp_rvalue_references
  156. inline string_buffer operator+(string_buffer &&s1, const std::string &s2)
  157. {
  158. return std::move(s1 << s2);
  159. }
  160. inline string_buffer operator+(const std::string &s1, string_buffer &&s2)
  161. {
  162. return std::move(s2.insert(0, s1));
  163. }
  164. inline string_buffer operator+(string_buffer &&s1, const string_buffer &s2)
  165. {
  166. return std::move(s1) + static_cast<const std::string &>(s2);
  167. }
  168. inline string_buffer operator+(const string_buffer &s1, string_buffer &&s2)
  169. {
  170. return static_cast<const std::string &>(s1) + std::move(s2);
  171. }
  172. inline string_buffer operator+(string_buffer &&s1, string_buffer &&s2)
  173. {
  174. return std::move(s1 << s2);
  175. }
  176. inline string_buffer operator+(string_buffer &&s1, const char *s2)
  177. {
  178. return std::move(s1 << s2);
  179. }
  180. inline string_buffer operator+(const char *s1, string_buffer &&s2)
  181. {
  182. return std::move(s2.insert(0, s1));
  183. }
  184. inline string_buffer operator+(string_buffer &&s, char ch)
  185. {
  186. return std::move(s << ch);
  187. }
  188. inline string_buffer operator+(char ch, string_buffer &&s)
  189. {
  190. return std::move(s.insert(0, 1, ch));
  191. }
  192. //----------------------------------------------------------------------------
  193. template<class T>
  194. inline string_buffer &operator<<(string_buffer &&s, const T &v)
  195. {
  196. s << v;
  197. return s;
  198. }
  199. //----------------------------------------------------------------------------
  200. #endif
  201. // Used primarly for creating messages with single expression
  202. typedef string_buffer msg;
  203. } // namespace
  204. #endif // header guard