From 98d793152cc1d0c75d5800050376ce52245bc91c Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sat, 15 Dec 2012 16:38:07 +0000 Subject: [PATCH] Move string_ref to Boost.Utility; first crack at docs [SVN r81972] --- doc/Jamfile.v2 | 23 +- doc/string_ref.qbk | 100 +++++++ include/boost/utility/string_ref.hpp | 386 +++++++++++++++++++++++++++ index.html | 1 + test/Jamfile.v2 | 2 + test/string_ref_test1.cpp | 111 ++++++++ test/string_ref_test2.cpp | 256 ++++++++++++++++++ 7 files changed, 875 insertions(+), 4 deletions(-) create mode 100644 doc/string_ref.qbk create mode 100644 include/boost/utility/string_ref.hpp create mode 100644 test/string_ref_test1.cpp create mode 100644 test/string_ref_test2.cpp diff --git a/doc/Jamfile.v2 b/doc/Jamfile.v2 index b12d71a..10b7c7e 100644 --- a/doc/Jamfile.v2 +++ b/doc/Jamfile.v2 @@ -62,7 +62,22 @@ boostbook standalone ; - - - - +xml string_ref : string_ref.qbk ; +boostbook standalone + : + string_ref + : + # File name of HTML output: + root.filename=string_ref + # How far down we chunk nested sections, basically all of them: + chunk.section.depth=0 + # Don't put the first section on the same page as the TOC: + chunk.first.sections=0 + # How far down sections get TOC's + toc.section.depth=1 + # Max depth in each TOC: + toc.max.depth=1 + # How far down we go with TOC's + generate.section.toc.level=1 + + ; diff --git a/doc/string_ref.qbk b/doc/string_ref.qbk new file mode 100644 index 0000000..0242096 --- /dev/null +++ b/doc/string_ref.qbk @@ -0,0 +1,100 @@ +[/ + / Copyright (c) 2012 Marshall Clow + / + / Distributed under the Boost Software License, Version 1.0. (See accompanying + / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + /] + +[article String_Ref + [quickbook 1.5] + [authors [Clow, Marshall]] + [copyright 2012 Marshall Clow] + [license + Distributed under the Boost Software License, Version 1.0. + (See accompanying file LICENSE_1_0.txt or copy at + [@http://www.boost.org/LICENSE_1_0.txt]) + ] +] + +[/===============] +[section Overview] +[/===============] + +Boost.StringRef is an implementation of Jeffrey Yaskin's [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html N3442: +string_ref: a non-owning reference to a string]. + +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: + +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. + +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. + +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. + +`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. + +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. + +`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) + +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`. + +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. + +[endsect] + + +[/===============] +[section Examples] +[/===============] + +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`. + + std::string extract_part ( const std::string &bar ) { + return bar.substr ( 2, 3 ); + } + + if ( extract_part ( "ABCDEFG" ).front() == "C" ) { /* do something */ } + +Let's figure out what happens in this (contrived) example. + +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. + +Two `std::string`s are created, and two copy operations. That's (potentially) four memory allocations and deallocations, and the associated copying of data. + +Now let's look at the same code with `string_ref`: + + boost::string_ref extract_part ( boost::string_ref bar ) { + return bar.substr ( 2, 3 ); + } + + if ( extract_part ( "ABCDEFG" ).front() == "C" ) { /* do something */ } + +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. + +[endsect] + + +[/=================] +[section:reference Reference ] +[/=================] + +The header file "string_ref.hpp" defines a template `boost::basic_string_ref`, and four specializations - for `char` / `wchar_t` / `char16_t` / `char32_t` . + +`#include ` + +[endsect] + +[/===============] +[section History] +[/===============] + +[heading boost 1.53] +* Introduced + + +[endsect] + + + + diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp new file mode 100644 index 0000000..d333628 --- /dev/null +++ b/include/boost/utility/string_ref.hpp @@ -0,0 +1,386 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org + + Based on the StringRef implementation in LLVM (http://llvm.org) and + N3422 by Jeffrey Yasskin + http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html + +*/ + +#ifndef BOOST_STRING_REF_HPP +#define BOOST_STRING_REF_HPP + +#include +#include + +#include +#include +#include +#include + +namespace boost { + + namespace detail { + // A helper functor because sometimes we don't have lambdas + template + class string_ref_traits_eq { + public: + string_ref_traits_eq ( charT ch ) : ch_(ch) {} + bool operator () ( charT val ) const { return traits::eq ( ch_, val ); } + charT ch_; + }; + } + + template class basic_string_ref; + typedef basic_string_ref > string_ref; + typedef basic_string_ref > wstring_ref; + +#ifndef BOOST_NO_CXX11_CHAR16_T + typedef basic_string_ref > u16string_ref; +#endif + +#ifndef BOOST_NO_CXX11_CHAR32_T + typedef basic_string_ref > u32string_ref; +#endif + + template + class basic_string_ref { + public: + // types + typedef charT value_type; + typedef const charT* pointer; + typedef const charT& reference; + typedef const charT& const_reference; + typedef pointer const_iterator; // impl-defined + typedef const_iterator iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef const_reverse_iterator reverse_iterator; + typedef std::size_t size_type; + typedef ptrdiff_t difference_type; + static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1); + + // construct/copy + BOOST_CONSTEXPR basic_string_ref () + : ptr_(NULL), len_(0) {} + + BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) + : ptr_(rhs.ptr_), len_(rhs.len_) {} + + basic_string_ref& operator=(const basic_string_ref &rhs) { + ptr_ = rhs.ptr_; + len_ = rhs.len_; + return *this; + } + + basic_string_ref(const charT* str) + : ptr_(str), len_(traits::length(str)) {} + + template + basic_string_ref(const std::basic_string& str) + : ptr_(str.data()), len_(str.length()) {} + + BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) + : ptr_(str), len_(len) {} + +#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS + template + explicit operator std::basic_string() const { + return std::basic_string ( ptr_, len_ ); + } +#endif + + // iterators + BOOST_CONSTEXPR const_iterator begin() const { return ptr_; } + BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; } + BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; } + BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; } + const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); } + const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); } + const_reverse_iterator rend() const { return const_reverse_iterator (begin()); } + const_reverse_iterator crend() const { return const_reverse_iterator (begin()); } + + // capacity + BOOST_CONSTEXPR size_type size() const { return len_; } + BOOST_CONSTEXPR size_type length() const { return len_; } + BOOST_CONSTEXPR size_type max_size() const { return len_; } + BOOST_CONSTEXPR bool empty() const { return len_ == 0; } + + // element access + BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; } + + const charT& at(size_t pos) const { + if ( pos >= len_ ) + throw std::out_of_range ( "boost::string_ref::at" ); + return ptr_[pos]; + } + + BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; } + BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; } + BOOST_CONSTEXPR const charT* data() const { return ptr_; } + + // modifiers + void clear() { len_ = 0; } + void remove_prefix(size_type n) { + if ( n > len_ ) + n = len_; + ptr_ += n; + len_ -= n; + } + + void remove_suffix(size_type n) { + if ( n > len_ ) + n = len_; + len_ -= n; + } + + + // basic_string_ref string operations + BOOST_CONSTEXPR + basic_string_ref substr(size_type pos, size_type n=npos) const { +#if BOOST_WORKAROUND(BOOST_MSVC, <= 1600) + // Looks like msvc 8 and 9 have a codegen bug when one branch of + // a conditional operator is a throw expression. -EAN 2012/12/04 + if ( pos > size()) throw std::out_of_range ( "string_ref::substr" ); + if ( n == npos || pos + n > size()) n = size () - pos; + return basic_string_ref ( data() + pos, n ); +#else + return pos > size() ? throw std::out_of_range ( "string_ref::substr" ) : + basic_string_ref ( data() + pos, n == npos || pos + n > size() ? size() - pos : n ); +#endif + } + + int compare(basic_string_ref x) const { + int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_)); + return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 ); + } + + bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); } + bool starts_with(basic_string_ref x) const { + return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0; + } + + bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); } + bool ends_with(basic_string_ref x) const { + return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0; + } + + size_type find(basic_string_ref s) const { + const_iterator iter = std::search ( this->cbegin (), this->cend (), + s.cbegin (), s.cend (), traits::eq ); + return iter = this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find(charT c) const { + const_iterator iter = std::find_if ( this->cbegin (), this->cend (), + detail::string_ref_traits_eq ( c )); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type rfind(basic_string_ref s) const { + const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (), + s.crbegin (), s.crend (), traits::eq ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type rfind(charT c) const { + const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (), + detail::string_ref_traits_eq ( c )); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type find_first_of(charT c) const { return find (c); } + size_type find_last_of (charT c) const { return rfind (c); } + + size_type find_first_of(basic_string_ref s) const { + const_iterator iter = std::find_first_of + ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find_last_of(basic_string_ref s) const { + const_reverse_iterator iter = std::find_first_of + ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); + } + + size_type find_first_not_of(basic_string_ref s) const { + const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find_first_not_of(charT c) const { + for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter ) + if ( !traits::eq ( c, *iter )) + return std::distance ( this->cbegin (), iter ); + return npos; + } + + size_type find_last_not_of(basic_string_ref s) const { + const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type find_last_not_of(charT c) const { + for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter ) + if ( !traits::eq ( c, *iter )) + return reverse_distance ( this->crbegin (), iter ); + return npos; + } + + private: + template + size_type reverse_distance ( r_iter first, r_iter last ) const { + return len_ - 1 - std::distance ( first, last ); + } + + template + Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const { + for ( ; first != last ; ++first ) + if ( 0 == traits::find ( s.ptr_, s.len_, *first )) + return first; + return last; + } + + + + const charT *ptr_; + std::size_t len_; + }; + + // Comparison operators + template + bool operator==(basic_string_ref x, basic_string_ref y) { + if ( x.size () != y.size ()) return false; + return x.compare(y) == 0; + } + + template + bool operator!=(basic_string_ref x, basic_string_ref y) { + if ( x.size () != y.size ()) return true; + return x.compare(y) != 0; + } + + template + bool operator<(basic_string_ref x, basic_string_ref y) { + return x.compare(y) < 0; + } + + template + bool operator>(basic_string_ref x, basic_string_ref y) { + return x.compare(y) > 0; + } + + template + bool operator<=(basic_string_ref x, basic_string_ref y) { + return x.compare(y) <= 0; + } + + template + bool operator>=(basic_string_ref x, basic_string_ref y) { + return x.compare(y) >= 0; + } + + + // Inserter + template + std::basic_ostream& + operator<<(std::basic_ostream& os, const basic_string_ref& str) { +#ifdef BOOST_NO_CXX11_RANGE_BASED_FOR + for ( typename basic_string_ref::const_iterator iter = str.begin (); iter != str.end (); ++iter ) + os << *iter; +#else + for ( charT x : str ) + os << x; +#endif + return os; + } + +#if 0 + // numeric conversions + // + // These are short-term implementations. + // In a production environment, I would rather avoid the copying. + // + int stoi (string_ref str, size_t* idx=0, int base=10) { + return std::stoi ( std::string(str), idx, base ); + } + + long stol (string_ref str, size_t* idx=0, int base=10) { + return std::stol ( std::string(str), idx, base ); + } + + unsigned long stoul (string_ref str, size_t* idx=0, int base=10) { + return std::stoul ( std::string(str), idx, base ); + } + + long long stoll (string_ref str, size_t* idx=0, int base=10) { + return std::stoll ( std::string(str), idx, base ); + } + + unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) { + return std::stoull ( std::string(str), idx, base ); + } + + float stof (string_ref str, size_t* idx=0) { + return std::stof ( std::string(str), idx ); + } + + double stod (string_ref str, size_t* idx=0) { + return std::stod ( std::string(str), idx ); + } + + long double stold (string_ref str, size_t* idx=0) { + return std::stold ( std::string(str), idx ); + } + + int stoi (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoi ( std::wstring(str), idx, base ); + } + + long stol (wstring_ref str, size_t* idx=0, int base=10) { + return std::stol ( std::wstring(str), idx, base ); + } + + unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoul ( std::wstring(str), idx, base ); + } + + long long stoll (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoll ( std::wstring(str), idx, base ); + } + + unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoull ( std::wstring(str), idx, base ); + } + + float stof (wstring_ref str, size_t* idx=0) { + return std::stof ( std::wstring(str), idx ); + } + + double stod (wstring_ref str, size_t* idx=0) { + return std::stod ( std::wstring(str), idx ); + } + + long double stold (wstring_ref str, size_t* idx=0) { + return std::stold ( std::wstring(str), idx ); + } +#endif + +} + +#if 0 +namespace std { + // Hashing + template<> struct hash; + template<> struct hash; + template<> struct hash; + template<> struct hash; +} +#endif + +#endif diff --git a/index.html b/index.html index d384487..d5b4884 100644 --- a/index.html +++ b/index.html @@ -34,6 +34,7 @@ swap
throw_exception
utility
+ string_ref
value_init

diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d3fc34c..4b2d6e0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -31,6 +31,8 @@ test-suite utility [ run ../ref_test.cpp ../../test/build//boost_test_exec_monitor/static ] [ compile result_of_test.cpp ] [ run ../shared_iterator_test.cpp ] + [ run string_ref_test1.cpp ] + [ run string_ref_test2.cpp ] [ run ../value_init_test.cpp ] [ run ../value_init_workaround_test.cpp ] [ run ../initialized_test.cpp ] diff --git a/test/string_ref_test1.cpp b/test/string_ref_test1.cpp new file mode 100644 index 0000000..32ff801 --- /dev/null +++ b/test/string_ref_test1.cpp @@ -0,0 +1,111 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include + + +#include + +typedef boost::string_ref string_ref; + +// Should be equal +void interop ( const std::string &str, string_ref ref ) { +// BOOST_CHECK ( str == ref ); + BOOST_CHECK ( str.size () == ref.size ()); + BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ())); + BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ())); + } + +void null_tests ( const char *p ) { +// All zero-length string-refs should be equal + string_ref sr1; // NULL, 0 + string_ref sr2 ( NULL, 0 ); + string_ref sr3 ( p, 0 ); + string_ref sr4 ( p ); + sr4.clear (); + + BOOST_CHECK ( sr1 == sr2 ); + BOOST_CHECK ( sr1 == sr3 ); + BOOST_CHECK ( sr2 == sr3 ); + BOOST_CHECK ( sr1 == sr4 ); + } + +// make sure that substrings work just like strings +void test_substr ( const std::string &str ) { + const size_t sz = str.size (); + string_ref ref ( str ); + +// Substrings at the end + for ( size_t i = 0; i <= sz; ++ i ) + interop ( str.substr ( i ), ref.substr ( i )); + +// Substrings at the beginning + for ( size_t i = 0; i <= sz; ++ i ) + interop ( str.substr ( 0, i ), ref.substr ( 0, i )); + +// All possible substrings + for ( size_t i = 0; i < sz; ++i ) + for ( size_t j = i; j < sz; ++j ) + interop ( str.substr ( i, j ), ref.substr ( i, j )); + } + +// make sure that removing prefixes and suffixes work just like strings +void test_remove ( const std::string &str ) { + const size_t sz = str.size (); + std::string work; + string_ref ref; + + for ( size_t i = 1; i <= sz; ++i ) { + work = str; + ref = str; + while ( ref.size () >= i ) { + interop ( work, ref ); + work.erase ( 0, i ); + ref.remove_prefix (i); + } + } + + for ( size_t i = 1; i < sz; ++ i ) { + work = str; + ref = str; + while ( ref.size () >= i ) { + interop ( work, ref ); + work.erase ( work.size () - i, i ); + ref.remove_suffix (i); + } + } + } + +const char *test_strings [] = { + "", + "1", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + "0123456789", + NULL + }; + +int test_main( int , char* [] ) { + + const char **p = &test_strings[0]; + + while ( *p != NULL ) { + interop ( *p, *p ); + test_substr ( *p ); + test_remove ( *p ); + null_tests ( *p ); + + p++; + } + + return 0; + } diff --git a/test/string_ref_test2.cpp b/test/string_ref_test2.cpp new file mode 100644 index 0000000..dbd5bf9 --- /dev/null +++ b/test/string_ref_test2.cpp @@ -0,0 +1,256 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include // for std::strchr + +#include + +#include + +typedef boost::string_ref string_ref; + +void ends_with ( const char *arg ) { + const size_t sz = strlen ( arg ); + string_ref sr ( arg ); + string_ref sr2 ( arg ); + const char *p = arg; + + while ( !*p ) { + BOOST_CHECK ( sr.ends_with ( p )); + ++p; + } + + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.ends_with ( sr2 )); + sr2.remove_prefix (1); + } + + sr2 = arg; + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.ends_with ( sr2 )); + sr2.remove_prefix (1); + } + + char ch = sz == 0 ? '\0' : arg [ sz - 1 ]; + sr2 = arg; + if ( sz > 0 ) + BOOST_CHECK ( sr2.ends_with ( ch )); + BOOST_CHECK ( !sr2.ends_with ( ++ch )); + BOOST_CHECK ( sr2.ends_with ( string_ref ())); + } + +void starts_with ( const char *arg ) { + const size_t sz = strlen ( arg ); + string_ref sr ( arg ); + string_ref sr2 ( arg ); + const char *p = arg + std::strlen ( arg ) - 1; + while ( p >= arg ) { + std::string foo ( arg, p + 1 ); + BOOST_CHECK ( sr.starts_with ( foo )); + --p; + } + + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.starts_with ( sr2 )); + sr2.remove_suffix (1); + } + + char ch = *arg; + sr2 = arg; + if ( sz > 0 ) + BOOST_CHECK ( sr2.starts_with ( ch )); + BOOST_CHECK ( !sr2.starts_with ( ++ch )); + BOOST_CHECK ( sr2.starts_with ( string_ref ())); + } + +void reverse ( const char *arg ) { +// Round trip + string_ref sr1 ( arg ); + std::string string1 ( sr1.rbegin (), sr1.rend ()); + string_ref sr2 ( string1 ); + std::string string2 ( sr2.rbegin (), sr2.rend ()); + + BOOST_CHECK ( std::equal ( sr2.rbegin (), sr2.rend (), arg )); + BOOST_CHECK ( string2 == arg ); + BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ())); + } + +// This helper function eliminates signed vs. unsigned warnings +string_ref::size_type ptr_diff ( const char *res, const char *base ) { + BOOST_CHECK ( res >= base ); + return static_cast ( res - base ); + } + +void find ( const char *arg ) { + string_ref sr1; + string_ref sr2; + const char *p; + +// Look for each character in the string(searching from the start) + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos = sr1.find(*p); + BOOST_CHECK ( pos != string_ref::npos && ( pos <= ptr_diff ( p, arg ))); + ++p; + } + +// Look for each character in the string (searching from the end) + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos = sr1.rfind(*p); + BOOST_CHECK ( pos != string_ref::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg ))); + ++p; + } + + sr1 = arg; + p = arg; +// for all possible chars, see if we find them in the right place. +// Note that strchr will/might do the _wrong_ thing if we search for NULL + for ( int ch = 1; ch < 256; ++ch ) { + string_ref::size_type pos = sr1.find(ch); + const char *strp = std::strchr ( arg, ch ); + BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); + if ( strp != NULL ) + BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); + } + + sr1 = arg; + p = arg; +// for all possible chars, see if we find them in the right place. +// Note that strchr will/might do the _wrong_ thing if we search for NULL + for ( int ch = 1; ch < 256; ++ch ) { + string_ref::size_type pos = sr1.rfind(ch); + const char *strp = std::strrchr ( arg, ch ); + BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); + if ( strp != NULL ) + BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); + } + + +// Find everything at the start + p = arg; + sr1 = arg; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find(*p); + BOOST_CHECK ( pos == 0 ); + sr1.remove_prefix (1); + ++p; + } + +// Find everything at the end + sr1 = arg; + p = arg + strlen ( arg ) - 1; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.rfind(*p); + BOOST_CHECK ( pos == sr1.size () - 1 ); + sr1.remove_suffix (1); + --p; + } + +// Find everything at the start + sr1 = arg; + p = arg; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find_first_of(*p); + BOOST_CHECK ( pos == 0 ); + sr1.remove_prefix (1); + ++p; + } + + +// Find everything at the end + sr1 = arg; + p = arg + strlen ( arg ) - 1; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find_last_of(*p); + BOOST_CHECK ( pos == sr1.size () - 1 ); + sr1.remove_suffix (1); + --p; + } + +// Basic sanity checking for "find_first_of / find_first_not_of" + sr1 = arg; + sr2 = arg; + while ( !sr1.empty() ) { + BOOST_CHECK ( sr1.find_first_of ( sr2 ) == 0 ); + BOOST_CHECK ( sr1.find_first_not_of ( sr2 ) == string_ref::npos ); + sr1.remove_prefix ( 1 ); + } + + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos1 = sr1.find_first_of(*p); + string_ref::size_type pos2 = sr1.find_first_not_of(*p); + BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg )); + if ( pos2 != string_ref::npos ) { + for ( size_t i = 0 ; i < pos2; ++i ) + BOOST_CHECK ( sr1[i] == *p ); + BOOST_CHECK ( sr1 [ pos2 ] != *p ); + } + + BOOST_CHECK ( pos2 != pos1 ); + ++p; + } + +// Basic sanity checking for "find_last_of / find_last_not_of" + sr1 = arg; + sr2 = arg; + while ( !sr1.empty() ) { + BOOST_CHECK ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 )); + BOOST_CHECK ( sr1.find_last_not_of ( sr2 ) == string_ref::npos ); + sr1.remove_suffix ( 1 ); + } + + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos1 = sr1.find_last_of(*p); + string_ref::size_type pos2 = sr1.find_last_not_of(*p); + BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg )); + BOOST_CHECK ( pos2 == string_ref::npos || pos1 < sr1.size ()); + if ( pos2 != string_ref::npos ) { + for ( size_t i = sr1.size () -1 ; i > pos2; --i ) + BOOST_CHECK ( sr1[i] == *p ); + BOOST_CHECK ( sr1 [ pos2 ] != *p ); + } + + BOOST_CHECK ( pos2 != pos1 ); + ++p; + } + + } + +const char *test_strings [] = { + "", + "0", + "abc", + "AAA", // all the same + "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf", + "abc\0asdfadsfasf", + NULL + }; + +int test_main( int , char* [] ) { + const char **p = &test_strings[0]; + + while ( *p != NULL ) { + starts_with ( *p ); + ends_with ( *p ); + reverse ( *p ); + find ( *p ); + + p++; + } + + return 0; + }