Skip to content

Commit

Permalink
check for errors, combine common code
Browse files Browse the repository at this point in the history
and set timelimit_exceeded to false on first word
  • Loading branch information
caolanm committed Nov 2, 2023
1 parent e5251df commit 5809737
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions src/hunspell/affixmgr.cxx
Expand Up @@ -1593,24 +1593,24 @@ struct hentry* AffixMgr::compound_check(const std::string& word,
HUNSPELL_THREAD_LOCAL clock_t timelimit;
HUNSPELL_THREAD_LOCAL bool timelimit_exceeded;

if (wordnum == 0) {
// get the start time, seeing as we're reusing this set to 0
// to flag timeout, use clock() + 1 to avoid start clock()
// of 0 as being a timeout
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
// get the current time
clock_t now_clock;
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
// convert to how clock_t measures time
timelimit = (now.tv_sec * CLOCKS_PER_SEC) + now.tv_nsec / (1000 * 1000 * 1000 / CLOCKS_PER_SEC);
now_clock = (now.tv_sec * CLOCKS_PER_SEC) + now.tv_nsec / (1000 * 1000 * 1000 / CLOCKS_PER_SEC);
} else {
HUNSPELL_WARNING(stderr, "Failure in clock_gettime %s\n", strerror(errno));
now_clock = clock();
}
else if (timelimit != 0) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
// convert to how clock_t measures time
clock_t now2 = (now.tv_sec * CLOCKS_PER_SEC) + now.tv_nsec / (1000 * 1000 * 1000 / CLOCKS_PER_SEC);
if (now2 > timelimit + TIMELIMIT) {
timelimit_exceeded = true;
}

if (wordnum == 0) {
// set the start time
timelimit = now_clock;
timelimit_exceeded = false;
}
else if (now_clock > timelimit + TIMELIMIT)
timelimit_exceeded = true;

setcminmax(&cmin, &cmax, word.c_str(), len);

Expand Down Expand Up @@ -2227,16 +2227,26 @@ int AffixMgr::compound_check_morph(const std::string& word,
// combinatorical explosion of the overlapping words

HUNSPELL_THREAD_LOCAL clock_t timelimit;
HUNSPELL_THREAD_LOCAL bool timelimit_exceeded;

if (wordnum == 0) {
// get the start time, seeing as we're reusing this set to 0
// to flag timeout, use clock() + 1 to avoid start clock()
// of 0 as being a timeout
timelimit = clock() + 1;
// get the current time
clock_t now_clock;
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
// convert to how clock_t measures time
now_clock = (now.tv_sec * CLOCKS_PER_SEC) + now.tv_nsec / (1000 * 1000 * 1000 / CLOCKS_PER_SEC);
} else {
HUNSPELL_WARNING(stderr, "Failure in clock_gettime %s\n", strerror(errno));
now_clock = clock();
}
else if (timelimit != 0 && (clock() > timelimit + TIMELIMIT)) {
timelimit = 0;

if (wordnum == 0) {
// set the start time
timelimit = now_clock;
timelimit_exceeded = false;
}
else if (now_clock > timelimit + TIMELIMIT)
timelimit_exceeded = true;

setcminmax(&cmin, &cmax, word.c_str(), len);

Expand All @@ -2256,7 +2266,7 @@ int AffixMgr::compound_check_morph(const std::string& word,

do { // onlycpdrule loop

if (timelimit == 0)
if (timelimit_exceeded)
return 0;

oldnumsyllable = numsyllable;
Expand Down

0 comments on commit 5809737

Please sign in to comment.