+ str_replace(str, "\\", "\\\\");
+ str_replace(str, "]", "\\]");
+ str_replace(str, "[", "\\[");
+ str_replace(str, ";", "\\;");
+ str_replace(str, ",", "\\,");
+}
+
+/**
+ * Replace all occurrences of the character \p from in \p str with \p to.
+ *
+ * @param str The string to (potentially) modify.
+ * @param from The character in str to replace.
+ * @param to The replacement character.
+ */
+void str_replace(std::string &str, char from, char to);
+
+
+/**
+ * Check that a string only contains whitelisted characters. This is the
+ * opposite of string_allowed_blacklist().
+ *
+ * @param str The string to be checked.
+ * @param allowed_chars A string containing permitted characters.
+ * @return true if the string is allowed, otherwise false.
+ *
+ * @see string_allowed_blacklist()
+ */
+inline bool string_allowed(const std::string &str, const std::string &allowed_chars)
+{
+ return str.find_first_not_of(allowed_chars) == str.npos;
+}
+
+
+/**
+ * Check that a string contains no blacklisted characters. This is the
+ * opposite of string_allowed().
+ *
+ * @param str The string to be checked.
+ * @param blacklisted_chars A string containing prohibited characters.
+ * @return true if the string is allowed, otherwise false.
+
+ * @see string_allowed()
+ */
+inline bool string_allowed_blacklist(const std::string &str,
+ const std::string &blacklisted_chars)
+{
+ return str.find_first_of(blacklisted_chars) == str.npos;
+}
+
+
+/**
+ * Create a string based on \p from where a newline is forcefully inserted
+ * every \p row_len characters.
+ *
+ * @note This function does not honour word wraps and blindy inserts a newline
+ * every \p row_len characters whether it breaks a word or not. It is
+ * intended to be used for, for example, showing paths in the GUI.
+ *
+ * @note This function doesn't wrap inside utf-8 multibyte sequences and also
+ * counts multibyte sequences correcly as single characters.
+ *
+ * @param from The (utf-8) string to be wrapped into rows.
+ * @param row_len The row length (in characters).
+ * @return A new string with the wrapping applied.
+ */
+inline std::string wrap_rows(const std::string &from,
+ unsigned row_len)
+{
+ std::string to;
+
+ size_t character_idx = 0;
+ for (size_t i = 0; i < from.size(); i++) {
+ if (!IS_UTF8_MULTB_INNER(from[i])) {
+ // Wrap string after last inner byte of char
+ if (character_idx > 0 && character_idx % row_len == 0)
+ to += '\n';
+ character_idx++;
+ }
+ to += from[i];