2 * D header file for interaction with C++ std::string_view.
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)
9 * Source: $(DRUNTIMESRC core/stdcpp/string_view.d)
12 module core.stdcpp.string_view;
14 import core.stdc.stddef : wchar_t;
15 import core.stdcpp.xutility : StdNamespace;
17 // hacks to support DMD on Win32
18 version (CppRuntime_Microsoft)
20 version = CppRuntime_Windows; // use the MS runtime ABI for win32
22 else version (CppRuntime_DigitalMars)
24 version = CppRuntime_Windows; // use the MS runtime ABI for win32
25 pragma(msg, "std::basic_string_view not supported by DMC");
28 extern(C++, (StdNamespace)):
32 alias string_view = basic_string_view!char;
34 alias u16string_view = basic_string_view!wchar;
36 alias u32string_view = basic_string_view!dchar;
38 alias wstring_view = basic_string_view!wchar_t;
42 * Character traits classes specify character properties and provide specific
43 * semantics for certain operations on characters and sequences of characters.
45 extern(C++, struct) struct char_traits(CharT) {}
49 * D language counterpart to C++ std::basic_string_view.
51 * C++ reference: $(LINK2 hhttps://en.cppreference.com/w/cpp/string/basic_string_view)
53 extern(C++, class) struct basic_string_view(T, Traits = char_traits!T)
60 enum size_type npos = size_type.max;
63 alias size_type = size_t;
65 alias difference_type = ptrdiff_t;
71 alias const_pointer = const(T)*;
76 alias toString = as_array;
79 this(const(T)[] str) @trusted { __data = str.ptr; __size = str.length; }
84 alias opDollar = length;
86 size_type size() const @safe { return __size; }
88 bool empty() const @safe { return __size == 0; }
91 const(T)* data() const @safe { return __data; }
93 const(T)[] as_array() const @trusted { return __data[0 .. __size]; }
96 ref const(T) at(size_type i) const @trusted { return __data[0 .. __size][i]; }
99 ref const(T) front() const @safe { return this[0]; }
101 ref const(T) back() const @safe { return this[$-1]; }
104 // use the proper field names from C++ so debugging doesn't get weird
105 version (CppRuntime_Windows)
107 const_pointer _Mydata;
110 alias __data = _Mydata;
111 alias __size = _Mysize;
113 else version (CppRuntime_Gcc)
118 alias __data = _M_str;
119 alias __size = _M_len;
121 else version (CppRuntime_Clang)
123 const value_type* __data;
128 static assert(false, "C++ runtime not supported");