Skip to content

Commit 45351d1

Browse files
committed
perf: use reference if possible
1 parent 748165b commit 45351d1

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

ini/INIReader.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,20 @@ class INIReader {
232232
std::string section) const;
233233

234234
template <typename T = std::string>
235-
T Get(std::string section, std::string name) const;
235+
T Get(const std::string& section, const std::string& name) const;
236236

237237
template <typename T>
238-
T Get(std::string section, std::string name, T&& default_v) const;
238+
T Get(const std::string& section, const std::string& name,
239+
T&& default_v) const;
239240

240241
template <typename T = std::string>
241-
std::vector<T> GetVector(std::string section, std::string name) const;
242+
std::vector<T> GetVector(const std::string& section,
243+
const std::string& name) const;
242244

243245
template <typename T>
244-
std::vector<T> GetVector(std::string section, std::string name,
245-
std::vector<T> default_v) const;
246+
std::vector<T> GetVector(const std::string& section,
247+
const std::string& name,
248+
const std::vector<T>& default_v) const;
246249

247250
protected:
248251
int _error;
@@ -253,7 +256,7 @@ class INIReader {
253256
const char* value);
254257

255258
template <typename T>
256-
T Converter(std::string s) const;
259+
T Converter(const std::string& s) const;
257260

258261
const bool BoolConverter(std::string s) const;
259262
};
@@ -315,7 +318,8 @@ inline const std::unordered_map<std::string, std::string> INIReader::Get(
315318
}
316319

317320
template <typename T>
318-
inline T INIReader::Get(std::string section, std::string name) const {
321+
inline T INIReader::Get(const std::string& section,
322+
const std::string& name) const {
319323
auto const _section = Get(section);
320324
auto const _value = _section.find(name);
321325

@@ -336,7 +340,7 @@ inline T INIReader::Get(std::string section, std::string name) const {
336340
}
337341

338342
template <typename T>
339-
inline T INIReader::Get(std::string section, std::string name,
343+
inline T INIReader::Get(const std::string& section, const std::string& name,
340344
T&& default_v) const {
341345
try {
342346
return Get<T>(section, name);
@@ -346,16 +350,16 @@ inline T INIReader::Get(std::string section, std::string name,
346350
}
347351

348352
template <typename T>
349-
inline std::vector<T> INIReader::GetVector(std::string section,
350-
std::string name) const {
353+
inline std::vector<T> INIReader::GetVector(const std::string& section,
354+
const std::string& name) const {
351355
std::string value = Get(section, name);
352356

353357
std::istringstream out{value};
354358
const std::vector<std::string> strs{std::istream_iterator<std::string>{out},
355359
std::istream_iterator<std::string>()};
356360
try {
357361
std::vector<T> vs{};
358-
for (std::string s : strs) {
362+
for (const std::string& s : strs) {
359363
vs.emplace_back(Converter<T>(s));
360364
}
361365
return vs;
@@ -366,9 +370,9 @@ inline std::vector<T> INIReader::GetVector(std::string section,
366370
}
367371

368372
template <typename T>
369-
inline std::vector<T> INIReader::GetVector(std::string section,
370-
std::string name,
371-
std::vector<T> default_v) const {
373+
inline std::vector<T> INIReader::GetVector(
374+
const std::string& section, const std::string& name,
375+
const std::vector<T>& default_v) const {
372376
try {
373377
return GetVector<T>(section, name);
374378
} catch (std::runtime_error& e) {
@@ -377,7 +381,7 @@ inline std::vector<T> INIReader::GetVector(std::string section,
377381
}
378382

379383
template <typename T>
380-
inline T INIReader::Converter(std::string s) const {
384+
inline T INIReader::Converter(const std::string& s) const {
381385
try {
382386
T v{};
383387
std::istringstream _{s};
@@ -391,7 +395,7 @@ inline T INIReader::Converter(std::string s) const {
391395

392396
inline const bool INIReader::BoolConverter(std::string s) const {
393397
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
394-
const std::unordered_map<std::string, bool> s2b{
398+
static const std::unordered_map<std::string, bool> s2b{
395399
{"1", true}, {"true", true}, {"yes", true}, {"on", true},
396400
{"0", false}, {"false", false}, {"no", false}, {"off", false},
397401
};

0 commit comments

Comments
 (0)