| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "utfutils.h" |
| 21 |
|
| 22 |
int decodeUTF8(const unsigned char* data, signed char& len) { |
| 23 |
int ret=0xfffd; |
| 24 |
if (data[0]<0x80) { |
| 25 |
ret=data[0]; |
| 26 |
len=1; |
| 27 |
} else if (data[0]<0xc0) { |
| 28 |
ret=0xfffd; // invalid |
| 29 |
len=1; |
| 30 |
} else if (data[0]<0xe0) { |
| 31 |
if (data[1]>=0x80 && data[1]<0xc0) { |
| 32 |
len=2; |
| 33 |
ret=((data[0]&31)<<6)| |
| 34 |
(data[1]&63); |
| 35 |
} else len=1; |
| 36 |
} else if (data[0]<0xf0) { |
| 37 |
if (data[1]>=0x80 && data[1]<0xc0) { |
| 38 |
if (data[2]>=0x80 && data[2]<0xc0) { |
| 39 |
len=3; |
| 40 |
ret=((data[0]&15)<<12)| |
| 41 |
((data[1]&63)<<6)| |
| 42 |
(data[2]&63); |
| 43 |
} else len=2; |
| 44 |
} else len=1; |
| 45 |
} else if (data[0]<0xf5) { |
| 46 |
if (data[1]>=0x80 && data[1]<0xc0) { |
| 47 |
if (data[2]>=0x80 && data[2]<0xc0) { |
| 48 |
if (data[3]>=0x80 && data[3]<0xc0) { |
| 49 |
len=4; |
| 50 |
ret=((data[0]&7)<<18)| |
| 51 |
((data[1]&63)<<12)| |
| 52 |
((data[2]&63)<<6)| |
| 53 |
(data[3]&63); |
| 54 |
} else len=3; |
| 55 |
} else len=2; |
| 56 |
} else len=1; |
| 57 |
} else { |
| 58 |
len=1; |
| 59 |
return 0xfffd; |
| 60 |
} |
| 61 |
|
| 62 |
if ((ret>=0xd800 && ret<=0xdfff) || ret>=0x110000) return 0xfffd; |
| 63 |
return ret; |
| 64 |
} |
| 65 |
|
| 66 |
size_t utf8len(const char* s) { |
| 67 |
size_t p=0; |
| 68 |
size_t r=0; |
| 69 |
signed char cl; |
| 70 |
while (s[p]!=0) { |
| 71 |
r++; |
| 72 |
decodeUTF8((const unsigned char*)&s[p],cl); |
| 73 |
p+=cl; |
| 74 |
} |
| 75 |
return r; |
| 76 |
} |
| 77 |
|
| 78 |
char utf8csize(const unsigned char* c) { |
| 79 |
signed char ret; |
| 80 |
decodeUTF8(c,ret); |
| 81 |
return ret; |
| 82 |
} |
| 83 |
|
| 84 |
WString utf8To16(const char* s) { |
| 85 |
WString ret; |
| 86 |
int ch, p; |
| 87 |
signed char chs; |
| 88 |
p=0; |
| 89 |
while (s[p]!=0) { |
| 90 |
ch=decodeUTF8((const unsigned char*)&s[p],chs); |
| 91 |
// surrogates |
| 92 |
if (ch>=0x10000) { |
| 93 |
ch-=0x10000; |
| 94 |
ret+=(unsigned short)(0xd800|((ch>>10)&0x3ff)); |
| 95 |
ret+=(unsigned short)(0xdc00|(ch&0x3ff)); |
| 96 |
} else if (ch<0xd800 || ch>0xdfff) { |
| 97 |
ret+=(unsigned short)ch; |
| 98 |
} |
| 99 |
p+=chs; |
| 100 |
} |
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
String utf16To8(const wchar_t* s) { |
| 105 |
String ret; |
| 106 |
unsigned int next=0; |
| 107 |
for (size_t i=0; i<wcslen(s); i++) { |
| 108 |
if (s[i]>=0xd800 && s[i]<0xdc00) { |
| 109 |
next=0x10000+((s[i]&0x3ff)<<10); |
| 110 |
continue; |
| 111 |
} else if (s[i]>=0xdc00 && s[i]<0xe000) { |
| 112 |
next|=s[i]&0x3ff; |
| 113 |
} else { |
| 114 |
next=s[i]; |
| 115 |
} |
| 116 |
if (next<0x80) { |
| 117 |
ret+=next; |
| 118 |
} else if (next<0x800) { |
| 119 |
ret+=(0xc0+((next>>6)&31)); |
| 120 |
ret+=(0x80+((next)&63)); |
| 121 |
} else if (next<0x10000) { |
| 122 |
ret+=(0xe0+((next>>12)&15)); |
| 123 |
ret+=(0x80+((next>>6)&63)); |
| 124 |
ret+=(0x80+((next)&63)); |
| 125 |
} else { |
| 126 |
ret+=(0xf0+((next>>18)&7)); |
| 127 |
ret+=(0x80+((next>>12)&63)); |
| 128 |
ret+=(0x80+((next>>6)&63)); |
| 129 |
ret+=(0x80+((next)&63)); |
| 130 |
} |
| 131 |
} |
| 132 |
return ret; |
| 133 |
} |