string_buffer.h.xml 10 KB

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