OpenTTD Source  12.0-beta2
string.cpp File Reference
#include "stdafx.h"
#include "debug.h"
#include "core/alloc_func.hpp"
#include "core/math_func.hpp"
#include "string_func.h"
#include "string_base.h"
#include "table/control_codes.h"
#include <stdarg.h>
#include <ctype.h>
#include <sstream>
#include <unicode/ustring.h>
#include "language.h"
#include "gfx_func.h"
#include "safeguards.h"
#include <unicode/utext.h>
#include <unicode/brkiter.h>

Go to the source code of this file.

Data Structures

class  IcuStringIterator
 String iterator using ICU as a backend. More...
 

Functions

int CDECL vseprintf (char *str, const char *last, const char *format, va_list ap)
 Safer implementation of vsnprintf; same as vsnprintf except: More...
 
char * strecat (char *dst, const char *src, const char *last)
 Appends characters from one string to another. More...
 
char * strecpy (char *dst, const char *src, const char *last)
 Copies characters from one buffer to another. More...
 
char * stredup (const char *s, const char *last)
 Create a duplicate of the given string. More...
 
char *CDECL str_fmt (const char *str,...)
 Format, "printf", into a newly allocated string. More...
 
void str_fix_scc_encoded (char *str, const char *last)
 Scan the string for old values of SCC_ENCODED and fix it to it's new, static value. More...
 
template<class T >
static void StrMakeValidInPlace (T &dst, const char *str, const char *last, StringValidationSettings settings)
 
void StrMakeValidInPlace (char *str, const char *last, StringValidationSettings settings)
 Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored). More...
 
void StrMakeValidInPlace (char *str, StringValidationSettings settings)
 Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored). More...
 
std::string StrMakeValid (const std::string &str, StringValidationSettings settings)
 Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored). More...
 
bool StrValid (const char *str, const char *last)
 Checks whether the given string is valid, i.e. More...
 
static void StrLeftTrimInPlace (std::string &str)
 Trim the spaces from the begin of given string in place, i.e. More...
 
static void StrRightTrimInPlace (std::string &str)
 Trim the spaces from the end of given string in place, i.e. More...
 
void StrTrimInPlace (std::string &str)
 Trim the spaces from given string in place, i.e. More...
 
bool StrStartsWith (const std::string_view str, const std::string_view prefix)
 Check whether the given string starts with the given prefix. More...
 
bool StrEndsWith (const std::string_view str, const std::string_view suffix)
 Check whether the given string ends with the given suffix. More...
 
void str_strip_colours (char *str)
 Scans the string for colour codes and strips them.
 
size_t Utf8StringLength (const char *s)
 Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes that the encoded string contains. More...
 
size_t Utf8StringLength (const std::string &str)
 Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes that the encoded string contains. More...
 
bool strtolower (char *str)
 Convert a given ASCII string to lowercase. More...
 
bool strtolower (std::string &str, std::string::size_type offs)
 
bool IsValidChar (WChar key, CharSetFilter afilter)
 Only allow certain keys. More...
 
int CDECL seprintf (char *str, const char *last, const char *format,...)
 Safer implementation of snprintf; same as snprintf except: More...
 
char * md5sumToString (char *buf, const char *last, const uint8 md5sum[16])
 Convert the md5sum to a hexadecimal string representation. More...
 
size_t Utf8Decode (WChar *c, const char *s)
 Decode and consume the next UTF-8 encoded character. More...
 
template<class T >
size_t Utf8Encode (T buf, WChar c)
 Encode a unicode character and place it in the buffer. More...
 
size_t Utf8Encode (char *buf, WChar c)
 
size_t Utf8Encode (std::ostreambuf_iterator< char > &buf, WChar c)
 
size_t Utf8TrimString (char *s, size_t maxlen)
 Properly terminate an UTF8 string to some maximum length. More...
 
static const char * SkipGarbage (const char *str)
 Skip some of the 'garbage' in the string that we don't want to use to sort on. More...
 
int strnatcmp (const char *s1, const char *s2, bool ignore_garbage_at_front)
 Compares two strings using case insensitive natural sort. More...
 

Detailed Description

Handling of C-type strings (char*).

Definition in file string.cpp.

Function Documentation

◆ IsValidChar()

bool IsValidChar ( WChar  key,
CharSetFilter  afilter 
)

Only allow certain keys.

