string_buffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. string_buffer &operator<<(__VIC_LONGLONG n) { to_text_append(n, *this); return *this; }
  59. string_buffer &operator<<(unsigned __VIC_LONGLONG n) { to_text_append(n, *this); return *this; }
  60. string_buffer &operator<<(long double n) { to_text_append(n, *this); return *this; }
  61. string_buffer &operator<<(double n) { to_text_append(n, *this); return *this; }
  62. string_buffer &operator<<(float n) { to_text_append(n, *this); return *this; }
  63. string_buffer &operator<<(bool f) { to_text_append(f, *this); return *this; }
  64. string_buffer &operator<<(const void *p) { to_text_append(p, *this); return *this; }
  65. operator const char *() const { return c_str(); }
  66. #if __cplusplus < 201103L
  67. // a missing container interface of std::string in C++98
  68. reference front() { return *begin(); }
  69. reference back() { return *rbegin(); }
  70. const_reference front() const { return *begin(); }
  71. const_reference back() const { return *rbegin(); }
  72. void pop_back() { base::erase(length() - 1); }
  73. #endif
  74. // mapping for std::string operations
  75. string_buffer &operator=(char ch) { base::operator=(ch); return *this; }
  76. string_buffer &operator=(const char *st) { return assign(st); }
  77. string_buffer &operator=(const std::string &st) { return assign(st); }
  78. string_buffer &operator=(string_ref sr) { return assign(sr); }
  79. string_buffer &operator+=(char ch) { base::operator+=(ch); return *this; }
  80. string_buffer &operator+=(const char *st) { return append(st); }
  81. string_buffer &operator+=(const std::string &st) { return append(st); }
  82. string_buffer &operator+=(string_ref sr) { return append(sr); }
  83. string_buffer &assign(const char *st) { base::assign(st ? st : ""); return *this; }
  84. string_buffer &assign(const char *st, size_type n) { if(st) base::assign(st,n) ; return *this; }
  85. string_buffer &assign(const std::string &st) { base::assign(st); return *this; }
  86. string_buffer &assign(const std::string &st, size_type off, size_type n = npos) { base::assign(st,off,n); return *this; }
  87. string_buffer &assign(size_type n, char ch) { base::assign(n,ch); return *this; }
  88. template<class InputIterator>
  89. string_buffer &assign(InputIterator ib, InputIterator ie) { base::assign(ib,ie); return *this; }
  90. string_buffer &assign(string_ref sr) { base::assign(sr.begin(),sr.size()); return *this; }
  91. string_buffer &append(const char *st) { if(st) base::append(st); return *this; }
  92. string_buffer &append(const char *st, size_type n) { if(st) base::append(st,n); return *this; }
  93. string_buffer &append(const std::string &st) { base::append(st); return *this; }
  94. string_buffer &append(const std::string &st, size_type off, size_type n = npos) { base::append(st,off,n); return *this; }
  95. string_buffer &append(size_type n, char ch) { base::append(n,ch); return *this; }
  96. template<class InputIterator>
  97. string_buffer &append(InputIterator b, InputIterator e) { base::append(b,e); return *this; }
  98. string_buffer &append(string_ref sr) { base::append(sr.begin(),sr.size()); return *this; }
  99. using base::insert;
  100. string_buffer &insert(size_type pos, const char *st) { if(st) base::insert(pos,st); return *this; }
  101. string_buffer &insert(size_type pos, const char *st, size_type n) { if(st) base::insert(pos,st,n); return *this; }
  102. string_buffer &insert(size_type pos, const std::string &st) { base::insert(pos,st); return *this; }
  103. 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; }
  104. string_buffer &insert(size_type pos, size_type n, char ch) { base::insert(pos,n,ch); return *this; }
  105. using base::erase;
  106. string_buffer &erase(size_type pos=0, size_type n=npos) { base::erase(pos,n); return *this; }
  107. string_buffer &replace(size_type pos, size_type n, const char *st) { if(st) base::replace(pos,n,st); return *this; }
  108. 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; }
  109. string_buffer &replace(size_type pos, size_type n, const std::string &st) { base::replace(pos,n,st); return *this; }
  110. 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; }
  111. string_buffer &replace(size_type pos, size_type n, size_type count, char ch) { base::replace(pos,n,count,ch); return *this; }
  112. string_buffer &replace(const_iterator11 b, const_iterator11 e, const char *st) { if(st) base::replace(b,e,st); return *this; }
  113. string_buffer &replace(const_iterator11 b, const_iterator11 e, const std::string &st) { base::replace(b,e,st); return *this; }
  114. 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; }
  115. string_buffer &replace(const_iterator11 b, const_iterator11 e, size_type n, char ch) { base::replace(b,e,n,ch); return *this; }
  116. template<class InputIterator>
  117. string_buffer &replace(const_iterator11 f, const_iterator11 l, InputIterator b, InputIterator e) { base::replace(f,l,b,e); return *this; }
  118. string_buffer &reserve(size_type n) { base::reserve(n); return *this; }
  119. string_buffer &clear() { base::clear(); return *this; }
  120. };
  121. //////////////////////////////////////////////////////////////////////////////
  122. //----------------------------------------------------------------------------
  123. inline string_buffer operator+(const string_buffer &s1, const std::string &s2)
  124. {
  125. return string_buffer(s1.length() + s2.length()) << s1 << s2;
  126. }
  127. inline string_buffer operator+(const std::string &s1, const string_buffer &s2)
  128. {
  129. return string_buffer(s1.length() + s2.length()) << s1 << s2;
  130. }
  131. inline string_buffer operator+(const string_buffer &s1, const string_buffer &s2)
  132. {
  133. return s1 + static_cast<const std::string &>(s2);
  134. }
  135. inline string_buffer operator+(const string_buffer &s1, const char *s2)
  136. {
  137. size_t n = s2 ? std::strlen(s2) : 0;
  138. return string_buffer(s1.length() + n).append(s1).append(s2, n);
  139. }
  140. inline string_buffer operator+(const char *s1, const string_buffer &s2)
  141. {
  142. size_t n = s1 ? std::strlen(s1) : 0;
  143. return string_buffer(n + s2.length()).append(s1, n).append(s2);
  144. }
  145. inline string_buffer operator+(const string_buffer &s, char ch)
  146. {
  147. return string_buffer(s.length() + 1) << s << ch;
  148. }
  149. inline string_buffer operator+(char ch, const string_buffer &s)
  150. {
  151. return string_buffer(s.length() + 1) << ch << s;
  152. }
  153. #if __cpp_rvalue_references
  154. inline string_buffer operator+(string_buffer &&s1, const std::string &s2)
  155. {
  156. return std::move(s1 << s2);
  157. }
  158. inline string_buffer operator+(const std::string &s1, string_buffer &&s2)
  159. {
  160. return std::move(s2.insert(0, s1));
  161. }
  162. inline string_buffer operator+(string_buffer &&s1, const string_buffer &s2)
  163. {
  164. return std::move(s1) + static_cast<const std::string &>(s2);
  165. }
  166. inline string_buffer operator+(const string_buffer &s1, string_buffer &&s2)
  167. {
  168. return static_cast<const std::string &>(s1) + std::move(s2);
  169. }
  170. inline string_buffer operator+(string_buffer &&s1, string_buffer &&s2)
  171. {
  172. return std::move(s1 << s2);
  173. }
  174. inline string_buffer operator+(string_buffer &&s1, const char *s2)
  175. {
  176. return std::move(s1 << s2);
  177. }
  178. inline string_buffer operator+(const char *s1, string_buffer &&s2)
  179. {
  180. return std::move(s2.insert(0, s1));
  181. }
  182. inline string_buffer operator+(string_buffer &&s, char ch)
  183. {
  184. return std::move(s << ch);
  185. }
  186. inline string_buffer operator+(char ch, string_buffer &&s)
  187. {
  188. return std::move(s.insert(0, 1, ch));
  189. }
  190. #endif
  191. //----------------------------------------------------------------------------
  192. // Used primarly for creating messages with single expression
  193. typedef string_buffer msg;
  194. } // namespace
  195. #endif // header guard