Skip to content
This repository was archived by the owner on Sep 14, 2020. It is now read-only.

Commit e9b0a93

Browse files
committed
数値変換をサポート
1 parent 655e9fc commit e9b0a93

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

utflib/Source.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ int main()
8787
//SJIS (環境によっては異なる)
8888
const std::string sstr = "こんにちはabcdef";
8989
const char* scharP = "こんにちはabcdef";
90-
const char schar = 'K';
90+
const char schar = '';
9191

9292
//to_UTF-8--------------------------------------------------
9393

@@ -96,17 +96,17 @@ int main()
9696
//UTF-8
9797
std::cout << utf8_s(sstr) << std::endl;
9898
std::cout << utf8_s(scharP) << std::endl;
99-
//std::cout << utf8_s(schar) << std::endl;
99+
std::cout << utf8_s(schar) << std::endl;
100100

101101
//UTF-16
102102
std::cout << utf8(utf16_s(sstr)) << std::endl;
103103
std::cout << utf8(utf16_s(scharP)) << std::endl;
104-
//std::cout << utf8(utf16_s(schar)) << std::endl;
104+
std::cout << utf8(utf16_s(schar)) << std::endl;
105105

106106
//UTF-32
107107
std::cout << utf8(utf32_s(sstr)) << std::endl;
108108
std::cout << utf8(utf32_s(scharP)) << std::endl;
109-
//std::cout << utf8(utf32_s(schar)) << std::endl;
109+
std::cout << utf8(utf32_s(schar)) << std::endl;
110110

111111
//--------------------------------------------------
112112

utflib/utflib.hpp

+41-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ namespace utflib
313313
for (size_t j = 0; j < src_len;) {
314314
if (src[j] == 0) break;
315315
u32str += char_utf32(src, u8char, j, number_of_byte);
316-
j += number_of_byte;
316+
if(number_of_byte) j += number_of_byte;
317+
else ++j;
317318
}
318319
return u32str.c_str();
319320
}
@@ -346,9 +347,11 @@ namespace utflib
346347
char u8char[5]{};
347348
int32_t number_of_byte;
348349

349-
for (size_t j = 0;; j += number_of_byte) {
350+
for (size_t j = 0;;) {
350351
if (src[j] == 0) break;
351352
u32str += char_utf32(src, u8char, j, number_of_byte);
353+
if (number_of_byte) j += number_of_byte;
354+
else ++j;
352355
}
353356
return u32str.c_str();
354357
}
@@ -455,12 +458,29 @@ namespace utflib
455458
return u16str.c_str();
456459
}
457460

461+
// SJIS(char) から UTF-16(char16_t*)へ
462+
const char16_t* utf16_s(const char src)
463+
{
464+
static std::u16string u16str;
465+
u16str = u"";
466+
467+
const uint8_t src_8 = uint8_t(src);
468+
469+
if (src_8 <= 127) u16str += char16_t(src);
470+
else if (src_8 >= 161 && src_8 <= 223) u16str += char16_t(src_8 - 161) + 0xff61;
471+
472+
return u16str.c_str();
473+
}
474+
458475
// SJIS(string) から UTF-16(char16_t*)へ
459476
const char16_t* utf16_s(const std::string src) { return utf16_s(src.c_str()); }
460477

461478
// SJIS(char*) から UTF-8(char*)へ
462479
const char* utf8_s(const char* src) { return utf8(utf16_s(src)); }
463480

481+
// SJIS(char) から UTF-8(char*)へ
482+
const char* utf8_s(const char src) { return utf8(utf16_s(src)); }
483+
464484
// SJIS(string) から UTF-8(char*)へ
465485
const char* utf8_s(const std::string src) { return utf8(utf16_s(src.c_str())); }
466486

@@ -470,4 +490,23 @@ namespace utflib
470490
// SJIS(string) から UTF-32(char*)へ
471491
const char32_t* utf32_s(const std::string src) { return utf32(utf16_s(src.c_str())); }
472492

493+
// SJIS(char) から UTF-32(char32_t*)へ
494+
const char32_t* utf32_s(const char src) { return utf32(utf16_s(src)); }
495+
496+
// 数値 から UTF-8(char*)へ
497+
template <typename type_name>
498+
const char* utf8(type_name src) {
499+
static std::string u8str;
500+
u8str = std::to_string(src);
501+
return u8str.c_str();
502+
}
503+
504+
// 数値 から UTF-16(char16_t*)へ
505+
template <typename type_name>
506+
const char16_t* utf16(type_name src) { return utf16(utf8(src)); }
507+
508+
// 数値 から UTF-32(char32_t*)へ
509+
template <typename type_name>
510+
const char32_t* utf32(type_name src) { return utf32(utf8(src)); }
511+
473512
}

utflib/utflib.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
</ItemDefinitionGroup>
101101
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
102102
<ClCompile>
103-
<WarningLevel>Level3</WarningLevel>
103+
<WarningLevel>Level4</WarningLevel>
104104
<Optimization>MaxSpeed</Optimization>
105105
<FunctionLevelLinking>true</FunctionLevelLinking>
106106
<IntrinsicFunctions>true</IntrinsicFunctions>
@@ -119,7 +119,7 @@
119119
</ItemDefinitionGroup>
120120
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
121121
<ClCompile>
122-
<WarningLevel>Level3</WarningLevel>
122+
<WarningLevel>Level4</WarningLevel>
123123
<Optimization>MaxSpeed</Optimization>
124124
<FunctionLevelLinking>true</FunctionLevelLinking>
125125
<IntrinsicFunctions>true</IntrinsicFunctions>

0 commit comments

Comments
 (0)