string_ref.qbk 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. [/
  2. / Copyright (c) 2012 Marshall Clow
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [article String_Ref
  8. [quickbook 1.5]
  9. [authors [Clow, Marshall]]
  10. [copyright 2012 Marshall Clow]
  11. [license
  12. Distributed under the Boost Software License, Version 1.0.
  13. (See accompanying file LICENSE_1_0.txt or copy at
  14. [@http://www.boost.org/LICENSE_1_0.txt])
  15. ]
  16. ]
  17. [/===============]
  18. [section Overview]
  19. [/===============]
  20. Boost.StringRef is an implementation of Jeffrey Yaskin's [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html N3442:
  21. string_ref: a non-owning reference to a string].
  22. When you are parsing/processing strings from some external source, frequently you want to pass a piece of text to a procedure for specialized processing. The canonical way to do this is as a `std::string`, but that has certain drawbacks:
  23. 1) If you are processing a buffer of text (say a HTTP response or the contents of a file), then you have to create the string from the text you want to pass, which involves memory allocation and copying of data.
  24. 2) if a routine receives a constant `std::string` and wants to pass a portion of that string to another routine, then it must create a new string of that substring.
  25. 3) A routine receives a constant `std::string` and wants to return a portion of the string, then it must create a new string to return.
  26. `string_ref` is designed to solve these efficiency problems. A `string_ref` is a read-only reference to a contiguous sequence of characters, and provides much of the functionality of `std::string`. A `string_ref` is cheap to create, copy and pass by value, because it does not actually own the storage that it points to.
  27. A `string_ref` is implemented as a small struct that contains a pointer to the start of the character data and a count. A `string_ref` is cheap to create and cheap to copy.
  28. `string_ref` acts as a container; it includes all the methods that you would expect in a container, including iteration support, `operator []`, `at` and `size`. It can be used with any of the iterator-based algorithms in the STL - as long as you don't need to change the underlying data (`sort` and `remove`, for example, will not work)
  29. Besides generic container functionality, `string_ref` provides a subset of the interface of `std::string`. This makes it easy to replace parameters of type `const std::string &` with `boost::string_ref`. Like `std::string`, `string_ref` has a static member variable named `npos` to denote the result of failed searches, and to mean "the end".
  30. Because a `string_ref` does not own the data that it "points to", it introduces lifetime issues into code that uses it. The programmer must ensure that the data that a `string_ref` refers to exists as long as the `string_ref` does.
  31. [endsect]
  32. [/===============]
  33. [section Examples]
  34. [/===============]
  35. Integrating `string_ref` into your code is fairly simple. Wherever you pass a `const std::string &` or `std::string` as a parameter, that's a candidate for passing a `boost::string_ref`.
  36. std::string extract_part ( const std::string &bar ) {
  37. return bar.substr ( 2, 3 );
  38. }
  39. if ( extract_part ( "ABCDEFG" ).front() == 'C' ) { /* do something */ }
  40. Let's figure out what happens in this (contrived) example.
  41. First, a temporary string is created from the string literal `"ABCDEFG"`, and it is passed (by reference) to the routine `extract_part`. Then a second string is created in the call `std::string::substr` and returned to `extract_part` (this copy may be elided by RVO). Then `extract_part` returns that string back to the caller (again this copy may be elided). The first temporary string is deallocated, and `front` is called on the second string, and then it is deallocated as well.
  42. Two `std::string`s are created, and two copy operations. That's (potentially) four memory allocations and deallocations, and the associated copying of data.
  43. Now let's look at the same code with `string_ref`:
  44. boost::string_ref extract_part ( boost::string_ref bar ) {
  45. return bar.substr ( 2, 3 );
  46. }
  47. if ( extract_part ( "ABCDEFG" ).front() == "C" ) { /* do something */ }
  48. No memory allocations. No copying of character data. No changes to the code other than the types. There are two `string_ref`s created, and two `string_ref`s copied, but those are cheap operations.
  49. [endsect]
  50. [/=================]
  51. [section:reference Reference ]
  52. [/=================]
  53. The header file "string_ref.hpp" defines a template `boost::basic_string_ref`, and four specializations - for `char` / `wchar_t` / `char16_t` / `char32_t` .
  54. `#include <boost/utility/string_ref.hpp>`
  55. Construction and copying:
  56. BOOST_CONSTEXPR basic_string_ref (); // Constructs an empty string_ref
  57. BOOST_CONSTEXPR basic_string_ref(const charT* str); // Constructs from a NULL-terminated string
  58. BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len); // Constructs from a pointer, length pair
  59. template<typename Allocator>
  60. basic_string_ref(const std::basic_string<charT, traits, Allocator>& str); // Constructs from a std::string
  61. basic_string_ref (const basic_string_ref &rhs);
  62. basic_string_ref& operator=(const basic_string_ref &rhs);
  63. `string_ref` does not define a move constructor nor a move-assignment operator because copying a `string_ref` is just a cheap as moving one.
  64. Basic container-like functions:
  65. BOOST_CONSTEXPR size_type size() const ;
  66. BOOST_CONSTEXPR size_type length() const ;
  67. BOOST_CONSTEXPR size_type max_size() const ;
  68. BOOST_CONSTEXPR bool empty() const ;
  69. // All iterators are const_iterators
  70. BOOST_CONSTEXPR const_iterator begin() const ;
  71. BOOST_CONSTEXPR const_iterator cbegin() const ;
  72. BOOST_CONSTEXPR const_iterator end() const ;
  73. BOOST_CONSTEXPR const_iterator cend() const ;
  74. const_reverse_iterator rbegin() const ;
  75. const_reverse_iterator crbegin() const ;
  76. const_reverse_iterator rend() const ;
  77. const_reverse_iterator crend() const ;
  78. Access to the individual elements (all of which are const):
  79. BOOST_CONSTEXPR const charT& operator[](size_type pos) const ;
  80. const charT& at(size_t pos) const ;
  81. BOOST_CONSTEXPR const charT& front() const ;
  82. BOOST_CONSTEXPR const charT& back() const ;
  83. BOOST_CONSTEXPR const charT* data() const ;
  84. Modifying the `string_ref` (but not the underlying data):
  85. void clear();
  86. void remove_prefix(size_type n);
  87. void remove_suffix(size_type n);
  88. Searching:
  89. size_type find(basic_string_ref s) const ;
  90. size_type find(charT c) const ;
  91. size_type rfind(basic_string_ref s) const ;
  92. size_type rfind(charT c) const ;
  93. size_type find_first_of(charT c) const ;
  94. size_type find_last_of (charT c) const ;
  95. size_type find_first_of(basic_string_ref s) const ;
  96. size_type find_last_of(basic_string_ref s) const ;
  97. size_type find_first_not_of(basic_string_ref s) const ;
  98. size_type find_first_not_of(charT c) const ;
  99. size_type find_last_not_of(basic_string_ref s) const ;
  100. size_type find_last_not_of(charT c) const ;
  101. String-like operations:
  102. BOOST_CONSTEXPR basic_string_ref substr(size_type pos, size_type n=npos) const ; // Creates a new string_ref
  103. bool starts_with(charT c) const ;
  104. bool starts_with(basic_string_ref x) const ;
  105. bool ends_with(charT c) const ;
  106. bool ends_with(basic_string_ref x) const ;
  107. [endsect]
  108. [/===============]
  109. [section History]
  110. [/===============]
  111. [heading boost 1.71]
  112. * Glen Fernandes updated the implementation of the stream insertion operator to
  113. write directly to the `basic_streambuf` and refactored that functionality into
  114. a common utility.
  115. [heading boost 1.53]
  116. * Introduced
  117. [endsect]