Rob Clark | 0d138cf | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * charset conversion utils |
| 3 | * |
| 4 | * Copyright (c) 2017 Rob Clark |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #ifndef __CHARSET_H_ |
| 10 | #define __CHARSET_H_ |
| 11 | |
| 12 | #define MAX_UTF8_PER_UTF16 4 |
| 13 | |
| 14 | /** |
| 15 | * utf16_strlen() - Get the length of an utf16 string |
| 16 | * |
| 17 | * Returns the number of 16 bit characters in an utf16 string, not |
| 18 | * including the terminating NULL character. |
| 19 | * |
| 20 | * @in the string to measure |
| 21 | * @return the string length |
| 22 | */ |
| 23 | size_t utf16_strlen(const uint16_t *in); |
| 24 | |
| 25 | /** |
| 26 | * utf16_strnlen() - Get the length of a fixed-size utf16 string. |
| 27 | * |
| 28 | * Returns the number of 16 bit characters in an utf16 string, |
| 29 | * not including the terminating NULL character, but at most |
| 30 | * 'count' number of characters. In doing this, utf16_strnlen() |
| 31 | * looks at only the first 'count' characters. |
| 32 | * |
| 33 | * @in the string to measure |
| 34 | * @count the maximum number of characters to count |
| 35 | * @return the string length, up to a maximum of 'count' |
| 36 | */ |
| 37 | size_t utf16_strnlen(const uint16_t *in, size_t count); |
| 38 | |
| 39 | /** |
| 40 | * utf16_strcpy() - UTF16 equivalent of strcpy() |
| 41 | */ |
| 42 | uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src); |
| 43 | |
| 44 | /** |
| 45 | * utf16_strdup() - UTF16 equivalent of strdup() |
| 46 | */ |
| 47 | uint16_t *utf16_strdup(const uint16_t *s); |
| 48 | |
| 49 | /** |
| 50 | * utf16_to_utf8() - Convert an utf16 string to utf8 |
| 51 | * |
| 52 | * Converts 'size' characters of the utf16 string 'src' to utf8 |
| 53 | * written to the 'dest' buffer. |
| 54 | * |
| 55 | * NOTE that a single utf16 character can generate up to 4 utf8 |
| 56 | * characters. See MAX_UTF8_PER_UTF16. |
| 57 | * |
| 58 | * @dest the destination buffer to write the utf8 characters |
| 59 | * @src the source utf16 string |
| 60 | * @size the number of utf16 characters to convert |
| 61 | * @return the pointer to the first unwritten byte in 'dest' |
| 62 | */ |
| 63 | uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size); |
| 64 | |
| 65 | #endif /* __CHARSET_H_ */ |