Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d95a5ac

Browse files
committed
add string_to_int to MiscUtils.h
1 parent ccd43f1 commit d95a5ac

3 files changed

Lines changed: 11 additions & 15 deletions

File tree

docs/changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
7373
- ``Lua::PushInterfaceKeys()``: transforms viewscreen ``feed()`` keys into something that can be interpreted by lua-based widgets
7474
- ``Lua::Push()``: now handles maps with otherwise supported keys and values
7575
- Constructions module: added ``insert()`` to insert constructions into the game's sorted list.
76-
- MiscUtils: moved the following string transformation functions from ``uicommon.h``: ``int_to_string``, ``ltrim``, ``rtrim``, and ``trim``
76+
- MiscUtils: moved the following string transformation functions from ``uicommon.h``: ``int_to_string``, ``ltrim``, ``rtrim``, and ``trim``; added ``string_to_int``
7777

7878
## Lua
7979
- ``widgets.Scrollbar``: new scrollbar widget that can be paired with an associated scrollable widget. Integrated with ``widgets.Label`` and ``widgets.List``.

library/include/MiscUtils.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,21 @@ DFHACK_EXPORT std::string toUpper(const std::string &str);
387387
DFHACK_EXPORT std::string toLower(const std::string &str);
388388
DFHACK_EXPORT std::string to_search_normalized(const std::string &str);
389389

390-
static inline std::string int_to_string(const int n)
391-
{
390+
static inline std::string int_to_string(const int n) {
392391
std::ostringstream ss;
393392
ss << n;
394393
return ss.str();
395394
}
396395

396+
static inline int string_to_int(const std::string s, int default_ = 0) {
397+
try {
398+
return std::stoi(s);
399+
}
400+
catch (std::exception&) {
401+
return default_;
402+
}
403+
}
404+
397405
// trim from start
398406
static inline std::string &ltrim(std::string &s) {
399407
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char x){ return !std::isspace(x); }));

plugins/autochop.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,6 @@ class WatchedBurrows
185185

186186
static WatchedBurrows watchedBurrows;
187187

188-
static int string_to_int(string s, int default_ = 0)
189-
{
190-
try
191-
{
192-
return std::stoi(s);
193-
}
194-
catch (std::exception&)
195-
{
196-
return default_;
197-
}
198-
}
199-
200188
static void save_config()
201189
{
202190
config_autochop.val() = watchedBurrows.getSerialisedIds();

0 commit comments

Comments
 (0)