diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ucs2_string.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/ucs2_string.c b/lib/ucs2_string.c index 6f500ef2301d..f0b323abb4c6 100644 --- a/lib/ucs2_string.c +++ b/lib/ucs2_string.c | |||
| @@ -49,3 +49,65 @@ ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len) | |||
| 49 | } | 49 | } |
| 50 | } | 50 | } |
| 51 | EXPORT_SYMBOL(ucs2_strncmp); | 51 | EXPORT_SYMBOL(ucs2_strncmp); |
| 52 | |||
| 53 | unsigned long | ||
| 54 | ucs2_utf8size(const ucs2_char_t *src) | ||
| 55 | { | ||
| 56 | unsigned long i; | ||
| 57 | unsigned long j = 0; | ||
| 58 | |||
| 59 | for (i = 0; i < ucs2_strlen(src); i++) { | ||
| 60 | u16 c = src[i]; | ||
| 61 | |||
| 62 | if (c >= 0x800) | ||
| 63 | j += 3; | ||
| 64 | else if (c >= 0x80) | ||
| 65 | j += 2; | ||
| 66 | else | ||
| 67 | j += 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | return j; | ||
| 71 | } | ||
| 72 | EXPORT_SYMBOL(ucs2_utf8size); | ||
| 73 | |||
| 74 | /* | ||
| 75 | * copy at most maxlength bytes of whole utf8 characters to dest from the | ||
| 76 | * ucs2 string src. | ||
| 77 | * | ||
| 78 | * The return value is the number of characters copied, not including the | ||
| 79 | * final NUL character. | ||
| 80 | */ | ||
| 81 | unsigned long | ||
| 82 | ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength) | ||
| 83 | { | ||
| 84 | unsigned int i; | ||
| 85 | unsigned long j = 0; | ||
| 86 | unsigned long limit = ucs2_strnlen(src, maxlength); | ||
| 87 | |||
| 88 | for (i = 0; maxlength && i < limit; i++) { | ||
| 89 | u16 c = src[i]; | ||
| 90 | |||
| 91 | if (c >= 0x800) { | ||
| 92 | if (maxlength < 3) | ||
| 93 | break; | ||
| 94 | maxlength -= 3; | ||
| 95 | dest[j++] = 0xe0 | (c & 0xf000) >> 12; | ||
| 96 | dest[j++] = 0x80 | (c & 0x0fc0) >> 6; | ||
| 97 | dest[j++] = 0x80 | (c & 0x003f); | ||
| 98 | } else if (c >= 0x80) { | ||
| 99 | if (maxlength < 2) | ||
| 100 | break; | ||
| 101 | maxlength -= 2; | ||
| 102 | dest[j++] = 0xc0 | (c & 0x7c0) >> 6; | ||
| 103 | dest[j++] = 0x80 | (c & 0x03f); | ||
| 104 | } else { | ||
| 105 | maxlength -= 1; | ||
| 106 | dest[j++] = c & 0x7f; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | if (maxlength) | ||
| 110 | dest[j] = '\0'; | ||
| 111 | return j; | ||
| 112 | } | ||
| 113 | EXPORT_SYMBOL(ucs2_as_utf8); | ||
