From f8f3f2f59bae15a9241c52f944c31d86690f2e27 Mon Sep 17 00:00:00 2001 From: Xnoe Date: Thu, 9 Jul 2020 20:32:44 +0100 Subject: [PATCH] Updated Makefile... again --- Makefile | 1 + usr/include/utf8.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 usr/include/utf8.h diff --git a/Makefile b/Makefile index aeba68b..cd1e4ba 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,3 @@ install: + mkdir -p $(DESTDIR)/usr/include/ cp -f utf8.h $(DESTDIR)/usr/include/utf8.h diff --git a/usr/include/utf8.h b/usr/include/utf8.h new file mode 100644 index 0000000..94268b7 --- /dev/null +++ b/usr/include/utf8.h @@ -0,0 +1,76 @@ +// license: cc0 + +#include +#include +#include +#include +#include + +class char32 { + uint32_t fetch32(char** cstr) { + uint32_t r(0); + int i(1); + unsigned char compare = (unsigned char)**cstr; + if (compare >> 3 == 0b11110) i = 4; + if (compare >> 4 == 0b1110) i = 3; + if (compare >> 5 == 0b110) i = 2; + for (;i>0;i--) { + r <<= 8; + r += (unsigned char)**cstr; + (*cstr)++; + } + return r; + } +public: + uint32_t c; + char32(uint32_t i) {c = i;} + char32(const char* s) {c = fetch32((char**)&s);} + char32(char** s) {c = fetch32(s);} + bool operator==(char* cs) {return c==fetch32(&cs);} + bool operator==(char32 cs) {return c==cs.c;} + uint32_t operator>>(int a) const {return c>>a;} + char* toChar() const { + return new char[4]{(char)(c>>24), (char)(c>>16), (char)(c>>8), (char)c}; + } +}; + +std::ostream& operator<<(std::ostream& stream, const char32& c32) { + char* asChar = c32.toChar(); + for (int i(0);i<4;i++) + if (asChar[i]) + stream << asChar[i]; + delete asChar; + return stream; +} + +struct string32 { + std::vector cs; + string32(char* sd) { + while (sd[0]) + cs.push_back(char32(&sd)); + } + int size() const {return cs.size();} + char32 operator[](int i) const {return cs[i];} + void replace(string32 find, string32 with) { + int havematched(0); + int findsize = find.cs.size(); + for (int index(0); index < cs.size(); index++) { + if (cs.at(index) == find.cs.at(havematched)) + havematched++; + else + havematched=0; + if (havematched == findsize) { + index -= findsize-1; + cs.erase(cs.begin()+index, cs.begin()+index+findsize); + cs.insert(cs.begin()+index, with.cs.begin(), with.cs.end()); + break; + } + } + } +}; + +std::ostream& operator<<(std::ostream& stream, const string32& s32) { + for (int i=0;i