From a5fe1fae19f299b59d68562636b041b75ae9a05d Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Thu, 4 Jan 2024 06:01:19 -0800 Subject: [PATCH] Update MiscUtils.h --- library/include/MiscUtils.h | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 06915fde5..2ecda4d9c 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -61,6 +61,8 @@ namespace DFHack { class color_ostream; } +DFHACK_EXPORT int random_int(int max); + template void print_bits ( T val, std::ostream& out ) { @@ -178,6 +180,17 @@ inline int binsearch_index(const std::vector &vec, typename CT::key_pointer return CT::binsearch_index(vec, key, exact); } +template +int random_index(const std::vector& vec) +{ + if (vec.empty()) + return -1; + else if (vec.size() == 1) + return 0; + + return random_int(vec.size()); +} + template inline bool vector_contains(const std::vector &vec, KT key) { @@ -199,6 +212,12 @@ inline T vector_get(const std::vector &vec, unsigned idx, const T &defval = T return defval; } +template +inline T vector_get_random(const std::vector& vec, const T& defval = T()) +{ + return vector_get(vec, random_index(vec), defval); +} + template inline void vector_insert_at(std::vector &vec, unsigned idx, const T &val) { @@ -401,6 +420,7 @@ inline std::string join_strings(const std::string &separator, T &items) { DFHACK_EXPORT std::string toUpper(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 capitalize_string_words(const std::string& str); static inline std::string int_to_string(const int n) { std::ostringstream ss; @@ -444,6 +464,13 @@ DFHACK_EXPORT bool word_wrap(std::vector *out, const std::string &str, size_t line_length = 80, word_wrap_whitespace_mode mode = WSMODE_KEEP_ALL); +/** +* Function to assist in parsing tokens. Returns string from source pos until next compc, ']', or end. +* pos should be set to position after '[' to start with, then incremented by the result's size+1 before subsequent calls. +* compc is usually ':', but can be '.' for parsing number ranges. +* Based on Bay12's g_src/basics.cpp +*/ +std::string grab_token_string_pos(const std::string& source, int32_t pos, char compc = ':'); inline bool bits_match(unsigned required, unsigned ok, unsigned mask) { @@ -457,8 +484,6 @@ inline T clip_range(T a, T1 minv, T2 maxv) { return a; } -DFHACK_EXPORT int random_int(int max); - /** * Returns the amount of milliseconds elapsed since the UNIX epoch. * Works on both windows and linux.