Back to home page

LXR

 
 

    


0001 #include <linux/ucs2_string.h>
0002 #include <linux/module.h>
0003 
0004 /* Return the number of unicode characters in data */
0005 unsigned long
0006 ucs2_strnlen(const ucs2_char_t *s, size_t maxlength)
0007 {
0008         unsigned long length = 0;
0009 
0010         while (*s++ != 0 && length < maxlength)
0011                 length++;
0012         return length;
0013 }
0014 EXPORT_SYMBOL(ucs2_strnlen);
0015 
0016 unsigned long
0017 ucs2_strlen(const ucs2_char_t *s)
0018 {
0019         return ucs2_strnlen(s, ~0UL);
0020 }
0021 EXPORT_SYMBOL(ucs2_strlen);
0022 
0023 /*
0024  * Return the number of bytes is the length of this string
0025  * Note: this is NOT the same as the number of unicode characters
0026  */
0027 unsigned long
0028 ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength)
0029 {
0030         return ucs2_strnlen(data, maxlength/sizeof(ucs2_char_t)) * sizeof(ucs2_char_t);
0031 }
0032 EXPORT_SYMBOL(ucs2_strsize);
0033 
0034 int
0035 ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len)
0036 {
0037         while (1) {
0038                 if (len == 0)
0039                         return 0;
0040                 if (*a < *b)
0041                         return -1;
0042                 if (*a > *b)
0043                         return 1;
0044                 if (*a == 0) /* implies *b == 0 */
0045                         return 0;
0046                 a++;
0047                 b++;
0048                 len--;
0049         }
0050 }
0051 EXPORT_SYMBOL(ucs2_strncmp);
0052 
0053 unsigned long
0054 ucs2_utf8size(const ucs2_char_t *src)
0055 {
0056     unsigned long i;
0057     unsigned long j = 0;
0058 
0059     for (i = 0; src[i]; i++) {
0060         u16 c = src[i];
0061 
0062         if (c >= 0x800)
0063             j += 3;
0064         else if (c >= 0x80)
0065             j += 2;
0066         else
0067             j += 1;
0068     }
0069 
0070     return j;
0071 }
0072 EXPORT_SYMBOL(ucs2_utf8size);
0073 
0074 /*
0075  * copy at most maxlength bytes of whole utf8 characters to dest from the
0076  * ucs2 string src.
0077  *
0078  * The return value is the number of characters copied, not including the
0079  * final NUL character.
0080  */
0081 unsigned long
0082 ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength)
0083 {
0084     unsigned int i;
0085     unsigned long j = 0;
0086     unsigned long limit = ucs2_strnlen(src, maxlength);
0087 
0088     for (i = 0; maxlength && i < limit; i++) {
0089         u16 c = src[i];
0090 
0091         if (c >= 0x800) {
0092             if (maxlength < 3)
0093                 break;
0094             maxlength -= 3;
0095             dest[j++] = 0xe0 | (c & 0xf000) >> 12;
0096             dest[j++] = 0x80 | (c & 0x0fc0) >> 6;
0097             dest[j++] = 0x80 | (c & 0x003f);
0098         } else if (c >= 0x80) {
0099             if (maxlength < 2)
0100                 break;
0101             maxlength -= 2;
0102             dest[j++] = 0xc0 | (c & 0x7c0) >> 6;
0103             dest[j++] = 0x80 | (c & 0x03f);
0104         } else {
0105             maxlength -= 1;
0106             dest[j++] = c & 0x7f;
0107         }
0108     }
0109     if (maxlength)
0110         dest[j] = '\0';
0111     return j;
0112 }
0113 EXPORT_SYMBOL(ucs2_as_utf8);