string_buffer.h.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <chapter xml:id="string_buffer.h">
  2. <title><tt>__vic/string_buffer.h</tt></title>
  3. <chapter xml:id="string_buffer">
  4. <title><tt>string_buffer</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. class string_buffer : public std::string
  7. {
  8. public:
  9. string_buffer();
  10. explicit string_buffer(size_type n);
  11. string_buffer(const char *str);
  12. string_buffer(std::string str);
  13. string_buffer(string_ref sr);
  14. string_buffer(const std::string &str, size_type off, size_type n = npos);
  15. string_buffer(const char *char_buf, size_type n);
  16. string_buffer(const char *begin, const char *end);
  17. template<class InputIterator>
  18. string_buffer(InputIterator begin, InputIterator end);
  19. string_buffer &operator<<(const char *str);
  20. string_buffer &operator<<(const std::string &str);
  21. string_buffer &operator<<(string_ref sr);
  22. string_buffer &operator<<(char ch);
  23. string_buffer &operator<<(long long n);
  24. string_buffer &operator<<(long n);
  25. string_buffer &operator<<(int n);
  26. string_buffer &operator<<(short n);
  27. string_buffer &operator<<(signed char ch);
  28. string_buffer &operator<<(unsigned long long n);
  29. string_buffer &operator<<(unsigned long n);
  30. string_buffer &operator<<(unsigned n);
  31. string_buffer &operator<<(unsigned short n);
  32. string_buffer &operator<<(unsigned char ch);
  33. string_buffer &operator<<(long double n);
  34. string_buffer &operator<<(double n);
  35. string_buffer &operator<<(float n);
  36. string_buffer &operator<<(bool flag);
  37. string_buffer &operator<<(const void *p);
  38. string_buffer &operator=(string_ref sr);
  39. string_buffer &operator+=(string_ref sr);
  40. string_buffer &assign(string_ref sr);
  41. string_buffer &append(string_ref sr);
  42. // improved std::string calls
  43. string_buffer &assign(const std::string &str,
  44. size_type off, size_type n = npos);
  45. string_buffer &append(const std::string &str,
  46. size_type off, size_type n = npos);
  47. string_buffer &insert(size_type pos, const std::string &str,
  48. size_type off, size_type n = npos);
  49. string_buffer &reserve(size_type n);
  50. string_buffer &clear();
  51. // missing container interface of std::string
  52. reference front();
  53. reference back();
  54. const_reference front() const;
  55. const_reference back() const;
  56. void pop_back();
  57. operator const char *() const;
  58. };
  59. string_buffer operator+(const string_buffer &s1, const string_buffer &s2);
  60. string_buffer operator+(const string_buffer &s1, const std::string &s2);
  61. string_buffer operator+(const std::string &s1, const string_buffer &s2);
  62. string_buffer operator+(const string_buffer &s1, const char *s2);
  63. string_buffer operator+(const char *s1, const string_buffer &s2);
  64. string_buffer operator+(const string_buffer &s, char ch);
  65. string_buffer operator+(char ch, const string_buffer &s);
  66. template<class T>
  67. string_buffer &operator<<(string_buffer &&s, const T &v); // C++11
  68. using msg = string_buffer;
  69. ]]></code-block>
  70. <p>Class is an extended and improved <tt>std::string</tt>. It has following
  71. advantages:</p>
  72. <list style="numbered">
  73. <item>Left-associative append operator (<tt>&lt;&lt;</tt>) that allows
  74. constructions like this:
  75. <code-block lang="C++"><![CDATA[
  76. str << "Error message: " << err_msg << "\n";
  77. ]]></code-block></item>
  78. <item>Specific amount of bytes can be reserved using constructor as well as
  79. <tt>reserve()</tt> call. Reserving required amount of space in advance can
  80. improve performance significantly.
  81. <code-block lang="C++">
  82. __vic::string_buffer st(4096);
  83. // is same as
  84. std::string st;
  85. st.reserve(4096);
  86. </code-block></item>
  87. <item>Operator <tt>&lt;&lt;</tt> accepts all fundamental types: numbers, chars,
  88. pointers, <tt>bool</tt>:
  89. <code-block lang="C++"><![CDATA[
  90. for(int i=0; i<10; i++)
  91. str << "i = " << i << '\n';
  92. ]]></code-block></item>
  93. <item>All input <tt>nullptr</tt> values of <tt>const char *</tt> are treated as
  94. an empty string. Such values usually cause crash with <tt>std::string</tt>.
  95. <code-block lang="C++">
  96. std::string s1("Str");
  97. const char *p = nullptr;
  98. s1.append(p); // Oops.... Null pointer access!
  99. __vic::string_buffer s2("Str");
  100. s2.append(p); // Ok. s2 == "Str" still
  101. s2 = p; // Ok. s2.empty() == true
  102. </code-block></item>
  103. <item>Automatic conversion to <tt>const char *</tt> that allows usage of the
  104. object in context requires C-string, without explicit <tt>c_str()</tt> call.
  105. <code-block lang="C++">
  106. std::string fname(...);
  107. FILE *fp = fopen(fname.c_str(), "r");
  108. __vic::string_buffer fname(...);
  109. FILE *fp = fopen(fname, "r");)
  110. </code-block></item>
  111. <item>Some design irregularities of <tt>std::string</tt> are corrected. For
  112. instance <tt>std::string</tt> is a complete container but operations
  113. <tt>front()</tt> and <tt>back()</tt> are missed in C++98. There is
  114. <tt>push_back()</tt> but no <tt>pop_back()</tt>.</item>
  115. </list>
  116. <p>In spite of all these improvements, the interface of this class is completely
  117. backward compatible with <tt>std::string</tt>. Objects can be passed in contexts
  118. that require <tt>std::string</tt>. Class has no additional data-members.</p>
  119. <p>Using of inserter (operator <tt>&lt;&lt;</tt>) provides the easiest way to
  120. convert numbers to decimal string representation. Using of
  121. <tt>std::ostringstream</tt> for this purposes is more functional (you can
  122. specify radix, formatting, etc) but too tedious and not efficient in most
  123. cases.</p>
  124. <p>Additionally this type has a short synonym <tt>msg</tt>. It is very
  125. convenient for usage when one needs to construct a complex message with a
  126. single expression, without introducing unnecessary variables:</p>
  127. <code-block lang="C++"><![CDATA[
  128. oresult res = db_open(db_name);
  129. if(res != 0) throw __vic::exception(
  130. __vic::msg(64) << "Cannot open DB " << db_name << ". res = " << res
  131. );
  132. ]]></code-block>
  133. <p>As you can see, the maximum size of the message expected is specified in the
  134. constructor for optimization purposes.</p>
  135. <section><title>Class members</title>
  136. <synopsis>
  137. <prototype>string_buffer()</prototype>
  138. <p>Create an empty string.</p>
  139. <postcondition><tt>empty() == true</tt></postcondition>
  140. </synopsis>
  141. <synopsis>
  142. <prototype>explicit string_buffer(size_type n)</prototype>
  143. <p>Calls <tt>reserve(n)</tt>.</p>
  144. <postcondition><tt>capacity() >= n</tt></postcondition>
  145. </synopsis>
  146. <synopsis>
  147. <prototype>string_buffer(const char *str)</prototype>
  148. <prototype>string_buffer(std::string str)</prototype>
  149. <p>Creates the copy of <tt>str</tt>.</p>
  150. </synopsis>
  151. <synopsis>
  152. <prototype>string_buffer(const std::string &amp;str, size_type off, size_type n = npos)</prototype>
  153. <p>Creates the copy of <tt>str</tt> substring.</p>
  154. </synopsis>
  155. <synopsis>
  156. <prototype>string_buffer(const char *char_buf, size_type n)</prototype>
  157. <p>Creates string using buffer and its length.</p>
  158. </synopsis>
  159. <synopsis>
  160. <prototype>string_buffer(string_ref sr)</prototype>
  161. <prototype>string_buffer(const char *begin, const char *end)</prototype>
  162. <prototype>template&lt;class InputIterator>
  163. string_buffer(InputIterator begin, InputIterator end)</prototype>
  164. <p>Creates string from the chars range.</p>
  165. </synopsis>
  166. <synopsis>
  167. <prototype>string_buffer &amp;operator&lt;&lt;(const char *str)</prototype>
  168. <prototype>string_buffer &amp;operator&lt;&lt;(const std::string &amp;str)</prototype>
  169. <prototype>string_buffer &amp;operator&lt;&lt;(string_ref sr)</prototype>
  170. <prototype>string_buffer &amp;operator&lt;&lt;(char ch)</prototype>
  171. <p>Calls <tt>std::string::append()</tt>.</p>
  172. </synopsis>
  173. <synopsis>
  174. <prototype>string_buffer &amp;operator&lt;&lt;(long long n)</prototype>
  175. <prototype>string_buffer &amp;operator&lt;&lt;(long n)</prototype>
  176. <prototype>string_buffer &amp;operator&lt;&lt;(int n)</prototype>
  177. <prototype>string_buffer &amp;operator&lt;&lt;(short n)</prototype>
  178. <prototype>string_buffer &amp;operator&lt;&lt;(signed char ch)</prototype>
  179. <prototype>string_buffer &amp;operator&lt;&lt;(unsigned long long n)</prototype>
  180. <prototype>string_buffer &amp;operator&lt;&lt;(unsigned long n)</prototype>
  181. <prototype>string_buffer &amp;operator&lt;&lt;(unsigned n)</prototype>
  182. <prototype>string_buffer &amp;operator&lt;&lt;(unsigned short n)</prototype>
  183. <prototype>string_buffer &amp;operator&lt;&lt;(unsigned char ch)</prototype>
  184. <prototype>string_buffer &amp;operator&lt;&lt;(long double n)</prototype>
  185. <prototype>string_buffer &amp;operator&lt;&lt;(double n)</prototype>
  186. <prototype>string_buffer &amp;operator&lt;&lt;(float n)</prototype>
  187. <p>Appends decimal representaion of <tt>n</tt> to the string.</p>
  188. </synopsis>
  189. <synopsis>
  190. <prototype>string_buffer &amp;operator&lt;&lt;(const void *p)</prototype>
  191. <p>Appends pointers value in <tt>%p</tt> format of <tt>std::printf</tt>.</p>
  192. </synopsis>
  193. <synopsis>
  194. <prototype>string_buffer &amp;operator&lt;&lt;(bool flag)</prototype>
  195. <p>Appends <tt>1</tt> (for <tt>true</tt>) or <tt>0</tt> (for <tt>false</tt>)
  196. to the string.</p>
  197. </synopsis>
  198. <synopsis>
  199. <prototype>string_buffer &amp;operator=(string_ref sr)</prototype>
  200. <prototype>string_buffer &amp;operator+=(string_ref sr)</prototype>
  201. <prototype>string_buffer &amp;assign(string_ref sr)</prototype>
  202. <prototype>string_buffer &amp;append(string_ref sr)</prototype>
  203. <p>Operations for <tt>string_ref</tt>.</p>
  204. </synopsis>
  205. <synopsis>
  206. <prototype>string_buffer &amp;reserve(size_type n)</prototype>
  207. <prototype>string_buffer &amp;clear()</prototype>
  208. <p>Calls the corresponding <tt>std::string</tt> operation and additionally
  209. returns the reference to itself, so the call can be used in complex
  210. expressions.</p>
  211. </synopsis>
  212. <synopsis>
  213. <prototype>reference front()</prototype>
  214. <prototype>reference back()</prototype>
  215. <prototype>const_reference front() const</prototype>
  216. <prototype>const_reference back() const</prototype>
  217. <prototype>void pop_back()</prototype>
  218. <p>Missed container operations in the <tt>std::string</tt> interface in
  219. C++98.</p>
  220. </synopsis>
  221. <synopsis>
  222. <prototype>operator const char *() const</prototype>
  223. <p>Calls <tt>std::string::c_str()</tt>.</p>
  224. </synopsis>
  225. <synopsis>
  226. <prototype>string_buffer operator+(const string_buffer &amp;s1, const string_buffer &amp;s2)</prototype>
  227. <prototype>string_buffer operator+(const string_buffer &amp;s1, const std::string &amp;s2)</prototype>
  228. <prototype>string_buffer operator+(const std::string &amp;s1, const string_buffer &amp;s2)</prototype>
  229. <prototype>string_buffer operator+(const string_buffer &amp;s1, const char *s2)</prototype>
  230. <prototype>string_buffer operator+(const char *s1, const string_buffer &amp;s2)</prototype>
  231. <prototype>string_buffer operator+(const string_buffer &amp;s, char ch)</prototype>
  232. <prototype>string_buffer operator+(char ch, const string_buffer &amp;s)</prototype>
  233. <p>Concatenation of strings and characters.</p>
  234. </synopsis>
  235. <synopsis>
  236. <prototype><![CDATA[template<class T>
  237. string_buffer &operator<<(string_buffer &&s, const T &v)]]> <sign>C++11</sign></prototype>
  238. <p>Calls <tt>operator&lt;&lt;(string_buffer &amp;s, const T &amp;v)</tt>.</p>
  239. </synopsis>
  240. </section>
  241. </chapter>
  242. </chapter>