' UTF-16 encoding with surrogate pair handling for codepoints above U+FFFF
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL cp  AS LONG
    LOCAL hiSurrogate  AS LONG
    LOCAL loSurrogate  AS LONG
    LOCAL numCP  AS LONG
    LOCAL idx  AS LONG
    LOCAL pairCount  AS LONG
    LOCAL outWords(20) AS LONG
    LOCAL wordCount  AS LONG
    LOCAL decoded  AS LONG
    LOCAL testVal  AS LONG
    LOCAL codepoints(12) AS LONG

    PRINT "=== UTF-16 Encoding / Decoding ==="
    PRINT

    numCP = 10
    codepoints(0) = 65
    codepoints(1) = 8364
    codepoints(2) = 1025
    codepoints(3) = 1040
    codepoints(4) = 1488
    codepoints(5) = 66666
    codepoints(6) = 119070
    codepoints(7) = 128169
    codepoints(8) = 128578
    codepoints(9) = 129302

    PRINT "--- Encoding codepoints to UTF-16 ---"
    PRINT

    FOR i = 0 TO numCP - 1
        cp = codepoints(i)
        wordCount = 0

        IF cp < 0 OR cp > 1114111 THEN
            PRINT "U+" & FORMAT$(cp) & " -> [INVALID]"
        ELSE
            IF cp < 65536 THEN
                outWords(0) = cp
                wordCount = 1
            ELSE
                cp = cp - 65536
                hiSurrogate = 55296 + (cp / 1024)
                loSurrogate = 56320 + (cp AND 1023)
                outWords(0) = hiSurrogate
                outWords(1) = loSurrogate
                wordCount = 2
            END IF

            PRINT "U+" & FORMAT$(codepoints(i)) & " ->"
            idx = 0
            WHILE idx < wordCount
                PRINT " " & FORMAT$(outWords(idx))
                idx = idx + 1
            WEND
            PRINT " (" & STR$(wordCount) & " words, " & STR$(wordCount * 2) & " bytes)"
        END IF
    NEXT i

    PRINT
    PRINT "--- Decoding UTF-16 back to codepoints ---"
    PRINT

    pairCount = 0
    FOR i = 0 TO numCP - 1
        cp = codepoints(i)
        IF cp >= 65536 THEN
            pairCount = pairCount + 1
        END IF
    NEXT i
    PRINT "Surrogate pairs in test set:" & STR$(pairCount)
    PRINT

    FOR i = 0 TO numCP - 1
        cp = codepoints(i)
        wordCount = 0
        IF cp < 65536 THEN
            outWords(0) = cp
            wordCount = 1
        ELSE
            cp = cp - 65536
            hiSurrogate = 55296 + (cp / 1024)
            loSurrogate = 56320 + (cp AND 1023)
            outWords(0) = hiSurrogate
            outWords(1) = loSurrogate
            wordCount = 2
        END IF

        IF wordCount = 1 THEN
            decoded = outWords(0)
            IF decoded >= 55296 AND decoded <= 57343 THEN
                PRINT "U+" & FORMAT$(decoded) & " [LONE SURROGATE - invalid]"
            ELSE
                PRINT "U+" & FORMAT$(decoded)
            END IF
        ELSE
            hiSurrogate = outWords(0)
            loSurrogate = outWords(1)
            IF hiSurrogate >= 55296 AND hiSurrogate <= 56319 AND loSurrogate >= 56320 AND loSurrogate <= 57343 THEN
                decoded = (hiSurrogate - 55296) * 1024 + (loSurrogate - 56320) + 65536
                PRINT "U+" & FORMAT$(decoded) & " (decoded from surrogate pair " & FORMAT$(hiSurrogate) & " + " & FORMAT$(loSurrogate) & ")"
            ELSE
                PRINT "[INVALID SURROGATE PAIR: " & FORMAT$(hiSurrogate) & ", " & FORMAT$(loSurrogate) & "]"
            END IF
        END IF
    NEXT i

    PRINT
    PRINT "--- Surrogate range validation ---"
    PRINT "High surrogates: U+D800 (" & STR$(55296) & ") to U+DBFF (" & STR$(56319) & ")"
    PRINT "Low surrogates:  U+DC00 (" & STR$(56320) & ") to U+DFFF (" & STR$(57343) & ")"
    PRINT "Valid codepoints above U+FFFF: U+10000 to U+10FFFF"
    PRINT

    testVal = 55296
    IF testVal >= 55296 AND testVal <= 56319 THEN
        PRINT "U+D800 is a high surrogate"
    END IF
    testVal = 56320
    IF testVal >= 56320 AND testVal <= 57343 THEN
        PRINT "U+DC00 is a low surrogate"
    END IF
    testVal = 65535
    IF testVal > 57343 THEN
        PRINT "U+FFFF is a valid BMP codepoint"
    END IF

    PRINT
    PRINT "UTF-16 encode/decode complete."

    FUNCTION = 0
END FUNCTION
