aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/static_string.h
blob: d4246363f3e729163db71f9ac69ccaa894d5a7af (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <string_view>
#include <vespa/vespalib/stllike/string.h>

namespace vespalib {

class StaticStringView;
namespace literals {
constexpr StaticStringView operator "" _ssv(const char *literal, size_t size);
} // literals

/**
 * Contains the view of a literal string
 **/
class StaticStringView {
private:
    std::string_view _view;
    friend constexpr StaticStringView literals::operator "" _ssv(const char *, size_t);
    constexpr StaticStringView(const char *literal, size_t size) noexcept
      : _view(literal, size) {}
public:
    constexpr std::string_view view() const noexcept { return _view; }
    constexpr operator std::string_view() const noexcept { return _view; }
    vespalib::stringref ref() const noexcept { return {_view.data(), _view.size()}; }
    operator vespalib::stringref() const noexcept { return ref(); }
};

namespace literals {
constexpr StaticStringView operator "" _ssv(const char *literal, size_t size) {
    return vespalib::StaticStringView(literal, size);
}
} // literals

} // vespalib