OpenTTD
Functions
string_func.h File Reference

Functions related to low-level strings. More...

#include <stdarg.h>
#include "core/bitmath_func.hpp"
#include "string_type.h"

Go to the source code of this file.

Functions

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 *src, const char *last=NULL)
 Create a duplicate of the given string. More...
 
int CDECL seprintf (char *str, const char *last, const char *format,...) WARN_FORMAT(3
 
int CDECL int CDECL vseprintf (char *str, const char *last, const char *format, va_list ap)
 Safer implementation of vsnprintf; same as vsnprintf except: More...
 
char *CDECL str_fmt (const char *str,...) WARN_FORMAT(1
 
char *CDECL void str_validate (char *str, const char *last, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
 Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?' (if not ignored) More...
 
void ValidateString (const char *str)
 Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?'. 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...
 
void str_strip_colours (char *str)
 Scans the string for colour codes and strips them.
 
bool strtolower (char *str)
 Convert a given ASCII string to lowercase. More...
 
bool StrValid (const char *str, const char *last)
 Checks whether the given string is valid, i.e. More...
 
static bool StrEmpty (const char *s)
 Check if a string buffer is empty. More...
 
static size_t ttd_strnlen (const char *str, size_t maxlen)
 Get the length of a string, within a limited buffer. More...
 
char * md5sumToString (char *buf, const char *last, const uint8 md5sum[16])
 Convert the md5sum to a hexadecimal string representation. More...
 
bool IsValidChar (WChar key, CharSetFilter afilter)
 Only allow certain keys. More...
 
size_t Utf8Decode (WChar *c, const char *s)
 Decode and consume the next UTF-8 encoded character. More...
 
size_t Utf8Encode (char *buf, WChar c)
 Encode a unicode character and place it in the buffer. More...
 
size_t Utf8TrimString (char *s, size_t maxlen)
 Properly terminate an UTF8 string to some maximum length. More...
 
static WChar Utf8Consume (const char **s)
 
static int8 Utf8CharLen (WChar c)
 Return the length of a UTF-8 encoded character. More...
 
static int8 Utf8EncodedCharLen (char c)
 Return the length of an UTF-8 encoded value based on a single char. More...
 
static bool IsUtf8Part (char c)
 
static char * Utf8PrevChar (char *s)
 Retrieve the previous UNICODE character in an UTF-8 encoded string. More...
 
static const char * Utf8PrevChar (const char *s)
 
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...
 
static bool Utf16IsLeadSurrogate (uint c)
 Is the given character a lead surrogate code point? More...
 
static bool Utf16IsTrailSurrogate (uint c)
 Is the given character a lead surrogate code point? More...
 
static WChar Utf16DecodeSurrogate (uint lead, uint trail)
 Convert an UTF-16 surrogate pair to the corresponding Unicode character. More...
 
static WChar Utf16DecodeChar (const uint16 *c)
 Decode an UTF-16 character. More...
 
static bool IsTextDirectionChar (WChar c)
 Is the given character a text direction character. More...
 
static bool IsPrintable (WChar c)
 
static bool IsWhitespace (WChar c)
 Check whether UNICODE character is whitespace or not, i.e. More...
 
int strnatcmp (const char *s1, const char *s2, bool ignore_garbage_at_front=false)
 Compares two strings using case insensitive natural sort. More...
 

Detailed Description

Functions related to low-level strings.

Note
Be aware of "dangerous" string functions; string functions that have behaviour that could easily cause buffer overruns and such:
  • strncpy: does not '\0' terminate when input string is longer than the size of the output string. Use strecpy instead.
  • [v]snprintf: returns the length of the string as it would be written when the output is large enough, so it can be more than the size of the buffer and than can underflow size_t (uint-ish) which makes all subsequent snprintf alikes write outside of the buffer. Use [v]seprintf instead; it will return the number of bytes actually added so no [v]seprintf will cause outside of bounds writes.
  • [v]sprintf: does not bounds checking: use [v]seprintf instead.

Definition in file string_func.h.

Function Documentation

◆ IsTextDirectionChar()

static bool IsTextDirectionChar ( WChar  c)
inlinestatic

Is the given character a text direction character.

Parameters
cThe character to test.
Returns
true iff the character is used to influence the text direction.

Definition at line 210 of file string_func.h.

References CHAR_TD_LRE, CHAR_TD_LRM, CHAR_TD_LRO, CHAR_TD_PDF, CHAR_TD_RLE, CHAR_TD_RLM, and CHAR_TD_RLO.

◆ 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 338 of file string.cpp.

References CS_ALPHANUMERAL.

Referenced by IConsoleCmdExec(), and ttd_strnlen().

◆ IsWhitespace()

static bool IsWhitespace ( WChar  c)
inlinestatic

Check whether UNICODE character is whitespace or not, i.e.

whether this is a potential line-break character.

Parameters
cUNICODE character to check
Returns
a boolean value whether 'c' is a whitespace character or not
See also
http://www.fileformat.info/info/unicode/category/Zs/list.htm

Definition at line 242 of file string_func.h.

References strnatcmp().

Referenced by IConsoleHistoryAdd(), IcuStringIterator::Next(), and IcuStringIterator::Prev().

◆ 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 416 of file string.cpp.

References seprintf().

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

◆ 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 158 of file string.cpp.

References Utf8Decode(), and Utf8EncodedCharLen().

◆ str_validate()

char* CDECL void str_validate ( char *  str,
const char *  last,
StringValidationSettings  settings 
)

Scans the string for valid characters and if it finds invalid ones, replaces them 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 184 of file string.cpp.

References Utf8Decode(), and Utf8EncodedCharLen().

Referenced by IniGroup::IniGroup(), IniItem::IniItem(), TextfileWindow::LoadTextfile(), Packet::Recv_string(), and ValidateString().

◆ 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 NULL 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 73 of file string.cpp.

◆ 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 NULL 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 68 of file depend.cpp.

Referenced by ScenarioScanner::AddFile(), Textbuf::Assign(), CloneVehicleName(), DriverFactoryBase::DriverFactoryBase(), DumpDebugFacilityNames(), GameScannerInfo::FindInfo(), AIScannerInfo::FindInfo(), FiosMakeHeightmapName(), FormatNumber(), NetworkAddress::GetAddressAsString(), ServerNetworkGameSocketHandler::GetClientName(), GetClipboardContents(), GRFBuildParamList(), FileStringReader::HandlePragma(), IConsoleAliasExec(), LoadTranslations(), MakeCatalanTownName(), MakeCzechTownName(), MakeFinnishTownName(), MakeFrenchTownName(), MakeHungarianTownName(), MakeItalianTownName(), MakeNorwegianTownName(), MakeRomanianTownName(), MakeSillyTownName(), MakeSlovakTownName(), MakeSpanishTownName(), MakeSwissTownName(), MakeTurkishTownName(), mkpath(), NetworkAddChatMessage(), NetworkAddress::NetworkAddress(), NetworkFindBroadcastIPsInternal(), NetworkFindName(), NetworkGameListHandleDelayedInsert(), NetworkServerSetCompanyPassword(), NetworkUDPQueryServer(), BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(), NetworkGameWindow::OnEditboxChanged(), NetworkStartServerWindow::OnEditboxChanged(), AIDebugWindow::OnEditboxChanged(), NetworkStartServerWindow::OnQueryTextFinished(), MusicDriver_ExtMidi::PlaySong(), StringListReader::ReadLine(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), ClientNetworkContentSocketHandler::Receive_SERVER_INFO(), NetworkAddress::Resolve(), DriverFactoryBase::SelectDriverImpl(), ServerNetworkGameSocketHandler::SendCompanyInfo(), TextfileWindow::SetFontNames(), LanguagePackGlyphSearcher::SetFontNames(), FileToSaveLoad::SetName(), FileToSaveLoad::SetTitle(), ShowMissingContentWindow(), 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 NULL, 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 126 of file string.cpp.

References ttd_strnlen().

Referenced by ScriptScanner::AddFile(), BaseMedia< GraphicsSet >::AddFile(), ScriptInfo::AddLabels(), ScriptInfo::AddSetting(), ScriptConfig::Change(), CloneVehicleName(), CmdRenameCompany(), CmdRenameDepot(), CmdRenameEngine(), CmdRenamePresident(), CmdRenameTown(), CmdRenameVehicle(), CmdRenameWaypoint(), CmdSetGoalProgress(), CmdSetGoalText(), CmdSetStoryPageTitle(), CmdTownSetText(), AIInfo::Constructor(), ScriptInfo::Constructor(), BaseConsist::CopyConsistPropertiesFrom(), DisableStaticNewGRFInfluencingNonStaticNewGRFs(), DriverFactoryBase::DriverFactoryBase(), ErrorMessageData::ErrorMessageData(), FileWriter::FileWriter(), BaseSet< GraphicsSet, MAX_GFT, true >::FillSetDetails(), UnmappedChoiceList::Flush(), GetFontByFaceName(), IniLoadFile::GetGroup(), GRFConfig::GRFConfig(), GRFError::GRFError(), GRFFile::GRFFile(), IConsoleAliasRegister(), IniGroup::IniGroup(), IniItem::IniItem(), IniLoadSettingList(), IsGoodGRFConfigList(), LanguageStrings::LanguageStrings(), NetworkServerKickOrBanIP(), ScriptScanner::RegisterScript(), ScriptConfig::ScriptConfig(), ErrorMessageData::SetDParamStr(), AIConfig::SetSetting(), ScriptConfig::SetSetting(), IniItem::SetValue(), SlError(), MusicDriver_ExtMidi::Start(), ScriptConfig::StringToSettings(), UpdateElement(), UpdateOSKOriginalText(), and StringNameWriter::WriteStringID().

◆ StrEmpty()

static bool StrEmpty ( const char *  s)
inlinestatic

Check if a string buffer is empty.

Parameters
sThe pointer to the first element of the buffer
Returns
true if the buffer starts with the terminating null-character or if the given pointer points to NULL else return false

Definition at line 59 of file string_func.h.

Referenced by ServerNetworkAdminSocketHandler::AllowConnection(), CStrA::AppendStr(), CmdAlterGroup(), CmdPlaceSign(), CmdRenameCompany(), CmdRenameDepot(), CmdRenameEngine(), CmdRenamePresident(), CmdRenameStation(), CmdRenameTown(), CmdRenameVehicle(), CmdRenameWaypoint(), CmdSetGoalProgress(), CmdSetGoalText(), CmdSetStoryPageTitle(), CmdTownSetText(), NetworkStartServerWindow::DrawWidget(), GenerateCompanyPasswordHash(), BlitterFactory::GetBlitterFactory(), ServerNetworkGameSocketHandler::GetClientName(), NetworkAddress::GetHostname(), GetKeyboardLayout(), GRFConfig::GetName(), GetSavegameFormat(), GRFLoadConfig(), IConsoleCmdExec(), IConsoleHistoryAdd(), InitializeMusic(), MakeScreenshotName(), NetworkAddress::NetworkAddress(), NetworkGameListAddItem(), NetworkGameListHandleDelayedInsert(), NetworkServerSetCompanyPassword(), NetworkStartUp(), NetworkGameWindow::NGameAllowedSorter(), SavePresetWindow::OnClick(), NetworkGameWindow::OnEditboxChanged(), NetworkContentListWindow::OnInvalidateData(), CheatWindow::OnQueryTextFinished(), NewGRFParametersWindow::OnQueryTextFinished(), AISettingsWindow::OnQueryTextFinished(), NewGRFInspectWindow::OnQueryTextFinished(), TimetableWindow::OnQueryTextFinished(), GenerateLandscapeWindow::OnQueryTextFinished(), NetworkGameWindow::OnQueryTextFinished(), SpriteAlignerWindow::OnQueryTextFinished(), IndustryViewWindow::OnQueryTextFinished(), CreateScenarioWindow::OnQueryTextFinished(), OrdersWindow::OnQueryTextFinished(), SelectCompanyManagerFaceWindow::OnQueryTextFinished(), NetworkJoinStatusWindow::OnQueryTextFinished(), ScenarioEditorToolbarWindow::OnQueryTextFinished(), FileStringReader::ParseFile(), ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(), ClientNetworkContentSocketHandler::Receive_SERVER_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD(), ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSWORD(), ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(), RenameSign(), NetworkAddress::Resolve(), BlitterFactory::SelectBlitter(), DriverFactoryBase::SelectDriver(), DriverFactoryBase::SelectDriverImpl(), SendChat(), ServerNetworkGameSocketHandler::SendCompanyInfo(), BaseMedia< GraphicsSet >::SetSet(), MusicDriver_ExtMidi::Start(), LandInfoWindow::UpdateWidgetSize(), MusicTrackSelectionWindow::UpdateWidgetSize(), ValidatePlaylist(), and VerifyElementContentParameters().

◆ 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 569 of file string.cpp.

References _current_collator, and SkipGarbage().

Referenced by GRFSorter(), IsWhitespace(), NetworkContentListWindow::NameSorter(), NewGRFWindow::NameSorter(), NetworkGameWindow::NGameNameSorter(), and NetworkContentListWindow::TypeSorter().

◆ 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 320 of file string.cpp.

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

◆ 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 247 of file string.cpp.

References Utf8Decode(), and Utf8EncodedCharLen().

◆ ttd_strnlen()

static size_t ttd_strnlen ( const char *  str,
size_t  maxlen 
)
inlinestatic

Get the length of a string, within a limited buffer.

Parameters
strThe pointer to the first element of the buffer
maxlenThe maximum size of the buffer
Returns
The length of the string

Definition at line 71 of file string_func.h.

References IsValidChar(), md5sumToString(), Utf8Decode(), Utf8Encode(), and Utf8TrimString().

Referenced by stredup().

◆ Utf16DecodeChar()

static WChar Utf16DecodeChar ( const uint16 *  c)
inlinestatic

Decode an UTF-16 character.

Parameters
cPointer to one or two UTF-16 code points.
Returns
Decoded Unicode character.

Definition at line 195 of file string_func.h.

References Utf16DecodeSurrogate(), and Utf16IsLeadSurrogate().

Referenced by IcuStringIterator::Next(), and IcuStringIterator::Prev().

◆ Utf16DecodeSurrogate()

static WChar Utf16DecodeSurrogate ( uint  lead,
uint  trail 
)
inlinestatic

Convert an UTF-16 surrogate pair to the corresponding Unicode character.

Parameters
leadLead surrogate code point.
trailTrail surrogate code point.
Returns
Decoded Unicode character.

Definition at line 185 of file string_func.h.

Referenced by HandleCharMsg(), and Utf16DecodeChar().

◆ Utf16IsLeadSurrogate()

static bool Utf16IsLeadSurrogate ( uint  c)
inlinestatic

Is the given character a lead surrogate code point?

Parameters
cThe character to test.
Returns
True if the character is a lead surrogate code point.

Definition at line 164 of file string_func.h.

Referenced by HandleCharMsg(), HandleIMEComposition(), and Utf16DecodeChar().

◆ Utf16IsTrailSurrogate()

static bool Utf16IsTrailSurrogate ( uint  c)
inlinestatic

Is the given character a lead surrogate code point?

Parameters
cThe character to test.
Returns
True if the character is a lead surrogate code point.

Definition at line 174 of file string_func.h.

Referenced by HandleCharMsg().

◆ Utf8CharLen()

static int8 Utf8CharLen ( WChar  c)
inlinestatic

Return the length of a UTF-8 encoded character.

Parameters
cUnicode character.
Returns
Length of UTF-8 encoding for character.

Definition at line 99 of file string_func.h.

◆ 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 437 of file string.cpp.

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

◆ Utf8Encode()

size_t Utf8Encode ( char *  buf,
WChar  c 
)

Encode a unicode character and place it in the buffer.

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

Definition at line 477 of file string.cpp.

References GB().

Referenced by UnmappedChoiceList::Flush(), and ttd_strnlen().

◆ Utf8EncodedCharLen()

static int8 Utf8EncodedCharLen ( char  c)
inlinestatic

Return the length of an UTF-8 encoded value based on a single char.

This char should be the first byte of the UTF-8 encoding. If not, or encoding is invalid, return value is 0

Parameters
cchar to query length of
Returns
requested size

Definition at line 118 of file string_func.h.

References GB().

Referenced by str_fix_scc_encoded(), str_validate(), StrValid(), TranslateTTDPatchCodes(), and Utf8TrimString().

◆ Utf8PrevChar()

static char* Utf8PrevChar ( char *  s)
inlinestatic

Retrieve the previous UNICODE character in an UTF-8 encoded string.

Parameters
schar pointer pointing to (the first char of) the next character
Returns
a pointer in 's' to the previous UNICODE character's first byte
Note
The function should not be used to determine the length of the previous encoded char because it might be an invalid/corrupt start-sequence

Definition at line 143 of file string_func.h.

◆ Utf8StringLength()

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 300 of file string.cpp.

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

◆ 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 511 of file string.cpp.

References Utf8EncodedCharLen().

Referenced by NetworkAddChatMessage(), and ttd_strnlen().

◆ ValidateString()

void ValidateString ( const char *  str)

Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?'.

Parameters
strthe string to validate

Definition at line 233 of file string.cpp.

References str_validate().

Referenced by ScriptInfo::AddLabels(), and ScriptInfo::AddSetting().

◆ vseprintf()

int CDECL 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 50 of file string.cpp.

References min().

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