Use std::size instead of sizeof(a) / sizeof(a[0])

This commit is contained in:
Dennis Felsing 2022-03-30 15:16:19 +02:00 committed by Jupeyy
parent 8a942a21dd
commit 843acf6a5a
34 changed files with 77 additions and 67 deletions

View file

@ -2911,7 +2911,7 @@ foreach(target ${TARGETS_LINK})
endforeach()
foreach(target ${TARGETS_OWN})
if((CMAKE_VERSION VERSION_GREATER 3.1 OR CMAKE_VERSION VERSION_EQUAL 3.1) AND NOT TARGET_OS STREQUAL "mac")
if((CMAKE_VERSION VERSION_GREATER 3.1 OR CMAKE_VERSION VERSION_EQUAL 3.1))
set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
set_property(TARGET ${target} PROPERTY CXX_STANDARD_REQUIRED ON)
endif()

View file

@ -1,5 +1,6 @@
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <array> // std::size
#include <cctype>
#include <cmath>
#include <cstdarg>
@ -97,7 +98,7 @@ IOHANDLE io_current_exe()
#if defined(CONF_FAMILY_WINDOWS)
wchar_t wpath[IO_MAX_PATH_LENGTH];
char path[IO_MAX_PATH_LENGTH];
if(!GetModuleFileNameW(NULL, wpath, sizeof(wpath) / sizeof(wpath[0])))
if(!GetModuleFileNameW(NULL, wpath, std::size(wpath)))
{
return 0;
}
@ -247,7 +248,7 @@ static void logger_win_debugger(const char *line, void *user)
{
(void)user;
WCHAR wBuffer[512];
MultiByteToWideChar(CP_UTF8, 0, line, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, line, -1, wBuffer, std::size(wBuffer));
OutputDebugStringW(wBuffer);
OutputDebugStringW(L"\n");
}
@ -406,7 +407,7 @@ IOHANDLE io_open_impl(const char *filename, int flags)
dbg_assert(flags == (IOFLAG_READ | IOFLAG_SKIP_BOM) || flags == IOFLAG_READ || flags == IOFLAG_WRITE || flags == IOFLAG_APPEND, "flags must be read, read+skipbom, write or append");
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wBuffer, std::size(wBuffer));
if((flags & IOFLAG_READ) != 0)
return (IOHANDLE)_wfsopen(wBuffer, L"rb", _SH_DENYNO);
if(flags == IOFLAG_WRITE)
@ -1575,7 +1576,7 @@ static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, i
char buf[128];
WCHAR wBuffer[128];
int error = WSAGetLastError();
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, wBuffer, sizeof(wBuffer) / sizeof(WCHAR), 0) == 0)
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, wBuffer, std::size(wBuffer), 0) == 0)
wBuffer[0] = 0;
WideCharToMultiByte(CP_UTF8, 0, wBuffer, -1, buf, sizeof(buf), NULL, NULL);
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
@ -1614,7 +1615,7 @@ static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, i
char buf[128];
WCHAR wBuffer[128];
int error = WSAGetLastError();
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, wBuffer, sizeof(wBuffer) / sizeof(WCHAR), 0) == 0)
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, wBuffer, std::size(wBuffer), 0) == 0)
wBuffer[0] = 0;
WideCharToMultiByte(CP_UTF8, 0, wBuffer, -1, buf, sizeof(buf), NULL, NULL);
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
@ -2213,7 +2214,7 @@ void fs_listdir(const char *dir, FS_LISTDIR_CALLBACK cb, int type, void *user)
int length;
str_format(buffer, sizeof(buffer), "%s/*", dir);
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wBuffer, std::size(wBuffer));
handle = FindFirstFileW(wBuffer, &finddata);
if(handle == INVALID_HANDLE_VALUE)
@ -2267,7 +2268,7 @@ void fs_listdir_fileinfo(const char *dir, FS_LISTDIR_CALLBACK_FILEINFO cb, int t
int length;
str_format(buffer, sizeof(buffer), "%s/*", dir);
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wBuffer, std::size(wBuffer));
handle = FindFirstFileW(wBuffer, &finddata);
if(handle == INVALID_HANDLE_VALUE)
@ -2382,7 +2383,7 @@ int fs_makedir(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, path, -1, wBuffer, std::size(wBuffer));
if(_wmkdir(wBuffer) == 0)
return 0;
if(errno == EEXIST)
@ -2406,7 +2407,7 @@ int fs_removedir(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wPath[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, sizeof(wPath) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, std::size(wPath));
if(RemoveDirectoryW(wPath) != 0)
return 0;
return -1;
@ -2421,7 +2422,7 @@ int fs_is_dir(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wPath[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, sizeof(wPath) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, std::size(wPath));
DWORD attributes = GetFileAttributesW(wPath);
return attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0;
#else
@ -2438,7 +2439,7 @@ int fs_chdir(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, path, -1, wBuffer, std::size(wBuffer));
if(_wchdir(wBuffer))
return 1;
else
@ -2490,7 +2491,7 @@ int fs_remove(const char *filename)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wFilename[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, sizeof(wFilename) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, std::size(wFilename));
return DeleteFileW(wFilename) == 0;
#else
return unlink(filename) != 0;
@ -2502,8 +2503,8 @@ int fs_rename(const char *oldname, const char *newname)
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wOldname[IO_MAX_PATH_LENGTH];
WCHAR wNewname[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, oldname, -1, wOldname, sizeof(wOldname) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, newname, -1, wNewname, sizeof(wNewname) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, oldname, -1, wOldname, std::size(wOldname));
MultiByteToWideChar(CP_UTF8, 0, newname, -1, wNewname, std::size(wNewname));
if(MoveFileExW(wOldname, wNewname, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) == 0)
return 1;
#else
@ -2520,7 +2521,7 @@ int fs_file_time(const char *name, time_t *created, time_t *modified)
HANDLE handle;
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, name, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, name, -1, wBuffer, std::size(wBuffer));
handle = FindFirstFileW(wBuffer, &finddata);
if(handle == INVALID_HANDLE_VALUE)
return 1;
@ -3791,7 +3792,7 @@ PROCESS shell_execute(const char *file)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wBuffer[512];
MultiByteToWideChar(CP_UTF8, 0, file, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, file, -1, wBuffer, std::size(wBuffer));
SHELLEXECUTEINFOW info;
mem_zero(&info, sizeof(SHELLEXECUTEINFOW));
info.cbSize = sizeof(SHELLEXECUTEINFOW);
@ -3835,7 +3836,7 @@ int open_link(const char *link)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wBuffer[512];
MultiByteToWideChar(CP_UTF8, 0, link, -1, wBuffer, sizeof(wBuffer) / sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, link, -1, wBuffer, std::size(wBuffer));
return (uintptr_t)ShellExecuteW(NULL, L"open", wBuffer, NULL, NULL, SW_SHOWDEFAULT) > 32;
#elif defined(CONF_PLATFORM_LINUX)
const int pid = fork();

View file

@ -1783,8 +1783,8 @@ bool CCommandProcessorFragment_OpenGL2::Cmd_Init(const SCommand_Init *pCommand)
if(AnalysisCorrect)
{
g_Config.m_Gfx3DTextureAnalysisRan = 1;
str_copy(g_Config.m_Gfx3DTextureAnalysisRenderer, pCommand->m_pRendererString, sizeof(g_Config.m_Gfx3DTextureAnalysisRenderer) / sizeof(g_Config.m_Gfx3DTextureAnalysisRenderer[0]));
str_copy(g_Config.m_Gfx3DTextureAnalysisVersion, pCommand->m_pVersionString, sizeof(g_Config.m_Gfx3DTextureAnalysisVersion) / sizeof(g_Config.m_Gfx3DTextureAnalysisVersion[0]));
str_copy(g_Config.m_Gfx3DTextureAnalysisRenderer, pCommand->m_pRendererString, std::size(g_Config.m_Gfx3DTextureAnalysisRenderer));
str_copy(g_Config.m_Gfx3DTextureAnalysisVersion, pCommand->m_pVersionString, std::size(g_Config.m_Gfx3DTextureAnalysisVersion));
}
}
}
@ -2095,7 +2095,7 @@ void CCommandProcessorFragment_OpenGL2::RenderBorderTileEmulation(SBufferContain
Vertex.m_Pos.x += pOffset[0] + pDir[0] * XCount;
Vertex.m_Pos.y += pOffset[1] + pDir[1] * YCount;
if(VertexCount >= sizeof(m_aStreamVertices) / sizeof(m_aStreamVertices[0]))
if(VertexCount >= std::size(m_aStreamVertices))
{
glDrawArrays(GL_QUADS, 0, VertexCount);
VertexCount = 0;
@ -2178,7 +2178,7 @@ void CCommandProcessorFragment_OpenGL2::RenderBorderTileLineEmulation(SBufferCon
Vertex.m_Pos.x += pOffset[0] + pDir[0] * i;
Vertex.m_Pos.y += pOffset[1] + pDir[1] * i;
if(VertexCount >= sizeof(m_aStreamVertices) / sizeof(m_aStreamVertices[0]))
if(VertexCount >= std::size(m_aStreamVertices))
{
glDrawArrays(GL_QUADS, 0, VertexCount);
VertexCount = 0;
@ -2318,7 +2318,7 @@ void CCommandProcessorFragment_OpenGL2::Cmd_RenderTileLayer(const CCommandBuffer
mem_copy(&Vertex.m_Tex, pTex, sizeof(vec3));
}
if(VertexCount >= sizeof(m_aStreamVertices) / sizeof(m_aStreamVertices[0]))
if(VertexCount >= std::size(m_aStreamVertices))
{
glDrawArrays(GL_QUADS, 0, VertexCount);
VertexCount = 0;

View file

@ -853,7 +853,7 @@ void CGraphicsBackend_SDL_GL::GetCurrentVideoMode(CVideoMode &CurMode, int HiDPI
CGraphicsBackend_SDL_GL::CGraphicsBackend_SDL_GL()
{
mem_zero(m_aErrorString, sizeof(m_aErrorString) / sizeof(m_aErrorString[0]));
mem_zero(m_aErrorString, std::size(m_aErrorString));
}
int CGraphicsBackend_SDL_GL::Init(const char *pName, int *pScreen, int *pWidth, int *pHeight, int *pRefreshRate, int FsaaSamples, int Flags, int *pDesktopWidth, int *pDesktopHeight, int *pCurrentWidth, int *pCurrentHeight, IStorage *pStorage)
@ -966,7 +966,7 @@ int CGraphicsBackend_SDL_GL::Init(const char *pName, int *pScreen, int *pWidth,
CVideoMode aModes[256];
int ModesCount = 0;
int IndexOfResolution = -1;
GetVideoModes(aModes, sizeof(aModes) / sizeof(aModes[0]), &ModesCount, 1, *pDesktopWidth, *pDesktopHeight, *pScreen);
GetVideoModes(aModes, std::size(aModes), &ModesCount, 1, *pDesktopWidth, *pDesktopHeight, *pScreen);
for(int i = 0; i < ModesCount; i++)
{
@ -1206,7 +1206,7 @@ int CGraphicsBackend_SDL_GL::Init(const char *pName, int *pScreen, int *pWidth,
if(pErrorStr != NULL)
{
str_copy(m_aErrorString, pErrorStr, sizeof(m_aErrorString) / sizeof(m_aErrorString[0]));
str_copy(m_aErrorString, pErrorStr, std::size(m_aErrorString));
}
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED;

View file

@ -2902,7 +2902,7 @@ void CClient::Run()
mem_zero(&BindAddr, sizeof(BindAddr));
BindAddr.type = NETTYPE_ALL;
}
for(unsigned int i = 0; i < sizeof(m_NetClient) / sizeof(m_NetClient[0]); i++)
for(unsigned int i = 0; i < std::size(m_NetClient); i++)
{
BindAddr.port = i == CONN_MAIN ? g_Config.m_ClPort : i == CONN_DUMMY ? g_Config.m_ClDummyPort : g_Config.m_ClContactPort;
while(BindAddr.port == 0 || !m_NetClient[i].Open(BindAddr))
@ -4087,7 +4087,7 @@ void CClient::LoadFont()
if(!FontLoaded)
{
str_format(aBuff, sizeof(aBuff) / sizeof(aBuff[0]), "failed to load the fallback font. filename='%s'", pFallbackFontFile);
str_format(aBuff, std::size(aBuff), "failed to load the fallback font. filename='%s'", pFallbackFontFile);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "gameclient", aBuff);
}
}
@ -4097,7 +4097,7 @@ void CClient::LoadFont()
if(!pDefaultFont)
{
str_format(aBuff, sizeof(aBuff) / sizeof(aBuff[0]), "failed to load font. filename='%s'", pFontFile);
str_format(aBuff, std::size(aBuff), "failed to load font. filename='%s'", pFontFile);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "gameclient", aBuff);
}
}

View file

@ -2820,7 +2820,7 @@ int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes, int Scre
{
if(g_Config.m_GfxDisplayAllVideoModes)
{
int Count = sizeof(g_aFakeModes) / sizeof(CVideoMode);
int Count = std::size(g_aFakeModes);
mem_copy(pModes, g_aFakeModes, sizeof(g_aFakeModes));
if(MaxModes < Count)
Count = MaxModes;

View file

@ -1721,7 +1721,7 @@ bool CServerInfo::ParseLocation(int *pResult, const char *pString)
"sa", // LOC_SOUTH_AMERICA
"as:cn", // LOC_CHINA
};
for(int i = sizeof(LOCATIONS) / sizeof(LOCATIONS[0]) - 1; i >= 0; i--)
for(int i = std::size(LOCATIONS) - 1; i >= 0; i--)
{
if(str_startswith(pString, LOCATIONS[i]))
{

View file

@ -515,7 +515,7 @@ IServerBrowserHttp *CreateServerBrowserHttp(IEngine *pEngine, IConsole *pConsole
if(NumUrls == 0)
{
ppUrls = DEFAULT_SERVERLIST_URLS;
NumUrls = sizeof(DEFAULT_SERVERLIST_URLS) / sizeof(DEFAULT_SERVERLIST_URLS[0]);
NumUrls = std::size(DEFAULT_SERVERLIST_URLS);
}
int PreviousBestIndex = -1;
for(int i = 0; i < NumUrls; i++)

View file

@ -87,7 +87,7 @@ void CDbConnectionPool::Execute(
const char *pName)
{
m_aTasks[m_FirstElem++] = std::make_unique<CSqlExecData>(pFunc, std::move(pSqlRequestData), pName);
m_FirstElem %= sizeof(m_aTasks) / sizeof(m_aTasks[0]);
m_FirstElem %= std::size(m_aTasks);
m_NumElem.Signal();
}
@ -97,7 +97,7 @@ void CDbConnectionPool::ExecuteWrite(
const char *pName)
{
m_aTasks[m_FirstElem++] = std::make_unique<CSqlExecData>(pFunc, std::move(pSqlRequestData), pName);
m_FirstElem %= sizeof(m_aTasks) / sizeof(m_aTasks[0]);
m_FirstElem %= std::size(m_aTasks);
m_NumElem.Signal();
}
@ -144,7 +144,7 @@ void CDbConnectionPool::Worker()
m_Shutdown.store(false);
return;
}
m_LastElem %= sizeof(m_aTasks) / sizeof(m_aTasks[0]);
m_LastElem %= std::size(m_aTasks);
bool Success = false;
switch(pThreadData->m_Mode)
{

View file

@ -1,5 +1,7 @@
#include "name_ban.h"
#include <array> // std::size
CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans)
{
char aTrimmed[MAX_NAME_LENGTH];
@ -7,14 +9,14 @@ CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans)
str_utf8_trim_right(aTrimmed);
int aSkeleton[MAX_NAME_SKELETON_LENGTH];
int SkeletonLength = str_utf8_to_skeleton(aTrimmed, aSkeleton, sizeof(aSkeleton) / sizeof(aSkeleton[0]));
int SkeletonLength = str_utf8_to_skeleton(aTrimmed, aSkeleton, std::size(aSkeleton));
int aBuffer[MAX_NAME_SKELETON_LENGTH * 2 + 2];
CNameBan *pResult = 0;
for(int i = 0; i < NumNameBans; i++)
{
CNameBan *pBan = &pNameBans[i];
int Distance = str_utf32_dist_buffer(aSkeleton, SkeletonLength, pBan->m_aSkeleton, pBan->m_SkeletonLength, aBuffer, sizeof(aBuffer) / sizeof(aBuffer[0]));
int Distance = str_utf32_dist_buffer(aSkeleton, SkeletonLength, pBan->m_aSkeleton, pBan->m_SkeletonLength, aBuffer, std::size(aBuffer));
if(Distance <= pBan->m_Distance || (pBan->m_IsSubstring == 1 && str_utf8_find_nocase(pName, pBan->m_aName)))
{
pResult = pBan;

View file

@ -4,6 +4,8 @@
#include <base/system.h>
#include <engine/shared/protocol.h>
#include <array> // std::size
enum
{
MAX_NAME_SKELETON_LENGTH = MAX_NAME_LENGTH * 4,
@ -18,7 +20,7 @@ public:
m_Distance(Distance), m_IsSubstring(IsSubstring)
{
str_copy(m_aName, pName, sizeof(m_aName));
m_SkeletonLength = str_utf8_to_skeleton(m_aName, m_aSkeleton, sizeof(m_aSkeleton) / sizeof(m_aSkeleton[0]));
m_SkeletonLength = str_utf8_to_skeleton(m_aName, m_aSkeleton, std::size(m_aSkeleton));
str_copy(m_aReason, pReason, sizeof(m_aReason));
}
char m_aName[MAX_NAME_LENGTH];

View file

@ -4,6 +4,8 @@
#include "compression.h"
#include <array> // std::size
// Format: ESDDDDDD EDDDDDDD EDD... Extended, Data, Sign
unsigned char *CVariableInt::Pack(unsigned char *pDst, int i, int DstSize)
{
@ -47,7 +49,7 @@ const unsigned char *CVariableInt::Unpack(const unsigned char *pSrc, int *pInOut
const static int s_aMasks[] = {0x7F, 0x7F, 0x7F, 0x0F};
const static int s_aShifts[] = {6, 6 + 7, 6 + 7 + 7, 6 + 7 + 7 + 7};
for(unsigned i = 0; i < sizeof(s_aMasks) / sizeof(int); i++)
for(unsigned i = 0; i < std::size(s_aMasks); i++)
{
if(!(*pSrc & 0x80))
break;

View file

@ -15,6 +15,8 @@
#include "console.h"
#include "linereader.h"
#include <array> // std::size
// todo: rework this
const char *CConsole::CResult::GetString(unsigned Index)
@ -348,7 +350,7 @@ void CConsole::InitChecksum(CChecksumData *pData) const
pData->m_NumCommands = 0;
for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext)
{
if(pData->m_NumCommands < (int)(sizeof(pData->m_aCommandsChecksum) / sizeof(pData->m_aCommandsChecksum[0])))
if(pData->m_NumCommands < (int)(std::size(pData->m_aCommandsChecksum)))
{
FCommandCallback pfnCallback = pCommand->m_pfnCallback;
void *pUserData = pCommand->m_pUserData;

View file

@ -969,7 +969,7 @@ void CDemoPlayer::SetSpeed(float Speed)
void CDemoPlayer::SetSpeedIndex(int Offset)
{
m_SpeedIndex = clamp(m_SpeedIndex + Offset, 0, (int)(sizeof(g_aSpeeds) / sizeof(g_aSpeeds[0]) - 1));
m_SpeedIndex = clamp(m_SpeedIndex + Offset, 0, (int)(std::size(g_aSpeeds) - 1));
SetSpeed(g_aSpeeds[m_SpeedIndex]);
}

View file

@ -233,7 +233,7 @@ public:
"/usr/pkg/share/ddnet",
"/usr/pkg/share/games/ddnet",
"/opt/ddnet"};
const int DirsCount = sizeof(apDirs) / sizeof(apDirs[0]);
const int DirsCount = std::size(apDirs);
int i;
for(i = 0; i < DirsCount; i++)

View file

@ -39,7 +39,7 @@ class CChat : public CComponent
int m_TextContainerIndex;
int m_QuadContainerIndex;
char m_aSkinName[sizeof(g_Config.m_ClPlayerSkin) / sizeof(g_Config.m_ClPlayerSkin[0])];
char m_aSkinName[std::size(g_Config.m_ClPlayerSkin)];
CSkin::SSkinTextures m_RenderSkin;
CSkin::SSkinMetrics m_RenderSkinMetrics;
bool m_CustomColoredSkin;

View file

@ -32,7 +32,7 @@ void CDebugHud::RenderNetCorrections()
float Ramp = VelocityRamp(Velspeed, m_pClient->m_Tuning[g_Config.m_ClDummy].m_VelrampStart, m_pClient->m_Tuning[g_Config.m_ClDummy].m_VelrampRange, m_pClient->m_Tuning[g_Config.m_ClDummy].m_VelrampCurvature);
const char *paStrings[] = {"velspeed:", "velspeed*ramp:", "ramp:", "checkpoint:", "Pos", " x:", " y:", "angle:", "netobj corrections", " num:", " on:"};
const int Num = sizeof(paStrings) / sizeof(char *);
const int Num = std::size(paStrings);
const float LineHeight = 6.0f;
const float Fontsize = 5.0f;

View file

@ -30,7 +30,7 @@ CMapImages::CMapImages(int TextureSize)
str_copy(m_aEntitiesPath, "editor/entities_clear", sizeof(m_aEntitiesPath));
static_assert(sizeof(gs_aModEntitiesNames) / sizeof(gs_aModEntitiesNames[0]) == MAP_IMAGE_MOD_TYPE_COUNT, "Mod name string count is not equal to mod type count");
static_assert(std::size(gs_aModEntitiesNames) == MAP_IMAGE_MOD_TYPE_COUNT, "Mod name string count is not equal to mod type count");
}
void CMapImages::OnInit()
@ -74,7 +74,7 @@ void CMapImages::OnMapLoadImpl(class CLayers *pLayers, IMap *pMap)
if(pLayer->m_Type == LAYERTYPE_TILES)
{
CMapItemLayerTilemap *pTLayer = (CMapItemLayerTilemap *)pLayer;
if(pTLayer->m_Image != -1 && pTLayer->m_Image < (int)(sizeof(m_aTextures) / sizeof(m_aTextures[0])))
if(pTLayer->m_Image != -1 && pTLayer->m_Image < (int)(std::size(m_aTextures)))
{
m_aTextureUsedByTileOrQuadLayerFlag[pTLayer->m_Image] |= 1;
}
@ -82,7 +82,7 @@ void CMapImages::OnMapLoadImpl(class CLayers *pLayers, IMap *pMap)
else if(pLayer->m_Type == LAYERTYPE_QUADS)
{
CMapItemLayerQuads *pQLayer = (CMapItemLayerQuads *)pLayer;
if(pQLayer->m_Image != -1 && pQLayer->m_Image < (int)(sizeof(m_aTextures) / sizeof(m_aTextures[0])))
if(pQLayer->m_Image != -1 && pQLayer->m_Image < (int)(std::size(m_aTextures)))
{
m_aTextureUsedByTileOrQuadLayerFlag[pQLayer->m_Image] |= 2;
}

View file

@ -2069,9 +2069,9 @@ int CMenus::Render()
UI()->DoLabel(&Part, aBuffer, 12.8f, TEXTALIGN_LEFT);
if(IncDemoSpeed)
m_Speed = clamp(m_Speed + 1, 0, (int)(sizeof(g_aSpeeds) / sizeof(g_aSpeeds[0]) - 1));
m_Speed = clamp(m_Speed + 1, 0, (int)(std::size(g_aSpeeds) - 1));
else if(DecDemoSpeed)
m_Speed = clamp(m_Speed - 1, 0, (int)(sizeof(g_aSpeeds) / sizeof(g_aSpeeds[0]) - 1));
m_Speed = clamp(m_Speed - 1, 0, (int)(std::size(g_aSpeeds) - 1));
Part.VSplitLeft(207.0f, 0, &Part);
if(DoButton_CheckBox(&g_Config.m_ClVideoShowhud, Localize("Show ingame HUD"), g_Config.m_ClVideoShowhud, &Part))

View file

@ -109,7 +109,7 @@ void CMenus::RenderServerbrowserServerList(CUIRect View)
// This is just for scripts/update_localization.py to work correctly (all other strings are already Localize()'d somewhere else). Don't remove!
// Localize("Type");
int NumCols = sizeof(s_aCols) / sizeof(CColumn);
int NumCols = std::size(s_aCols);
// do layout
for(int i = 0; i < NumCols; i++)

View file

@ -216,7 +216,7 @@ void CMenus::RenderDemoPlayer(CUIRect MainView)
// seek to 0-90%
const int SeekPercentKeys[] = {KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9};
for(unsigned i = 0; i < sizeof(SeekPercentKeys) / sizeof(SeekPercentKeys[0]); i++)
for(unsigned i = 0; i < std::size(SeekPercentKeys); i++)
{
if(Input()->KeyPress(SeekPercentKeys[i]))
{
@ -1016,7 +1016,7 @@ void CMenus::RenderDemoList(CUIRect MainView)
RenderTools()->DrawUIRect(&Headers, ColorRGBA(0.0f, 0, 0, 0.15f), 0, 0);
int NumCols = sizeof(s_aCols) / sizeof(CColumn);
int NumCols = std::size(s_aCols);
// do layout
for(int i = 0; i < NumCols; i++)

View file

@ -1014,7 +1014,7 @@ void CMenus::RenderGhost(CUIRect MainView)
{COL_TIME, "Time", 200.0f, {0}, {0}}, // Localize("Time")
};
int NumCols = sizeof(s_aCols) / sizeof(CColumn);
int NumCols = std::size(s_aCols);
// do layout
for(int i = 0; i < NumCols; i++)

View file

@ -1187,7 +1187,7 @@ void CMenus::RenderSettingsGraphics(CUIRect MainView)
// switches
static float s_ScrollValueDrop = 0;
const char *pWindowModes[] = {Localize("Windowed"), Localize("Windowed borderless"), Localize("Windowed fullscreen"), Localize("Desktop fullscreen"), Localize("Fullscreen")};
static const int s_NumWindowMode = sizeof(pWindowModes) / sizeof(pWindowModes[0]);
static const int s_NumWindowMode = std::size(pWindowModes);
static int s_aWindowModeIDs[s_NumWindowMode];
const void *aWindowModeIDs[s_NumWindowMode];
for(int i = 0; i < s_NumWindowMode; ++i)
@ -1764,7 +1764,7 @@ void CMenus::RenderSettings(CUIRect MainView)
Localize("DDNet"),
Localize("Assets")};
int NumTabs = (int)(sizeof(aTabs) / sizeof(*aTabs));
int NumTabs = (int)std::size(aTabs);
int PreviousPage = g_Config.m_UiSettingsPage;
for(int i = 0; i < NumTabs; i++)

View file

@ -329,7 +329,7 @@ void CGameClient::OnInit()
pChecksum->m_NumComponents = m_All.m_Num;
for(int i = 0; i < m_All.m_Num; i++)
{
if(i >= (int)(sizeof(pChecksum->m_aComponentsChecksum) / sizeof(pChecksum->m_aComponentsChecksum[0])))
if(i >= (int)(std::size(pChecksum->m_aComponentsChecksum)))
{
break;
}

View file

@ -5100,7 +5100,7 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
const char *paTypeName[] = {
"N", "L", "S", "F", "M"};
const char *pTypeName = "Invalid";
if(0 <= pEnvelope->m_lPoints[i].m_Curvetype && pEnvelope->m_lPoints[i].m_Curvetype < (int)(sizeof(paTypeName) / sizeof(const char *)))
if(0 <= pEnvelope->m_lPoints[i].m_Curvetype && pEnvelope->m_lPoints[i].m_Curvetype < (int)std::size(paTypeName))
pTypeName = paTypeName[pEnvelope->m_lPoints[i].m_Curvetype];
if(DoButton_Editor(pID, pTypeName, 0, &v, 0, "Switch curve type"))
pEnvelope->m_lPoints[i].m_Curvetype = (pEnvelope->m_lPoints[i].m_Curvetype + 1) % NUM_CURVETYPES;

View file

@ -1427,7 +1427,7 @@ int CEditor::PopupSelectGametileOp(CEditor *pEditor, CUIRect View, void *pContex
"Live Freeze",
"Live Unfreeze",
};
static unsigned s_NumButtons = sizeof(s_pButtonNames) / sizeof(char *);
static unsigned s_NumButtons = std::size(s_pButtonNames);
CUIRect Button;
for(unsigned i = 0; i < s_NumButtons; ++i)

View file

@ -199,7 +199,7 @@ bool IGameController::OnEntity(int Index, vec2 Pos, int Layer, int Flags, int Nu
{
int Type = Index - ENTITY_SPAWN;
m_aaSpawnPoints[Type][m_aNumSpawnPoints[Type]] = Pos;
m_aNumSpawnPoints[Type] = minimum(m_aNumSpawnPoints[Type] + 1, (int)(sizeof(m_aaSpawnPoints[0]) / sizeof(m_aaSpawnPoints[0][0])));
m_aNumSpawnPoints[Type] = minimum(m_aNumSpawnPoints[Type] + 1, (int)std::size(m_aaSpawnPoints[0]));
}
else if(Index == ENTITY_DOOR)

View file

@ -5,10 +5,10 @@
#include <base/system.h>
static const int INT_DATA[] = {0, 1, -1, 32, 64, 256, -512, 12345, -123456, 1234567, 12345678, 123456789, 2147483647, (-2147483647 - 1)};
static const int INT_NUM = sizeof(INT_DATA) / sizeof(int);
static const int INT_NUM = std::size(INT_DATA);
static const unsigned UINT_DATA[] = {0u, 1u, 2u, 32u, 64u, 256u, 512u, 12345u, 123456u, 1234567u, 12345678u, 123456789u, 2147483647u, 2147483648u, 4294967295u};
static const int UINT_NUM = sizeof(INT_DATA) / sizeof(unsigned);
static const int UINT_NUM = std::size(INT_DATA);
TEST(BytePacking, RoundtripInt)
{

View file

@ -3,7 +3,7 @@
#include <engine/shared/compression.h>
static const int DATA[] = {0, 1, -1, 32, 64, 256, -512, 12345, -123456, 1234567, 12345678, 123456789, 2147483647, (-2147483647 - 1)};
static const int NUM = sizeof(DATA) / sizeof(int);
static const int NUM = std::size(DATA);
static const int SIZES[NUM] = {1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5};
TEST(CVariableInt, RoundtripPackUnpack)

View file

@ -56,7 +56,7 @@ TEST(Prng, EqualsPcg32GlobalDemo)
CPrng Prng;
Prng.Seed(aSeed);
for(unsigned i = 0; i < sizeof(PCG32_GLOBAL_DEMO) / sizeof(PCG32_GLOBAL_DEMO[0]); i++)
for(unsigned i = 0; i < std::size(PCG32_GLOBAL_DEMO); i++)
{
EXPECT_EQ(Prng.RandomBits(), PCG32_GLOBAL_DEMO[i]);
}

View file

@ -22,7 +22,7 @@ TEST(SecureRandom, Below1)
TEST(SecureRandom, Below)
{
int BOUNDS[] = {2, 3, 4, 5, 10, 100, 127, 128, 129};
for(unsigned i = 0; i < sizeof(BOUNDS) / sizeof(BOUNDS[0]); i++)
for(unsigned i = 0; i < std::size(BOUNDS); i++)
{
int Below = BOUNDS[i];
for(int j = 0; j < 10; j++)

View file

@ -115,7 +115,7 @@ TEST(Str, Utf8FixTruncation)
"привет Наташа",
"до свидания\xffОлег",
};
for(unsigned i = 0; i < sizeof(aaBuf) / sizeof(aaBuf[0]); i++)
for(unsigned i = 0; i < std::size(aaBuf); i++)
{
EXPECT_EQ(str_utf8_fix_truncation(aaBuf[i]), str_length(apExpected[i]));
EXPECT_STREQ(aaBuf[i], apExpected[i]);

View file

@ -2,6 +2,7 @@
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <base/system.h>
#include <array> // std::size
#include <cstdlib>
struct CPacket
@ -37,7 +38,7 @@ static CPingConfig m_aConfigPings[] = {
{140, 40, 200, 0, 0, 0},
};
static int m_ConfigNumpingconfs = sizeof(m_aConfigPings) / sizeof(CPingConfig);
static int m_ConfigNumpingconfs = std::size(m_aConfigPings);
static int m_ConfigInterval = 10; // seconds between different pingconfigs
static int m_ConfigLog = 0;
static int m_ConfigReorder = 0;

View file

@ -305,7 +305,7 @@ int main(int argc, const char **argv)
GetImageSHA256(pImgBuff, ImgSize, Width, Height, aSHA256Str);
char aNewName[IO_MAX_PATH_LENGTH];
int StrLen = str_format(aNewName, sizeof(aNewName) / sizeof(aNewName[0]), "%s_cut_%s", pImgName, aSHA256Str);
int StrLen = str_format(aNewName, std::size(aNewName), "%s_cut_%s", pImgName, aSHA256Str);
DeletePtr = true;
// make the new name ready