Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
string.cpp File Reference
#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>
+ Include dependency graph for string.cpp:

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.
 

Function Documentation

◆ convert()

static bool convert ( iconv_t cd,
char * outbuf,
size_t * outbuf_size,
char * inbuf,
size_t inbuf_size )
static

Referenced by utf8_to_wide(), and wide_to_utf8().

+ Here is the caller graph for this function:

◆ encodeHexColorString()

std::string encodeHexColorString ( video::SColor color)

References hex_encode().

Referenced by WearBarParams::serializeJson().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mystrlcpy()

size_t mystrlcpy ( char * dst,
const char * src,
size_t size )
noexcept

References MYMIN.

◆ mystrtok_r()

char * mystrtok_r ( char * s,
const char * sep,
char ** lasts )
noexcept

◆ parseColorString()

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseHexColorString()

static bool parseHexColorString ( const std::string & value,
video::SColor & color,
unsigned char default_alpha )
static

References hex_digit_decode().

Referenced by parseColorString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseNamedColorString()

static bool parseNamedColorString ( const std::string & value,
video::SColor & color )
static

References hex_digit_decode(), lowercase(), and s_named_colors.

Referenced by parseColorString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_seed()

u64 read_seed ( const char * str)

References murmur_hash_64_ua().

Referenced by MapgenParams::readParams().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readFlagString()

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().

+ Here is the caller graph for this function:

◆ remove_indexed()

template<class F >
void remove_indexed ( std::string & s,
F pred )

Referenced by sanitize_untrusted().

+ Here is the caller graph for this function:

◆ safe_print_string()

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().

+ Here is the caller graph for this function:

◆ sanitize_untrusted()

std::string sanitize_untrusted ( std::string_view str,
bool keep_escapes = true )
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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sanitizeDirName()

std::string sanitizeDirName ( std::string_view str,
std::string_view optional_prefix )

Sanitize the name of a new directory.

This consists of two stages:

  1. Check for 'reserved filenames' that can't be used on some filesystems and add a prefix to them
  2. Remove 'unsafe' characters from the name by replacing them with '_'

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ str_replace()

void str_replace ( std::string & str,
char from,
char to )

Replace all occurrences of the character from in str with to.

Parameters
strThe string to (potentially) modify.
fromThe character in str to replace.
toThe replacement character.

Referenced by ImageSource::generateImagePart(), Client::initLocalMapSaving(), and TestUtilities::testStringReplace().

+ Here is the caller graph for this function:

◆ str_to_v3f()

v3f str_to_v3f ( std::string_view str)

Parses a string of form (1, 2, 3) to a v3f.

Parameters
strstring
Returns
float vector

References BasicStrfnd< T >::next(), and stof.

Referenced by Settings::getV3F(), and ItemStack::getWieldScale().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translate_all()

static void translate_all ( std::wstring_view s,
size_t & i,
Translations * translations,
std::wstring & res )
static

References split(), and translate_string().

Referenced by translate_string(), and translate_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translate_string() [1/3]

std::wstring translate_string ( std::wstring_view s)

References g_client_translations, and translate_string().

+ Here is the call graph for this function:

◆ translate_string() [2/3]

std::wstring translate_string ( std::wstring_view s,
Translations * translations )

References translate_all().

Referenced by unescape_translate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translate_string() [3/3]

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 )
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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ urldecode()

std::string urldecode ( std::string_view str)

References hex_digit_decode().

Referenced by TestUtilities::testUrlDecode().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ urlencode()

std::string urlencode ( std::string_view str)

Referenced by HTTPFetchOngoing::HTTPFetchOngoing(), ModApiUtil::Initialize(), ModApiUtil::InitializeAsync(), ModApiUtil::InitializeClient(), ModApiUtil::l_urlencode(), and TestUtilities::testUrlEncode().

+ Here is the caller graph for this function:

◆ utf8_to_wide()

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().

+ Here is the call graph for this function:

◆ wide_add_codepoint()

void wide_add_codepoint ( std::wstring & result,
char32_t codepoint )

Referenced by Translations::unescapeC().

+ Here is the caller graph for this function:

◆ wide_to_utf8()

◆ wrap_rows()

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.

Note
This function does not honour word wraps and blindly inserts a newline every row_len characters whether it breaks a word or not. It is intended to be used for, for example, showing paths in the GUI.
This function doesn't wrap inside utf-8 multibyte sequences and also counts multibyte sequences correcly as single characters.
Parameters
fromThe (utf-8) string to be wrapped into rows.
row_lenThe row length (in characters).
has_color_codesWhether the source string has colorize codes.
Returns
A new string with the wrapping applied.

References IS_UTF8_MULTB_INNER.

Referenced by GUIOpenURLMenu::regenerateGui(), and TestUtilities::testWrapRows().

+ Here is the caller graph for this function:

◆ writeFlagString()

std::string writeFlagString ( u32 flags,
const FlagDesc * flagdesc,
u32 flagmask )

References FlagDesc::name.

Referenced by push_flags_string(), Settings::setDefault(), and Settings::setFlagStr().

+ Here is the caller graph for this function:

Variable Documentation

◆ DEFAULT_ENCODING

auto DEFAULT_ENCODING
constexpr
Initial value:
= ([] () -> const char* {
constexpr auto sz = sizeof(wchar_t);
static_assert(sz == 2 || sz == 4, "Unexpected wide char size");
if constexpr (sz == 2) {
return (BYTE_ORDER == BIG_ENDIAN) ? "UTF-16BE" : "UTF-16LE";
} else {
return (BYTE_ORDER == BIG_ENDIAN) ? "UTF-32BE" : "UTF-32LE";
}
})()
#define BIG_ENDIAN
Definition serialize.h:34

Referenced by utf8_to_wide(), and wide_to_utf8().

◆ disallowed_dir_names

const std::array<std::wstring_view, 30> disallowed_dir_names
static

Referenced by sanitizeDirName().

◆ disallowed_path_chars

const std::wstring_view disallowed_path_chars = L"<>:\"/\\|?*."
static

List of characters that are blacklisted from created directories.

Referenced by sanitizeDirName().

◆ s_named_colors

const std::unordered_map<std::string, u32> s_named_colors
static

Referenced by parseNamedColorString().