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