See TODOs in these functions.
This will probably be easy to implement. It might be trickier and will require some research to ensure we're covering all cases correctly. Also not sure if we can assume UTF-8 or need to handle some exotic encoding.
See also the xmlEncodeEntitiesReentrant() function in libxml2. It has problems (see T768) but its Unicode logic might be mostly correct:
https://gitlab.gnome.org/GNOME/libxml2/-/blob/master/entities.c
Alternatively, xmlEncodeSpecialChars() (also problematic) thinks Unicode characters will "just work" somehow... I haven't done any research on this yet.