string_ref.h.xml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <chapter xml:id="string_ref.h">
  2. <title><tt>__vic/string_ref.h</tt></title>
  3. <chapter xml:id="string_ref">
  4. <title><tt>string_ref</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. template<class charT>
  7. class basic_string_ref
  8. {
  9. public:
  10. using value_type = charT;
  11. using iterator = const value_type *;
  12. using const_iterator = iterator;
  13. // Constructors
  14. basic_string_ref();
  15. basic_string_ref(const charT *str);
  16. basic_string_ref(const charT *chars, size_t n);
  17. basic_string_ref(const charT *begin, const charT *end);
  18. template<class Traits, class Alloc>
  19. explicit basic_string_ref(
  20. const std::basic_string<charT,Traits,Alloc> &str);
  21. basic_string_ref(
  22. typename std::basic_string<charT>::const_iterator begin,
  23. typename std::basic_string<charT>::const_iterator end);
  24. // BEGIN C++11
  25. basic_string_ref(std::initializer_list<charT> );
  26. // END C++11
  27. #if __cpp_lib_string_view // since C++17
  28. template<class Traits>
  29. basic_string_ref(std::basic_string_view<charT,Traits> s);
  30. operator std::basic_string_view<charT>() const;
  31. #endif
  32. // Accessors
  33. iterator begin() const;
  34. iterator end() const;
  35. iterator cbegin() const;
  36. iterator cend() const;
  37. charT front() const;
  38. charT back() const;
  39. charT operator[](size_t i) const;
  40. const charT *data() const;
  41. bool empty() const;
  42. size_t size() const;
  43. size_t length() const;
  44. int compare(basic_string_ref s) const;
  45. // Converters
  46. std::basic_string<charT> str() const;
  47. template<class Traits>
  48. std::basic_string<charT,Traits> str() const;
  49. template<class Traits, class Alloc>
  50. std::basic_string<charT,Traits,Alloc> str(const Alloc &a = Alloc())const;
  51. operator std::basic_string<charT>() const;
  52. };
  53. using string_ref = basic_string_ref<char> ;
  54. template<class charT>
  55. bool operator==(basic_string_ref<charT> s1, basic_string_ref<charT> s2);
  56. template<class charT>
  57. bool operator!=(basic_string_ref<charT> s1, basic_string_ref<charT> s2);
  58. template<class charT>
  59. bool operator<(basic_string_ref<charT> s1, basic_string_ref<charT> s2);
  60. template<class charT>
  61. bool operator>(basic_string_ref<charT> s1, basic_string_ref<charT> s2);
  62. template<class charT>
  63. bool operator<=(basic_string_ref<charT> s1, basic_string_ref<charT> s2);
  64. template<class charT>
  65. bool operator>=(basic_string_ref<charT> s1, basic_string_ref<charT> s2);
  66. #ifdef __VIC_DEFINE_OSTREAM_INSERTERS
  67. template<class charT, class Traits>
  68. std::basic_ostream<charT,Traits> &operator<<(
  69. std::basic_ostream<charT,Traits> &os, const basic_string_ref<charT> &sr);
  70. #endif
  71. ]]></code-block>
  72. <p>The class represents a reference to the read-only continuous range of
  73. characters. When used as a return value, it is significantly more lightweight
  74. than <tt>std::string</tt>, because no string copy or memory allocation
  75. performed. But, when <tt>std::string</tt> is required, automatic conversion
  76. happens. Let's consider the example:</p>
  77. <code-block lang="C++">
  78. class C
  79. {
  80. std::string v;
  81. public:
  82. std::string get_v_1() const { return v; }
  83. __vic::string_ref get_v_2() const { return v; }
  84. };
  85. </code-block>
  86. <p>As you can see, class contains one string field. Two read-only
  87. access-functions are defined. The first as usual returns <tt>std::string</tt>,
  88. the second - <tt>string_ref</tt>. When the first is used, temporay string is
  89. created every time. When the second is used, just reference is returned.</p>
  90. <p>Another use case - input read-only string argument. The class is a drop-in
  91. replacement for <tt>const std::string &amp;</tt>. In most cases, it can also
  92. be used instead of <tt>const char *</tt>. The overhead in this case is an
  93. additional scan of the string to find the NULL-terminator, which is nothing in
  94. cases when we need the string end or length anyway. Let's consider 3 sets of
  95. overloaded functions:</p>
  96. <code-block lang="C++"><![CDATA[
  97. void f1(const std::string & );
  98. void f2(const std::string & );
  99. void f2(const char * );
  100. void f3(string_ref );
  101. ]]></code-block>
  102. <p>Each of them can be used as</p>
  103. <code-block lang="C++">
  104. fx("Nul-terminated string");
  105. </code-block>
  106. <p>as well as</p>
  107. <code-block lang="C++">
  108. fx(std::string("std::string"));
  109. </code-block>
  110. <p>But with <tt>f1()</tt> we will have redundant string copying in the first
  111. case, just to read the value. With <tt>f2()</tt> several overloads are
  112. required. And while it isn't a big issue when function has single argument,
  113. with two or more string arguments it quickly becomes very tedious. The last
  114. alternative - <tt>f3()</tt> - is at the same time as short and universal as
  115. <tt>f1()</tt> and "friendlier" to the string literals and strings from the
  116. C-world - they are not copied to the heap and not converted to
  117. <tt>std::string</tt>.</p>
  118. <section><title>Class members</title>
  119. <synopsis>
  120. <prototype>basic_string_ref()</prototype>
  121. <postcondition><tt>empty() == true</tt></postcondition>
  122. </synopsis>
  123. <synopsis>
  124. <prototype>basic_string_ref(const charT *str)</prototype>
  125. <prototype>template&lt;class Traits, class Alloc>
  126. basic_string_ref(const std::basic_string&lt;charT,Traits,Alloc> &amp;str)</prototype>
  127. <p>Creates reference to <tt>str</tt>.</p>
  128. <postcondition><tt>*this == str</tt></postcondition>
  129. </synopsis>
  130. <synopsis>
  131. <prototype>basic_string_ref(const charT *chars, size_t n)</prototype>
  132. <prototype>basic_string_ref(const charT *begin, const charT *end)</prototype>
  133. <prototype>basic_string_ref(const charT *chars, size_t n)</prototype>
  134. <prototype>basic_string_ref(
  135. typename std::basic_string&lt;charT>::const_iterator begin,
  136. typename std::basic_string&lt;charT>::const_iterator end)</prototype>
  137. <prototype>basic_string_ref(std::initializer_list&lt;charT> ) <sign>C++11</sign></prototype>
  138. <p>Create reference to the range of the characters.</p>
  139. </synopsis>
  140. <synopsis>
  141. <prototype>template&lt;class Traits>
  142. basic_string_ref(std::basic_string_view&lt;charT,Traits> s) <sign>C++17</sign></prototype>
  143. <prototype>operator std::basic_string_view&lt;charT>() const <sign>C++17</sign></prototype>
  144. <p>Converters from/to <tt>std::basic_string_view</tt>.</p>
  145. </synopsis>
  146. <synopsis>
  147. <prototype>iterator begin() const</prototype>
  148. <prototype>iterator cbegin() const</prototype>
  149. <prototype>const charT *data() const</prototype>
  150. <p>Begin of the range of the characters.</p>
  151. </synopsis>
  152. <synopsis>
  153. <prototype>iterator end() const</prototype>
  154. <prototype>iterator cend() const</prototype>
  155. <p>End of the range of the characters.</p>
  156. </synopsis>
  157. <synopsis>
  158. <prototype>charT front() const</prototype>
  159. <prototype>charT back() const</prototype>
  160. <p>The first and the last character of the string correspondingly.</p>
  161. <precondition><tt>!empty()</tt></precondition>
  162. </synopsis>
  163. <synopsis>
  164. <prototype>charT operator[](size_t i) const</prototype>
  165. <p><tt>i</tt>-th character of the string.</p>
  166. <precondition><tt>i &lt; length()</tt></precondition>
  167. </synopsis>
  168. <synopsis>
  169. <prototype>bool empty() const</prototype>
  170. <p>Returns <tt>begin() == end()</tt>.</p>
  171. </synopsis>
  172. <synopsis>
  173. <prototype>size_t size() const</prototype>
  174. <prototype>size_t length() const</prototype>
  175. <p>Size of the string.</p>
  176. </synopsis>
  177. <synopsis>
  178. <prototype>int compare(basic_string_ref s) const</prototype>
  179. <p>Compares the string with <tt>s</tt>. Returning values are the same as for
  180. <tt>std::string::compare()</tt>.</p>
  181. </synopsis>
  182. <synopsis>
  183. <prototype>std::basic_string&lt;charT> str() const</prototype>
  184. <prototype>template&lt;class Traits>
  185. std::basic_string&lt;charT,Traits> str() const</prototype>
  186. <prototype>template&lt;class Traits, class Alloc>
  187. std::basic_string&lt;charT,Traits,Alloc> str(const Alloc &amp;a = Alloc()) const</prototype>
  188. <p>Explicit converter to <tt>std::basic_string</tt>.</p>
  189. </synopsis>
  190. <synopsis>
  191. <prototype>operator std::basic_string&lt;charT>() const</prototype>
  192. <p>Implicit converter to <tt>std::basic_string</tt>.</p>
  193. </synopsis>
  194. </section>
  195. <section><title>Free functions</title>
  196. <synopsis>
  197. <prototype>template&lt;class charT>
  198. bool operator==(basic_string_ref&lt;charT> s1, basic_string_ref&lt;charT> s2)</prototype>
  199. <prototype>template&lt;class charT>
  200. bool operator!=(basic_string_ref&lt;charT> s1, basic_string_ref&lt;charT> s2)</prototype>
  201. <prototype>template&lt;class charT>
  202. bool operator&lt;(basic_string_ref&lt;charT> s1, basic_string_ref&lt;charT> s2)</prototype>
  203. <prototype>template&lt;class charT>
  204. bool operator>(basic_string_ref&lt;charT> s1, basic_string_ref&lt;charT> s2)</prototype>
  205. <prototype>template&lt;class charT>
  206. bool operator&lt;=(basic_string_ref&lt;charT> s1, basic_string_ref&lt;charT> s2)</prototype>
  207. <prototype>template&lt;class charT>
  208. bool operator>=(basic_string_ref&lt;charT> s1, basic_string_ref&lt;charT> s2)</prototype>
  209. <p>Full set of the comparison operators.</p>
  210. </synopsis>
  211. <synopsis>
  212. <prototype>template&lt;class charT, class Traits>
  213. std::basic_ostream&lt;charT,Traits> &amp;operator&lt;&lt;(
  214. std::basic_ostream&lt;charT,Traits> &amp;os, const basic_string_ref&lt;charT> &amp;sr)</prototype>
  215. <p>Inserter to an output stream. Defined (and <tt>&lt;ostream></tt> is included)
  216. only if <tt>__VIC_DEFINE_OSTREAM_INSERTERS</tt> macro was defined before the
  217. header inclusion.</p>
  218. </synopsis>
  219. </section>
  220. <section><title>Example</title>
  221. <code-block lang="C++"><![CDATA[
  222. C c; // see the class description above
  223. __vic::string_ref s = c. get_v_2();
  224. // print the string using different ways
  225. for(__vic::string_ref::iterator it = s.begin(); it != s.end(); ++it)
  226. std::cout << *it;
  227. // C++11
  228. for(auto ch : s) std::cout << ch;
  229. std::copy(s.begin(), s.end(), std::ostream_iterator<char>(std::cout));
  230. std::cout << s;
  231. // automatic conversion to std::string
  232. std::string ss = s;
  233. ]]></code-block>
  234. </section>
  235. </chapter>
  236. </chapter>