LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
font_utf8.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2024, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
28 #ifndef _NANO_FONT_UTF8_H_
29 #define _NANO_FONT_UTF8_H_
30 
31 #include <stdint.h>
32 #include <stddef.h>
33 
38 #define NANO_UTF8_INVALID 0xFFFFFFFFu
39 
73 static inline uint32_t nano_utf8_decode(const char **p, const char *end)
74 {
75  const unsigned char *s = (const unsigned char *)(*p);
76  if ( end != NULL && (const char *)s >= end )
77  {
78  return NANO_UTF8_INVALID;
79  }
80  unsigned char b0 = s[0];
81  if ( b0 == 0 )
82  {
83  return NANO_UTF8_INVALID;
84  }
85  uint32_t cp;
86  int extra;
87  uint32_t min_cp;
88 
89  if ( b0 < 0x80u )
90  {
91  *p = (const char *)(s + 1);
92  return b0;
93  }
94  if ( b0 < 0xC2u || b0 > 0xF4u )
95  {
96  // 0x80..0xBF lone continuation, 0xC0/0xC1 overlong, 0xF5..0xFF invalid.
97  *p = (const char *)(s + 1);
98  return NANO_UTF8_INVALID;
99  }
100  if ( b0 < 0xE0u )
101  {
102  cp = b0 & 0x1Fu;
103  extra = 1;
104  min_cp = 0x80u;
105  }
106  else if ( b0 < 0xF0u )
107  {
108  cp = b0 & 0x0Fu;
109  extra = 2;
110  min_cp = 0x800u;
111  }
112  else
113  {
114  cp = b0 & 0x07u;
115  extra = 3;
116  min_cp = 0x10000u;
117  }
118  for ( int i = 1; i <= extra; i++ )
119  {
120  if ( end != NULL && (const char *)(s + i) >= end )
121  {
122  *p = (const char *)(s + 1);
123  return NANO_UTF8_INVALID;
124  }
125  unsigned char bi = s[i];
126  if ( bi == 0 || (bi & 0xC0u) != 0x80u )
127  {
128  *p = (const char *)(s + 1);
129  return NANO_UTF8_INVALID;
130  }
131  cp = (cp << 6) | (bi & 0x3Fu);
132  }
133  if ( cp < min_cp || (cp >= 0xD800u && cp <= 0xDFFFu) || cp > 0x10FFFFu )
134  {
135  *p = (const char *)(s + 1);
136  return NANO_UTF8_INVALID;
137  }
138  *p = (const char *)(s + extra + 1);
139  return cp;
140 }
141 
142 #endif
#define NANO_UTF8_INVALID
Sentinel value returned by nano_utf8_decode() when the input is malformed, truncated, or contains an invalid sequence.
Definition: font_utf8.h:38
static uint32_t nano_utf8_decode(const char **p, const char *end)
Decode a single UTF-8 codepoint from p in a stateless way.
Definition: font_utf8.h:73