summaryrefslogtreecommitdiff
path: root/src/rc_string.cpp
blob: b56f2e62ac91b193cbf9dab89c798e420ac1dd48 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * MRustC - Mutabah's Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * rc_string.cpp
 * - Reference-counted string
 */
#include <rc_string.hpp>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>    // std::max

RcString::RcString(const char* s, unsigned int len):
    m_ptr(nullptr)
{
    if( len > 0 )
    {
        m_ptr = new unsigned int[2 + (len+1 + sizeof(unsigned int)-1) / sizeof(unsigned int)];
        m_ptr[0] = 1;
        m_ptr[1] = len;
        char* data_mut = reinterpret_cast<char*>(m_ptr + 2);
        for(unsigned int j = 0; j < len; j ++ )
            data_mut[j] = s[j];
        data_mut[len] = '\0';

        //::std::cout << "RcString(" << m_ptr << " \"" << *this << "\") - " << *m_ptr << " (creation)" << ::std::endl;
    }
}
RcString::~RcString()
{
    if(m_ptr)
    {
        *m_ptr -= 1;
        //::std::cout << "RcString(" << m_ptr << " \"" << *this << "\") - " << *m_ptr << " refs left (drop)" << ::std::endl;
        if( *m_ptr == 0 )
        {
            delete[] m_ptr;
            m_ptr = nullptr;
        }
    }
}
Ordering RcString::ord(const char* s, size_t len) const
{
    if( m_ptr == nullptr )
        return (len == 0 ? OrdEqual : OrdLess);
    if( len == 0 )
        return OrdGreater;

    assert(this->size() > 0);
    assert(len > 0);

    int cmp = memcmp(this->c_str(), s, ::std::min(len, this->size()));
    if(cmp == 0)
        return ::ord(this->size(), len);
    return ::ord(cmp, 0);
}
Ordering RcString::ord(const char* s) const
{
    if( m_ptr == nullptr )
        return (*s == '\0' ? OrdEqual : OrdLess);

    int cmp = strncmp(this->c_str(), s, this->size());
    if( cmp == 0 )
    {
        if( s[this->size()] == '\0' )
            return OrdEqual;
        else
            return OrdLess;
    }
    return ::ord(cmp, 0);
}

::std::ostream& operator<<(::std::ostream& os, const RcString& x)
{
    for(size_t i = 0; i < x.size(); i ++)
    {
        os << x.c_str()[i];
    }
    return os;
}


::std::set<RcString>    RcString_interned_strings;

RcString RcString::new_interned(const ::std::string& s)
{
#if 0
    auto it = RcString_interned_strings.find(s);
    if( it == RcString_interned_strings.end() )
    {
        it = RcString_interned_strings.insert(RcString(s)).first;
    }
    return *it;
#else
    // TODO: interning flag, so comparisons can just be a pointer comparison
    // - Only want to set this flag on the cached instance
    return *RcString_interned_strings.insert(RcString(s)).first;
#endif
}
RcString RcString::new_interned(const char* s)
{
#if 0
    auto it = RcString_interned_strings.find(s);
    if( it == RcString_interned_strings.end() )
    {
        it = RcString_interned_strings.insert(RcString(s)).first;
    }
    return *it;
#else
    // TODO: interning flag, so comparisons can just be a pointer comparison
    // - Only want to set this flag on the cached instance
    return *RcString_interned_strings.insert(RcString(s)).first;
#endif
}

size_t std::hash<RcString>::operator()(const RcString& s) const noexcept
{
    // http://www.cse.yorku.ca/~oz/hash.html "djb2"
    size_t h = 5381;
    for(auto c : s) {
        h = h * 33 + (unsigned)c;
    }
    return h;
    //return hash<std::string_view>(s.c_str(), s.size());
}