You can define the filter to be used. This makes sure no invalid keys can get into an editbox, like BELL.

Parameters
keycharacter to be checked
afilterthe filter to use
Returns
true or false depending if the character is printable/valid or not

Definition at line 476 of file string.cpp.

References CS_ALPHANUMERAL.

Referenced by IConsoleCmdExec().

◆ md5sumToString()

char* md5sumToString ( char *  buf,
const char *  last,
const uint8  md5sum[16] 
)

Convert the md5sum to a hexadecimal string representation.

Parameters
bufbuffer to put the md5sum into
lastlast character of buffer (usually lastof(buf))
md5sumthe md5sum itself
Returns
a pointer to the next character after the md5sum

Definition at line 553 of file string.cpp.

References seprintf().

Referenced by HandleSavegameLoadCrash(), IsGoodGRFConfigList(), OutputContentState(), PrintGrfInfo(), and ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS().

◆ seprintf()

int CDECL seprintf ( char *  str,
const char *  last,
const char *  format,
  ... 
)

Safer implementation of snprintf; same as snprintf except:

  • last instead of size, i.e. replace sizeof with lastof.
  • return gives the amount of characters added, not what it would add.
    Parameters
    strbuffer to write to up to last
    lastlast character we may write to
    formatthe formatting (see snprintf)
    Returns
    the number of added characters

Definition at line 535 of file string.cpp.

References vseprintf().

Referenced by NetworkError::AsString(), NetworkChatWindow::ChatTabCompletion(), ScriptAllocator::CheckAllocation(), CheckExternalFiles(), ScriptInfo::CheckMethod(), Squirrel::CompileError(), Squirrel::CreateClassInstanceVM(), DeleteGRFPresetFromConfig(), CrashLogOSX::DisplayCrashDialog(), AIInfo::DummyConstructor(), CrashLog::FillCrashLog(), GameScannerInfo::FindInfo(), AIScannerInfo::FindInfo(), GameScannerLibrary::FindLibrary(), AIScannerLibrary::FindLibrary(), FormatNumber(), StringSettingDesc::FormatValue(), CrashLog::GamelogFillCrashLog(), GamelogPrint(), BlitterFactory::GetBlittersInfo(), ScriptScanner::GetConsoleList(), DriverFactoryBase::GetDriversInfo(), GameScannerInfo::GetScriptName(), AIScannerInfo::GetScriptName(), GameScannerLibrary::GetScriptName(), AIScannerLibrary::GetScriptName(), BaseMedia< GraphicsSet >::GetSetsList(), GRFBuildParamList(), HandleSavegameLoadCrash(), ScriptInstance::LoadCompatibilityScripts(), LoadGRFPresetFromConfig(), CrashLog::LogCompiler(), CrashLog::LogConfiguration(), CrashLogWindows::LogError(), CrashLogUnix::LogError(), CrashLogOSX::LogError(), CrashLog::LogGamelog(), CrashLog::LogLibraries(), CrashLog::LogOpenTTDVersion(), CrashLogUnix::LogOSVersion(), CrashLogWindows::LogOSVersion(), CrashLog::LogRecentNews(), CrashLogWindows::LogRegisters(), CrashLogWindows::LogStacktrace(), CrashLogOSX::LogStacktrace(), CrashLogUnix::LogStacktrace(), md5sumToString(), PrintGrfInfo(), ScriptScanner::RegisterScript(), NetworkAddress::Resolve(), TCPConnecter::Resolve(), Squirrel::RunError(), SaveGRFPresetToConfig(), Script_CreateDummyInfo(), SetFallbackFont(), ScriptConfig::SettingsToString(), ShowHelp(), CrashLog::WriteCrashLog(), and CrashLog::WriteSavegame().

◆ SkipGarbage()

static const char* SkipGarbage ( const char *  str)
static

Skip some of the 'garbage' in the string that we don't want to use to sort on.

This way the alphabetical sorting will work better as we would be actually using those characters instead of some other characters such as spaces and tildes at the begin of the name.

Parameters
strThe string to skip the initial garbage of.
Returns
The string with the garbage skipped.

Definition at line 704 of file string.cpp.

References IsInsideMM().

Referenced by strnatcmp().

◆ str_fix_scc_encoded()

void str_fix_scc_encoded ( char *  str,
const char *  last 
)

Scan the string for old values of SCC_ENCODED and fix it to it's new, static value.

