smallstringliteral.h 3.42 KB
Newer Older
Marco Bubke's avatar
Marco Bubke committed
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "smallstringiterator.h"
#include "smallstringlayout.h"
#include "smallstringview.h"

namespace Utils {

34 35
template <int Size>
class BasicSmallStringLiteral
Marco Bubke's avatar
Marco Bubke committed
36
{
37 38
    template<uint>
    friend class BasicSmallString;
Marco Bubke's avatar
Marco Bubke committed
39 40 41 42 43 44

public:
    using const_iterator = Internal::SmallStringIterator<std::random_access_iterator_tag, const char>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
    using size_type = std::size_t;

45
    template<size_type ArraySize>
Marco Bubke's avatar
Marco Bubke committed
46
    constexpr
47
    BasicSmallStringLiteral(const char(&string)[ArraySize]) noexcept
Marco Bubke's avatar
Marco Bubke committed
48 49
        : m_data(string)
    {
50
        static_assert(ArraySize >= 1, "Invalid string literal! Length is zero!");
Marco Bubke's avatar
Marco Bubke committed
51 52 53
    }

    constexpr
54
    BasicSmallStringLiteral(const char *string, const size_type size) noexcept
Marco Bubke's avatar
Marco Bubke committed
55 56 57 58
        : m_data(string, size)
    {
    }

59
    const char *data() const noexcept
Marco Bubke's avatar
Marco Bubke committed
60 61 62 63
    {
        return Q_LIKELY(isShortString()) ? m_data.shortString.string : m_data.allocated.data.pointer;
    }

64
    size_type size() const noexcept
Marco Bubke's avatar
Marco Bubke committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    {
        return Q_LIKELY(isShortString()) ? m_data.shortString.shortStringSize : m_data.allocated.data.size;
    }

    const_iterator begin() const noexcept
    {
        return data();
    }

    const_iterator end() const noexcept
    {
        return data() + size();
    }

    const_reverse_iterator rbegin() const noexcept
    {
81
        return const_reverse_iterator(end() - static_cast<std::size_t>(1));
Marco Bubke's avatar
Marco Bubke committed
82 83 84 85
    }

    const_reverse_iterator rend() const noexcept
    {
86
        return const_reverse_iterator(begin() - static_cast<std::size_t>(1));
Marco Bubke's avatar
Marco Bubke committed
87 88 89 90 91
    }

    constexpr static
    size_type shortStringCapacity() noexcept
    {
Marco Bubke's avatar
Marco Bubke committed
92
        return Internal::StringDataLayout<Size>::shortStringCapacity();
Marco Bubke's avatar
Marco Bubke committed
93 94 95 96
    }

    bool isShortString() const noexcept
    {
97
        return !m_data.shortString.isReference;
Marco Bubke's avatar
Marco Bubke committed
98 99
    }

100
    bool isReadOnlyReference() const noexcept
Marco Bubke's avatar
Marco Bubke committed
101
    {
102
        return m_data.shortString.isReadOnlyReference;
Marco Bubke's avatar
Marco Bubke committed
103 104 105 106 107 108 109 110
    }

    operator SmallStringView() const
    {
        return SmallStringView(data(), size());
    }

private:
Thomas Hartmann's avatar
Thomas Hartmann committed
111
    BasicSmallStringLiteral(const Internal::StringDataLayout<Size> &data) noexcept
Marco Bubke's avatar
Marco Bubke committed
112 113 114 115
        : m_data(data)
    {
    }
private:
116
    Internal::StringDataLayout<Size> m_data;
Marco Bubke's avatar
Marco Bubke committed
117 118
};

119 120
using SmallStringLiteral = BasicSmallStringLiteral<31>;

Marco Bubke's avatar
Marco Bubke committed
121
}  // namespace Utils