Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/string_view.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added `core.stdcpp.string_view`.

Added `core.stdcpp.string_view`, which links against C++ `std::string_view`
1 change: 1 addition & 0 deletions mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ COPY=\
\
$(IMPDIR)\core\stdcpp\array.d \
$(IMPDIR)\core\stdcpp\exception.d \
$(IMPDIR)\core\stdcpp\string_view.d \
$(IMPDIR)\core\stdcpp\typeinfo.d \
$(IMPDIR)\core\stdcpp\xutility.d \
\
Expand Down
1 change: 1 addition & 0 deletions mak/DOCS
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ DOCS=\
$(DOCDIR)\core_stdc_wctype.html \
\
$(DOCDIR)\core_stdcpp_array.html \
$(DOCDIR)\core_stdcpp_string_view.html \
$(DOCDIR)\core_stdcpp_exception.html \
$(DOCDIR)\core_stdcpp_typeinfo.html \
\
Expand Down
1 change: 1 addition & 0 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SRCS=\
src\core\stdc\wchar_.d \
\
src\core\stdcpp\array.d \
src\core\stdcpp\string_view.d \
src\core\stdcpp\xutility.d \
\
src\core\sync\barrier.d \
Expand Down
3 changes: 3 additions & 0 deletions mak/WINDOWS
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ $(IMPDIR)\core\stdcpp\exception.d : src\core\stdcpp\exception.d
$(IMPDIR)\core\stdcpp\array.d : src\core\stdcpp\array.d
copy $** $@

$(IMPDIR)\core\stdcpp\string_view.d : src\core\stdcpp\string_view.d
copy $** $@

$(IMPDIR)\core\stdcpp\typeinfo.d : src\core\stdcpp\typeinfo.d
copy $** $@

Expand Down
155 changes: 155 additions & 0 deletions src/core/stdcpp/string_view.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* 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;

// hacks to support DMD on Win32
version (CppRuntime_Microsoft)
{
version = CppRuntime_Windows; // use the MS runtime ABI for win32
}
else version (CppRuntime_DigitalMars)
{
version = CppRuntime_Windows; // use the MS runtime ABI for win32
pragma(msg, "std::basic_string_view not supported by DMC");
}
version (CppRuntime_Clang)
{
private alias AliasSeq(Args...) = Args;
private enum StdNamespace = AliasSeq!("std", "__1");
}
else
{
private enum StdNamespace = "std";
}

extern(C++, (StdNamespace)):

///
alias string_view = basic_string_view!char;
//alias u16string_view = basic_string_view!wchar; // TODO: can't mangle these yet either...
//alias u32string_view = basic_string_view!dchar;
//alias wstring_view = basic_string_view!wchar_t; // TODO: we can't mangle wchar_t properly (yet?)


/**
* 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 hhttps://en.cppreference.com/w/cpp/string/basic_string_view)
*/
extern(C++, class) struct basic_string_view(T, Traits = char_traits!T)
{
extern(D):
pragma(inline, true):

///
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 length = size;
///
alias opDollar = length;
///
bool empty() const nothrow @safe @nogc { return size() == 0; }

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

version (CppRuntime_Windows)
{
///
this(const(T)[] str) nothrow @trusted @nogc { _Mydata = str.ptr; _Mysize = str.length; }

///
size_type size() const nothrow @safe @nogc { return _Mysize; }
///
const(T)* data() const nothrow @safe @nogc { return _Mydata; }
///
const(T)[] as_array() const inout @trusted @nogc { return _Mydata[0 .. _Mysize]; }
///
ref const(T) at(size_type i) const nothrow @trusted @nogc { return _Mydata[0 .. _Mysize][i]; }

version (CppRuntime_Microsoft)
{
import core.stdcpp.xutility : MSVCLinkDirectives;
mixin MSVCLinkDirectives!false;
}

private:
const_pointer _Mydata;
size_type _Mysize;
}
else version (CppRuntime_Gcc)
{
///
this(const(T)[] str) nothrow @trusted @nogc { _M_str = str.ptr; _M_len = str.length; }

///
size_type size() const nothrow @safe @nogc { return _M_len; }
///
const(T)* data() const nothrow @safe @nogc { return _M_str; }
///
const(T)[] as_array() const nothrow @trusted @nogc { return _M_str[0 .. _M_len]; }
///
ref const(T) at(size_type i) const nothrow @trusted @nogc { return _M_str[0 .. _M_len][i]; }

private:
size_t _M_len;
const(T)* _M_str;
}
else version (CppRuntime_Clang)
{
///
this(const(T)[] str) nothrow @trusted @nogc { __data = str.ptr; __size = str.length; }

///
size_type size() const nothrow @safe @nogc { return __size; }
///
const(T)* data() const nothrow @safe @nogc { return __data; }
///
const(T)[] as_array() const nothrow @trusted @nogc { return __data[0 .. __size]; }
///
ref const(T) at(size_type i) const nothrow @trusted @nogc { return __data[0 .. __size][i]; }

private:
const value_type* __data;
size_type __size;
}
else
{
static assert(false, "C++ runtime not supported");
}
}
17 changes: 17 additions & 0 deletions test/stdcpp/src/string_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <string_view>

int fromC_val(std::string_view);
int fromC_ref(const std::string_view&);

int sumOfElements_ref(const std::string_view& str)
{
int r = 0;
for (size_t i = 0; i < str.size(); ++i)
r += str[i];
return r;
}

int sumOfElements_val(std::string_view str)
{
return sumOfElements_ref(str) + fromC_ref(str) + fromC_val(str);
}
50 changes: 50 additions & 0 deletions test/stdcpp/src/string_view_test.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import core.stdcpp.string_view;

unittest
{
string_view str = string_view("Hello");

assert(str.size == 5);
assert(str.length == 5);
assert(str.empty == false);

assert(sumOfElements_val(str) == 1500);
assert(sumOfElements_ref(str) == 500);

string_view str2 = string_view();
assert(str2.size == 0);
assert(str2.length == 0);
assert(str2.empty == true);
assert(str2[] == []);
}


extern(C++):

// test the ABI for calls to C++
int sumOfElements_val(string_view);
int sumOfElements_ref(ref const(string_view));

// test the ABI for calls from C++
int fromC_val(string_view str)
{
assert(str[] == "Hello");
assert(str.front == 'H');
assert(str.back == 'o');
assert(str.at(2) == 'l');

int r;
foreach (e; str)
r += e;

assert(r == 500);
return r;
}

int fromC_ref(ref const(string_view) str)
{
int r;
foreach (e; str)
r += e;
return r;
}