Debian Codegraph @

libphobos/libdruntime/core/stdcpp/string_view.d

raw
1 /**
2 * D header file for interaction with C++ std::string_view.
3 *
4 * Copyright: Copyright (c) 2018 D Language Foundation
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Manu Evans
9 * Source: $(DRUNTIMESRC core/stdcpp/string_view.d)
10 */
11
12 module core.stdcpp.string_view;
13
14 import core.stdc.stddef : wchar_t;
15 import core.stdcpp.xutility : StdNamespace;
16
17 extern(C++, (StdNamespace)):
18 @nogc:
19
20 ///
21 alias string_view = basic_string_view!char;
22 ///
23 alias u16string_view = basic_string_view!wchar;
24 ///
25 alias u32string_view = basic_string_view!dchar;
26 ///
27 alias wstring_view = basic_string_view!wchar_t;
28
29
30 /**
31 * Character traits classes specify character properties and provide specific
32 * semantics for certain operations on characters and sequences of characters.
33 */
34 extern(C++, struct) struct char_traits(CharT) {}
35
36
37 /**
38 * D language counterpart to C++ std::basic_string_view.
39 *
40 * C++ reference: $(LINK2 https://en.cppreference.com/w/cpp/string/basic_string_view, std::basic_string_view)
41 */
42 extern(C++, class) struct basic_string_view(T, Traits = char_traits!T)
43 {
44 extern(D):
45 pragma(inline, true):
46 pure nothrow @nogc:
47
48 ///
49 enum size_type npos = size_type.max;
50
51 ///
52 alias size_type = size_t;
53 ///
54 alias difference_type = ptrdiff_t;
55 ///
56 alias value_type = T;
57 ///
58 alias pointer = T*;
59 ///
60 alias const_pointer = const(T)*;
61
62 ///
63 alias as_array this;
64 ///
65 alias toString = as_array;
66
67 ///
68 this(const(T)[] str) @trusted { __data = str.ptr; __size = str.length; }
69
70 ///
71 alias length = size;
72 ///
73 alias opDollar = length;
74 ///
75 size_type size() const @safe { return __size; }
76 ///
77 bool empty() const @safe { return __size == 0; }
78
79 ///
80 const(T)* data() const @safe { return __data; }
81 ///
82 const(T)[] as_array() const @trusted { return __data[0 .. __size]; }
83
84 ///
85 ref const(T) at(size_type i) const @trusted { return __data[0 .. __size][i]; }
86
87 ///
88 ref const(T) front() const @safe { return this[0]; }
89 ///
90 ref const(T) back() const @safe { return this[$-1]; }
91
92 private:
93 // use the proper field names from C++ so debugging doesn't get weird
94 version (CppRuntime_Microsoft)
95 {
96 const_pointer _Mydata;
97 size_type _Mysize;
98
99 alias __data = _Mydata;
100 alias __size = _Mysize;
101 }
102 else version (CppRuntime_GNU)
103 {
104 size_t _M_len;
105 const(T)* _M_str;
106
107 alias __data = _M_str;
108 alias __size = _M_len;
109 }
110 else version (CppRuntime_LLVM)
111 {
112 const value_type* __data;
113 size_type __size;
114 }
115 else
116 {
117 static assert(false, "C++ runtime not supported");
118 }
119 }