GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_utility_unicode_to_utf8.c Lines: 34 34 100.0 %
Date: 2026-03-06 19:21:09 Branches: 10 10 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** GUIX Component                                                        */
17
/**                                                                       */
18
/**   Utility (Utility)                                                   */
19
/**                                                                       */
20
/**************************************************************************/
21
22
#define GX_SOURCE_CODE
23
24
/* Include necessary system files.  */
25
26
#include "gx_api.h"
27
#include "gx_utility.h"
28
29
/**************************************************************************/
30
/*                                                                        */
31
/*  FUNCTION                                               RELEASE        */
32
/*                                                                        */
33
/*    _gx_utility_utf8_string_character_get               PORTABLE C      */
34
/*                                                           6.1          */
35
/*  AUTHOR                                                                */
36
/*                                                                        */
37
/*    Kenneth Maxwell, Microsoft Corporation                              */
38
/*                                                                        */
39
/*  DESCRIPTION                                                           */
40
/*                                                                        */
41
/*    This function convert a unicode to a utf8.                          */
42
/*                                                                        */
43
/*  INPUT                                                                 */
44
/*                                                                        */
45
/*    unicode                               Unicode to convert            */
46
/*    return_utf8_str                       Returned utf8 string          */
47
/*    return_utf8_size                      utf8 string size              */
48
/*                                                                        */
49
/*  OUTPUT                                                                */
50
/*                                                                        */
51
/*    status                                Completion status             */
52
/*                                                                        */
53
/*  CALLS                                                                 */
54
/*                                                                        */
55
/*                                                                        */
56
/*  CALLED BY                                                             */
57
/*                                                                        */
58
/*    Application Code                                                    */
59
/*    GUIX Internal Code                                                  */
60
/*                                                                        */
61
/**************************************************************************/
62
#ifdef GX_UTF8_SUPPORT
63
128
UINT  _gx_utility_unicode_to_utf8(ULONG unicode, GX_UBYTE *return_utf8_str, UINT *return_utf8_size)
64
{
65
128
    if (unicode < 0x0080)
66
    {
67
1
        *return_utf8_size = 1;
68
1
        return_utf8_str[0] = (GX_UBYTE)unicode;
69
    }
70
127
    else if (unicode < 0x0800)
71
    {
72
1
        *return_utf8_size = 2;
73
1
        return_utf8_str[0] = (GX_UBYTE)(0xC0 + (GX_UBYTE)((unicode & 0x7C0) >> 6));
74
1
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
75
    }
76
126
    else if (unicode < 0x10000)
77
    {
78
121
        *return_utf8_size = 3;
79
121
        return_utf8_str[0] = (GX_UBYTE)(0xE0 + (GX_UBYTE)((unicode & 0xF000) >> 12));
80
121
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
81
121
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
82
    }
83
5
    else if (unicode < 0x200000)
84
    {
85
2
        *return_utf8_size = 4;
86
2
        return_utf8_str[0] = (GX_UBYTE)(0xF0 + (GX_UBYTE)((unicode & 0x1C0000) >> 18));
87
2
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
88
2
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
89
2
        return_utf8_str[3] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
90
    }
91
3
    else if (unicode < 0x4000000)
92
    {
93
2
        *return_utf8_size = 5;
94
2
        return_utf8_str[0] = (GX_UBYTE)(0xFC + (GX_UBYTE)((unicode & 0x3000000) >> 24));
95
2
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0000) >> 18));
96
2
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
97
2
        return_utf8_str[3] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
98
2
        return_utf8_str[4] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
99
    }
100
    else
101
    {
102
1
        *return_utf8_size = 6;
103
1
        return_utf8_str[0] = (GX_UBYTE)(0xFC + (GX_UBYTE)((unicode & 0x40000000) >> 30));
104
1
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000000) >> 24));
105
1
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0000) >> 18));
106
1
        return_utf8_str[3] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
107
1
        return_utf8_str[4] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
108
1
        return_utf8_str[5] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
109
    }
110
111
128
    return GX_SUCCESS;
112
}
113
#endif /* GX_UTF8_SUPPORT */
114