Parameters
strthe string to scan
lastthe last valid character of str

Definition at line 169 of file string.cpp.

References Utf8Decode(), and Utf8EncodedCharLen().

◆ str_fmt()

char* CDECL str_fmt ( const char *  str,
  ... 
)

Format, "printf", into a newly allocated string.

Parameters
strThe formatting string.
Returns
The formatted string. You must free this!

Definition at line 150 of file string.cpp.

References lastof, and vseprintf().

Referenced by DEF_CONSOLE_CMD().

◆ strecat()

char* strecat ( char *  dst,
const char *  src,
const char *  last 
)

Appends characters from one string to another.

Appends the source string to the destination string with respect of the terminating null-character and and the last pointer to the last element in the destination buffer. If the last pointer is set to nullptr no boundary check is performed.

Note
usage: strecat(dst, src, lastof(dst));
lastof() applies only to fixed size arrays
Parameters
dstThe buffer containing the target string
srcThe buffer containing the string to append
lastThe pointer to the last element of the destination buffer
Returns
The pointer to the terminating null-character in the destination buffer

Definition at line 84 of file string.cpp.

References strecpy().

Referenced by KeycodeToString(), Squirrel::PrintFunc(), SaveKeycodes(), and ScriptConfig::SettingsToString().

◆ strecpy()

char* strecpy ( char *  dst,
const char *  src,
const char *  last 
)

Copies characters from one buffer to another.

Copies the source string to the destination buffer with respect of the terminating null-character and the last pointer to the last element in the destination buffer. If the last pointer is set to nullptr no boundary check is performed.

Note
usage: strecpy(dst, src, lastof(dst));
lastof() applies only to fixed size arrays
Parameters
dstThe destination buffer
srcThe buffer containing the string to copy
lastThe pointer to the last element of the destination buffer
Returns
The pointer to the terminating null-character in the destination buffer

Definition at line 112 of file string.cpp.

References Debug, and error().

Referenced by ScenarioScanner::AddFile(), Textbuf::Assign(), DriverFactoryBase::DriverFactoryBase(), GameScannerInfo::FindInfo(), AIScannerInfo::FindInfo(), BoolSettingDesc::FormatValue(), StringSettingDesc::FormatValue(), GamelogRevision(), GenerateTownNameString(), GetClipboardContents(), GRFBuildParamList(), FileStringReader::HandlePragma(), IConsoleAliasExec(), MakeCatalanTownName(), MakeCzechTownName(), MakeFinnishTownName(), MakeFrenchTownName(), MakeHungarianTownName(), MakeItalianTownName(), MakeNorwegianTownName(), MakeRomanianTownName(), MakeSillyTownName(), MakeSlovakTownName(), MakeSpanishTownName(), MakeSwissTownName(), MakeTurkishTownName(), mkpath(), NetworkFindBroadcastIPsInternal(), AIDebugWindow::OnEditboxChanged(), MusicDriver_ExtMidi::PlaySong(), StringListReader::ReadLine(), SetFallbackFont(), FileToSaveLoad::SetTitle(), strecat(), and DriverFactoryBase::~DriverFactoryBase().

◆ stredup()

char* stredup ( const char *  s,
const char *  last 
)

Create a duplicate of the given string.

Parameters
sThe string to duplicate.
lastThe last character that is safe to duplicate. If nullptr, the whole string is duplicated.
Note
The maximum length of the resulting string might therefore be last - s + 1.
Returns
The duplicate of the string.

Definition at line 137 of file string.cpp.

References ttd_strnlen().

Referenced by ScriptConfig::Change(), NetworkChatWindow::ChatTabCompletion(), CmdSetGoalProgress(), CmdSetGoalText(), CmdSetStoryPageTitle(), AIInfo::Constructor(), ErrorMessageData::ErrorMessageData(), FileWriter::FileWriter(), GamelogSetting(), GetFontByFaceName(), GRFConfig::GRFConfig(), GRFFile::GRFFile(), IConsolePrint(), IsGoodGRFConfigList(), ScriptScanner::RegisterScript(), ScriptConfig::ScriptConfig(), ErrorMessageData::SetDParamStr(), AIConfig::SetSetting(), ScriptConfig::SetSetting(), SlError(), MusicDriver_ExtMidi::Start(), ScriptConfig::StringToSettings(), UpdateElement(), and UpdateOSKOriginalText().

