-
Notifications
You must be signed in to change notification settings - Fork 62
Add StringView and Span #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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
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,41 @@ | ||
| /* | ||
| * This file is part of liblcf. Copyright (c) 2020 liblcf authors. | ||
| * https://github.com/EasyRPG/liblcf - https://easyrpg.org | ||
| * | ||
| * liblcf is Free/Libre Open Source Software, released under the MIT License. | ||
| * For the full copyright and license information, please view the COPYING | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| #ifndef LCF_SPAN_H | ||
| #define LCF_SPAN_H | ||
| #include <cstring> | ||
| #include <string> | ||
| #include <cassert> | ||
| #include <iterator> | ||
| #include <ostream> | ||
| #include <algorithm> | ||
|
|
||
| #define span_CONFIG_NO_EXCEPTIONS 1 | ||
| #define span_FEATURE_WITH_CONTAINER 1 | ||
| #define span_FEATURE_CONSTRUCTION_FROM_STDARRAY_ELEMENT_TYPE 1 | ||
| #define span_FEATURE_MAKE_SPAN 1 | ||
| #include <lcf/third_party/span.h> | ||
|
|
||
| namespace lcf { | ||
|
|
||
| using ExtentT = nonstd::span_lite::extent_t; | ||
| using nonstd::dynamic_extent; | ||
|
|
||
| template <typename T, ExtentT Extent= dynamic_extent> | ||
| using Span = nonstd::span<T,Extent>; | ||
|
|
||
| template <typename... Args> | ||
| constexpr inline auto MakeSpan(Args&&... args) noexcept -> decltype(nonstd::make_span(std::forward<Args>(args)...)) { | ||
| return nonstd::make_span(std::forward<Args>(args)...); | ||
| } | ||
|
|
||
|
|
||
| } // namespace lcf | ||
|
|
||
| #endif |
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,76 @@ | ||
| /* | ||
| * This file is part of liblcf. Copyright (c) 2020 liblcf authors. | ||
| * https://github.com/EasyRPG/liblcf - https://easyrpg.org | ||
| * | ||
| * liblcf is Free/Libre Open Source Software, released under the MIT License. | ||
| * For the full copyright and license information, please view the COPYING | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| #ifndef LCF_STRING_VIEW_H | ||
| #define LCF_STRING_VIEW_H | ||
| #include <cstring> | ||
| #include <string> | ||
| #include <cassert> | ||
| #include <iterator> | ||
| #include <ostream> | ||
| #include <algorithm> | ||
|
|
||
| #define nssv_CONFIG_NO_EXCEPTIONS 1 | ||
| #define nssv_CONFIG_CONVERSION_STD_STRING 1 | ||
| #include <lcf/third_party/string_view.h> | ||
|
|
||
| namespace lcf { | ||
|
|
||
| template <typename CharT, typename Traits = std::char_traits<CharT>> | ||
| using BasicStringView = nonstd::basic_string_view<CharT,Traits>; | ||
|
|
||
| using StringView = BasicStringView<char>; | ||
| using WStringView = BasicStringView<wchar_t>; | ||
| using U16StringView = BasicStringView<char16_t>; | ||
| using U32StringView = BasicStringView<char32_t>; | ||
|
|
||
| template< class CharT, class Traits, class Allocator = std::allocator<CharT> > | ||
| std::basic_string<CharT, Traits, Allocator> | ||
| ToString(BasicStringView<CharT,Traits> sv, const Allocator& a = Allocator()) { | ||
| return nonstd::to_string(sv, a); | ||
| } | ||
|
|
||
| template< class CharT, class Traits, class Allocator > | ||
| BasicStringView<CharT, Traits> | ||
| ToStringView(const std::basic_string<CharT, Traits, Allocator>& s ) | ||
| { | ||
| return nonstd::to_string_view(s); | ||
| } | ||
|
|
||
| // Make sure converting string to string is an error. To catch bugs. | ||
| template <typename C, typename T, typename A> | ||
| void ToString(const std::basic_string<C,T,A>&) = delete; | ||
|
|
||
| // We use macros instead of template to enable implicit string -> string_view conversion here. | ||
| #define LCF_STRING_VIEW_FREE_FUNCTIONS(TYPE)\ | ||
| inline TYPE Substr(TYPE s, size_t pos = 0, size_t count = TYPE::npos) { return s.substr(pos, count); }\ | ||
| inline bool StartsWith(TYPE str, TYPE prefix) { return str.starts_with(prefix); }\ | ||
| inline bool StartsWith(TYPE str, TYPE::value_type prefix) { return str.starts_with(prefix); }\ | ||
| inline bool StartsWith(TYPE str, const TYPE::value_type* prefix) { return str.starts_with(prefix); }\ | ||
| inline bool EndsWith(TYPE str, TYPE suffix) { return str.ends_with(suffix); }\ | ||
| inline bool EndsWith(TYPE str, TYPE::value_type suffix) { return str.ends_with(suffix); }\ | ||
| inline bool EndsWith(TYPE str, const TYPE::value_type* suffix) { return str.ends_with(suffix); } | ||
|
|
||
| LCF_STRING_VIEW_FREE_FUNCTIONS(StringView) | ||
| LCF_STRING_VIEW_FREE_FUNCTIONS(WStringView) | ||
| LCF_STRING_VIEW_FREE_FUNCTIONS(U16StringView) | ||
| LCF_STRING_VIEW_FREE_FUNCTIONS(U32StringView) | ||
|
|
||
| #undef LCF_STRING_VIEW_FREE_FUNCTIONS | ||
|
|
||
| inline int SvAtoi(StringView str) { | ||
| const char* b = str.data(); | ||
| const char* e = str.data() + str.length(); | ||
| auto value = std::strtol(b, const_cast<char**>(&e), 10); | ||
| return e > b ? value : 0; | ||
| } | ||
|
|
||
| } // namespace lcf | ||
|
|
||
| #endif | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example what you get with these macros that looks much worse without?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the macro I can write this:
Without it, I have to write this because C++ templates aren't smart enough to figure out that basic_string<C,T,A> should be implicitly convertable to BasicStringView<C,T>;
Maybe C++17 deduction guides can help with this?
Converting string -> string_view is normal and doesn't cost in performance, so it should be allowed freely. Going the other way means making a copy and thus should be flagged.