Skip to content

Commit

Permalink
core: avoid .at() and use [] operator (#9347)
Browse files Browse the repository at this point in the history
avoid .at() where it makes sense and use [] operator in loops.
  • Loading branch information
gulafaran authored Feb 6, 2025
1 parent 868b2b5 commit f1e32cd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/desktop/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,17 +1626,17 @@ PHLWINDOW CWindow::getSwallower() {
if (!(*PSWALLOWREGEX).empty())
std::erase_if(candidates, [&](const auto& other) { return !RE2::FullMatch(other->m_szClass, *PSWALLOWREGEX); });

if (candidates.size() <= 0)
if (candidates.size() == 0)
return nullptr;

if (!(*PSWALLOWEXREGEX).empty())
std::erase_if(candidates, [&](const auto& other) { return RE2::FullMatch(other->m_szTitle, *PSWALLOWEXREGEX); });

if (candidates.size() <= 0)
if (candidates.size() == 0)
return nullptr;

if (candidates.size() == 1)
return candidates.at(0);
return candidates[0];

// walk up the focus history and find the last focused
for (auto const& w : g_pCompositor->m_vWindowFocusHistory) {
Expand All @@ -1648,7 +1648,7 @@ PHLWINDOW CWindow::getSwallower() {
}

// if none are found (??) then just return the first one
return candidates.at(0);
return candidates[0];
}

void CWindow::unsetWindowData(eOverridePriority priority) {
Expand Down
20 changes: 10 additions & 10 deletions src/devices/IKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ void IKeyboard::setKeymap(const SStringRuleNames& rules) {
updateModifiers(0, 0, modifiersState.locked, modifiersState.group);
}

for (size_t i = 0; i < LEDNAMES.size(); ++i) {
ledIndexes.at(i) = xkb_map_led_get_index(xkbKeymap, LEDNAMES.at(i));
Debug::log(LOG, "xkb: LED index {} (name {}) got index {}", i, LEDNAMES.at(i), ledIndexes.at(i));
for (size_t i = 0; i < std::min(LEDNAMES.size(), ledIndexes.size()); ++i) {
ledIndexes[i] = xkb_map_led_get_index(xkbKeymap, LEDNAMES[i]);
Debug::log(LOG, "xkb: LED index {} (name {}) got index {}", i, LEDNAMES[i], ledIndexes[i]);
}

for (size_t i = 0; i < MODNAMES.size(); ++i) {
modIndexes.at(i) = xkb_map_mod_get_index(xkbKeymap, MODNAMES.at(i));
Debug::log(LOG, "xkb: Mod index {} (name {}) got index {}", i, MODNAMES.at(i), modIndexes.at(i));
for (size_t i = 0; i < std::min(MODNAMES.size(), modIndexes.size()); ++i) {
modIndexes[i] = xkb_map_mod_get_index(xkbKeymap, MODNAMES[i]);
Debug::log(LOG, "xkb: Mod index {} (name {}) got index {}", i, MODNAMES[i], modIndexes[i]);
}

updateKeymapFD();
Expand Down Expand Up @@ -289,8 +289,8 @@ std::optional<uint32_t> IKeyboard::getLEDs() {
return {};

uint32_t leds = 0;
for (uint32_t i = 0; i < LED_COUNT; ++i) {
if (xkb_state_led_index_is_active(xkbState, ledIndexes.at(i)))
for (uint32_t i = 0; i < std::min((size_t)LED_COUNT, ledIndexes.size()); ++i) {
if (xkb_state_led_index_is_active(xkbState, ledIndexes[i]))
leds |= (1 << i);
}

Expand Down Expand Up @@ -323,10 +323,10 @@ uint32_t IKeyboard::getModifiers() {
uint32_t modMask = modifiersState.depressed | modifiersState.latched;
uint32_t mods = 0;
for (size_t i = 0; i < modIndexes.size(); ++i) {
if (modIndexes.at(i) == XKB_MOD_INVALID)
if (modIndexes[i] == XKB_MOD_INVALID)
continue;

if (!(modMask & (1 << modIndexes.at(i))))
if (!(modMask & (1 << modIndexes[i])))
continue;

mods |= (1 << i);
Expand Down
2 changes: 1 addition & 1 deletion src/layout/MasterLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ void CHyprMasterLayout::runOrientationCycle(SLayoutMessageHeader& header, CVarLi

int nextOrPrev = 0;
for (size_t i = 0; i < cycle.size(); ++i) {
if (PWORKSPACEDATA->orientation == cycle.at(i)) {
if (PWORKSPACEDATA->orientation == cycle[i]) {
nextOrPrev = i + direction;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/managers/PointerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ SP<Aquamarine::IBuffer> CPointerManager::renderHWCursorBuffer(SP<CPointerManager

if (flipRB) {
for (size_t i = 0; i < shmBuffer.size(); i += 4) {
std::swap(shmBuffer.at(i), shmBuffer.at(i + 2)); // little-endian!!!!!!
std::swap(shmBuffer[i], shmBuffer[i + 2]); // little-endian!!!!!!
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/managers/XCursorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ std::vector<SP<SXCursors>> CXCursorManager::loadStandardCursors(std::string cons

// load the default xcursor shapes that exist in the theme
for (size_t i = 0; i < XCURSOR_STANDARD_NAMES.size(); ++i) {
std::string shape{XCURSOR_STANDARD_NAMES.at(i)};
std::string shape{XCURSOR_STANDARD_NAMES[i]};
auto xImages = XcursorShapeLoadImages(i << 1 /* wtf xcursor? */, name.c_str(), size);

if (!xImages) {
Expand Down
8 changes: 4 additions & 4 deletions src/render/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,14 @@ std::optional<std::vector<uint64_t>> CHyprOpenGLImpl::getModsForFormat(EGLint fo
result.reserve(mods.size());

bool linearIsExternal = false;
for (size_t i = 0; i < mods.size(); ++i) {
if (external.at(i)) {
if (mods.at(i) == DRM_FORMAT_MOD_LINEAR)
for (size_t i = 0; i < std::min(mods.size(), external.size()); ++i) {
if (external[i]) {
if (mods[i] == DRM_FORMAT_MOD_LINEAR)
linearIsExternal = true;
continue;
}

result.push_back(mods.at(i));
result.push_back(mods[i]);
}

// if the driver doesn't mark linear as external, add it. It's allowed unless the driver says otherwise. (e.g. nvidia)
Expand Down
4 changes: 2 additions & 2 deletions src/xwayland/XDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void CXDataSource::send(const std::string& mime, CFileDescriptor fd) {
mimeAtom = HYPRATOMS["UTF8_STRING"];
else {
for (size_t i = 0; i < mimeTypes.size(); ++i) {
if (mimeTypes.at(i) == mime) {
mimeAtom = mimeAtoms.at(i);
if (mimeTypes[i] == mime) {
mimeAtom = mimeAtoms[i];
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/xwayland/XWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,13 +1051,13 @@ void CXWM::readWindowData(SP<CXWaylandSurface> surf) {
};

for (size_t i = 0; i < interestingProps.size(); i++) {
xcb_get_property_cookie_t cookie = xcb_get_property(connection, 0, surf->xID, interestingProps.at(i), XCB_ATOM_ANY, 0, 2048);
xcb_get_property_cookie_t cookie = xcb_get_property(connection, 0, surf->xID, interestingProps[i], XCB_ATOM_ANY, 0, 2048);
xcb_get_property_reply_t* reply = xcb_get_property_reply(connection, cookie, nullptr);
if (!reply) {
Debug::log(ERR, "[xwm] Failed to get window property");
continue;
}
readProp(surf, interestingProps.at(i), reply);
readProp(surf, interestingProps[i], reply);
free(reply);
}
}
Expand Down

0 comments on commit f1e32cd

Please sign in to comment.