Skip to content

Commit b2769e7

Browse files
authored
Merge pull request #46 from NiceAndPeter/claude/modernize-loops-std-algorithms-01UTjAgW4n6LrVm3PBZ6TEye
Modernize loops with standard algorithms
2 parents 00ab586 + c0b91a2 commit b2769e7

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/core/ldebug.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lprefix.h"
1111

1212

13+
#include <algorithm>
1314
#include <cstdarg>
1415
#include <cstddef>
1516
#include <cstring>
@@ -66,14 +67,18 @@ static int getbaseline (const Proto *f, int pc, int *basepc) {
6667
return f->getLineDefined();
6768
}
6869
else {
69-
int i = pc / MAXIWTHABS - 1; /* get an estimate */
70-
/* estimate must be a lower bound of the correct base */
71-
lua_assert(i < 0 ||
72-
(i < f->getAbsLineInfoSize() && f->getAbsLineInfo()[i].getPC() <= pc));
73-
while (i + 1 < f->getAbsLineInfoSize() && pc >= f->getAbsLineInfo()[i + 1].getPC())
74-
i++; /* low estimate; adjust it */
75-
*basepc = f->getAbsLineInfo()[i].getPC();
76-
return f->getAbsLineInfo()[i].getLine();
70+
/* Binary search for the last AbsLineInfo with PC <= pc */
71+
const AbsLineInfo* absLineInfo = f->getAbsLineInfo();
72+
int size = f->getAbsLineInfoSize();
73+
/* std::upper_bound finds first element with PC > pc, so we go back one */
74+
auto it = std::upper_bound(absLineInfo, absLineInfo + size, pc,
75+
[](int target_pc, const AbsLineInfo& info) {
76+
return target_pc < info.getPC();
77+
});
78+
lua_assert(it != absLineInfo); /* we know there's at least one element with PC <= pc */
79+
--it; /* go back to last element with PC <= pc */
80+
*basepc = it->getPC();
81+
return it->getLine();
7782
}
7883
}
7984

@@ -690,13 +695,12 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
690695
** region boundaries (undefined behavior in ISO C).
691696
*/
692697
static int instack (CallInfo *ci, const TValue *o) {
693-
int pos;
694698
StkId base = ci->funcRef().p + 1;
695-
for (pos = 0; base + pos < ci->topRef().p; pos++) {
696-
if (o == s2v(base + pos))
697-
return pos;
698-
}
699-
return -1; /* not found */
699+
StkId end = ci->topRef().p;
700+
auto it = std::find_if(base, end, [o](const StackValue& sv) {
701+
return o == s2v(&sv);
702+
});
703+
return (it != end) ? static_cast<int>(it - base) : -1; /* return position or -1 if not found */
700704
}
701705

702706

src/objects/lstring.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ unsigned luaS_hashlongstr (TString *ts) {
6767

6868
static void tablerehash (TString **vect, unsigned int osize, unsigned int nsize) {
6969
unsigned int i;
70-
for (i = osize; i < nsize; i++) /* clear new elements */
71-
vect[i] = NULL;
70+
/* clear new elements (only when growing) */
71+
if (nsize > osize)
72+
std::fill_n(vect + osize, nsize - osize, nullptr);
7273
for (i = 0; i < osize; i++) { /* rehash old part of the array */
7374
TString *p = vect[i];
7475
vect[i] = NULL;

src/objects/ltable.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) {
726726
t->setDummy(); /* signal that it is using dummy node */
727727
}
728728
else {
729-
unsigned int i;
730729
unsigned int lsize = luaO_ceillog2(size);
731730
if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
732731
luaG_runerror(L, "table overflow");
@@ -736,7 +735,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) {
736735
t->setNodeArray(nodes);
737736
t->setLsizenode(cast_byte(lsize));
738737
t->setNoDummy();
739-
for (i = 0; i < size; i++) {
738+
for (unsigned int i = 0; i < size; i++) {
740739
Node *n = gnode(t, i);
741740
gnext(n) = 0;
742741
n->setKeyNil();

0 commit comments

Comments
 (0)