refactor: use std::size to get size of arrays

This commit is contained in:
sithlord48
2025-07-11 10:08:24 -04:00
committed by Nick Bolton
parent 6bf25c0089
commit d1b810baa5
6 changed files with 11 additions and 10 deletions

View File

@ -106,7 +106,8 @@ std::string sprintf(const char *fmt, ...)
{
char tmp[1024];
char *buffer = tmp;
auto len = (int)(sizeof(tmp) / sizeof(tmp[0]));
auto len = static_cast<int>(std::size(tmp));
std::string result;
while (buffer != nullptr) {
// try printing into the buffer

View File

@ -1028,7 +1028,7 @@ void KeyState::addKeypadEntries()
// map every numpad key to its equivalent non-numpad key if it's not
// on the keyboard.
for (int32_t g = 0, n = m_keyMap.getNumGroups(); g < n; ++g) {
for (size_t i = 0; i < sizeof(s_numpadTable) / sizeof(s_numpadTable[0]); i += 2) {
for (size_t i = 0; i < std::size(s_numpadTable); i += 2) {
m_keyMap.addKeyCombinationEntry(s_numpadTable[i], g, s_numpadTable + i + 1, 1);
}
}

View File

@ -249,8 +249,7 @@ QTextStream &operator<<(QTextStream &outStream, const ServerConfig &config)
for (int i = 0; i < config.screens().size(); i++) {
if (!config.screens()[i].isNull()) {
outStream << "\t" << config.screens()[i].name() << ":" << Qt::endl;
for (unsigned int j = 0; j < sizeof(neighbourDirs) / sizeof(neighbourDirs[0]); j++) {
for (unsigned int j = 0; j < std::size(neighbourDirs); j++) {
int idx = config.adjacentScreenIndex(i, neighbourDirs[j].x, neighbourDirs[j].y);
if (idx != -1 && !config.screens()[idx].isNull())
outStream << "\t\t" << neighbourDirs[j].name << " = " << config.screens()[idx].name() << Qt::endl;

View File

@ -113,12 +113,13 @@ TCPSocket::JobResult SecureSocket::doRead()
{
using enum JobResult;
static uint8_t buffer[4096];
memset(buffer, 0, sizeof(buffer));
static const auto bufferSize = std::size(buffer);
memset(buffer, 0, bufferSize);
int bytesRead = 0;
int status = 0;
if (isSecureReady()) {
status = secureRead(buffer, sizeof(buffer), bytesRead);
status = secureRead(buffer, bufferSize, bytesRead);
if (status < 0) {
return Break;
} else if (status == 0) {
@ -139,7 +140,7 @@ TCPSocket::JobResult SecureSocket::doRead()
break;
}
status = secureRead(buffer, sizeof(buffer), bytesRead);
status = secureRead(buffer, bufferSize, bytesRead);
if (status < 0) {
return Break;
}

View File

@ -556,7 +556,7 @@ uint32_t XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
};
XModifierKeymap *modKeymap = XGetModifierMapping(m_display);
for (size_t j = 0; j < sizeof(s_hotKeyModifiers) / sizeof(s_hotKeyModifiers[0]) && !err; ++j) {
for (size_t j = 0; j < std::size(s_hotKeyModifiers) && !err; ++j) {
// skip modifier if not in mask
if ((mask & s_hotKeyModifiers[j]) == 0) {
continue;
@ -1728,7 +1728,7 @@ KeyID XWindowsScreen::mapKeyFromX(XKeyEvent *event) const
// do multibyte lookup. can only call XmbLookupString with a
// key press event and a valid XIC so we checked those above.
char scratch[32];
int n = sizeof(scratch) / sizeof(scratch[0]);
auto n = static_cast<int>(std::size(scratch));
char *buffer = scratch;
int status;
n = XmbLookupString(m_ic, event, buffer, n, &keysym, &status);

View File

@ -1885,7 +1885,7 @@ Bool XWindowsUtil::propertyNotifyPredicate(Display *, XEvent *xevent, XPointer a
void XWindowsUtil::initKeyMaps()
{
if (s_keySymToUCS4.empty()) {
for (size_t i = 0; i < sizeof(s_keymap) / sizeof(s_keymap[0]); ++i) {
for (size_t i = 0; i < std::size(s_keymap); ++i) {
s_keySymToUCS4[s_keymap[i].keysym] = s_keymap[i].ucs4;
}
}