(not much optimal and maybe improved, but it's not for performance)
; Also, THIS IS TRICKY STUFF AND REALLY "NOT A GOOD PRACTICE" so no guarantee and use at own risks (and fix if needed)
Code: Select all
; ASCII <> Unicode converter, without using additional buffers
; The ASCII/UTF8 string is packed inside PB UTF16 string here
; (alternative of using PB Ascii()/UTF8() functions)
; v 1.0.0.2
; 2016-2023 Luna Sole
; str$ : PB unicode string
; RETURN: UTF8 string packed into PB unicode string
Procedure$ ToUTF8(str$)
Protected res$ = Space(StringByteLength(str$, #PB_UTF8))
PokeS(@res$, str$, -1, #PB_UTF8)
ProcedureReturn res$
EndProcedure
; str$ : PB unicode string
; RETURN: ASCII string packed into PB unicode string
Procedure$ ToASCII(str$)
Protected nE = PokeS(@str$+1, str$, -1, #PB_Ascii)
MoveMemory(@str$+1, @str$, nE)
PokeU(@str$ + nE, 0)
ProcedureReturn str$
EndProcedure
; ;; +variant
; Procedure$ ToASCII(str$)
; PokeC(@str$+PokeS(@str$+1, str$, -1, #PB_Ascii)+1, 0)
; ProcedureReturn PeekS(@str$+1, -1, #PB_Unicode)
; EndProcedure
; back to unicode
Procedure$ FromUTF8(str$)
ProcedureReturn PeekS(@str$, -1, #PB_UTF8)
EndProcedure
; back to unicode
Procedure$ FromASCII(str$)
ProcedureReturn PeekS(@str$, -1, #PB_Ascii)
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Define T$ = "123а'ї"
Define T2$ = ToASCII(T$)
Debug T$
Debug T2$
Debug FromASCII(T2$)
CompilerEndIf