Skip to content

Commit 8eff66c

Browse files
committed
Convert the text encoding stuff to C++17.
1 parent b0f78d5 commit 8eff66c

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

CMakeLists.txt

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ add_executable(clownmdemu-frontend WIN32
4444
"frontend.cpp"
4545
"frontend.h"
4646
"sdl-wrapper.h"
47-
"text-encoding.c"
47+
"text-encoding.cpp"
4848
"text-encoding.h"
4949
"winapi.c"
5050
"winapi.h"
@@ -89,8 +89,8 @@ set_target_properties(clownmdemu-frontend PROPERTIES
8989
C_STANDARD 90
9090
C_STANDARD_REQUIRED NO
9191
C_EXTENSIONS OFF
92-
CXX_STANDARD 11
93-
CXX_STANDARD_REQUIRED NO
92+
CXX_STANDARD 17
93+
CXX_STANDARD_REQUIRED YES
9494
CXX_EXTENSIONS OFF
9595
)
9696

@@ -99,11 +99,6 @@ set_target_properties(clownmdemu-frontend PROPERTIES
9999
####################
100100

101101
if(EMSCRIPTEN)
102-
# Emscripten needs C++17
103-
set_target_properties(clownmdemu-frontend PROPERTIES
104-
CXX_STANDARD 17
105-
)
106-
107102
# Add the file dialog library
108103
target_sources(clownmdemu-frontend PRIVATE "libraries/emscripten-browser-file/emscripten_browser_file.h")
109104

emulator-instance.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,10 @@ std::string EmulatorInstance::GetSoftwareName()
373373
// Eliminate padding (the Sonic games tend to use padding to make the name look good in a hex editor).
374374
if (codepoint != ' ' || previous_codepoint != ' ')
375375
{
376-
unsigned char utf8_buffer[4];
377-
const auto total_bytes = UTF32ToUTF8(utf8_buffer, codepoint);
378-
name_buffer.append(reinterpret_cast<char*>(utf8_buffer), total_bytes);
376+
const auto utf8_codepoint = UTF32ToUTF8(codepoint);
377+
378+
if (utf8_codepoint.has_value())
379+
name_buffer += *utf8_codepoint;
379380
}
380381

381382
previous_codepoint = codepoint;

text-encoding.c renamed to text-encoding.cpp

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "text-encoding.h"
22

3-
#include <stddef.h>
3+
#include <array>
44

5-
static const cc_u16l shiftjis_to_unicode_lookup[0x3100] = {
5+
static const std::array<cc_u16l, 0x3100> shiftjis_to_unicode_lookup = {
66
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
77
0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
88
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
@@ -796,7 +796,7 @@ cc_u16l ShiftJISToUTF32(const unsigned char* const in_buffer, cc_u8f* const byte
796796
switch (in_buffer[0] & 0xF0)
797797
{
798798
case 0x80:
799-
lookup_index = 0x100;
799+
lookup_index = 0x0100;
800800
break;
801801
case 0x90:
802802
lookup_index = 0x1100;
@@ -806,52 +806,51 @@ cc_u16l ShiftJISToUTF32(const unsigned char* const in_buffer, cc_u8f* const byte
806806
break;
807807

808808
default:
809-
if (bytes_read != NULL)
809+
if (bytes_read != nullptr)
810810
*bytes_read = 1;
811811

812812
return shiftjis_to_unicode_lookup[in_buffer[0]];
813813
}
814814

815-
lookup_index += ((in_buffer[0] << 8) | in_buffer[1]) & 0xFFF;
815+
lookup_index += ((static_cast<cc_u16f>(in_buffer[0]) << 8) | in_buffer[1]) & 0xFFF;
816816

817-
if (bytes_read != NULL)
817+
if (bytes_read != nullptr)
818818
*bytes_read = 2;
819819

820820
return shiftjis_to_unicode_lookup[lookup_index];
821821
}
822822

823-
cc_u8f UTF32ToUTF8(unsigned char* const out_buffer, const cc_u32f utf32_codepoint)
823+
std::optional<std::string> UTF32ToUTF8(const cc_u32f utf32_codepoint)
824824
{
825+
std::string utf8;
826+
utf8.reserve(4);
827+
825828
if (utf32_codepoint < 0x80)
826829
{
827-
out_buffer[0] = utf32_codepoint;
828-
return 1;
830+
utf8.push_back(static_cast<char>(utf32_codepoint));
829831
}
830832
else if (utf32_codepoint < 0x800)
831833
{
832-
out_buffer[0] = 0xC0 | utf32_codepoint >> 6;
833-
out_buffer[1] = 0x80 | (utf32_codepoint >> 0 & 0x3F);
834-
return 2;
834+
utf8.push_back(static_cast<char>(0xC0 | utf32_codepoint >> 6));
835+
utf8.push_back(static_cast<char>(0x80 | (utf32_codepoint >> 0 & 0x3F)));
835836
}
836837
else if (utf32_codepoint < 0x10000)
837838
{
838-
out_buffer[0] = 0xE0 | utf32_codepoint >> 12;
839-
out_buffer[1] = 0x80 | (utf32_codepoint >> 6 & 0x3F);
840-
out_buffer[2] = 0x80 | (utf32_codepoint >> 0 & 0x3F);
841-
return 3;
839+
utf8.push_back(static_cast<char>(0xE0 | utf32_codepoint >> 12));
840+
utf8.push_back(static_cast<char>(0x80 | (utf32_codepoint >> 6 & 0x3F)));
841+
utf8.push_back(static_cast<char>(0x80 | (utf32_codepoint >> 0 & 0x3F)));
842842
}
843843
else if (utf32_codepoint < 0x110000)
844844
{
845-
out_buffer[0] = 0xF0 | utf32_codepoint >> 18;
846-
out_buffer[1] = 0x80 | (utf32_codepoint >> 12 & 0x3F);
847-
out_buffer[2] = 0x80 | (utf32_codepoint >> 6 & 0x3F);
848-
out_buffer[3] = 0x80 | (utf32_codepoint >> 0 & 0x3F);
849-
return 4;
845+
utf8.push_back(static_cast<char>(0xF0 | utf32_codepoint >> 18));
846+
utf8.push_back(static_cast<char>(0x80 | (utf32_codepoint >> 12 & 0x3F)));
847+
utf8.push_back(static_cast<char>(0x80 | (utf32_codepoint >> 6 & 0x3F)));
848+
utf8.push_back(static_cast<char>(0x80 | (utf32_codepoint >> 0 & 0x3F)));
850849
}
851850
else
852851
{
853-
/* TODO: Report failure. */
854-
out_buffer[0] = ' ';
855-
return 1;
852+
return std::nullopt;
856853
}
854+
855+
return utf8;
857856
}

text-encoding.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
#ifndef TEXT_ENCODING_H
22
#define TEXT_ENCODING_H
33

4-
#include "clownmdemu-frontend-common/clownmdemu/clowncommon/clowncommon.h"
4+
#include <optional>
5+
#include <string>
56

6-
#ifdef __cplusplus
7-
extern "C" {
8-
#endif
7+
#include "clownmdemu-frontend-common/clownmdemu/clowncommon/clowncommon.h"
98

109
/* Returns UTF-32 codepoint. */
1110
/* Reads a maximum of two bytes. */
1211
cc_u16l ShiftJISToUTF32(const unsigned char* const in_buffer, cc_u8f* const bytes_read);
1312

1413
/* Returns number of bytes written (maximum 4). */
15-
cc_u8f UTF32ToUTF8(unsigned char* const out_buffer, const cc_u32f utf32_codepoint);
16-
17-
#ifdef __cplusplus
18-
}
19-
#endif
14+
std::optional<std::string> UTF32ToUTF8(const cc_u32f utf32_codepoint);
2015

2116
#endif /* TEXT_ENCODING_H */

0 commit comments

Comments
 (0)