◆ StrEndsWith()

bool StrEndsWith ( const std::string_view  str,
const std::string_view  suffix 
)

Check whether the given string ends with the given suffix.

Parameters
strThe string to look at.
suffixThe suffix to look for.
Returns
True iff the end of the string is the same as the suffix.

Definition at line 380 of file string.cpp.

Referenced by GetSettingFromName().

◆ StrLeftTrimInPlace()

static void StrLeftTrimInPlace ( std::string &  str)
static

Trim the spaces from the begin of given string in place, i.e.

the string buffer that is passed will be modified whenever spaces exist in the given string. When there are spaces at the begin, the whole string is moved forward.

Parameters
strThe string to perform the in place left trimming on.

Definition at line 330 of file string.cpp.

Referenced by StrTrimInPlace().

◆ StrMakeValid()

std::string StrMakeValid ( const std::string &  str,
StringValidationSettings  settings 
)

Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).

Parameters
strThe string to validate.
settingsThe settings for the string validation.

Definition at line 281 of file string.cpp.

References settings, and StrMakeValidInPlace().

Referenced by IniGroup::IniGroup(), IniItem::IniItem(), IniLoadFile::LoadFromDisk(), StringSettingDesc::MakeValueValid(), and Packet::Recv_string().

◆ StrMakeValidInPlace() [1/2]

void StrMakeValidInPlace ( char *  str,
const char *  last,
StringValidationSettings  settings 
)

Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).

Parameters
strThe string to validate.
lastThe last valid character of str.
settingsThe settings for the string validation.

Definition at line 255 of file string.cpp.

References settings, and StrMakeValidInPlace().

Referenced by StrMakeValid(), and StrMakeValidInPlace().

◆ StrMakeValidInPlace() [2/2]

void StrMakeValidInPlace ( char *  str,
StringValidationSettings  settings 
)

Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).

Only use this function when you are sure the string ends with a '\0'; otherwise use StrMakeValidInPlace(str, last, settings) variant.

Parameters
strThe string (of which you are sure ends with '\0') to validate.

Definition at line 269 of file string.cpp.

References settings, and StrMakeValidInPlace().

◆ strnatcmp()

int strnatcmp ( const char *  s1,
const char *  s2,
bool  ignore_garbage_at_front 
)

Compares two strings using case insensitive natural sort.

Parameters
s1First string to compare.
s2Second string to compare.
ignore_garbage_at_frontSkip punctuation characters in the front
Returns
Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2.

Definition at line 718 of file string.cpp.

References _current_collator, and SkipGarbage().

Referenced by GRFSorter(), IndustryDirectoryWindow::IndustryNameSorter(), NetworkContentListWindow::NameSorter(), NewGRFWindow::NameSorter(), NetworkGameWindow::NGameNameSorter(), SignList::SignNameSorter(), CompanyStationsWindow::StationNameSorter(), TownDirectoryWindow::TownNameSorter(), and NetworkContentListWindow::TypeSorter().

◆ StrRightTrimInPlace()

static void StrRightTrimInPlace ( std::string &  str)
static

Trim the spaces from the end of given string in place, i.e.

the string buffer that is passed will be modified whenever spaces exist in the given string. When there are spaces at the end, the '\0' will be moved forward.

Parameters
strThe string to perform the in place left trimming on.

Definition at line 342 of file string.cpp.

Referenced by StrTrimInPlace().

◆ StrStartsWith()

bool StrStartsWith ( const std::string_view  str,
const std::string_view  prefix 
)

Check whether the given string starts with the given prefix.

Parameters
strThe string to look at.
prefixThe prefix to look for.
Returns
True iff the begin of the string is the same as the prefix.

Definition at line 367 of file string.cpp.

Referenced by ClientNetworkCoordinatorSocketHandler::ConnectToServer(), GetCompanySettingFromName(), and ServerAddress::Parse().

◆ strtolower()

bool strtolower ( char *  str)

Convert a given ASCII string to lowercase.

NOTE: only support ASCII characters, no UTF8 fancy. As currently the function is only used to lowercase data-filenames if they are not found, this is sufficient. If more, or general functionality is needed, look to r7271 where it was removed because it was broken when using certain locales: eg in Turkish the uppercase 'I' was converted to '?', so just revert to the old functionality

Parameters
strstring to convert
Returns
String has changed.

