Skip to content

Commit

Permalink
replace ugly utf8 cache with wrapper around c++ unordered_maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
amoldeshpande committed Jan 11, 2021
1 parent 7a568a1 commit 79a14db
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
12 changes: 7 additions & 5 deletions win32/Makefile.win32
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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. */ > $@
Expand Down Expand Up @@ -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

Expand Down
37 changes: 5 additions & 32 deletions win32/nt.screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -324,48 +319,26 @@ 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) {
#if defined(WINNT_NATIVE_UTF8_SUPPORT)
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
Expand Down
34 changes: 34 additions & 0 deletions win32/utf8_chars.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <unordered_map>
#include <sh.h>

static std::unordered_map<uint32_t,Char> utf8_chars;
static std::unordered_map<Char,uint32_t> 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;
}

0 comments on commit 79a14db

Please sign in to comment.