foxygit / fake-08-xbox360 Log in
commit 8ece1ea19a8d363a485df4d81da0cf2d6785cb1b
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Jun 18 07:28:10 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Fri Jun 18 07:28:10 2021 -0600

    Use zepto8 emoji conversion

    bump back to c++17
---
 libs/utf8-util/LICENSE        |  21 -----
 libs/utf8-util/README.md      |   6 --
 libs/utf8-util/utf8-util.cpp  |  76 ------------------
 libs/utf8-util/utf8-util.h    | 181 ------------------------------------------
 platform/3ds/Makefile         |   2 +-
 platform/SDL2Desktop/Makefile |   2 +-
 platform/switch/Makefile      |   2 +-
 platform/vita/Makefile        |   2 +-
 source/cart.cpp               |   3 +-
 source/emojiconversion.cpp    | 178 +++++++++++++++++++++++++----------------
 source/emojiconversion.h      |  18 ++++-
 source/picoluaapi.cpp         |   2 +-
 source/picoluaapi.h           |   2 +-
 source/vm.cpp                 |   7 +-
 test/Makefile                 |   2 +-
 15 files changed, 140 insertions(+), 364 deletions(-)

diff --git a/libs/utf8-util/LICENSE b/libs/utf8-util/LICENSE
deleted file mode 100644
index 74829da..0000000
--- a/libs/utf8-util/LICENSE
+++ /dev/null
@@ -1,21 +0,0 @@
-The MIT License (MIT)
-
-Copyright (c) 2014 L Witek
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
\ No newline at end of file
diff --git a/libs/utf8-util/README.md b/libs/utf8-util/README.md
deleted file mode 100644
index fb86b05..0000000
--- a/libs/utf8-util/README.md
+++ /dev/null
@@ -1,6 +0,0 @@
-https://github.com/0xcafed00d/utf8-util
-
-utf8-util
-=========
-
-c++ utilites for handling utf8 strings
diff --git a/libs/utf8-util/utf8-util.cpp b/libs/utf8-util/utf8-util.cpp
deleted file mode 100644
index 8f3fd2e..0000000
--- a/libs/utf8-util/utf8-util.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-#include <stdexcept>
-#include "utf8-util.h"
-
-namespace utf8
-{
-    namespace impl
-    {
-        int processLeading (char c, char32_t& initialcp, bool throwOnErr)
-        {
-            if ((c & 0x80) == 0)
-            {
-                initialcp = c;
-                return  0;
-            }
-
-            if ((c & 0xe0) == 0xc0)
-            {
-                initialcp = c & 0x1f;
-                return 1;
-            }
-            else if ((c & 0xf0) == 0xe0)
-            {
-                initialcp = c & 0x0f;
-                return 2;
-            }
-            else if ((c & 0xf8) == 0xf0)
-            {
-                initialcp = c & 0x07;
-                return 3;
-            }
-            else
-            {
-                if (throwOnErr)
-                    throw std::runtime_error("invalid continuation count in leading byte");
-                return -1;
-            }
-        }
-
-        int countContinuations (char c, bool throwOnErr)
-        {
-            if ((c & 0x80) == 0)
-                return  0;
-            else if ((c & 0xe0) == 0xc0)
-                return 1;
-            else if ((c & 0xf0) == 0xe0)
-                return 2;
-            else if ((c & 0xf8) == 0xf0)
-                return 3;
-            else
-            {
-                if (throwOnErr)
-                    throw std::runtime_error("invalid continuation count in leading byte");
-
-                return -1;
-            }
-        }
-
-        bool isContinuation (char c)
-        {
-            return (c & 0xc0) == 0x80;
-        }
-
-        bool addContinuation (char c, char32_t& cp, bool throwOnError)
-        {
-            if ((c & 0xc0) == 0x80)
-            {
-                cp = (cp << 6) + (c & 0x3f);
-                return true;
-            }
-            if (throwOnErr)
-                throw std::runtime_error("expected continuation byte not found");
-            return false;
-        }
-    }
-}
-
diff --git a/libs/utf8-util/utf8-util.h b/libs/utf8-util/utf8-util.h
deleted file mode 100644
index 61d5ec0..0000000
--- a/libs/utf8-util/utf8-util.h
+++ /dev/null
@@ -1,181 +0,0 @@
-#pragma once
-
-#include <cstdint>
-#include <stdexcept>
-
-namespace utf8
-{
-    bool isValidUTF8 (const char* str);
-
-    namespace impl
-    {
-        const bool throwOnErr = true;
-
-        int processLeading (char c, char32_t& initialcp, bool throwOnErr = false);
-        bool addContinuation (char c, char32_t& cp, bool throwOnErr = false);
-        int countContinuations (char c, bool throwOnErr = false);
-        bool isContinuation (char c);
-
-        template <typename container_t>
-        class CodepointIteratorImpl
-        {
-            private:
-                const container_t& m_container;
-
-            public:
-                class iterator_impl
-                {
-                    private:
-                        typename container_t::const_iterator m_itr;
-                        typename container_t::const_iterator m_endItr;
-
-                        void verifyItrInc (typename container_t::const_iterator& i, const char* errmsg)
-                        {
-                            if (i == m_endItr)
-                                throw std::runtime_error(errmsg);
-
-                            ++i;
-                        }
-
-                    public:
-                        iterator_impl (const typename container_t::const_iterator& i, const typename container_t::const_iterator& end) : m_itr (i), m_endItr (end) {}
-
-                        char32_t operator* ()
-                        {
-                            auto itr = m_itr;
-                            char32_t cp = 0;
-                            int ncont = impl::processLeading(*itr, cp, throwOnErr);
-                            while (ncont)
-                            {
-                                verifyItrInc(itr, "incomplete codepoint at end of input");
-                                ncont--;
-                                impl::addContinuation(*itr, cp, throwOnErr);
-                            }
-                            return cp;
-                        }
-
-                        iterator_impl& operator++()
-                        {
-                            int ncont = impl::countContinuations(*m_itr, throwOnErr);
-                            verifyItrInc(m_itr, "attempt to move past end of input");
-                            while (ncont)
-                            {
-                                verifyItrInc(m_itr, "incomplete codepoint at end of input");
-                                ncont--;
-                            }
-                            return *this;
-                        }
-
-                        bool operator== (const iterator_impl& that)
-                        {
-                            return m_itr == that.m_itr;
-                        }
-
-                        bool operator!= (const iterator_impl& that)
-                        {
-                            return m_itr != that.m_itr;
-                        }
-                };
-
-                CodepointIteratorImpl (const container_t& c) : m_container(c)
-                {
-                }
-
-                iterator_impl begin () const
-                {
-                    return iterator_impl(m_container.cbegin(), m_container.cend());
-                }
-
-                iterator_impl end () const
-                {
-                    return iterator_impl(m_container.cend(), m_container.cend());
-                }
-        };
-    }
-
-    template <typename container_t>
-    inline impl::CodepointIteratorImpl<container_t> CodepointIterator (const container_t& c)
-    {
-        return impl::CodepointIteratorImpl<container_t>(c);
-    }
-
-    template <typename iterator_t>
-    bool isValidUTF8 (iterator_t begin, iterator_t end)
-    {
-        int cc = 0;
-        while (begin != end)
-        {
-            cc = impl::countContinuations(*begin);
-            if (cc < 0)
-                return false;
-
-            ++begin;
-            while (cc)
-            {
-                if ((begin != end) && impl::isContinuation(*begin))
-                {
-                    ++begin;
-                    cc--;
-                }
-                else
-                    return false;
-            }
-        }
-        return cc == 0;
-    }
-
-    template <typename container_t>
-    bool isValidUTF8 (const container_t& c)
-    {
-        return isValidUTF8(c.begin(), c.end());
-    }
-
-    namespace impl
-    {
-        template <typename value_t>
-        char32_t appendContinuation (char32_t codepoint, value_t& c)
-        {
-            c = static_cast<value_t>((codepoint & 0x3f) | 0x80);
-            return codepoint >> 6;
-        }
-    }
-
-    template <typename container_t>
-    void encodeCodepoint (char32_t codepoint, container_t& container)
-    {
-        if (codepoint > 0x10ffff)
-            throw std::runtime_error ("codepoint out of range");
-
-        if (codepoint < 0x80)
-        {
-            container.push_back(static_cast<typename container_t::value_type>(codepoint));
-        }
-        else if (codepoint < 0x800)
-        {
-            typename container_t::value_type c;
-            codepoint = impl::appendContinuation(codepoint, c);
-            container.push_back(static_cast<typename container_t::value_type>((codepoint & 0x1f) | 0xc0 ));
-            container.push_back(c);
-        }
-        else if (codepoint < 0x8000)
-        {
-            typename container_t::value_type c[2];
-            codepoint = impl::appendContinuation(codepoint, c[1]);
-            codepoint = impl::appendContinuation(codepoint, c[0]);
-            container.push_back(static_cast<typename container_t::value_type>((codepoint & 0x0f) | 0xe0));
-            container.push_back(c[0]);
-            container.push_back(c[1]);
-        }
-        else // codepoint < 0x80000
-        {
-            typename container_t::value_type c[3];
-            codepoint = impl::appendContinuation(codepoint, c[2]);
-            codepoint = impl::appendContinuation(codepoint, c[1]);
-            codepoint = impl::appendContinuation(codepoint, c[0]);
-            container.push_back(static_cast<typename container_t::value_type>((codepoint & 0x07) | 0xf0));
-            container.push_back(c[0]);
-            container.push_back(c[1]);
-            container.push_back(c[2]);
-        }
-    }
-}
diff --git a/platform/3ds/Makefile b/platform/3ds/Makefile
index 98567a7..a640536 100644
--- a/platform/3ds/Makefile
+++ b/platform/3ds/Makefile
@@ -72,7 +72,7 @@ CFLAGS	:=	-g -Wall -Ofast -mword-relocations \

 CFLAGS	+=	$(INCLUDE) -DARM11 -D_3DS -DVER_STR=\"$(APP_VERSION)\"

-CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++11
+CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++17

 ASFLAGS	:=	-g $(ARCH)
 LDFLAGS	=	-specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
diff --git a/platform/SDL2Desktop/Makefile b/platform/SDL2Desktop/Makefile
index 5036806..8761532 100644
--- a/platform/SDL2Desktop/Makefile
+++ b/platform/SDL2Desktop/Makefile
@@ -21,7 +21,7 @@ CFLAGS	:=	-g -Wall -ffunction-sections \

 CFLAGS	+=	$(INCLUDE) -DVER_STR=\"$(APP_VERSION)\"

-CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++11
+CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++17

 LIBS	:= -lSDL2

diff --git a/platform/switch/Makefile b/platform/switch/Makefile
index f16fefd..050607b 100644
--- a/platform/switch/Makefile
+++ b/platform/switch/Makefile
@@ -55,7 +55,7 @@ CFLAGS	:=	-g -Wall -O2 -ffunction-sections -DVER_STR=\"$(APP_VERSION)\" \

 CFLAGS	+=	$(INCLUDE) -D__SWITCH__

-CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++11
+CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++17

 ASFLAGS	:=	-g $(ARCH)
 LDFLAGS	=	-specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
diff --git a/platform/vita/Makefile b/platform/vita/Makefile
index ab5a040..bbfe79a 100644
--- a/platform/vita/Makefile
+++ b/platform/vita/Makefile
@@ -25,7 +25,7 @@ CC      = $(PREFIX)-gcc
 CXX      = $(PREFIX)-g++
 STRIP := $(PREFIX)-strip
 CFLAGS  = -g -Wl,-q -O2 -ftree-vectorize -D__VITA__ -DVER_STR=\"$(APP_VERSION)\" $(INCLUDE)
