From 79a14db7a2a50b8aac25f29087e8b1f2faf1d452 Mon Sep 17 00:00:00 2001 From: amol Date: Mon, 11 Jan 2021 06:27:02 -0800 Subject: [PATCH] replace ugly utf8 cache with wrapper around c++ unordered_maps. --- win32/Makefile.win32 | 12 +++++++----- win32/nt.screen.c | 37 +++++-------------------------------- win32/utf8_chars.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 win32/utf8_chars.cpp diff --git a/win32/Makefile.win32 b/win32/Makefile.win32 index bcf511aa..2ec46c36 100644 --- a/win32/Makefile.win32 +++ b/win32/Makefile.win32 @@ -67,13 +67,13 @@ BLDTYPE=retail NTLDFLAGS= -entry:silly_entry -debug -NTCFLAGS = -Zi -DNTDBG -Od +NTCFLAGS = -Zi -DNTDBG -D_DEBUG -Od LIBES= user32.lib advapi32.lib kernel32.lib $(DEBUG_CRTLIBS) strsafe.lib !else if "$(BLDTYPE)" == "retail" NTLDFLAGS= -entry:silly_entry -debug -NTCFLAGS = -Zi -O2 +NTCFLAGS = -Zi -O2 -DNDEBUG LIBES= user32.lib advapi32.lib kernel32.lib $(CRTLIBS) strsafe.lib !else @@ -148,14 +148,14 @@ NTSRCS1 = win32\io.c win32\stdio.c win32\dirent.c win32\signal.c\ win32\nt.char.c win32\bogus.c win32\console.c \ win32\ntfunc.c win32\ntb1.c win32\ntb2.c win32\globals.c \ win32\ps.c win32\nt.const.c win32\clip.c\ - win32\nt.bind.c win32\nt.screen.c + win32\nt.bind.c win32\nt.screen.c win32\utf8_chars.cpp NTSRCS = $(NTSRCS1) win32\fork.c NTOBJS = io.$(SUF) stdio.$(SUF) dirent.$(SUF) signal.$(SUF) support.$(SUF) \ nt.char.$(SUF) bogus.$(SUF) console.$(SUF) fork.$(SUF) ntfunc.$(SUF) \ globals.$(SUF) ps.$(SUF) \ - clip.$(SUF) nt.const.$(SUF) nt.bind.$(SUF) nt.screen.$(SUF) + clip.$(SUF) nt.const.$(SUF) nt.bind.$(SUF) nt.screen.$(SUF) utf8_chars.$(SUF) VHSRCS=$(PVSRCS) $(AVSRCS) @@ -200,7 +200,6 @@ chlog: .c.$(SUF): $(CC) $(CF) $(CFLAGS) $(REST_WARNING_LEVEL) $(DFLAGS) $(EXTRAFLAGS) $< - ed.defns.h: ed.defns.c -@del $@ @echo /* Do not edit this file, make creates it. */ > $@ @@ -321,6 +320,9 @@ CFLAGS_WX = $(CFLAGS) $(WINDOWS_WARNING_LEVEL) -DWINDOWS_ONLY $(ANALYZE_FLAGS) {win32}.c.$(SUF): $(CC) $(CF) $(CFLAGS_WX) $(DFLAGS) $(EXTRAFLAGS) $< +{win32}.cpp.$(SUF): + $(CC) $(CF) $(CFLAGS_WX) $(DFLAGS) $(EXTRAFLAGS) $< + fork.$(SUF):win32\fork.c $(CC) $(CF) $(CFLAGS_WX) $(DFLAGS) $(EXTRAFLAGS) -GS- win32\fork.c diff --git a/win32/nt.screen.c b/win32/nt.screen.c index 3eedc488..1d89f243 100644 --- a/win32/nt.screen.c +++ b/win32/nt.screen.c @@ -69,8 +69,8 @@ terminit(void) int T_ActualWindowSize; static void ReBufferDisplay (void); -static uint32_t* utf8_chars = NULL; -static Char currentIndex = 0; +Char get_or_cache_utf8_mb(uint32_t inChar); +uint32_t get_cached_utf8_mb(Char); /*ARGSUSED*/ @@ -120,11 +120,6 @@ ReBufferDisplay(void) b[TermV] = NULL; Vdisplay = b; -#ifdef WINNT_NATIVE_UTF8_SUPPORT - if(utf8_chars != NULL) { - memset(utf8_chars,0,NT_UTF8_MB); - } -#endif// WINNT_NATIVE_UTF8_SUPPORT } void @@ -324,40 +319,18 @@ MoveToChar(int where) #ifdef WINNT_NATIVE_UTF8_SUPPORT Char nt_make_utf8_multibyte(Char* cp, int len) { - Char retIdex = 0; uint32_t mbchar = 0; if(len == 1){ return *cp; } - if(utf8_chars == NULL){ - utf8_chars = xmalloc(sizeof(uint32_t)*NT_UTF8_MB); - } for(Char i = 0; i < len;i++) { mbchar <<= 8; mbchar |= *cp; cp++; } - for(Char i=0; i < NT_UTF8_MB;i++) { - if(utf8_chars[i] == mbchar) { - return i | NT_UTF8_MB; - } - if(utf8_chars[i] ==0) { - break; - } - } - retIdex = currentIndex++; - - if(currentIndex == LITERAL){ - currentIndex++; - } - //TODO deal with wraparound better - if(currentIndex == NT_UTF8_MB){ - currentIndex = 0; - } - - utf8_chars[retIdex] = mbchar; - return (retIdex | NT_UTF8_MB); + Char i = get_or_cache_utf8_mb(mbchar); + return i | NT_UTF8_MB; } #endif // WINNT_NATIVE_UTF8_SUPPORT void putraw_utf8(Char c) { @@ -365,7 +338,7 @@ void putraw_utf8(Char c) { if (c & NT_UTF8_MB) { Char index = c & ~NT_UTF8_MB; if (index >= 0 && index < NT_UTF8_MB) { - uint32_t mbchar = utf8_chars[index]; + uint32_t mbchar = get_cached_utf8_mb(index); int start = 0; // // there have to be at least 2 bytes in the utf8 sequence diff --git a/win32/utf8_chars.cpp b/win32/utf8_chars.cpp new file mode 100644 index 00000000..6c5c69d4 --- /dev/null +++ b/win32/utf8_chars.cpp @@ -0,0 +1,34 @@ +#include +#include + +static std::unordered_map utf8_chars; +static std::unordered_map reverse_map; + +static Char currentIndex; +extern "C" Char get_or_cache_utf8_mb(uint32_t inChar) { + auto iter = utf8_chars.find(inChar); + if(iter == utf8_chars.end()) { + auto ret = utf8_chars.insert({inChar,currentIndex}); + currentIndex++; + if(currentIndex == LITERAL ) { + currentIndex++; + } + if (ret.second) { + reverse_map.insert({ret.first->second,inChar}); + return ret.first->second; + } + return 0; + } + else { + return iter->second; + } +} +extern "C" uint32_t get_cached_utf8_mb(Char index) { + + auto iter = reverse_map.find(index); + if(iter == reverse_map.end()) { + return 0x00e29aa0; //"\U+26A0", Warning sign + } + return iter->second; +} +