#include "string.h"
#include "serialize.h"
#include "numeric.h"
#include "log.h"
#include "hex.h"
#include "porting.h"
#include "translation.h"
#include "strfnd.h"
#include <algorithm>
#include <array>
#include <sstream>
#include <iomanip>
#include <unordered_map>
#include <iconv.h>
Functions | |
static bool | convert (iconv_t cd, char *outbuf, size_t *outbuf_size, char *inbuf, size_t inbuf_size) |
std::wstring | utf8_to_wide (std::string_view input) |
std::string | wide_to_utf8 (std::wstring_view input) |
void | wide_add_codepoint (std::wstring &result, char32_t codepoint) |
std::string | urlencode (std::string_view str) |
std::string | urldecode (std::string_view str) |
u32 | readFlagString (std::string str, const FlagDesc *flagdesc, u32 *flagmask) |
std::string | writeFlagString (u32 flags, const FlagDesc *flagdesc, u32 flagmask) |
size_t | mystrlcpy (char *dst, const char *src, size_t size) noexcept |
char * | mystrtok_r (char *s, const char *sep, char **lasts) noexcept |
u64 | read_seed (const char *str) |
static bool | parseHexColorString (const std::string &value, video::SColor &color, unsigned char default_alpha) |
static bool | parseNamedColorString (const std::string &value, video::SColor &color) |
bool | parseColorString (const std::string &value, video::SColor &color, bool quiet, unsigned char default_alpha) |
std::string | encodeHexColorString (video::SColor color) |
void | str_replace (std::string &str, char from, char to) |
Replace all occurrences of the character from in str with to . | |
std::string | wrap_rows (std::string_view from, unsigned row_len, bool has_color_codes) |
Create a string based on from where a newline is forcefully inserted every row_len characters. | |
static void | translate_all (std::wstring_view s, size_t &i, Translations *translations, std::wstring &res) |
static void | translate_string (std::wstring_view s, Translations *translations, const std::wstring &textdomain, size_t &i, std::wstring &res, bool use_plural, unsigned long int number) |
std::wstring | translate_string (std::wstring_view s, Translations *translations) |
std::wstring | translate_string (std::wstring_view s) |
std::string | sanitizeDirName (std::string_view str, std::string_view optional_prefix) |
Sanitize the name of a new directory. | |
template<class F > | |
void | remove_indexed (std::string &s, F pred) |
std::string | sanitize_untrusted (std::string_view str, bool keep_escapes) |
Sanitize an untrusted string (e.g. | |
void | safe_print_string (std::ostream &os, std::string_view str) |
Prints a sanitized version of a string without control characters. | |
v3f | str_to_v3f (std::string_view str) |
Parses a string of form (1, 2, 3) to a v3f. | |
Variables | |
constexpr auto | DEFAULT_ENCODING |
static const std::unordered_map< std::string, u32 > | s_named_colors |
static const std::array< std::wstring_view, 30 > | disallowed_dir_names |
static const std::wstring_view | disallowed_path_chars = L"<>:\"/\\|?*." |
List of characters that are blacklisted from created directories. | |
|
static |
std::string encodeHexColorString | ( | video::SColor | color | ) |
References hex_encode().
Referenced by WearBarParams::serializeJson().
|
noexcept |
References MYMIN.
|
noexcept |
bool parseColorString | ( | const std::string & | value, |
video::SColor & | color, | ||
bool | quiet, | ||
unsigned char | default_alpha ) |
References errorstream, parseHexColorString(), and parseNamedColorString().
Referenced by ChatBuffer::ChatBuffer(), EnrichedString::addAtEnd(), check_color(), WearBarParams::deserializeJson(), ImageSource::generateImagePart(), StyleSpec::getColor(), StyleSpec::getColor(), StyleSpec::getColorArray(), ParsedText::globalTag(), GUIFormSpecMenu::parseBackgroundColor(), GUIFormSpecMenu::parseBox(), GUIFormSpecMenu::parseListColors(), GUIFormSpecMenu::parseTooltip(), read_color(), ParsedText::Element::setStyle(), GUITable::setTable(), and GUITable::setTextList().
|
static |
References hex_digit_decode().
Referenced by parseColorString().
|
static |
References hex_digit_decode(), lowercase(), and s_named_colors.
Referenced by parseColorString().
u64 read_seed | ( | const char * | str | ) |
References murmur_hash_64_ua().
Referenced by MapgenParams::readParams().
u32 readFlagString | ( | std::string | str, |
const FlagDesc * | flagdesc, | ||
u32 * | flagmask ) |
References FlagDesc::flag, and FlagDesc::name.
Referenced by Settings::getFlagStr(), read_flags(), and TestSettings::testFlagDesc().
void remove_indexed | ( | std::string & | s, |
F | pred ) |
void safe_print_string | ( | std::ostream & | os, |
std::string_view | str ) |
Prints a sanitized version of a string without control characters.
'\t' and '
' are allowed, as are UTF-8 control characters (e.g. RTL). ASCII control characters are replaced with their hex encoding in angle brackets (e.g. "a\x1eb" -> "a<1e>b").
References IS_ASCII_PRINTABLE_CHAR, IS_UTF8_MULTB_INNER, and IS_UTF8_MULTB_START.
Referenced by HTTPFetchOngoing::complete(), and IMetadata::resolveString().
|
nodiscard |
Sanitize an untrusted string (e.g.
from the network). This will get strip control characters and (optionally) any MT-style escape sequences too. Note that they won't be removed cleanly but rather just broken, unlike with unescape_enriched. Line breaks and UTF-8 is permitted.
References remove_indexed().
Referenced by pkt_read_formspec_fields(), and TestUtilities::testSanitizeUntrusted().
std::string sanitizeDirName | ( | std::string_view | str, |
std::string_view | optional_prefix ) |
Sanitize the name of a new directory.
This consists of two stages:
References disallowed_dir_names, disallowed_path_chars, str_equal(), utf8_to_wide(), and wide_to_utf8().
Referenced by ModApiMainMenu::l_create_world(), and TestUtilities::testSanitizeDirName().
void str_replace | ( | std::string & | str, |
char | from, | ||
char | to ) |
Replace all occurrences of the character from
in str
with to
.
str | The string to (potentially) modify. |
from | The character in str to replace. |
to | The replacement character. |
Referenced by ImageSource::generateImagePart(), Client::initLocalMapSaving(), and TestUtilities::testStringReplace().
v3f str_to_v3f | ( | std::string_view | str | ) |
Parses a string of form (1, 2, 3)
to a v3f.
str | string |
References BasicStrfnd< T >::next(), and stof.
Referenced by Settings::getV3F(), and ItemStack::getWieldScale().
|
static |
References split(), and translate_string().
Referenced by translate_string(), and translate_string().
std::wstring translate_string | ( | std::wstring_view | s | ) |
std::wstring translate_string | ( | std::wstring_view | s, |
Translations * | translations ) |
References translate_all().
Referenced by unescape_translate().
|
static |
References errorstream, Translations::getPluralTranslation(), Translations::getTranslation(), split(), translate_all(), and wide_to_utf8().
Referenced by ChatBackend::addMessage(), Camera::drawNametags(), ModApiMainMenu::l_get_content_translation(), ModApiEnv::l_get_translated_string(), EnrichedString::operator=(), GUIFormSpecMenu::parseButton(), GUIFormSpecMenu::parseCheckbox(), GUIFormSpecMenu::parseHyperText(), GUIFormSpecMenu::parsePwdField(), GUIFormSpecMenu::parseSimpleField(), GUIFormSpecMenu::parseTextArea(), GUIEngine::setTopleftText(), translate_all(), and translate_string().
std::string urldecode | ( | std::string_view | str | ) |
References hex_digit_decode().
Referenced by TestUtilities::testUrlDecode().
std::string urlencode | ( | std::string_view | str | ) |
Referenced by HTTPFetchOngoing::HTTPFetchOngoing(), ModApiUtil::Initialize(), ModApiUtil::InitializeAsync(), ModApiUtil::InitializeClient(), ModApiUtil::l_urlencode(), and TestUtilities::testUrlEncode().
std::wstring utf8_to_wide | ( | std::string_view | input | ) |
References convert(), DEFAULT_ENCODING, hex_encode(), and infostream.
Referenced by GUITable::allocString(), Game::createClient(), ProfilerGraph::draw(), drawItemStack(), Hud::drawLuaElements(), GUIFormSpecMenu::drawMenu(), Camera::drawNametags(), Game::getServerContent(), FontEngine::getTextWidth(), FontEngine::getTextWidth(), Server::handleChat(), Game::handlePointingAtNode(), Game::handlePointingAtObject(), GameUI::init(), ModApiServer::l_chat_send_all(), ModApiServer::l_chat_send_player(), ModApiClient::l_display_chat_message(), ModApiMainMenu::l_get_content_translation(), ModApiEnv::l_get_translated_string(), ModApiClient::l_send_chat_message(), Translations::loadMoEntry(), Translations::loadMoTranslation(), Translations::loadPoTranslation(), Translations::loadTrTranslation(), Client::makeScreenshot(), ChatPrompt::nickCompletion(), GUIChatConsole::OnEvent(), GUIFormSpecMenu::parseButton(), GUIFormSpecMenu::parseCheckbox(), GUIFormSpecMenu::parseDropDown(), GUIFormSpecMenu::parseHyperText(), GUIFormSpecMenu::parseImageButton(), GUIFormSpecMenu::parseItemImageButton(), GUIFormSpecMenu::parseLabel(), Translations::parsePoLine(), GUIFormSpecMenu::parsePwdField(), GUIFormSpecMenu::parseSimpleField(), GUIFormSpecMenu::parseTabHeader(), GUIFormSpecMenu::parseTable(), GUIFormSpecMenu::parseTextArea(), GUIFormSpecMenu::parseTextList(), GUIFormSpecMenu::parseTooltip(), GUIFormSpecMenu::parseVertLabel(), Server::printToConsoleOnly(), Game::processKeyInput(), GUIOpenURLMenu::regenerateGui(), Server::requestShutdown(), ClientLauncher::run(), sanitizeDirName(), GUIEngine::setTopleftText(), TestUtilities::testStrEqual(), TestUtilities::testUTF8(), TestUtilities::testWrapRows(), Game::toggleMinimap(), GameUI::update(), Game::updateChat(), GameUI::updateProfiler(), GUIChatConsole::weblinkClick(), and wstrgettext().
void wide_add_codepoint | ( | std::wstring & | result, |
char32_t | codepoint ) |
std::string wide_to_utf8 | ( | std::wstring_view | input | ) |
References convert(), DEFAULT_ENCODING, hex_encode(), and infostream.
Referenced by GUIFileSelectMenu::acceptInput(), GUIFormSpecMenu::acceptInput(), EnrichedString::addAtEnd(), ChatBuffer::formatChatLine(), TextDestGuiEngine::gotText(), TextDestNodeMetadata::gotText(), Server::handleChat(), Client::handleCommand_AccessDenied(), Client::handleCommand_ChatMessage(), ModApiMainMenu::l_get_content_translation(), ModApiEnv::l_get_translated_string(), Translations::loadPoEntry(), Translations::loadPoTranslation(), GUIChatConsole::OnEvent(), GUIFormSpecMenu::parseTable(), ParsedText::parseTag(), GUIFormSpecMenu::parseTextList(), GUIModalMenu::preprocessEvent(), GUIPasswordChange::processInput(), sanitizeDirName(), TestUtilities::testUTF8(), Server::ShutdownState::tick(), translate_string(), Client::typeChatMessage(), Translations::unescapeC(), and GUIChatConsole::updatePrimarySelection().
std::string wrap_rows | ( | std::string_view | from, |
unsigned | row_len, | ||
bool | has_color_codes = false ) |
Create a string based on from
where a newline is forcefully inserted every row_len
characters.
row_len
characters whether it breaks a word or not. It is intended to be used for, for example, showing paths in the GUI.from | The (utf-8) string to be wrapped into rows. |
row_len | The row length (in characters). |
has_color_codes | Whether the source string has colorize codes. |
References IS_UTF8_MULTB_INNER.
Referenced by GUIOpenURLMenu::regenerateGui(), and TestUtilities::testWrapRows().
std::string writeFlagString | ( | u32 | flags, |
const FlagDesc * | flagdesc, | ||
u32 | flagmask ) |
References FlagDesc::name.
Referenced by push_flags_string(), Settings::setDefault(), and Settings::setFlagStr().
|
constexpr |
Referenced by utf8_to_wide(), and wide_to_utf8().
|
static |
Referenced by sanitizeDirName().
|
static |
List of characters that are blacklisted from created directories.
Referenced by sanitizeDirName().
|
static |
Referenced by parseNamedColorString().