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 // LDC: empty module for unsupported C++ runtimes 15 version (CppRuntime_Microsoft) version = Supported; 16 else version (CppRuntime_Gcc) version = Supported; 17 else version (CppRuntime_Clang) version = Supported; 18 version (Supported): 19 20 import core.stdc.stddef : wchar_t; 21 import core.stdcpp.xutility : StdNamespace; 22 23 // hacks to support DMD on Win32 24 version (CppRuntime_Microsoft) 25 { 26 version = CppRuntime_Windows; // use the MS runtime ABI for win32 27 } 28 else version (CppRuntime_DigitalMars) 29 { 30 version = CppRuntime_Windows; // use the MS runtime ABI for win32 31 pragma(msg, "std::basic_string_view not supported by DMC"); 32 } 33 34 extern(C++, (StdNamespace)): 35 @nogc: 36 37 /// 38 alias string_view = basic_string_view!char; 39 /// 40 alias u16string_view = basic_string_view!wchar; 41 /// 42 alias u32string_view = basic_string_view!dchar; 43 /// 44 alias wstring_view = basic_string_view!wchar_t; 45 46 47 /** 48 * Character traits classes specify character properties and provide specific 49 * semantics for certain operations on characters and sequences of characters. 50 */ 51 extern(C++, struct) struct char_traits(CharT) {} 52 53 54 /** 55 * D language counterpart to C++ std::basic_string_view. 56 * 57 * C++ reference: $(LINK2 hhttps://en.cppreference.com/w/cpp/string/basic_string_view) 58 */ 59 extern(C++, class) struct basic_string_view(T, Traits = char_traits!T) 60 { 61 extern(D): 62 pragma(inline, true): 63 pure nothrow @nogc: 64 65 /// 66 enum size_type npos = size_type.max; 67 68 /// 69 alias size_type = size_t; 70 /// 71 alias difference_type = ptrdiff_t; 72 /// 73 alias value_type = T; 74 /// 75 alias pointer = T*; 76 /// 77 alias const_pointer = const(T)*; 78 79 /// 80 alias as_array this; 81 /// 82 alias toString = as_array; 83 84 /// 85 this(const(T)[] str) @trusted { __data = str.ptr; __size = str.length; } 86 87 /// 88 alias length = size; 89 /// 90 alias opDollar = length; 91 /// 92 size_type size() const @safe { return __size; } 93 /// 94 bool empty() const @safe { return __size == 0; } 95 96 /// 97 const(T)* data() const @safe { return __data; } 98 /// 99 const(T)[] as_array() const @trusted { return __data[0 .. __size]; } 100 101 /// 102 ref const(T) at(size_type i) const @trusted { return __data[0 .. __size][i]; } 103 104 /// 105 ref const(T) front() const @safe { return this[0]; } 106 /// 107 ref const(T) back() const @safe { return this[$-1]; } 108 109 private: 110 // use the proper field names from C++ so debugging doesn't get weird 111 version (CppRuntime_Windows) 112 { 113 const_pointer _Mydata; 114 size_type _Mysize; 115 116 alias __data = _Mydata; 117 alias __size = _Mysize; 118 } 119 else version (CppRuntime_Gcc) 120 { 121 size_t _M_len; 122 const(T)* _M_str; 123 124 alias __data = _M_str; 125 alias __size = _M_len; 126 } 127 else version (CppRuntime_Clang) 128 { 129 const value_type* __data; 130 size_type __size; 131 } 132 else 133 { 134 static assert(false, "C++ runtime not supported"); 135 } 136 }