Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ set(LCF_HEADERS
src/lcf/reader_xml.h
src/lcf/saveopt.h
src/lcf/scope_guard.h
src/lcf/span.h
src/lcf/string_view.h
src/lcf/writer_lcf.h
src/lcf/writer_xml.h
src/generated/lcf/ldb/chunks.h
Expand Down Expand Up @@ -281,6 +283,8 @@ set(LCF_HEADERS
src/generated/lcf/rpg/trooppage.h
src/generated/lcf/rpg/trooppagecondition.h
src/generated/lcf/rpg/variable.h
src/lcf/third_party/span.h
src/lcf/third_party/string_view.h
)

list(TRANSFORM LCF_SOURCES PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/)
Expand All @@ -290,7 +294,7 @@ list(APPEND LCF_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/src/lcf/config.h)
target_sources(lcf PRIVATE ${LCF_SOURCES} PUBLIC ${LCF_HEADERS})

# IDE source grouping
foreach(SG LDB LMT LMU LSD RPG)
foreach(SG LDB LMT LMU LSD RPG THIRD_PARTY)
string(TOLOWER ${SG} LSG)
source_group("Source Files\\generated\\${SG}" REGULAR_EXPRESSION "generated/${LSG}_.*\\.cpp")
source_group("Header Files\\generated\\${SG}" REGULAR_EXPRESSION "generated/lcf/${LSG}/.*\\.h")
Expand Down Expand Up @@ -387,7 +391,7 @@ install(
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

set(LCF_INSTALL_HEADERS ${LCF_HEADERS})
foreach(S ldb lmt lmu lsd rpg)
foreach(S ldb lmt lmu lsd rpg third_party)
set(SUBDIR_HEADERS ${LCF_INSTALL_HEADERS})
list(FILTER SUBDIR_HEADERS INCLUDE REGEX "/${S}/")
install(FILES ${SUBDIR_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lcf/${S})
Expand Down
7 changes: 7 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lcflmtincludedir = $(includedir)/lcf/lmt
lcflmuincludedir = $(includedir)/lcf/lmu
lcflsdincludedir = $(includedir)/lcf/lsd
lcfrpgincludedir = $(includedir)/lcf/rpg
lcfthirdpartyincludedir = $(includedir)/lcf/third_party

lib_LTLIBRARIES = liblcf.la
liblcf_la_CPPFLAGS = \
Expand Down Expand Up @@ -210,6 +211,8 @@ lcfinclude_HEADERS = \
src/lcf/reader_xml.h \
src/lcf/saveopt.h \
src/lcf/scope_guard.h \
src/lcf/span.h \
src/lcf/string_view.h \
src/lcf/writer_lcf.h \
src/lcf/writer_xml.h

Expand Down Expand Up @@ -298,6 +301,10 @@ lcfrpginclude_HEADERS = \
src/generated/lcf/rpg/trooppagecondition.h \
src/generated/lcf/rpg/variable.h

lcfthirdpartyinclude_HEADERS = \
src/lcf/third_party/span.h \
src/lcf/third_party/string_view.h

nodist_lcfinclude_HEADERS = autogen/lcf/config.h

check_PROGRAMS = test_runner
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ See the file [COPYING] for copying conditions.
### 3rd party software

liblcf code includes a copy of [inih] under New BSD license.
liblcf code includes a copy of [string-view-lite] and [span-lite] under Boost Software License, Version 1.0.
See the source code comment headers for license details.


Expand All @@ -119,3 +120,5 @@ See the source code comment headers for license details.
[#easyrpg at irc.freenode.net]: https://kiwiirc.com/nextclient/#ircs://irc.freenode.net/#easyrpg?nick=rpgguest??
[COPYING]: COPYING
[inih]: https://github.com/benhoyt/inih
[string-view-lite]: https://github.com/martinmoene/string-view-lite
[span-lite]: https://github.com/martinmoene/span-lite
41 changes: 41 additions & 0 deletions src/lcf/span.h
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
76 changes: 76 additions & 0 deletions src/lcf/string_view.h
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.
Copy link
Member

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?

Copy link
Contributor Author

@mateofio mateofio Aug 19, 2020

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:

std::string x;
Substr(x, 2, 3);

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?

std::string x;
Substr(ToStringView(x), 2, 3);

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.

std::string y;
StringView x;
x = y; // Ok
y = x; // Error
y = ToString(x); // Ok, you meant to make a copy 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
Loading