Skip to content

Commit 95cbcc5

Browse files
eloycotoitchyny
andcommitted
style: fixes from suggestions.
Co-authored-by: itchyny <[email protected]> Signed-off-by: Eloy Coto <[email protected]>
1 parent 949b647 commit 95cbcc5

File tree

2 files changed

+25
-47
lines changed

2 files changed

+25
-47
lines changed

src/builtin.c

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -784,54 +784,30 @@ static jv f_sort_by_impl(jq_state *jq, jv input, jv keys) {
784784
/* (-1 - ix), where ix is the insertion point that would leave the array sorted. */
785785
/* If the input is not sorted, bsearch will terminate but with irrelevant results. */
786786
static jv f_bsearch(jq_state *jq, jv input, jv target) {
787-
assert(jv_get_kind(input) == JV_KIND_ARRAY);
788-
int len = jv_array_length(jv_copy(input));
789-
if (len == 0) {
790-
jv_free(input);
791-
jv_free(target);
792-
return jv_number(-1);
793-
} else if (len == 1) {
794-
int result = jv_cmp(target, jv_array_get(input, 0));
795-
if (result == 0 ) {
796-
return jv_number(0);
797-
} else if (result > 0) {
798-
return jv_number(-2);
799-
} else {
800-
return jv_number(-1);
801-
}
802-
}
803-
804-
int start = 0;
805-
int end = len - 1;
806-
jv answer = jv_null();
807-
while (start <end) {
808-
int mid = (start + end) / 2;
809-
int result = jv_cmp(jv_copy(target), jv_array_get(jv_copy(input), mid));
810-
if (result == 0) {
811-
answer = jv_number(mid);
812-
break;
813-
} else if (start == end ) {
814-
answer = jv_number(-1);
815-
break;
816-
} else if (result < 0 ) {
817-
end = mid -1;
818-
} else {
819-
start = mid +1;
820-
}
821-
}
822-
if (jv_get_kind(answer) == JV_KIND_NULL) {
823-
int result = jv_cmp(target, jv_array_get(jv_copy(input), start));
824-
if (result < 0) {
825-
answer = jv_number(-1 - start);
826-
}else {
827-
answer = jv_number(-2 - start);
828-
}
787+
if (jv_get_kind(input) != JV_KIND_ARRAY) {
788+
return type_error(input, "cannot be searched from");
789+
}
790+
int start = 0;
791+
int end = jv_array_length(jv_copy(input));
792+
jv answer = jv_invalid();
793+
while (start < end) {
794+
int mid = (start + end) / 2;
795+
int result = jv_cmp(jv_copy(target), jv_array_get(jv_copy(input), mid));
796+
if (result == 0) {
797+
answer = jv_number(mid);
798+
break;
799+
} else if (result < 0) {
800+
end = mid;
829801
} else {
830-
jv_free(target);
802+
start = mid + 1;
831803
}
832-
833-
jv_free(input);
834-
return answer;
804+
}
805+
if (!jv_is_valid(answer)) {
806+
answer = jv_number(-1 - start);
807+
}
808+
jv_free(input);
809+
jv_free(target);
810+
return answer;
835811
}
836812

837813
static jv f_group_by_impl(jq_state *jq, jv input, jv keys) {

tests/jq.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,10 +1547,12 @@ ascii_upcase
15471547
"useful but not for é"
15481548
"USEFUL BUT NOT FOR é"
15491549

1550-
bsearch(0,2,4)
1550+
bsearch(0,1,2,3,4)
15511551
[1,2,3]
15521552
-1
1553+
0
15531554
1
1555+
2
15541556
-4
15551557

15561558
bsearch({x:1})

0 commit comments

Comments
 (0)