Merge pull request #2375 from myk002/myk_uicommon

Move generally useful string functions from uicommon to MiscUtils
develop
Myk 2022-11-09 10:31:50 -08:00 committed by GitHub
commit f2a386f0e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 54 additions and 44 deletions

@ -73,6 +73,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
- ``Lua::PushInterfaceKeys()``: transforms viewscreen ``feed()`` keys into something that can be interpreted by lua-based widgets - ``Lua::PushInterfaceKeys()``: transforms viewscreen ``feed()`` keys into something that can be interpreted by lua-based widgets
- ``Lua::Push()``: now handles maps with otherwise supported keys and values - ``Lua::Push()``: now handles maps with otherwise supported keys and values
- Constructions module: added ``insert()`` to insert constructions into the game's sorted list. - Constructions module: added ``insert()`` to insert constructions into the game's sorted list.
- MiscUtils: moved the following string transformation functions from ``uicommon.h``: ``int_to_string``, ``ltrim``, ``rtrim``, and ``trim``
## Lua ## Lua
- ``widgets.Scrollbar``: new scrollbar widget that can be paired with an associated scrollable widget. Integrated with ``widgets.Label`` and ``widgets.List``. - ``widgets.Scrollbar``: new scrollbar widget that can be paired with an associated scrollable widget. Integrated with ``widgets.Label`` and ``widgets.List``.

@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib> #include <cstdlib>
#include <sstream> #include <sstream>
using namespace std;
using namespace DFHack; using namespace DFHack;
#include "tinythread.h" #include "tinythread.h"

@ -61,6 +61,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "json/json.h" #include "json/json.h"
using namespace std;
using namespace DFHack; using namespace DFHack;
using dfproto::CoreTextNotification; using dfproto::CoreTextNotification;

