Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include/internal/catch_stringdata.cpp
blob: 434368def1482e22313826620d0d8bbbfd6cda1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 *  Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
 *
 *  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)
 */

#include "catch_stringdata.h"
#include "catch_stringref.h"

#include <new>
#include <cstring>
#include <cassert>

namespace Catch {
    
    auto StringData::getEmpty() -> StringData* {
        static StringData s_empty( 0 );
        return &s_empty;
    }
    auto StringData::create( StringRef const& stringRef ) -> StringData* {
        return create( stringRef, stringRef.size() );
    }
    auto StringData::create( StringRef const& stringRef, unsigned long capacity ) -> StringData* {
        if( capacity == 0 ) {
            return getEmpty();
        }
        else {
            assert( stringRef.size() <= capacity );
            auto bufferLen = sizeof(StringData)+capacity;
            void* buffer = new char[bufferLen];
            
            return new(buffer) StringData( stringRef, capacity );
        }
    }
    StringData::StringData( unsigned int initialRef )
    :   m_refs( initialRef ),
        size( 0 )
    {}
    StringData::StringData( StringRef const& stringRef, unsigned long capacity )
    :   m_refs( 1 ),
        size( capacity)
    {
        std::memcpy( chars, stringRef.data(), stringRef.size() );
        chars[stringRef.size() ] = 0;
    }
    
} // namespace Catch