-CXXFLAGS  = $(CFLAGS) -fexceptions -std=gnu++11 -fpermissive
+CXXFLAGS  = $(CFLAGS) -fexceptions -std=gnu++17 -fpermissive
 ASFLAGS = $(CFLAGS)

 CC = $(CXX)
diff --git a/source/cart.cpp b/source/cart.cpp
index 73dca47..b7f9dee 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -340,7 +340,8 @@ Cart::Cart(std::string filename, std::string cartDirectory){

         while (std::getline(s, line)) {
             line = utils::trimright(line, " \n\r");
-            line = convert_emojis(line);
+            line = charset::utf8_to_pico8(line);
+            //line = convert_emojis(line);

             if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
                 currSec = line;
diff --git a/source/emojiconversion.cpp b/source/emojiconversion.cpp
index 52f12d0..96c3501 100644
--- a/source/emojiconversion.cpp
+++ b/source/emojiconversion.cpp
@@ -1,71 +1,113 @@
+
+//
+//  ZEPTO-8 — Fantasy console emulator
+//
+//  Copyright © 2016—2020 Sam Hocevar <sam@hocevar.net>
+//
+//  This program is free software. It comes without any warranty, to
+//  the extent permitted by applicable law. You can redistribute it
+//  and/or modify it under the terms of the Do What the Fuck You Want
+//  to Public License, Version 2, as published by the WTFPL Task Force.
+//  See http://www.wtfpl.net/ for more details.
+//
+
+
+// I know codecvt_utf8 is deprecated, but let’s hope C++ comes with a
+// replacement before they actually remove the feature.
+#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1
+
+
+#include <locale>
 #include <string>
-#include <map>
+#include <codecvt>
+#include <cstring>
+#include <regex>
+#include <string_view>

 #include "emojiconversion.h"
-#include "utf8-util.h"
-
-std::map<char32_t, uint8_t> emoji = {
-    {0x025ae, 0x10}, {0x025a0, 0x11}, {0x025a1, 0x12}, {0x02059, 0x13}, {0x02058, 0x14},
-    {0x02016, 0x15}, {0x025c0, 0x16}, {0x025b6, 0x17}, {0x0300c, 0x18}, {0x0300d, 0x19},
-    {0x000a5, 0x1a}, {0x02022, 0x1b}, {0x03001, 0x1c}, {0x03002, 0x1d}, {0x0309b, 0x1e},
-    {0x0309c, 0x1f}, {0x00020, 0x20}, {0x00021, 0x21}, {0x00022, 0x22}, {0x00023, 0x23},
-    {0x00024, 0x24}, {0x00025, 0x25}, {0x00026, 0x26}, {0x00027, 0x27}, {0x00028, 0x28},
-    {0x00029, 0x29}, {0x0002a, 0x2a}, {0x0002b, 0x2b}, {0x0002c, 0x2c}, {0x0002d, 0x2d},
-    {0x0002e, 0x2e}, {0x0002f, 0x2f}, {0x00030, 0x30}, {0x00031, 0x31}, {0x00032, 0x32},
-    {0x00033, 0x33}, {0x00034, 0x34}, {0x00035, 0x35}, {0x00036, 0x36}, {0x00037, 0x37},
-    {0x00038, 0x38}, {0x00039, 0x39}, {0x0003a, 0x3a}, {0x0003b, 0x3b}, {0x0003c, 0x3c},
-    {0x0003d, 0x3d}, {0x0003e, 0x3e}, {0x0003f, 0x3f}, {0x00040, 0x40}, {0x1d622, 0x41},
-    {0x1d623, 0x42}, {0x1d624, 0x43}, {0x1d625, 0x44}, {0x1d626, 0x45}, {0x1d627, 0x46},
-    {0x1d628, 0x47}, {0x1d629, 0x48}, {0x1d62a, 0x49}, {0x1d62b, 0x4a}, {0x1d62c, 0x4b},
-    {0x1d62d, 0x4c}, {0x1d62e, 0x4d}, {0x1d62f, 0x4e}, {0x1d630, 0x4f}, {0x1d631, 0x50},
-    {0x1d632, 0x51}, {0x1d633, 0x52}, {0x1d634, 0x53}, {0x1d635, 0x54}, {0x1d636, 0x55},
-    {0x1d637, 0x56}, {0x1d638, 0x57}, {0x1d639, 0x58}, {0x1d63a, 0x59}, {0x1d63b, 0x5a},
-    {0x0005b, 0x5b}, {0x0005c, 0x5c}, {0x0005d, 0x5d}, {0x0005e, 0x5e}, {0x0005f, 0x5f},
-    {0x00060, 0x60}, {0x00061, 0x61}, {0x00062, 0x62}, {0x00063, 0x63}, {0x00064, 0x64},
-    {0x00065, 0x65}, {0x00066, 0x66}, {0x00067, 0x67}, {0x00068, 0x68}, {0x00069, 0x69},
-    {0x0006a, 0x6a}, {0x0006b, 0x6b}, {0x0006c, 0x6c}, {0x0006d, 0x6d}, {0x0006e, 0x6e},
-    {0x0006f, 0x6f}, {0x00070, 0x70}, {0x00071, 0x71}, {0x00072, 0x72}, {0x00073, 0x73},
-    {0x00074, 0x74}, {0x00075, 0x75}, {0x00076, 0x76}, {0x00077, 0x77}, {0x00078, 0x78},
-    {0x00079, 0x79}, {0x0007a, 0x7a}, {0x0007b, 0x7b}, {0x0007c, 0x7c}, {0x0007d, 0x7d},
-    {0x0007e, 0x7e}, {0x025cb, 0x7f}, {0x02588, 0x80}, {0x02592, 0x81}, {0x1f431, 0x82},
-    {0x02b07, 0x83}, {0x02591, 0x84}, {0x0273d, 0x85}, {0x025cf, 0x86}, {0x02665, 0x87},
-    {0x02609, 0x88}, {0x0c6c3, 0x89}, {0x02302, 0x8a}, {0x02b05, 0x8b}, {0x1f610, 0x8c},
-    {0x0266a, 0x8d}, {0x1f17e, 0x8e}, {0x025c6, 0x8f}, {0x02026, 0x90}, {0x027a1, 0x91},
-    {0x02605, 0x92}, {0x029d7, 0x93}, {0x02b06, 0x94}, {0x002c7, 0x95}, {0x02227, 0x96},
-    {0x0274e, 0x97}, {0x025a4, 0x98}, {0x025a5, 0x99}, {0x03042, 0x9a}, {0x03044, 0x9b},
-    {0x03046, 0x9c}, {0x03048, 0x9d}, {0x0304a, 0x9e}, {0x0304b, 0x9f}, {0x0304d, 0xa0},
-    {0x0304f, 0xa1}, {0x03051, 0xa2}, {0x03053, 0xa3}, {0x03055, 0xa4}, {0x03057, 0xa5},
-    {0x03059, 0xa6}, {0x0305b, 0xa7}, {0x0305d, 0xa8}, {0x0305f, 0xa9}, {0x03061, 0xaa},
-    {0x03064, 0xab}, {0x03066, 0xac}, {0x03068, 0xad}, {0x0306a, 0xae}, {0x0306b, 0xaf},
-    {0x0306c, 0xb0}, {0x0306d, 0xb1}, {0x0306e, 0xb2}, {0x0306f, 0xb3}, {0x03072, 0xb4},
-    {0x03075, 0xb5}, {0x03078, 0xb6}, {0x0307b, 0xb7}, {0x0307e, 0xb8}, {0x0307f, 0xb9},
-    {0x03080, 0xba}, {0x03081, 0xbb}, {0x03082, 0xbc}, {0x03084, 0xbd}, {0x03086, 0xbe},
-    {0x03088, 0xbf}, {0x03089, 0xc0}, {0x0308a, 0xc1}, {0x0308b, 0xc2}, {0x0308c, 0xc3},
-    {0x0308d, 0xc4}, {0x0308f, 0xc5}, {0x03092, 0xc6}, {0x03093, 0xc7}, {0x03063, 0xc8},
-    {0x03083, 0xc9}, {0x03085, 0xca}, {0x03087, 0xcb}, {0x030a2, 0xcc}, {0x030a4, 0xcd},
-    {0x030a6, 0xce}, {0x030a8, 0xcf}, {0x030aa, 0xd0}, {0x030ab, 0xd1}, {0x030ad, 0xd2},
-    {0x030af, 0xd3}, {0x030b1, 0xd4}, {0x030b3, 0xd5}, {0x030b5, 0xd6}, {0x030b7, 0xd7},
-    {0x030b9, 0xd8}, {0x030bb, 0xd9}, {0x030bd, 0xda}, {0x030bf, 0xdb}, {0x030c1, 0xdc},
-    {0x030c4, 0xdd}, {0x030c6, 0xde}, {0x030c8, 0xdf}, {0x030ca, 0xe0}, {0x030cb, 0xe1},
-    {0x030cc, 0xe2}, {0x030cd, 0xe3}, {0x030ce, 0xe4}, {0x030cf, 0xe5}, {0x030d2, 0xe6},
-    {0x030d5, 0xe7}, {0x030d8, 0xe8}, {0x030db, 0xe9}, {0x030de, 0xea}, {0x030df, 0xeb},
-    {0x030e0, 0xec}, {0x030e1, 0xed}, {0x030e2, 0xee}, {0x030e4, 0xef}, {0x030e6, 0xf0},
-    {0x030e8, 0xf1}, {0x030e9, 0xf2}, {0x030ea, 0xf3}, {0x030eb, 0xf4}, {0x030ec, 0xf5},
-    {0x030ed, 0xf6}, {0x030ef, 0xf7}, {0x030f2, 0xf8}, {0x030f3, 0xf9}, {0x030c3, 0xfa},
-    {0x030e3, 0xfb}, {0x030e5, 0xfc}, {0x030e7, 0xfd}, {0x025dc, 0xfe}, {0x025dd, 0xff},
-};
-
-std::string convert_emojis(const std::string& lua) {
-	std::string res;
-	for (char32_t codepoint : utf8::CodepointIterator(lua)) {
-		if (codepoint < 0x80) {
-			res.push_back(uint8_t(codepoint));
-		} else {
-			auto i = emoji.find(codepoint);
-			if (i != emoji.end()) {
-				res.push_back(i->second);
-			}
-		}
-	}
-	return res;
-}
\ No newline at end of file
+
+
+std::string_view charset::to_utf8[256];
+std::u32string_view charset::to_utf32[256];
+
+static uint8_t multibyte_start[256];
+static std::map<std::string, uint8_t> to_pico8;
+std::regex charset::utf8_regex = charset::static_init();
+
+std::regex charset::static_init()
+{
+    std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cvt;
+
+    // The complete PICO-8 charmap, from 0 to 255. We cannot just store
+    // codepoints because some emoji glyphs are combinations of several
+    // codepoints, e.g. ⬇️ is U+2B07 (down arrow) + U+FE0F (variation
+    // selector-16).
+    static char const utf8_chars[] =
+        "\0¹²³⁴⁵⁶⁷⁸\t\nᵇᶜ\rᵉᶠ▮■□⁙⁘‖◀▶「」¥•、。゛゜"
+        " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO"
+        "PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~○"
+        "█▒🐱⬇️░✽●♥☉웃⌂⬅️😐♪🅾️◆…➡️★⧗⬆️ˇ∧❎▤▥あいうえおか"
+        "きくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよ"
+        "らりるれろわをんっゃゅょアイウエオカキクケコサシスセソタチツテト"
+        "ナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンッャュョ◜◝";
+    static auto utf32_chars = cvt.from_bytes(utf8_chars, &utf8_chars[sizeof(utf8_chars)]);
+
+    // Create all sorts of lookup tables for PICO-8 character conversions
+    char const *p8 = utf8_chars;
+    auto const *p32 = (char32_t const *)utf32_chars.data();
+    std::string regex("(");
+    for (int i = 0; i < 256; ++i)
+    {
+        size_t len32 = p32[1] == 0xfe0f ? 2 : 1;
+        size_t len8 = ((0xe5000000 >> ((*p8 >> 3) & 0x1e)) & 3) + len32 * len32;
+        to_utf8[i] = std::string_view(p8, len8);
+        to_utf32[i] = std::u32string_view(p32, len32);
+        to_pico8[std::string(p8, len8)] = i;
+
+        // Build a regex that lets us do faster (maybe?) UTF-8 conversions
+        if (len8 > 1)
+        {
+            multibyte_start[(uint8_t)*p8] = 1;
+            regex += std::string(p8, len8) + '|';
+        }
+
+        p8 += len8;
+        p32 += len32;
+    }
+    regex += ')'; // Fall back to an empty match on purpose
+
+    return std::regex(regex);
+}
+
+std::string charset::utf8_to_pico8(std::string const &str)
+{
+    std::string ret;
+    std::smatch sm;
+
+    for (auto p = str.begin(); p != str.end(); )
+    {
+        // Only pass known start characters through the expensive regex
+        if (multibyte_start[(uint8_t)*p]
+             && std::regex_search(p, str.end(), sm, utf8_regex)
+             && sm.length() > 1)
+        {
+            ret += to_pico8[sm.str()];
+            p += sm.length();
+        }
+        else
+        {
+            ret += *p++;
+        }
+    }
+
+    return ret;
+}
+
+std::string charset::pico8_to_utf8(std::string const &str)
+{
+    std::string ret;
+    for (uint8_t ch : str)
+        ret += std::string(to_utf8[ch]);
+    return ret;
+}
+
diff --git a/source/emojiconversion.h b/source/emojiconversion.h
index 1b65b06..a701b2b 100644
--- a/source/emojiconversion.h
+++ b/source/emojiconversion.h
@@ -1,3 +1,19 @@
 #include <string>
+#include <regex>

-std::string convert_emojis(const std::string& lua);
\ No newline at end of file
+struct charset
+{
+    // Convert between UTF-8 strings and 8-bit PICO-8 strings
+    static std::string utf8_to_pico8(std::string const &str);
+    static std::string pico8_to_utf8(std::string const &str);
+
+    // Map 8-bit PICO-8 characters to UTF-32 codepoints
+    static std::u32string_view to_utf32[256];
+
+    // Map 8-bit PICO-8 characters to UTF-8 string views
+    static std::string_view to_utf8[256];
+
+private:
+    static std::regex static_init();
+    static std::regex utf8_regex;
+};
\ No newline at end of file
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 9540b9d..d76df69 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -534,7 +534,7 @@ int mset(lua_State *L) {
     return 0;
 }

-int map(lua_State *L) {
+int gfx_map(lua_State *L) {
     int celx = 0, cely = 0, sx = 0, sy = 0, celw = 128, celh = 32, argc;
     argc = lua_gettop(L);
     if (argc > 0) {
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 713b97f..24cf749 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -39,7 +39,7 @@ int camera(lua_State *L);
 int clip(lua_State *L);
 int mget(lua_State *L);
 int mset(lua_State *L);
-int map(lua_State *L);
+int gfx_map(lua_State *L);
 int pal(lua_State *L);
 int palt(lua_State *L);
 int cursor(lua_State *L);
diff --git a/source/vm.cpp b/source/vm.cpp
index ecd80ca..5581276 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -169,7 +169,8 @@ bool Vm::loadCart(Cart* cart) {
     lua_register(_luaState, "load", load);

     //load in global lua fuctions for pico 8
-    auto convertedGlobalLuaFunctions = convert_emojis(p8GlobalLuaFunctions);
+    //auto convertedGlobalLuaFunctions = convert_emojis(p8GlobalLuaFunctions);
+    auto convertedGlobalLuaFunctions = charset::utf8_to_pico8(p8GlobalLuaFunctions);
     int loadedGlobals = luaL_dostring(_luaState, convertedGlobalLuaFunctions.c_str());

     if (loadedGlobals != LUA_OK) {
@@ -210,8 +211,8 @@ bool Vm::loadCart(Cart* cart) {

     lua_register(_luaState, "mget", mget);
     lua_register(_luaState, "mset", mset);
-    lua_register(_luaState, "map", map);
-    lua_register(_luaState, "mapdraw", map);
+    lua_register(_luaState, "map", gfx_map);
+    lua_register(_luaState, "mapdraw", gfx_map);

     //stubbed in graphics:
     lua_register(_luaState, "fillp", fillp);
diff --git a/test/Makefile b/test/Makefile
index 3b3a5ee..15e9046 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,7 +1,7 @@
 #match flags from 3ds and switch makefiles. This may not all be necessary
 # removed -O2 to make debugging tests easier
 CFLAGS := -Wall -g -c \
-		  -fno-rtti -fexceptions -std=gnu++11
+		  -fno-rtti -fexceptions -std=gnu++17


 all: runtests