This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
Added std::string_view<> #2309
Merged
Merged
Added std::string_view<> #2309
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.