@@ -301,6 +301,119 @@ static inline bool bitstring_insert_integer(term dst_bin, size_t offset, avm_int
301301 return bitstring_insert_any_integer ((uint8_t * ) term_binary_data (dst_bin ), offset , value , n , bs_flags );
302302}
303303
304+ /**
305+ * @brief Encode a character to UTF-8.
306+ *
307+ * @param c character to encode
308+ * @param buf the buffer to encode the sring to or NULL to only compute the
309+ * size.
310+ * @param out_size the size in bytes, on output (if not NULL)
311+ * @return \c true if encoding was successful, \c false if c is not a valid
312+ * unicode character
313+ */
314+ bool bitstring_utf8_encode (avm_int_t c , uint8_t * buf , size_t * out_size );
315+
316+ /**
317+ * @brief Encode a character to UTF-16.
318+ *
319+ * @param c character to encode
320+ * @param buf the buffer to encode the character to or NULL to only compute the
321+ * size.
322+ * @param bs_flags flags to encode the character (undefined/little/big/native)
323+ * @param out_size the size in bytes, on output (if not NULL)
324+ * @return \c true if encoding was successful, \c false if c is not a valid
325+ * unicode character
326+ */
327+ bool bitstring_utf16_encode (avm_int_t c , uint8_t * buf , enum BitstringFlags bs_flags , size_t * out_size );
328+
329+ /**
330+ * @brief Encode a character to UTF-32.
331+ *
332+ * @param c character to encode
333+ * @param buf the buffer to encode the character
334+ * @param bs_flags flags to encode the character (undefined/little/big/native)
335+ * @return \c true if encoding was successful, \c false if c is not a valid
336+ * unicode character
337+ */
338+ bool bitstring_utf32_encode (avm_int_t c , uint8_t * buf , enum BitstringFlags bs_flags );
339+
340+ /**
341+ * @brief Compute the size of a character when UTF-8 encoded.
342+ *
343+ * @param c character to encode
344+ * @param out_size the size in bytes, on output
345+ * @return \c true if encoding was successful, \c false if c is not a valid
346+ * unicode character
347+ */
348+ static inline bool bitstring_utf8_size (avm_int_t c , size_t * out_size )
349+ {
350+ return bitstring_utf8_encode (c , NULL , out_size );
351+ }
352+
353+ /**
354+ * @brief Compute the size of a unicode character when UTF-16 encoded.
355+ *
356+ * @param c character to encode
357+ * @param out_size the size in bytes, on output
358+ * @return \c true if encoding was successful, \c false if c is not a valid
359+ * unicode character
360+ */
361+ static inline bool bitstring_utf16_size (avm_int_t c , size_t * out_size ) {
362+ return bitstring_utf16_encode (c , NULL , 0 , out_size );
363+ }
364+
365+ /**
366+ * @brief Insert a character in UTF-8 format
367+ *
368+ * @param dst_bin binary to insert to
369+ * @param offset offset, in bits, to where to insert the character
370+ * @param c character to encode
371+ * @param out_size the size in bytes, on output
372+ * @return \c true if encoding was successful, \c false if c is not a valid
373+ * unicode character
374+ */
375+ static inline bool bitstring_insert_utf8 (term dst_bin , size_t offset , avm_int_t c , size_t * out_size )
376+ {
377+ // size was verified by a bs_utf8_size instruction call
378+ uint8_t * dst = (uint8_t * ) term_binary_data (dst_bin ) + (offset >> 3 );
379+ return bitstring_utf8_encode (c , dst , out_size );
380+ }
381+
382+ /**
383+ * @brief Insert a character in UTF-&§ format
384+ *
385+ * @param dst_bin binary to insert to
386+ * @param offset offset, in bits, to where to insert the character
387+ * @param c character to encode
388+ * @param bs_flags flags to encode the character (undefined/little/big/native)
389+ * @param out_size the size in bytes, on output
390+ * @return \c true if encoding was successful, \c false if c is not a valid
391+ * unicode character
392+ */
393+ static inline bool bitstring_insert_utf16 (term dst_bin , size_t offset , avm_int_t c , enum BitstringFlags bs_flags , size_t * out_size )
394+ {
395+ // size was verified by a bs_utf8_size instruction call
396+ uint8_t * dst = (uint8_t * ) term_binary_data (dst_bin ) + (offset >> 3 );
397+ return bitstring_utf16_encode (c , dst , bs_flags , out_size );
398+ }
399+
400+ /**
401+ * @brief Insert a character in UTF-32 format
402+ *
403+ * @param dst_bin binary to insert to
404+ * @param offset offset, in bits, to where to insert the character
405+ * @param c character to encode
406+ * @param bs_flags flags to encode the character (undefined/little/big/native)
407+ * @param out_size the size in bytes, on output
408+ * @return \c true if encoding was successful, \c false if c is not a valid
409+ * unicode character
410+ */
411+ static inline bool bitstring_insert_utf32 (term dst_bin , size_t offset , avm_int_t c , enum BitstringFlags bs_flags )
412+ {
413+ uint8_t * dst = (uint8_t * ) term_binary_data (dst_bin ) + (offset >> 3 );
414+ return bitstring_utf32_encode (c , dst , bs_flags );
415+ }
416+
304417#ifdef __cplusplus
305418}
306419#endif
0 commit comments