Skip to content

Commit

Permalink
Rename variables in 'get_range' to make code more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexxcons authored and t8m committed Oct 9, 2023
1 parent 55510c9 commit c16fc62
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,29 +554,29 @@ get_range(bitstr_t * bits, int low, int high, const char *names[],
* | [number] "~" [number]
*/

int ch, i, num1, num2, num3;
int ch, i, low_, high_, step;

/* default value for step
*/
num3 = 1;
step = 1;
range_state_t state = R_START;

while (state != R_FINISH && ((ch = get_char(file)) != EOF)) {
switch (state) {
case R_START:
if (ch == '*') {
num1 = low;
num2 = high;
low_ = low;
high_ = high;
state = R_AST;
break;
}
if (ch == '~') {
num1 = low;
low_ = low;
state = R_RANDOM;
break;
}
unget_char(ch, file);
if (get_number(&num1, low, names, file) != EOF) {
if (get_number(&low_, low, names, file) != EOF) {
state = R_NUM1;
break;
}
Expand All @@ -595,8 +595,8 @@ get_range(bitstr_t * bits, int low, int high, const char *names[],

case R_STEP:
unget_char(ch, file);
if (get_number(&num3, 0, PPC_NULL, file) != EOF
&& num3 != 0) {
if (get_number(&step, 0, PPC_NULL, file) != EOF
&& step != 0) {
state = R_TERMS;
break;
}
Expand All @@ -619,15 +619,15 @@ get_range(bitstr_t * bits, int low, int high, const char *names[],
break;
}
if (is_separator(ch)) {
num2 = num1;
high_ = low_;
state = R_FINISH;
break;
}
return (EOF);

case R_RANGE:
unget_char(ch, file);
if (get_number(&num2, low, names, file) != EOF) {
if (get_number(&high_, low, names, file) != EOF) {
state = R_RANGE_NUM2;
break;
}
Expand All @@ -646,11 +646,11 @@ get_range(bitstr_t * bits, int low, int high, const char *names[],

case R_RANDOM:
if (is_separator(ch)) {
num2 = high;
high_ = high;
state = R_FINISH;
}
else if (unget_char(ch, file),
get_number(&num2, low, names, file) != EOF) {
get_number(&high_, low, names, file) != EOF) {
state = R_TERMS;
}
/* fail if couldn't find match on previous term
Expand All @@ -659,12 +659,12 @@ get_range(bitstr_t * bits, int low, int high, const char *names[],
return (EOF);

/* if invalid random range was selected */
if (num1 > num2)
if (low_ > high_)
return (EOF);

/* select random number in range <num1, num2>
/* select random number in range <low_, high_>
*/
num1 = num2 = random() % (num2 - num1 + 1) + num1;
low_ = high_ = random() % (high_ - low_ + 1) + low_;
break;


Expand All @@ -677,7 +677,7 @@ get_range(bitstr_t * bits, int low, int high, const char *names[],
if (state != R_FINISH || ch == EOF)
return (EOF);

for (i = num1; i <= num2; i += num3)
for (i = low_; i <= high_; i += step)
if (EOF == set_element(bits, low, high, i)) {
unget_char(ch, file);
return (EOF);
Expand Down

0 comments on commit c16fc62

Please sign in to comment.