summaryrefslogtreecommitdiff
path: root/libphobos/libdruntime/core/stdcpp/string_view.d
blob: 986fa7ee273a3ff801e4de18bb067671fdbeb6d6 (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
/**
 * D header file for interaction with C++ std::string_view.
 *
 * Copyright: Copyright (c) 2018 D Language Foundation
 * License: Distributed under the
 *      $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
 *    (See accompanying file LICENSE)
 * Authors:   Manu Evans
 * Source:    $(DRUNTIMESRC core/stdcpp/string_view.d)
 */

module core.stdcpp.string_view;

import core.stdc.stddef : wchar_t;
import core.stdcpp.xutility : StdNamespace;

extern(C++, (StdNamespace)):
@nogc:

///
alias string_view = basic_string_view!char;
///
alias u16string_view = basic_string_view!wchar;
///
alias u32string_view = basic_string_view!dchar;
///
alias wstring_view = basic_string_view!wchar_t;


/**
 * Character traits classes specify character properties and provide specific
 * semantics for certain operations on characters and sequences of characters.
 */
extern(C++, struct) struct char_traits(CharT) {}


/**
* D language counterpart to C++ std::basic_string_view.
*
* C++ reference: $(LINK2 https://en.cppreference.com/w/cpp/string/basic_string_view, std::basic_string_view)
*/
extern(C++, class) struct basic_string_view(T, Traits = char_traits!T)
{
extern(D):
pragma(inline, true):
pure nothrow @nogc:

    ///
    enum size_type npos = size_type.max;

    ///
    alias size_type = size_t;
    ///
    alias difference_type = ptrdiff_t;
    ///
    alias value_type = T;
    ///
    alias pointer = T*;
    ///
    alias const_pointer = const(T)*;

    ///
    alias as_array this;
    ///
    alias toString = as_array;

    ///
    this(const(T)[] str) @trusted                   { __data = str.ptr; __size = str.length; }

    ///
    alias length = size;
    ///
    alias opDollar = length;
    ///
    size_type size() const @safe                    { return __size; }
    ///
    bool empty() const @safe                        { return __size == 0; }

    ///
    const(T)* data() const @safe                    { return __data; }
    ///
    const(T)[] as_array() const @trusted            { return __data[0 .. __size]; }

    ///
    ref const(T) at(size_type i) const @trusted     { return __data[0 .. __size][i]; }

    ///
    ref const(T) front() const @safe                { return this[0]; }
    ///
    ref const(T) back() const @safe                 { return this[$-1]; }

private:
    // use the proper field names from C++ so debugging doesn't get weird
    version (CppRuntime_Microsoft)
    {
        const_pointer _Mydata;
        size_type _Mysize;

        alias __data = _Mydata;
        alias __size = _Mysize;
    }
    else version (CppRuntime_GNU)
    {
        size_t _M_len;
        const(T)* _M_str;

        alias __data = _M_str;
        alias __size = _M_len;
    }
    else version (CppRuntime_LLVM)
    {
        const value_type* __data;
        size_type __size;
    }
    else
    {
        static assert(false, "C++ runtime not supported");
    }
}