2 * Misc support functions
5 #include "duk_internal.h"
7 DUK_INTERNAL duk_ucodepoint_t
duk_hstring_char_code_at_raw(duk_hthread
*thr
, duk_hstring
*h
, duk_uint_t pos
) {
9 const duk_uint8_t
*p
, *p_start
, *p_end
;
12 /* Caller must check character offset to be inside the string. */
13 DUK_ASSERT(thr
!= NULL
);
14 DUK_ASSERT(h
!= NULL
);
15 DUK_ASSERT_DISABLE(pos
>= 0); /* unsigned */
16 DUK_ASSERT(pos
< (duk_uint_t
) DUK_HSTRING_GET_CHARLEN(h
));
18 boff
= duk_heap_strcache_offset_char2byte(thr
, h
, (duk_uint32_t
) pos
);
19 DUK_DDD(DUK_DDDPRINT("charCodeAt: pos=%ld -> boff=%ld, str=%!O",
20 (long) pos
, (long) boff
, (duk_heaphdr
*) h
));
21 DUK_ASSERT_DISABLE(boff
>= 0);
22 DUK_ASSERT(boff
< DUK_HSTRING_GET_BYTELEN(h
));
24 p_start
= DUK_HSTRING_GET_DATA(h
);
25 p_end
= p_start
+ DUK_HSTRING_GET_BYTELEN(h
);
27 DUK_DDD(DUK_DDDPRINT("p_start=%p, p_end=%p, p=%p",
28 (const void *) p_start
, (const void *) p_end
,
31 /* This may throw an error though not for valid E5 strings. */
32 cp
= duk_unicode_decode_xutf8_checked(thr
, &p
, p_start
, p_end
);
36 #if !defined(DUK_USE_HSTRING_CLEN)
37 DUK_INTERNAL duk_size_t
duk_hstring_get_charlen(duk_hstring
*h
) {
38 if (DUK_HSTRING_HAS_ASCII(h
)) {
39 /* Most practical strings will go here. */
40 return DUK_HSTRING_GET_BYTELEN(h
);
42 return duk_unicode_unvalidated_utf8_length(DUK_HSTRING_GET_DATA(h
), DUK_HSTRING_GET_BYTELEN(h
));
45 #endif /* !DUK_USE_HSTRING_CLEN */