Skip to content

Commit

Permalink
style: fixes from suggestions.
Browse files Browse the repository at this point in the history
Co-authored-by: itchyny <itchyny@cybozu.co.jp>
Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
  • Loading branch information
eloycoto and itchyny committed Feb 10, 2024
1 parent 949b647 commit 42135ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ static jv f_bsearch(jq_state *jq, jv input, jv target) {
return jv_number(-1);
} else if (len == 1) {
int result = jv_cmp(target, jv_array_get(input, 0));
if (result == 0 ) {
if (result == 0) {
return jv_number(0);
} else if (result > 0) {
return jv_number(-2);
Expand All @@ -804,26 +804,26 @@ static jv f_bsearch(jq_state *jq, jv input, jv target) {
int start = 0;
int end = len - 1;
jv answer = jv_null();
while (start <end) {
while (start <= end) {
int mid = (start + end) / 2;
int result = jv_cmp(jv_copy(target), jv_array_get(jv_copy(input), mid));
if (result == 0) {
answer = jv_number(mid);
break;
} else if (start == end ) {
answer = jv_number(-1);
} else if (start == end) {
break;
} else if (result < 0 ) {
end = mid -1;
} else if (result < 0) {
end = mid - 1;
} else {
start = mid +1;
start = mid + 1;
}
}

if (jv_get_kind(answer) == JV_KIND_NULL) {
int result = jv_cmp(target, jv_array_get(jv_copy(input), start));
if (result < 0) {
answer = jv_number(-1 - start);
}else {
} else {
answer = jv_number(-2 - start);
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1547,10 +1547,12 @@ ascii_upcase
"useful but not for é"
"USEFUL BUT NOT FOR é"

bsearch(0,2,4)
bsearch(0,1,2,3,4)
[1,2,3]
-1
0
1
2
-4

bsearch({x:1})
Expand Down

0 comments on commit 42135ed

Please sign in to comment.