Use str_copy instead of str_format

Using `str_format` without format arguments is equivalent to `str_copy`, but using the latter is more efficient and readable.

As `str_format` also returns the result `str_utf8_fix_truncation`, i.e. the potentially truncated string length, the return value is also added to `str_copy` so existing invocations don't need to be adjusted.
This commit is contained in:
Robert Müller 2023-01-28 14:18:02 +01:00
parent 34ef28c6b6
commit 52aa8ac22a
8 changed files with 20 additions and 18 deletions

View file

@ -1113,14 +1113,14 @@ void net_addr_str_v6(const unsigned short ip[8], int port, char *buffer, int buf
longest_seq_len = 0;
longest_seq_start = -1;
}
w += str_format(buffer + w, buffer_size - w, "[");
w += str_copy(buffer + w, "[", buffer_size - w);
for(i = 0; i < 8; i++)
{
if(longest_seq_start <= i && i < longest_seq_start + longest_seq_len)
{
if(i == longest_seq_start)
{
w += str_format(buffer + w, buffer_size - w, "::");
w += str_copy(buffer + w, "::", buffer_size - w);
}
}
else
@ -1129,7 +1129,7 @@ void net_addr_str_v6(const unsigned short ip[8], int port, char *buffer, int buf
w += str_format(buffer + w, buffer_size - w, "%s%x", colon, ip[i]);
}
}
w += str_format(buffer + w, buffer_size - w, "]");
w += str_copy(buffer + w, "]", buffer_size - w);
if(port >= 0)
{
str_format(buffer + w, buffer_size - w, ":%d", port);
@ -2629,11 +2629,11 @@ void str_append(char *dst, const char *src, int dst_size)
str_utf8_fix_truncation(dst);
}
void str_copy(char *dst, const char *src, int dst_size)
int str_copy(char *dst, const char *src, int dst_size)
{
dst[0] = '\0';
strncat(dst, src, dst_size - 1);
str_utf8_fix_truncation(dst);
return str_utf8_fix_truncation(dst);
}
void str_utf8_truncate(char *dst, int dst_size, const char *src, int truncation_len)

View file

@ -1192,10 +1192,12 @@ void str_append(char *dst, const char *src, int dst_size);
* @param src String to be copied.
* @param dst_size Size of the buffer dst.
*
* @return Length of written string, even if it has been truncated
*
* @remark The strings are treated as zero-terminated strings.
* @remark Guarantees that dst string will contain zero-termination.
*/
void str_copy(char *dst, const char *src, int dst_size);
int str_copy(char *dst, const char *src, int dst_size);
/**
* Truncates a utf8 encoded string to a given length.

View file

@ -316,7 +316,7 @@ void CStatboard::RenderGlobalStats()
// RATIO
{
if(pStats->m_Deaths == 0)
str_format(aBuf, sizeof(aBuf), "--");
str_copy(aBuf, "--");
else
str_format(aBuf, sizeof(aBuf), "%.2f", (float)(pStats->m_Frags) / pStats->m_Deaths);
tw = TextRender()->TextWidth(0, FontSize, aBuf, -1, -1.0f);
@ -469,7 +469,7 @@ void CStatboard::FormatStats(char *pDest, size_t DestSize)
}
char aPlayerStats[1024 * VANILLA_MAX_CLIENTS];
str_format(aPlayerStats, sizeof(aPlayerStats), "Local-player,Team,Name,Clan,Score,Frags,Deaths,Suicides,F/D-ratio,Net,FPM,Spree,Best,Hammer-F/D,Gun-F/D,Shotgun-F/D,Grenade-F/D,Laser-F/D,Ninja-F/D,GameWithFlags,Flag-grabs,Flag-captures\n");
str_copy(aPlayerStats, "Local-player,Team,Name,Clan,Score,Frags,Deaths,Suicides,F/D-ratio,Net,FPM,Spree,Best,Hammer-F/D,Gun-F/D,Shotgun-F/D,Grenade-F/D,Laser-F/D,Ninja-F/D,GameWithFlags,Flag-grabs,Flag-captures\n");
for(int i = 0; i < NumPlayers; i++)
{
const CNetObj_PlayerInfo *pInfo = apPlayers[i];

View file

@ -5159,8 +5159,8 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
s_pID = nullptr;
// update displayed text
str_format(s_aStrCurTime, sizeof(s_aStrCurTime), "0.000");
str_format(s_aStrCurValue, sizeof(s_aStrCurValue), "0.000");
str_copy(s_aStrCurTime, "0.000");
str_copy(s_aStrCurValue, "0.000");
}
{
@ -5253,8 +5253,8 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
s_pID = nullptr;
// update displayed text
str_format(s_aStrCurTime, sizeof(s_aStrCurTime), "0.000");
str_format(s_aStrCurValue, sizeof(s_aStrCurValue), "0.000");
str_copy(s_aStrCurTime, "0.000");
str_copy(s_aStrCurValue, "0.000");
}
pEnvelope->m_vPoints.erase(pEnvelope->m_vPoints.begin() + i);

View file

@ -1445,7 +1445,7 @@ void CGameContext::ConSetTimerType(IConsole::IResult *pResult, void *pUserData)
if(pPlayer->m_TimerType <= CPlayer::TIMERTYPE_SIXUP && pPlayer->m_TimerType >= CPlayer::TIMERTYPE_GAMETIMER)
str_format(aBuf, sizeof(aBuf), "Timer is displayed in %s", s_aaMsg[pPlayer->m_TimerType]);
else if(pPlayer->m_TimerType == CPlayer::TIMERTYPE_NONE)
str_format(aBuf, sizeof(aBuf), "Timer isn't displayed.");
str_copy(aBuf, "Timer isn't displayed.");
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "chatresp", aBuf);
}

View file

@ -1733,7 +1733,7 @@ void CCharacter::HandleTiles(int Index)
{
char aBuf[256];
if(NewJumps == -1)
str_format(aBuf, sizeof(aBuf), "You only have your ground jump now");
str_copy(aBuf, "You only have your ground jump now");
else if(NewJumps == 1)
str_format(aBuf, sizeof(aBuf), "You can jump %d time", NewJumps);
else

View file

@ -4134,7 +4134,7 @@ void CGameContext::List(int ClientID, const char *pFilter)
if(pFilter[0])
str_format(aBuf, sizeof(aBuf), "Listing players with \"%s\" in name:", pFilter);
else
str_format(aBuf, sizeof(aBuf), "Listing all players:");
str_copy(aBuf, "Listing all players:");
SendChatTarget(ClientID, aBuf);
for(int i = 0; i < MAX_CLIENTS; i++)
{

View file

@ -161,7 +161,7 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo)
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
str_format(aJson, sizeof(aJson), "},\"tuning\":{");
str_copy(aJson, "},\"tuning\":{");
Write(aJson, str_length(aJson));
First = true;
@ -180,7 +180,7 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo)
#include <game/tuning.h>
#undef MACRO_TUNING_PARAM
str_format(aJson, sizeof(aJson), "},\"uuids\":[");
str_copy(aJson, "},\"uuids\":[");
Write(aJson, str_length(aJson));
for(int i = 0; i < pGameInfo->m_pUuids->NumUuids(); i++)
@ -191,7 +191,7 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo)
Write(aJson, str_length(aJson));
}
str_format(aJson, sizeof(aJson), "]}");
str_copy(aJson, "]}");
Write(aJson, str_length(aJson));
Write("", 1); // Null termination.
}