core :: UCharString :: defaultinit
# ICU string `UChar *` which are UTF-16 strings
extern class UCharString `{ UChar *`}
# Returns an empty `UCharString` of length `length`
new empty (length: Int) `{
UChar * str = (UChar *)malloc(sizeof(UChar) * length);
u_memset(str, 0, length);
return str;
`}
# Returns a `NULL` `UCharString`
new nul `{ return NULL; `}
# Returns the number of code points up to `code_units` characters
fun code_points(code_units: Int): Int `{
if (self == NULL) {
return -1;
}
return u_countChar32(self, code_units);
`}
# Converts a `CString` to a `UCharString` and returns the required length of said `UCharString`
fun from_cstring(dest_length: Int, source: CString, source_length: Int): Int `{
UErrorCode error = U_ZERO_ERROR;
int32_t res;
u_strFromUTF8(self, dest_length, &res, source, source_length, &error);
return res;
`}
# Converts `self` to a `CString` and returns the required length (without the termination character) of said `CString`
fun to_cstring(dest: CString, dest_length: Int, source_length: Int): Int `{
UErrorCode error = U_ZERO_ERROR;
int32_t res;
u_strToUTF8(dest, dest_length, &res, self, source_length, &error);
return res;
`}
# Get code point at code unit `offset`
fun char_at_offset(offset: Int, code_units: Int): Char `{
UChar32 c = 0;
U16_NEXT(self, offset, code_units, c);
return c;
`}
end
lib/core/text/u16_string.nit:144,1--187,3