Skip to content

Commit

Permalink
Add support for std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
elrinor committed May 6, 2024
1 parent 6986c5a commit 63b1afe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

jobs:
build:
name: ${{matrix.config.name}}
name: "${{matrix.config.name}} C++${{matrix.cxx}}"
runs-on: ${{matrix.config.os}}
strategy:
matrix:
Expand All @@ -27,12 +27,15 @@ jobs:
- os: macos-latest
name: Mac OS Release
build_type: Release
cxx:
- 11
- 17
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Configure
run: cmake -S . -B ${{runner.workspace}}/build -DCMAKE_BUILD_TYPE=${{matrix.config.build_type}} -DBUILD_TESTS=True -DBUILD_EXAMPLES=True
run: cmake -S . -B ${{runner.workspace}}/build -DCMAKE_BUILD_TYPE=${{matrix.config.build_type}} -DBUILD_TESTS=True -DBUILD_EXAMPLES=True -DINICPP_CXX_STANDARD=${{matrix.cxx}}
- name: Build
run: cmake --build ${{runner.workspace}}/build --config ${{matrix.config.build_type}}
- name: Test
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ project(inifile-cpp)

include(CTest)

SET(CMAKE_CXX_STANDARD 11)
set(INICPP_CXX_STANDARD "11" CACHE STRING "C++ standard to use when building tests & examples.")

SET(CMAKE_CXX_STANDARD ${INICPP_CXX_STANDARD})
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_COMPILER_IS_GNUCXX)
Expand Down
21 changes: 21 additions & 0 deletions include/inicpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include <sstream>
#include <stdexcept>
#include <vector>
#include <string>

#ifdef __cpp_lib_string_view // This one is defined in <string> if we have std::string_view
# include <string_view>
#endif

namespace ini
{
Expand Down Expand Up @@ -311,6 +316,22 @@ namespace ini
}
};

#ifdef __cpp_lib_string_view
template<>
struct Convert<std::string_view>
{
void decode(const std::string &value, std::string_view &result)
{
result = value;
}

void encode(const std::string_view value, std::string &result)
{
result = value;
}
};
#endif

template<>
struct Convert<const char*>
{
Expand Down
25 changes: 25 additions & 0 deletions test/test_inifile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ TEST_CASE("parse field as const char*", "IniFile")
REQUIRE(std::strcmp(inif["Foo"]["bar2"].as<const char*>(), "world") == 0);
}

#ifdef __cpp_lib_string_view
TEST_CASE("parse field as std::string_view", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=hello\nbar2=world");
ini::IniFile inif(ss);

REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<std::string_view>() == "hello");
REQUIRE(inif["Foo"]["bar2"].as<std::string_view>() == "world");
}
#endif

TEST_CASE("parse field with custom field sep", "IniFile")
{
std::istringstream ss("[Foo]\nbar1:true\nbar2:false\nbar3:tRuE");
Expand Down Expand Up @@ -537,6 +550,18 @@ TEST_CASE("save with string literal fields", "IniFile")
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
}

#ifdef __cpp_lib_string_view
TEST_CASE("save with std::string_view fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = std::string_view("hello");
inif["Foo"]["bar2"] = std::string_view("world");

std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
}
#endif

TEST_CASE("save with custom field sep", "IniFile")
{
ini::IniFile inif(':', '#');
Expand Down

0 comments on commit 63b1afe

Please sign in to comment.