Skip to content

Commit

Permalink
sub_atom/5 fix choice-point, re issue #627
Browse files Browse the repository at this point in the history
  • Loading branch information
infradig committed Nov 13, 2024
1 parent 452e94d commit dfab1e1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/bif_predicates.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,11 +1257,11 @@ static bool do_sub_atom(query *q, cell *p1, cell *p2, pl_idx p2_ctx, cell *p3, p
check_heap_error(push_choice(q));

cell tmp;
make_int(&tmp, before);
make_int(&tmp, pos_at_offset(C_STR(q, p1), C_STRLEN(q, p1), before));
unify(q, p2, p2_ctx, &tmp, q->st.curr_frame);
make_int(&tmp, len);
unify(q, p3, p3_ctx, &tmp, q->st.curr_frame);
make_int(&tmp, after);
make_int(&tmp, pos_at_offset(C_STR(q, p1), C_STRLEN(q, p1), after));
unify(q, p4, p4_ctx, &tmp, q->st.curr_frame);
return true;
}
Expand All @@ -1283,11 +1283,9 @@ static bool bif_iso_sub_string_5(query *q)
if (is_integer(p4) && is_negative(p4))
return throw_error(q, p4, p4_ctx, "domain_error", "not_less_than_zero");

#if 0
if (!is_var(p1) && is_var(p2) && is_var(p3) && is_var(p4) && !is_var(p5)) {
return do_sub_atom(q, p1, p2, p2_ctx, p3, p3_ctx, p4, p4_ctx, p5);
}
#endif

const size_t len_p1 = C_STRLEN_UTF8(p1);
size_t before = 0, len = 0, after = 0;
Expand Down
13 changes: 13 additions & 0 deletions src/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,16 @@ size_t offset_at_pos(const char *buffer, size_t buflen, size_t i)

return src - buffer;
}

size_t pos_at_offset(const char *buffer, size_t buflen, size_t i)
{
const char *src = buffer;
size_t idx = 0;

while (src < (buffer+i)) {
get_char_utf8(&src);
idx++;
}

return idx;
}
1 change: 1 addition & 0 deletions src/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ extern size_t len_char_utf8(const char *src); // returns #bytes

extern int character_at_pos(const char *src, size_t srclen, size_t i);
extern size_t offset_at_pos(const char *src, size_t srclen, size_t i);
extern size_t pos_at_offset(const char *src, size_t srclen, size_t i);

0 comments on commit dfab1e1

Please sign in to comment.