Definition at line 447 of file string.cpp.

Referenced by GameScannerInfo::FindInfo(), AIScannerInfo::FindInfo(), GameScannerLibrary::FindLibrary(), AIScannerLibrary::FindLibrary(), ScriptScanner::RegisterScript(), and SimplifyFileName().

◆ StrTrimInPlace()

void StrTrimInPlace ( std::string &  str)

Trim the spaces from given string in place, i.e.

the string buffer that is passed will be modified whenever spaces exist in the given string. When there are spaces at the begin, the whole string is moved forward and when there are spaces at the back the '\0' termination is moved.

Parameters
strThe string to perform the in place trimming on.

Definition at line 355 of file string.cpp.

References StrLeftTrimInPlace(), and StrRightTrimInPlace().

Referenced by CheckClientAndServerName(), NetworkValidateClientName(), and NetworkValidateServerName().

◆ StrValid()

bool StrValid ( const char *  str,
const char *  last 
)

Checks whether the given string is valid, i.e.

contains only valid (printable) characters and is properly terminated.

Parameters
strThe string to validate.
lastThe last character of the string, i.e. the string must be terminated here or earlier.

Definition at line 300 of file string.cpp.

References Utf8Decode(), and Utf8EncodedCharLen().

◆ Utf8Decode()

size_t Utf8Decode ( WChar c,
const char *  s 
)

Decode and consume the next UTF-8 encoded character.

Parameters
cBuffer to place decoded character.
sCharacter stream to retrieve character from.
Returns
Number of characters in the sequence.

Definition at line 574 of file string.cpp.

References GB(), and HasBit().

Referenced by Layouter::GetCharPosition(), Layouter::Layouter(), str_fix_scc_encoded(), str_strip_colours(), StrValid(), and TranslateTTDPatchCodes().

◆ Utf8Encode()

template<class T >
size_t Utf8Encode ( buf,
WChar  c 
)
inline

Encode a unicode character and place it in the buffer.

Template Parameters
TType of the buffer.
Parameters
bufBuffer to place character.
cUnicode character to encode.
Returns
Number of characters in the encoded sequence.

Definition at line 616 of file string.cpp.

Referenced by UnmappedChoiceList::Flush().

◆ Utf8StringLength() [1/2]

size_t Utf8StringLength ( const char *  s)

Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes that the encoded string contains.

Parameters
sThe string to get the length for.
Returns
The length of the string in characters.

Definition at line 417 of file string.cpp.

Referenced by CmdAlterGroup(), CmdPlaceSign(), CmdRenameCompany(), CmdRenameDepot(), CmdRenameEngine(), CmdRenamePresident(), CmdRenameStation(), CmdRenameTown(), CmdRenameVehicle(), CmdRenameWaypoint(), Utf8StringLength(), and VerifyTownName().

◆ Utf8StringLength() [2/2]

size_t Utf8StringLength ( const std::string &  str)

Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes that the encoded string contains.

Parameters
sThe string to get the length for.
Returns
The length of the string in characters.

Definition at line 431 of file string.cpp.

References Utf8StringLength().

◆ Utf8TrimString()

size_t Utf8TrimString ( char *  s,
size_t  maxlen 
)

Properly terminate an UTF8 string to some maximum length.

Parameters
sstring to check if it needs additional trimming
maxlenthe maximum length the buffer can have.
Returns
the new length in bytes of the string (eg. strlen(new_string))
Note
maxlen is the string length INCLUDING the terminating '\0'

Definition at line 660 of file string.cpp.

References Utf8EncodedCharLen().

◆ vseprintf()

int CDECL vseprintf ( char *  str,
const char *  last,
const char *  format,
va_list  ap 
)

Safer implementation of vsnprintf; same as vsnprintf except:

  • last instead of size, i.e. replace sizeof with lastof.
  • return gives the amount of characters added, not what it would add.
    Parameters
    strbuffer to write to up to last
    lastlast character we may write to
    formatthe formatting (see snprintf)
    apthe list of arguments for the format
    Returns
    the number of added characters

Definition at line 61 of file string.cpp.

Referenced by error(), Squirrel::ErrorPrintFunc(), grfmsg(), MidiSendCommand(), Textbuf::Print(), Squirrel::PrintFunc(), seprintf(), ShowInfoF(), SlErrorCorruptFmt(), str_fmt(), and usererror().