1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include "charset.h"
19 
utf16_strlen(const char16_t * str)20 size_t utf16_strlen(const char16_t *str) {
21   size_t len = 0;
22 
23   for (; *str; str++) {
24     len++;
25   }
26 
27   return len;
28 }
29 
utf16_strcmp(const char16_t * s1,const char16_t * s2)30 int utf16_strcmp(const char16_t *s1, const char16_t *s2) {
31   int ret = 0;
32 
33   for (; ; s1++, s2++) {
34     ret = *s1 - *s2;
35     if (ret || !*s1)
36       break;
37   }
38 
39   return ret;
40 }
41 
42 
43 /**
44  * @brief convert utf-8 string to utf-16 string.
45  *
46  * This function converts utf-8 string to utf-16 string. However
47  * it only supports us-ascii input right now.
48  */
utf8_to_utf16(char16_t * dest,const char * src,size_t size)49 int utf8_to_utf16(char16_t *dest, const char *src, size_t size) {
50   size_t i = 0;
51   for (; i < size - 1 && *src; i++, src++) {
52     dest[i] = *src;
53   }
54 
55   dest[i] = 0;
56   return 0;
57 }
58 
59 /**
60  * @brief convert utf-16 string to utf-8 string.
61  *
62  * This function converts utf-16 string to utf-8 string. However
63  * it only supports us-ascii output right now.
64  */
utf16_to_utf8(char * dest,const char16_t * src,size_t size)65 int utf16_to_utf8(char *dest, const char16_t *src, size_t size) {
66   size_t i = 0;
67   for (; i < size - 1 && *src; i++, src++) {
68     dest[i] = *src;
69   }
70 
71   dest[i] = 0;
72   return 0;
73 }
74