@ -36,6 +36,7 @@ distribution.
#include "MiscUtils.h" #include "MiscUtils.h"
using namespace std;
using namespace DFHack; using namespace DFHack;
/* /*

@ -24,8 +24,10 @@ distribution.
#pragma once #pragma once
#include "Export.h" #include "Export.h"
#include <algorithm>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <cctype>
#include <climits> #include <climits>
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
@ -33,10 +35,6 @@ distribution.
#include <cstdio> #include <cstdio>
#include <memory> #include <memory>
using std::ostream;
using std::stringstream;
using std::endl;
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define DFHACK_FUNCTION_SIG __FUNCSIG__ #define DFHACK_FUNCTION_SIG __FUNCSIG__
#elif defined(__GNUC__) #elif defined(__GNUC__)
@ -83,9 +81,9 @@ using std::make_unique;
} }
template <typename T> template <typename T>
void print_bits ( T val, ostream& out ) void print_bits ( T val, std::ostream& out )
{ {
stringstream strs; std::stringstream strs;
T n_bits = sizeof ( val ) * CHAR_BIT; T n_bits = sizeof ( val ) * CHAR_BIT;
int cnt; int cnt;
for ( unsigned i = 0; i < n_bits; ++i ) for ( unsigned i = 0; i < n_bits; ++i )
@ -93,24 +91,24 @@ void print_bits ( T val, ostream& out )
cnt = i/10; cnt = i/10;
strs << cnt << " "; strs << cnt << " ";
} }
strs << endl; strs << std::endl;
for ( unsigned i = 0; i < n_bits; ++i ) for ( unsigned i = 0; i < n_bits; ++i )
{ {
cnt = i%10; cnt = i%10;
strs << cnt << " "; strs << cnt << " ";
} }
strs << endl; strs << std::endl;
for ( unsigned i = 0; i < n_bits; ++i ) for ( unsigned i = 0; i < n_bits; ++i )
{ {
strs << "--"; strs << "--";
} }
strs << endl; strs << std::endl;
for ( unsigned i = 0; i < n_bits; ++i ) for ( unsigned i = 0; i < n_bits; ++i )
{ {
strs<< !!( val & 1 ) << " "; strs<< !!( val & 1 ) << " ";
val >>= 1; val >>= 1;
} }
strs << endl; strs << std::endl;
out << strs.str(); out << strs.str();
} }
@ -389,6 +387,30 @@ DFHACK_EXPORT std::string toUpper(const std::string &str);
DFHACK_EXPORT std::string toLower(const std::string &str); DFHACK_EXPORT std::string toLower(const std::string &str);
DFHACK_EXPORT std::string to_search_normalized(const std::string &str); DFHACK_EXPORT std::string to_search_normalized(const std::string &str);
static inline std::string int_to_string(const int n)
{
std::ostringstream ss;
ss << n;
return ss.str();
}
// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char x){ return !std::isspace(x); }));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](char x){ return !std::isspace(x); }).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
enum word_wrap_whitespace_mode { enum word_wrap_whitespace_mode {
WSMODE_KEEP_ALL, WSMODE_KEEP_ALL,
WSMODE_COLLAPSE_ALL, WSMODE_COLLAPSE_ALL,

@ -24,6 +24,7 @@
#include "modules/Units.h" #include "modules/Units.h"
#include "modules/World.h" #include "modules/World.h"
using std::endl;
using std::string; using std::string;
using std::unordered_map; using std::unordered_map;
using std::unordered_set; using std::unordered_set;
@ -808,7 +809,7 @@ static void autobutcher_cycle(color_ostream &out) {
for (auto w : watched_races) { for (auto w : watched_races) {
int slaughter_count = w.second->ProcessUnits(); int slaughter_count = w.second->ProcessUnits();
if (slaughter_count) { if (slaughter_count) {
stringstream ss; std::stringstream ss;
ss << slaughter_count; ss << slaughter_count;
string announce = Units::getRaceNamePluralById(w.first) + " marked for slaughter: " + ss.str(); string announce = Units::getRaceNamePluralById(w.first) + " marked for slaughter: " + ss.str();
DEBUG(cycle,out).print("%s\n", announce.c_str()); DEBUG(cycle,out).print("%s\n", announce.c_str());

@ -23,6 +23,8 @@
#include "df/creature_raw.h" #include "df/creature_raw.h"
#include "df/world.h" #include "df/world.h"
using std::endl;
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Items; using namespace DFHack::Items;
using namespace DFHack::Units; using namespace DFHack::Units;
@ -80,7 +82,7 @@ struct ClothingRequirement
std::string Serialize() std::string Serialize()
{ {
stringstream stream; std::stringstream stream;
stream << ENUM_KEY_STR(job_type, jobType) << " "; stream << ENUM_KEY_STR(job_type, jobType) << " ";
stream << ENUM_KEY_STR(item_type,itemType) << " "; stream << ENUM_KEY_STR(item_type,itemType) << " ";
stream << item_subtype << " "; stream << item_subtype << " ";
@ -91,7 +93,7 @@ struct ClothingRequirement
void Deserialize(std::string s) void Deserialize(std::string s)
{ {
stringstream stream(s); std::stringstream stream(s);
std::string loadedJob; std::string loadedJob;
stream >> loadedJob; stream >> loadedJob;
FOR_ENUM_ITEMS(job_type, job) FOR_ENUM_ITEMS(job_type, job)
@ -138,7 +140,7 @@ struct ClothingRequirement
std::string ToReadableLabel() std::string ToReadableLabel()
{ {
stringstream stream; std::stringstream stream;
stream << bitfield_to_string(material_category) << " "; stream << bitfield_to_string(material_category) << " ";
std::string adjective = ""; std::string adjective = "";
std::string name = ""; std::string name = "";

@ -200,7 +200,7 @@ static command_result df_autonestbox(color_ostream &out, vector<string> &paramet
autonestbox_cycle(out); autonestbox_cycle(out);
} }
else { else {
out << "autonestbox is " << (is_enabled ? "" : "not ") << "running" << endl; out << "autonestbox is " << (is_enabled ? "" : "not ") << "running" << std::endl;
} }
return CR_OK; return CR_OK;
} }
@ -377,11 +377,11 @@ static size_t assign_nestboxes(color_ostream &out) {
did_complain = false; did_complain = false;
old_count = freeEgglayers; old_count = freeEgglayers;
if (!did_complain) { if (!did_complain) {
stringstream ss; std::stringstream ss;
ss << freeEgglayers; ss << freeEgglayers;
string announce = "Not enough free nestbox zones found! You need " + ss.str() + " more."; string announce = "Not enough free nestbox zones found! You need " + ss.str() + " more.";
Gui::showAnnouncement(announce, 6, true); Gui::showAnnouncement(announce, 6, true);
out << announce << endl; out << announce << std::endl;
did_complain = true; did_complain = true;
} }
} }
@ -396,12 +396,12 @@ static void autonestbox_cycle(color_ostream &out) {
size_t processed = assign_nestboxes(out); size_t processed = assign_nestboxes(out);
if (processed > 0) { if (processed > 0) {
stringstream ss; std::stringstream ss;
ss << processed << " nestboxes were assigned."; ss << processed << " nestboxes were assigned.";
string announce = ss.str(); string announce = ss.str();
DEBUG(cycle,out).print("%s\n", announce.c_str()); DEBUG(cycle,out).print("%s\n", announce.c_str());
Gui::showAnnouncement(announce, 2, false); Gui::showAnnouncement(announce, 2, false);
out << announce << endl; out << announce << std::endl;
// can complain again // can complain again
// (might lead to spamming the same message twice, but catches the case // (might lead to spamming the same message twice, but catches the case
// where for example 2 new egglayers hatched right after 2 zones were created and assigned) // where for example 2 new egglayers hatched right after 2 zones were created and assigned)

@ -256,7 +256,7 @@ command_result df_createitem (color_ostream &out, vector <string> & parameters)
if (parameters.size() == 3) if (parameters.size() == 3)
{ {
stringstream ss(parameters[2]); std::stringstream ss(parameters[2]);
ss >> count; ss >> count;
if (count < 1) if (count < 1)
{ {

@ -10,6 +10,7 @@
using std::vector; using std::vector;
using std::string; using std::string;
using std::endl;
using namespace DFHack; using namespace DFHack;
////////////////////// //////////////////////

@ -28,6 +28,7 @@
using std::vector; using std::vector;
using std::string; using std::string;
using std::endl;
using namespace DFHack; using namespace DFHack;
DFHACK_PLUGIN("kittens"); DFHACK_PLUGIN("kittens");

@ -6,6 +6,7 @@
#include <sstream> #include <sstream>
using std::vector; using std::vector;
using std::string; using std::string;
using std::endl;
#include "Core.h" #include "Core.h"
#include <Console.h> #include <Console.h>

@ -14,6 +14,7 @@
#include "df/unit.h" #include "df/unit.h"
#include "df/world.h" #include "df/world.h"
using std::endl;
using namespace DFHack; using namespace DFHack;
DFHACK_PLUGIN("fix-unit-occupancy"); DFHACK_PLUGIN("fix-unit-occupancy");

@ -13,6 +13,7 @@
//#include "df/world.h" //#include "df/world.h"
using namespace DFHack; using namespace DFHack;
using std::endl;
DFHACK_PLUGIN("generated-creature-renamer"); DFHACK_PLUGIN("generated-creature-renamer");
REQUIRE_GLOBAL(world); REQUIRE_GLOBAL(world);

@ -157,13 +157,6 @@ static inline void OutputToggleString(int &x, int &y, const char *text, df::inte
OutputToggleString(x, y, text, DFHack::Screen::getKeyDisplay(hotkey).c_str(), state, newline, left_margin, color, hotkey_color, map); OutputToggleString(x, y, text, DFHack::Screen::getKeyDisplay(hotkey).c_str(), state, newline, left_margin, color, hotkey_color, map);
} }
inline string int_to_string(const int n)
{
std::ostringstream ss;
ss << n;
return ss.str();
}
static inline void set_to_limit(int &value, const int maximum, const int min = 0) static inline void set_to_limit(int &value, const int maximum, const int min = 0)
{ {
if (value < min) if (value < min)
@ -172,23 +165,6 @@ static inline void set_to_limit(int &value, const int maximum, const int min = 0
value = maximum; value = maximum;
} }
// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char x){ return !std::isspace(x); }));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](char x){ return !std::isspace(x); }).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
inline void paint_text(const UIColor color, const int &x, const int &y, const std::string &text, const UIColor background = 0) inline void paint_text(const UIColor color, const int &x, const int &y, const std::string &text, const UIColor background = 0)
{ {
Screen::paintString(Screen::Pen(' ', color, background), x, y, text); Screen::paintString(Screen::Pen(' ', color, background), x, y, text);