catch_stringref.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #include "catch_enforce.h"
  8. #include "catch_stringref.h"
  9. #include <algorithm>
  10. #include <ostream>
  11. #include <cstring>
  12. #include <cstdint>
  13. namespace Catch {
  14. StringRef::StringRef( char const* rawChars ) noexcept
  15. : StringRef( rawChars, static_cast<StringRef::size_type>(std::strlen(rawChars) ) )
  16. {}
  17. auto StringRef::c_str() const -> char const* {
  18. CATCH_ENFORCE(isNullTerminated(), "Called StringRef::c_str() on a non-null-terminated instance");
  19. return m_start;
  20. }
  21. auto StringRef::data() const noexcept -> char const* {
  22. return m_start;
  23. }
  24. auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef {
  25. if (start < m_size) {
  26. return StringRef(m_start + start, (std::min)(m_size - start, size));
  27. } else {
  28. return StringRef();
  29. }
  30. }
  31. auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool {
  32. return m_size == other.m_size
  33. && (std::memcmp( m_start, other.m_start, m_size ) == 0);
  34. }
  35. auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& {
  36. return os.write(str.data(), str.size());
  37. }
  38. auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& {
  39. lhs.append(rhs.data(), rhs.size());
  40. return lhs;
  41. }
  42. } // namespace Catch