catch_stringref.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
  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. #ifndef CATCH_STRINGREF_H_INCLUDED
  8. #define CATCH_STRINGREF_H_INCLUDED
  9. #include <cstddef>
  10. #include <string>
  11. #include <iosfwd>
  12. #include <cassert>
  13. namespace Catch {
  14. /// A non-owning string class (similar to the forthcoming std::string_view)
  15. /// Note that, because a StringRef may be a substring of another string,
  16. /// it may not be null terminated.
  17. class StringRef {
  18. public:
  19. using size_type = std::size_t;
  20. using const_iterator = const char*;
  21. private:
  22. static constexpr char const* const s_empty = "";
  23. char const* m_start = s_empty;
  24. size_type m_size = 0;
  25. public: // construction
  26. constexpr StringRef() noexcept = default;
  27. StringRef( char const* rawChars ) noexcept;
  28. constexpr StringRef( char const* rawChars, size_type size ) noexcept
  29. : m_start( rawChars ),
  30. m_size( size )
  31. {}
  32. StringRef( std::string const& stdString ) noexcept
  33. : m_start( stdString.c_str() ),
  34. m_size( stdString.size() )
  35. {}
  36. explicit operator std::string() const {
  37. return std::string(m_start, m_size);
  38. }
  39. public: // operators
  40. auto operator == ( StringRef const& other ) const noexcept -> bool;
  41. auto operator != (StringRef const& other) const noexcept -> bool {
  42. return !(*this == other);
  43. }
  44. auto operator[] ( size_type index ) const noexcept -> char {
  45. assert(index < m_size);
  46. return m_start[index];
  47. }
  48. public: // named queries
  49. constexpr auto empty() const noexcept -> bool {
  50. return m_size == 0;
  51. }
  52. constexpr auto size() const noexcept -> size_type {
  53. return m_size;
  54. }
  55. // Returns the current start pointer. If the StringRef is not
  56. // null-terminated, throws std::domain_exception
  57. auto c_str() const -> char const*;
  58. public: // substrings and searches
  59. // Returns a substring of [start, start + length).
  60. // If start + length > size(), then the substring is [start, size()).
  61. // If start > size(), then the substring is empty.
  62. auto substr( size_type start, size_type length ) const noexcept -> StringRef;
  63. // Returns the current start pointer. May not be null-terminated.
  64. auto data() const noexcept -> char const*;
  65. constexpr auto isNullTerminated() const noexcept -> bool {
  66. return m_start[m_size] == '\0';
  67. }
  68. public: // iterators
  69. constexpr const_iterator begin() const { return m_start; }
  70. constexpr const_iterator end() const { return m_start + m_size; }
  71. };
  72. auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&;
  73. auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;
  74. constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
  75. return StringRef( rawChars, size );
  76. }
  77. } // namespace Catch
  78. constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef {
  79. return Catch::StringRef( rawChars, size );
  80. }
  81. #endif // CATCH_STRINGREF_H_INCLUDED