Skip to content

Commit b641dcb

Browse files
authored
Merge pull request #5 from cpp-gamedev/bugfix
fix: replace string_view with string to avoid missing letters
2 parents 10e1b34 + c5968a8 commit b641dcb

File tree

7 files changed

+16
-19
lines changed

7 files changed

+16
-19
lines changed

CMakePresets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": 1,
2+
"version": 2,
33
"cmakeMinimumRequired": {
44
"major": 3,
55
"minor": 19,

include/pini/pini.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class pini {
1515

1616
map_type const& get_pairs() const { return key_value_pairs; }
1717
bool load_file(std::filesystem::path const& filename);
18-
bool load_text(std::string_view text);
18+
bool load_text(std::string const& text);
1919

2020
// iteration
2121

include/pini/util.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ std::vector<std::string> get_lines(std::filesystem::path const& filename);
1717

1818
// extract key-value pairs from given vector of strings and insert them in an
1919
// unordered_map
20-
std::unordered_map<std::string, std::string> insert_pairs(std::vector<std::string> file_lines);
20+
std::unordered_map<std::string, std::string> insert_pairs(std::vector<std::string> const& file_lines);
2121

22-
std::string_view trim_whitespace(std::string_view str);
22+
std::string trim_whitespace(std::string const& str);
2323

2424
// data conversions:
2525

src/pini.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pini::map_type read_file(std::filesystem::path const& filename) {
1313
return util::insert_pairs(lines);
1414
}
1515

16-
pini::map_type read_text(std::string_view text) {
16+
pini::map_type read_text(std::string const& text) {
1717
std::string str;
1818
std::stringstream str_stream;
1919
str_stream << text;
@@ -29,7 +29,7 @@ bool pini::load_file(std::filesystem::path const& filename) {
2929
return !key_value_pairs.empty();
3030
}
3131

32-
bool pini::load_text(std::string_view text) {
32+
bool pini::load_text(std::string const& text) {
3333
key_value_pairs = read_text(text);
3434
return !key_value_pairs.empty();
3535
}

src/util.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#include <cassert>
21
#include <cctype>
3-
#include <cstddef>
4-
#include <string_view>
2+
53
#include <pini/pini.hpp>
64
#include <pini/util.hpp>
75
namespace pn {
@@ -20,29 +18,29 @@ std::vector<std::string> util::get_lines(std::filesystem::path const& filename)
2018
return ret;
2119
}
2220

23-
std::unordered_map<std::string, std::string> util::insert_pairs(std::vector<std::string> file_lines) {
21+
std::unordered_map<std::string, std::string> util::insert_pairs(std::vector<std::string> const& file_lines) {
2422
std::unordered_map<std::string, std::string> key_value_pairs;
2523
std::size_t line_number = 0;
2624
for (const auto& str : file_lines) {
2725
++line_number;
2826
if (auto delimeter_pos = str.find('='); delimeter_pos != std::string::npos) {
29-
std::string_view const key = trim_whitespace(str.substr(0, delimeter_pos));
27+
std::string const key = trim_whitespace(str.substr(0, delimeter_pos));
3028
if (key.empty()) {
3129
if (pn::pini::on_msg) {
3230
std::stringstream str;
3331
str << "Line number " << line_number << " is not valid because the key is empty.\n";
3432
(*pn::pini::on_msg)(str.str(), pn::pini::severity::warn);
3533
}
3634
} else {
37-
std::string_view const value = trim_whitespace(str.substr(delimeter_pos + 1));
35+
std::string const value = trim_whitespace(str.substr(delimeter_pos + 1));
3836
if (key[0] != '#') { key_value_pairs.insert({std::string(key), std::string(value)}); }
3937
}
4038
}
4139
}
4240
return key_value_pairs;
4341
}
4442

45-
std::string_view util::trim_whitespace(std::string_view str) {
43+
std::string util::trim_whitespace(std::string const& str) {
4644
if (str.empty()) { return {}; }
4745
std::size_t start = 0;
4846
std::size_t finish = str.size();

test/pini_test.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
int main() {
66
pn::pini pin;
77

8-
// std::string_view raw_input{"= 5\n#b=10\n c = 23"};
9-
std::filesystem::path filename{"/home/nikolaj/projects/cpp/pini/test/test.pini"};
8+
std::string raw_input{"health = 9\nattack = 4.5\nb=32\n#comment\ndef = -9.2314\nskill points = 8\n"};
109
static int ret = 0;
11-
pin.on_msg = [](std::string_view msg, pn::pini::severity level) {
10+
pn::pini::on_msg = [](std::string_view msg, pn::pini::severity level) {
1211
auto& out = level == pn::pini::severity::error ? std::cerr : std::cout;
1312
out << "Custom: || " << msg << '\n';
1413
if (level == pn::pini::severity::error) { ret = 1; }
1514
};
16-
if (!pin.load_file(filename)) { return 1; }
15+
if (!pin.load_text(raw_input)) { return 1; }
1716
if (pin.empty()) { return 1; }
1817
if (pin.contains({})) { return 1; }
1918
if (pin.contains("#comment")) { return 1; }
2019
if (pin.get_int32("health") != 9) { return 1; }
2120

2221
for (auto& pair : pin) { std::cout << pair.first << " " << pair.second << '\n'; }
23-
std::cout << pin.size() << " | " << pin.empty();
22+
std::cout << pin.size() << " | " << pin.empty() << '\n';
2423
pin.erase("def");
2524
for (auto& pair : pin) { std::cout << pair.first << " " << pair.second << '\n'; }
2625
return ret;

test/test.pini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
health = 9
22
attack = 4.5
3-
= 32
3+
b=32
44
#comment
55
def = -9.2314
66
skill points = 8

0 commit comments

Comments
 (0)