Updated the toChar function to return correctly lengthed pointer

This commit is contained in:
Xnoe 2020-07-10 06:00:01 +01:00 committed by Elsie
parent a866bb6388
commit e85191ee0c

19
utf8.h
View File

@ -7,15 +7,16 @@
#include <stdint.h> #include <stdint.h>
class char32 { class char32 {
int size = 1;
uint32_t fetch32(char** cstr) { uint32_t fetch32(char** cstr) {
uint32_t r(0); uint32_t r(0);
if (!**cstr) if (!**cstr)
return r; return r;
int i(1);
unsigned char compare = (unsigned char)**cstr; unsigned char compare = (unsigned char)**cstr;
if (compare >> 3 == 0b11110) i = 4; if (compare >> 3 == 0b11110) size = 4;
if (compare >> 4 == 0b1110) i = 3; if (compare >> 4 == 0b1110) size = 3;
if (compare >> 5 == 0b110) i = 2; if (compare >> 5 == 0b110) size = 2;
int i = size;
for (;i>0;i--) { for (;i>0;i--) {
r <<= 8; r <<= 8;
r += (unsigned char)**cstr; r += (unsigned char)**cstr;
@ -32,15 +33,17 @@ public:
bool operator==(char32 cs) {return c==cs.c;} bool operator==(char32 cs) {return c==cs.c;}
uint32_t operator>>(int a) const {return c>>a;} uint32_t operator>>(int a) const {return c>>a;}
char* toChar() const { char* toChar() const {
return new char[4]{(char)(c>>24), (char)(c>>16), (char)(c>>8), (char)c}; char* toReturn = new char[size];
int i = size;
for (;i>=0;i--)
toReturn[i] = (char)(c>>(8*i));
return toReturn;
} }
}; };
std::ostream& operator<<(std::ostream& stream, const char32& c32) { std::ostream& operator<<(std::ostream& stream, const char32& c32) {
char* asChar = c32.toChar(); char* asChar = c32.toChar();
for (int i(0);i<4;i++) stream << asChar;
if (asChar[i])
stream << asChar[i];
delete asChar; delete asChar;
return stream; return stream;
} }