Skip to content

Instantly share code, notes, and snippets.

@ISSOtm
Created March 7, 2021 20:00
Show Gist options
  • Save ISSOtm/06e27dbdfe172bfc5fef0ceec1d46f26 to your computer and use it in GitHub Desktop.
Save ISSOtm/06e27dbdfe172bfc5fef0ceec1d46f26 to your computer and use it in GitHub Desktop.
Macro for shimming 2-byte charmaps
NEWCHARMAP "Low byte"
charmap "A", $12
NEWCHARMAP "High byte"
charmap "A", $34
; ...
text: MACRO
PUSHC ; Preserve the outer charmap
NTH = 0
REPT _NARG
NTH = NTH + 1
I = 1
LEN = STRLEN(\1)
; We actually don't know how many bytes the string will output, but we know an upper bound
REPT LEN
IF I != LEN ; This will skip all remaining iterations once all bytes have been output
setcharmap "Low byte"
; Determine length of first charmap mapping
; (heuristically, but what can I do...)
CHAR_BYTES = 0
REPT 16 ; FIXME: assuming max mapping length is 16 chars
CHAR_BYTES = CHAR_BYTES + 1
IF !DEF(UNIT)
UNIT equs STRSUB(\1, I, CHAR_BYTES)
IF "{UNIT}" != LOW(\1)
PURGE UNIT
ENDC
ENDC
ENDR
; If failed to determine length, we have a problem
IF !DEF(UNIT)
FAIL "Failed to write \"\1\": couldn't determine {d:NTH}th mapping"
ENDC
; Write low byte
db "{UNIT}"
; Write high byte
setcharmap "High byte"
static_assert "{UNIT}" < 256, "\"{UNIT}\" doesn't have a high byte mapping?"
db "{UNIT}"
I = I + CHAR_BYTES
ENDC
ENDR
SHIFT
ENDR
POPC
ENDM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment