• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua][vJass] Ascii

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
Ascii - your resource for getting the ordinal value of a char or the char value of an integer.


The only useful function provided here is A2S. The others are just wrappers for backwards compatibility.
Lua:
Char2Ascii = string.byte
Ascii2Char = string.char
---@param value integer
function A2S(value)
    local result = ""
    for byteno=1,4 do
        result = string.char(value % 256) .. result
        value = value // 256
    end
    return result
end
S2A = FourCC




JASS:
library Ascii /* v1.1.0.0         TheDamian/Nestharus
************************************************************************************
*
*   function Char2Ascii takes string s returns integer
*       integer ascii = Char2Ascii("F")
*
*   function Ascii2Char takes integer a returns string
*       string char = Ascii2Char('F')
*
*   function A2S takes integer a returns string
*       string rawcode = A2S('CODE')
*
*   function S2A takes string s returns integer
*       integer rawcode = S2A("CODE")
*
************************************************************************************/
    globals
        private integer array i //hash
        private integer array h //hash2
        private integer array y //hash3
        private string array c  //char
    endglobals
    function Char2Ascii takes string p returns integer
        local integer z = i[StringHash(p)/0x1F0748+0x40D]
        if (c[z] != p) then
            if (c[z - 32] != p) then
                if (c[h[z]] != p) then
                    if (c[y[z]] != p) then
                        if (c[83] != p) then
                            debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII ERROR: INVALID CHARACTER: " + p)
                            return 0
                        endif
                        return 83
                    endif
                    return y[z]
                endif
                return h[z]
            endif
            return z - 32
        endif
        return z
    endfunction
    function Ascii2Char takes integer a returns string
        return c[a]
    endfunction
    function A2S takes integer a returns string
        local string s=""
        loop
            set s=c[a-a/256*256]+s
            set a=a/256
            exitwhen 0==a
        endloop
        return s
    endfunction
    function S2A takes string s returns integer
        local integer a=0
        local integer l=StringLength(s)
        local integer j=0
        local string m
        local integer h
        loop
            exitwhen j==l
            set a = a*256 + Char2Ascii(SubString(s,j,j+1))
            set j=j+1
        endloop
        return a
    endfunction
    private module Init
        private static method onInit takes nothing returns nothing
            set i[966] = 8
            set i[1110] = 9
            set i[1621] = 10
            set i[1375] = 12
            set i[447] = 13
            set i[233] = 32
            set i[2014] = 33
            set i[1348] = 34
            set i[1038] = 35
            set i[1299] = 36
            set i[1018] = 37
            set i[1312] = 38
            set i[341] = 39
            set i[939] = 40
            set i[969] = 41
            set i[952] = 42
            set i[2007] = 43
            set i[1415] = 44
            set i[2020] = 45
            set i[904] = 46
            set i[1941] = 47
            set i[918] = 48
            set i[1593] = 49
            set i[719] = 50
            set i[617] = 51
            set i[703] = 52
            set i[573] = 53
            set i[707] = 54
            set i[1208] = 55
            set i[106] = 56
            set i[312] = 57
            set i[124] = 58
            set i[1176] = 59
            set i[74] = 60
            set i[1206] = 61
            set i[86] = 62
            set i[340] = 63
            set i[35] = 64
            set i[257] = 65
            set i[213] = 66
            set i[271] = 67
            set i[219] = 68
            set i[1330] = 69
            set i[1425] = 70
            set i[1311] = 71
            set i[238] = 72
            set i[1349] = 73
            set i[244] = 74
            set i[1350] = 75
            set i[205] = 76
            set i[1392] = 77
            set i[1378] = 78
            set i[1432] = 79
            set i[1455] = 80
            set i[1454] = 81
            set i[1431] = 82
            set i[1409] = 83
            set i[1442] = 84
            set i[534] = 85
            set i[1500] = 86
            set i[771] = 87
            set i[324] = 88
            set i[1021] = 89
            set i[73] = 90
            set i[1265] = 91
            set i[1941] = 92
            set i[1671] = 93
            set i[1451] = 94
            set i[1952] = 95
            set i[252] = 96
            set i[257] = 97
            set i[213] = 98
            set i[271] = 99
            set i[219] = 100
            set i[1330] = 101
            set i[1425] = 102
            set i[1311] = 103
            set i[238] = 104
            set i[1349] = 105
            set i[244] = 106
            set i[1350] = 107
            set i[205] = 108
            set i[1392] = 109
            set i[1378] = 110
            set i[1432] = 111
            set i[1455] = 112
            set i[1454] = 113
            set i[1431] = 114
            set i[1409] = 115
            set i[1442] = 116
            set i[534] = 117
            set i[1500] = 118
            set i[771] = 119
            set i[324] = 120
            set i[1021] = 121
            set i[73] = 122
            set i[868] = 123
            set i[1254] = 124
            set i[588] = 125
            set i[93] = 126
            set i[316] = 161
            set i[779] = 162
            set i[725] = 163
            set i[287] = 164
            set i[212] = 165
            set i[7] = 166
            set i[29] = 167
            set i[1958] = 168
            set i[1009] = 169
            set i[1580] = 170
            set i[1778] = 171
            set i[103] = 172
            set i[400] = 174
            set i[1904] = 175
            set i[135] = 176
            set i[1283] = 177
            set i[469] = 178
            set i[363] = 179
            set i[550] = 180
            set i[1831] = 181
            set i[1308] = 182
            set i[1234] = 183
            set i[1017] = 184
            set i[1093] = 185
            set i[1577] = 186
            set i[606] = 187
            set i[1585] = 188
            set i[1318] = 189
            set i[980] = 190
            set i[1699] = 191
            set i[1292] = 192
            set i[477] = 193
            set i[709] = 194
            set i[1600] = 195
            set i[2092] = 196
            set i[50] = 197
            set i[546] = 198
            set i[408] = 199
            set i[853] = 200
            set i[205] = 201
            set i[411] = 202
            set i[1311] = 203
            set i[1422] = 204
            set i[1808] = 205
            set i[457] = 206
            set i[1280] = 207
            set i[614] = 208
            set i[1037] = 209
            set i[237] = 210
            set i[1409] = 211
            set i[1023] = 212
            set i[1361] = 213
            set i[695] = 214
            set i[161] = 215
            set i[1645] = 216
            set i[1822] = 217
            set i[644] = 218
            set i[1395] = 219
            set i[677] = 220
            set i[1677] = 221
            set i[881] = 222
            set i[861] = 223
            set i[1408] = 224
            set i[1864] = 225
            set i[1467] = 226
            set i[1819] = 227
            set i[1971] = 228
            set i[949] = 229
            set i[774] = 230
            set i[1828] = 231
            set i[865] = 232
            set i[699] = 233
            set i[786] = 234
            set i[1806] = 235
            set i[1286] = 236
            set i[1128] = 237
            set i[1490] = 238
            set i[1720] = 239
            set i[1817] = 240
            set i[729] = 241
            set i[1191] = 242
            set i[1164] = 243
            set i[413] = 244
            set i[349] = 245
            set i[1409] = 246
            set i[660] = 247
            set i[2016] = 248
            set i[1087] = 249
            set i[1497] = 250
            set i[753] = 251
            set i[1579] = 252
            set i[1456] = 253
            set i[606] = 254
            set i[1625] = 255
            set h[92] = 47
            set h[201] = 108
            set h[201] = 76
            set h[203] = 103
            set h[203] = 71
            set h[246] = 115
            set h[246] = 83
            set h[246] = 211
            set h[254] = 187
            set y[201] = 108
            set y[203] = 103
            set y[246] = 115

            set c[8]="\b"
            set c[9]="\t"
            set c[10]="\n"
            set c[12]="\f"
            set c[13]="\r"
            set c[32]=" "
            set c[33]="!"
            set c[34]="\""
            set c[35]="#"
            set c[36]="$"
            set c[37]="%"
            set c[38]="&"
            set c[39]="'"
            set c[40]="("
            set c[41]=")"
            set c[42]="*"
            set c[43]="+"
            set c[44]=","
            set c[45]="-"
            set c[46]="."
            set c[47]="/"
            set c[48]="0"
            set c[49]="1"
            set c[50]="2"
            set c[51]="3"
            set c[52]="4"
            set c[53]="5"
            set c[54]="6"
            set c[55]="7"
            set c[56]="8"
            set c[57]="9"
            set c[58]=":"
            set c[59]=";"
            set c[60]="<"
            set c[61]="="
            set c[62]=">"
            set c[63]="?"
            set c[64]="@"
            set c[65]="A"
            set c[66]="B"
            set c[67]="C"
            set c[68]="D"
            set c[69]="E"
            set c[70]="F"
            set c[71]="G"
            set c[72]="H"
            set c[73]="I"
            set c[74]="J"
            set c[75]="K"
            set c[76]="L"
            set c[77]="M"
            set c[78]="N"
            set c[79]="O"
            set c[80]="P"
            set c[81]="Q"
            set c[82]="R"
            set c[83]="S"
            set c[84]="T"
            set c[85]="U"
            set c[86]="V"
            set c[87]="W"
            set c[88]="X"
            set c[89]="Y"
            set c[90]="Z"
            set c[91]="["
            set c[92]="\\"
            set c[93]="]"
            set c[94]="^"
            set c[95]="_"
            set c[96]="`"
            set c[97]="a"
            set c[98]="b"
            set c[99]="c"
            set c[100]="d"
            set c[101]="e"
            set c[102]="f"
            set c[103]="g"
            set c[104]="h"
            set c[105]="i"
            set c[106]="j"
            set c[107]="k"
            set c[108]="l"
            set c[109]="m"
            set c[110]="n"
            set c[111]="o"
            set c[112]="p"
            set c[113]="q"
            set c[114]="r"
            set c[115]="s"
            set c[116]="t"
            set c[117]="u"
            set c[118]="v"
            set c[119]="w"
            set c[120]="x"
            set c[121]="y"
            set c[122]="z"
            set c[123]="{"
            set c[124]="|"
            set c[125]="}"
            set c[126]="~"
            set c[128] = "€"
            set c[130] = "‚"
            set c[131] = "ƒ"
            set c[132] = "„"
            set c[133] = "…"
            set c[134] = "†"
            set c[135] = "‡"
            set c[136] = "ˆ"
            set c[137] = "‰"
            set c[138] = "Š"
            set c[139] = "‹"
            set c[140] = "Œ"
            set c[142] = "Ž"
            set c[145] = "‘"
            set c[146] = "’"
            set c[147] = "“"
            set c[148] = "”"
            set c[149] = "•"
            set c[150] = "–"
            set c[151] = "—"
            set c[152] = "˜"
            set c[153] = "™"
            set c[154] = "š"
            set c[155] = "›"
            set c[156] = "œ"
            set c[158] = "ž"
            set c[159] = "Ÿ"
            set c[160] = " "
            set c[161] = "¡"
            set c[162] = "¢"
            set c[163] = "£"
            set c[164] = "¤"
            set c[165] = "¥"
            set c[166] = "¦"
            set c[167] = "§"
            set c[168] = "¨"
            set c[169] = "©"
            set c[170] = "ª"
            set c[171] = "«"
            set c[172] = "¬"
            set c[174] = "®"
            set c[175] = "¯"
            set c[176] = "°"
            set c[177] = "±"
            set c[178] = "²"
            set c[179] = "³"
            set c[180] = "´"
            set c[181] = "µ"
            set c[182] = "¶"
            set c[183] = "·"
            set c[184] = "¸"
            set c[185] = "¹"
            set c[186] = "º"
            set c[187] = "»"
            set c[188] = "¼"
            set c[189] = "½"
            set c[190] = "¾"
            set c[191] = "¿"
            set c[192] = "À"
            set c[193] = "Á"
            set c[194] = "Â"
            set c[195] = "Ã"
            set c[196] = "Ä"
            set c[197] = "Å"
            set c[198] = "Æ"
            set c[199] = "Ç"
            set c[200] = "È"
            set c[201] = "É"
            set c[202] = "Ê"
            set c[203] = "Ë"
            set c[204] = "Ì"
            set c[205] = "Í"
            set c[206] = "Î"
            set c[207] = "Ï"
            set c[208] = "Ð"
            set c[209] = "Ñ"
            set c[210] = "Ò"
            set c[211] = "Ó"
            set c[212] = "Ô"
            set c[213] = "Õ"
            set c[214] = "Ö"
            set c[215] = "×"
            set c[216] = "Ø"
            set c[217] = "Ù"
            set c[218] = "Ú"
            set c[219] = "Û"
            set c[220] = "Ü"
            set c[221] = "Ý"
            set c[222] = "Þ"
            set c[223] = "ß"
            set c[224] = "à"
            set c[225] = "á"
            set c[226] = "â"
            set c[227] = "ã"
            set c[228] = "ä"
            set c[229] = "å"
            set c[230] = "æ"
            set c[231] = "ç"
            set c[232] = "è"
            set c[233] = "é"
            set c[234] = "ê"
            set c[235] = "ë"
            set c[236] = "ì"
            set c[237] = "í"
            set c[238] = "î"
            set c[239] = "ï"
            set c[240] = "ð"
            set c[241] = "ñ"
            set c[242] = "ò"
            set c[243] = "ó"
            set c[244] = "ô"
            set c[245] = "õ"
            set c[246] = "ö"
            set c[247] = "÷"
            set c[248] = "ø"
            set c[249] = "ù"
            set c[250] = "ú"
            set c[251] = "û"
            set c[252] = "ü"
            set c[253] = "ý"
            set c[254] = "þ"
            set c[255] = "ÿ"
        endmethod
    endmodule
    private struct Inits extends array
        implement Init
    endstruct
endlibrary
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
This is incompatible with String Parser

gg

It already has A2S and S2A in it, and the implementation of A2S in it is better, plus it's a pain to remove -.-.

JASS:
    function A2S takes integer val returns string
        local string ascii = ""
        loop
            exitwhen val == 0
            set ascii = ascii + Ascii2Char(val - val/128*128)
            set val = val / 128
        endloop
        
        return ascii
    endfunction
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Here's a working version with a few debug statements, 1 syntax fix, and a few bug fixes. You can't do public onInits Bribe or it'll screw up the order of initialization. Private runs before public. A public onInit is a struct initializer.

JASS:
library Ascii
///////////////////////////////////////////////////////////////////
//      function Char2Ascii takes string s returns integer
//          integer ascii = Char2Ascii("F")
//
//      function Ascii2Char takes integer a returns string
//          string char = Ascii2Char('F')
//
//      function A2S takes integer a returns string
//          string rawcode = A2S('CODE')
//
//      function S2A takes string s returns integer
//          integer rawcode = S2A("CODE")
//
///////////////////////////////////////////////////////////////////
    globals
        private integer array i // Integers
        private string array c // Characters
    endglobals

    function Char2Ascii takes string s returns integer
        local integer a = i[StringHash(s) / 0x1F0748 + 0x3EA]
        if a == 47 and s == "\\" then
            set a = 92 // "/" and "\\" have same hash
        elseif a >= 65 and a <= 90 and s != c[a] then
            set a = a+32 //shift to lowercase, upper/lower have same hash
        endif
        
        debug if (((a >= 8 and a <= 13) or (a >= 32 and a <= 126))) then
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID CHAR " + s)
        debug return 0
    endfunction

    function Ascii2Char takes integer a returns string
        debug if ((a >= 8 and a <= 13) or (a >= 32 and a <= 126)) then
            return c[a]
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID INT " + I2S(a))
        debug return null
    endfunction

    function A2S takes integer a returns string
        local string s = ""
        debug if (a >= 8) then
            loop
                set s = s + c[a - a / 128 * 128]
                set a = a / 128
                exitwhen (a == 0)
            endloop
            return s
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID INT " + I2S(a))
        debug return null
    endfunction

    function S2A takes string s returns integer
        local integer a = 0
        local integer l = StringLength(s)
        debug if (l > 0) then
            loop
                set l = l - 1
                set a = a * 128 + Char2Ascii(SubString(s, l, l + 1))
                exitwhen (l == 0)
            endloop
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID STR " + s)
        debug return 0
    endfunction

    private module Init
        private static method onInit takes nothing returns nothing
            set i[931] = 8
            set i[1075] = 9
            set i[1586] = 10
            set i[1340] = 12
            set i[412] = 13
            
            set i[198] = 32
            set i[1979] = 33
            set i[1313] = 34
            set i[1003] = 35
            set i[1264] = 36
            set i[983] = 37
            set i[1277] = 38
            set i[306] = 39
            set i[904] = 40
            set i[934] = 41
            set i[917] = 42
            set i[1972] = 43
            set i[1380] = 44
            set i[1985] = 45
            set i[869] = 46
            
            set i[1906] = 47
            
            set i[883] = 48
            set i[1558] = 49
            set i[684] = 50
            set i[582] = 51
            set i[668] = 52
            set i[538] = 53
            set i[672] = 54
            set i[1173] = 55
            set i[71] = 56
            set i[277] = 57
            
            set i[89] = 58
            set i[1141] = 59
            set i[39] = 60
            set i[1171] = 61
            set i[51] = 62
            set i[305] = 63
            set i[0] = 64
            
            set i[222] = 65
            set i[178] = 66
            set i[236] = 67
            set i[184] = 68
            set i[1295] = 69
            set i[1390] = 70
            set i[1276] = 71
            set i[203] = 72
            set i[1314] = 73
            set i[209] = 74
            set i[1315] = 75
            set i[170] = 76
            set i[1357] = 77
            set i[1343] = 78
            set i[1397] = 79
            set i[1420] = 80
            set i[1419] = 81
            set i[1396] = 82
            set i[1374] = 83
            set i[1407] = 84
            set i[499] = 85
            set i[1465] = 86
            set i[736] = 87
            set i[289] = 88
            set i[986] = 89
            set i[38] = 90
            
            set i[1230] = 91
            set i[1636] = 93
            set i[1416] = 94
            set i[1917] = 95
            set i[217] = 96
            set i[833] = 123
            set i[1219] = 124
            set i[553] = 125
            set i[58] = 126
            
            set c[8] = "\b"
            set c[9] = "\t"
            set c[10] = "\n"
            set c[12] = "\f"
            set c[13] = "\r"
            
            set c[32] = " "
            set c[33] = "!"
            set c[34] = "\""
            set c[35] = "#"
            set c[36] = "$"
            set c[37] = "%"
            set c[38] = "&"
            set c[39] = "'"
            set c[40] = "("
            set c[41] = ")"
            set c[42] = "*"
            set c[43] = "+"
            set c[44] = ","
            set c[45] = "-"
            set c[46] = "."
            
            set c[47] = "/"
            
            set c[48] = "0"
            set c[49] = "1"
            set c[50] = "2"
            set c[51] = "3"
            set c[52] = "4"
            set c[53] = "5"
            set c[54] = "6"
            set c[55] = "7"
            set c[56] = "8"
            set c[57] = "9"
            
            set c[58] = ":"
            set c[59] = ";"
            set c[60] = "<"
            set c[61] = "="
            set c[62] = ">"
            set c[63] = "?"
            set c[64] = "@"
            
            set c[65] = "A"
            set c[66] = "B"
            set c[67] = "C"
            set c[68] = "D"
            set c[69] = "E"
            set c[70] = "F"
            set c[71] = "G"
            set c[72] = "H"
            set c[73] = "I"
            set c[74] = "J"
            set c[75] = "K"
            set c[76] = "L"
            set c[77] = "M"
            set c[78] = "N"
            set c[79] = "O"
            set c[80] = "P"
            set c[81] = "Q"
            set c[82] = "R"
            set c[83] = "S"
            set c[84] = "T"
            set c[85] = "U"
            set c[86] = "V"
            set c[87] = "W"
            set c[88] = "X"
            set c[89] = "Y"
            set c[90] = "Z"
            
            set c[92] = "\\"
            
            set c[97] = "a"
            set c[98] = "b"
            set c[99] = "c"
            set c[100] = "d"
            set c[101] = "e"
            set c[102] = "f"
            set c[103] = "g"
            set c[104] = "h"
            set c[105] = "i"
            set c[106] = "j"
            set c[107] = "k"
            set c[108] = "l"
            set c[109] = "m"
            set c[110] = "n"
            set c[111] = "o"
            set c[112] = "p"
            set c[113] = "q"
            set c[114] = "r"
            set c[115] = "s"
            set c[116] = "t"
            set c[117] = "u"
            set c[118] = "v"
            set c[119] = "w"
            set c[120] = "x"
            set c[121] = "y"
            set c[122] = "z"
            
            set c[91] = "["
            set c[93] = "]"
            set c[94] = "^"
            set c[95] = "_"
            set c[96] = "`"
            set c[123] = "{"
            set c[124] = "|"
            set c[125] = "}"
            set c[126] = "~"
        endmethod
    endmodule

    private struct Inits extends array
        implement Init
    endstruct
endlibrary
 
Last edited:
Warcraft 3 Wiki said:
JASS is an API scripting syntax that can be used by those with the Warcraft III World Editor to manipulate almost every part of the game. The most commonly accepted phrase that the acronym stands for is Just Another Scripting Syntax. Almost all aspects of the game are written in JASS, including units, locations, rects, destructables, items, weather effects, special effects, and sounds. Users can also access JASS directly, by creating a trigger and using the 'Covert to Custom Text' feature in the editor. You can also place scripts in the map header.

GUI triggers are converted into JASS when the map containing them is saved in World Editor...

http://wiki.thehelper.net/wc3/jass
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
Meh, I'm glad it's there but it was really more work to write it than what I was prepared for. It's annoying that you can't do static if's outside of functions, which is why Table is currently a requirement instead of optional. The main reasons I chose Zinc were because

1. I wanted to practice my C-syntax
2. (and most importantly) I felt too lazy to write using ugly JASS loop structure.

This thing was very annoying to convert from Luck, the language I had it in first... you have to enclose (parenthesis) for all if-statements in Zinc, you have those artsy curly brackets that are just in the most difficult possible spot to reach on the keyboard, etc.


Hex is going to be a seperate resource, I originally combined them in the development process.
Code:
library Ascii:
/*
    Useful API that can be used to help parse strings dynamically (in-game).

    For completeness, provides the constant string ASCII.

    API
      ---------------------
     |  integer.hex(256) -> string
     |      returns '0x100'
     |
     |  integer.chr(48) -> string
     |      returns '0'
     |
     |  integer.rawcode(1093677104) -> string
     |      returns 'A000'
     |
     |  '100'.hex() -> integer
     |      returns 256
     |
     |  '0'.ord() -> integer
     |      returns 48
     |
     |  'A000'.rawcode() -> integer
     |      returns 1093677104
*/

    public static constant string ASCII = "        \b\t\n \f\r                   !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

    static:
        # For converting integer to string:
        string array CHR, HEX

        # For converting string to integer:
        Dict:
            ORD = #! Dict()
            VAL = #! Dict()


    struct integer:
        public:
            string hex(): # Developed by Azlier
                local string s = "", p = ""
                if this < 0:
                    p = '-'
                    this = -this
                while this != 0:
                    s = HEX[this.mod(16)] .. s
                    this /= 16
                return p .. s


            string chr(): # Developed by TheDamien
                debug:
                    if CHR[this] == ""
                        print("TypeError: Tried to get the char of invalid int: " .. this)
                        return ""
                return CHR[this]


            string rawcode(): # Developed by Blizzard with changes by the community
                local string s = ""
                if this < '!!!!' or this > '~~~~':
                    debug print("TypeError: Tried to get rawcode of invalid int: " .. this)
                    return ""
                while this != 0:
                    s = this.mod(256).chr .. s
                    this /= 256
                return s


    struct string:
        public:
            integer hex(): # Developed by Bribe
                local:
                    integer i, p = 0, end = this.len()
                    boolean neg = false
                    debug boolean safe = true

                if this[0] == "-":
                    neg = true
                    p = 1
                if this[p:p+2] == "0x":
                    p += 2

                foreach(p, p, end):
                    i = i * 16 + VAL[this]
                    debug:
                        if not VAL.contains(this) and safe:
                            safe = false
                            print("TypeError: Returning the hex value of non-hex string: " .. this)

                return integer.tertiaryOp(neg, -i, i)


            integer ord(): # Developed by TheDamien with changes by Bribe
                local integer i = ORD[this]
                if this == "\\":
                    return '\\' # Switch slashes
                elseif this != i.chr():
                    if i == 0:
                        debug print("TypeError: Tried to get the ord value of invalid char: " .. this)
                        return 0
                    return i + 32 # Switch to lowercase
                return i


            integer rawcode(): # Developed by Nestharus
                local integer pos, val = 0
                if this.len() != 4:
                    debug print("TypeError: Tried to get rawcode of invalid string: " .. this)
                    return 0
                foreach(pos, 3): val = val * 256 + this[pos].ord()
                return val


    oninit:
        local integer i

        foreach(i, 8, 13):
            if i != 11:
                CHR[i] = ASCII[i]
                ORD[CHR[i]] = i
            # ASCII formatting characters

        foreach(i, 32, 126):
            CHR[i] = ASCII[i]
            if i != 92 and i < 97 or i > 122: ORD[CHR[i]] = i
            # ASCII text characters

        foreach(i, 15):
            if i < 10: HEX[i] = i # 0-9
            else:      HEX[i] = CHR[i + 55] # A-F
            VAL[HEX[i]] = i
            # Hex characters
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
Here is an optimized Ascii. Extra space is removed and the comparisons are more optimal.

JASS:
library Ascii
///////////////////////////////////////////////////////////////////
//      function Char2Ascii takes string s returns integer
//          integer ascii = Char2Ascii("F")
//
//      function Ascii2Char takes integer a returns string
//          string char = Ascii2Char('F')
//
//      function A2S takes integer a returns string
//          string rawcode = A2S('CODE')
//
//      function S2A takes string s returns integer
//          integer rawcode = S2A("CODE")
//
///////////////////////////////////////////////////////////////////
    globals
        private integer array i // Integers
        private string array c // Characters
    endglobals
    function Char2Ascii takes string s returns integer
        local integer a
        if ("\\"==s) then
            return 92 // "/" and "\\" have same hash
        endif
        set a=i[StringHash(s)/0x1F0748+0x3EA]
        if (64<a and 91>a) then
            return a+32 //shift to lowercase, upper/lower have same hash
        endif
        debug if ((7<a and 14>a) or (31<a and 127>a)) then
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII CONVERSION ERROR: INVALID CHAR "+s)
        debug return 0
    endfunction
    function Ascii2Char takes integer a returns string
        debug if ((7<a and 14>a) or (31<a and 127>a)) then
            return c[a]
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII CONVERSION ERROR: INVALID INT "+I2S(a))
        debug return null
    endfunction
    function A2S takes integer a returns string
        local string s=""
        debug if (7<a) then
            loop
                set s=c[a-a/256*256]+s
                set a=a/256
                exitwhen 0==a
            endloop
            return s
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII CONVERSION ERROR: INVALID INT "+I2S(a))
        debug return null
    endfunction
    function S2A takes string s returns integer
        local integer a=0
        local integer l=StringLength(s)
        local integer i=0
        debug if (0<l) then
            loop
                set a=a*256+Char2Ascii(SubString(s,i,i+1))
                set i=i+1
                exitwhen (i==l)
            endloop
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII CONVERSION ERROR: INVALID STR "+s)
        debug return 0
    endfunction
    private module Init
        private static method onInit takes nothing returns nothing
            set i[931]=8
            set i[1075]=9
            set i[1586]=10
            set i[1340]=12
            set i[412]=13
            set i[198]=32
            set i[1979]=33
            set i[1313]=34
            set i[1003]=35
            set i[1264]=36
            set i[983]=37
            set i[1277]=38
            set i[306]=39
            set i[904]=40
            set i[934]=41
            set i[917]=42
            set i[1972]=43
            set i[1380]=44
            set i[1985]=45
            set i[869]=46
            set i[1906]=47
            set i[883]=48
            set i[1558]=49
            set i[684]=50
            set i[582]=51
            set i[668]=52
            set i[538]=53
            set i[672]=54
            set i[1173]=55
            set i[71]=56
            set i[277]=57
            set i[89]=58
            set i[1141]=59
            set i[39]=60
            set i[1171]=61
            set i[51]=62
            set i[305]=63
            set i[0]=64
            set i[222]=65
            set i[178]=66
            set i[236] =67
            set i[184]=68
            set i[1295]=69
            set i[1390]=70
            set i[1276]=71
            set i[203]=72
            set i[1314]=73
            set i[209]=74
            set i[1315]=75
            set i[170]=76
            set i[1357]=77
            set i[1343]=78
            set i[1397]=79
            set i[1420]=80
            set i[1419]=81
            set i[1396]=82
            set i[1374]=83
            set i[1407]=84
            set i[499]=85
            set i[1465]=86
            set i[736]=87
            set i[289]=88
            set i[986]=89
            set i[38]=90
            set i[1230]=91
            set i[1636]=93
            set i[1416]=94
            set i[1917]=95
            set i[217]=96
            set i[833]=123
            set i[1219]=124
            set i[553]=125
            set i[58]=126
            set c[8]="\b"
            set c[9]="\t"
            set c[10]="\n"
            set c[12]="\f"
            set c[13]="\r"
            set c[32]=" "
            set c[33]="!"
            set c[34]="\""
            set c[35]="#"
            set c[36]="$"
            set c[37]="%"
            set c[38]="&"
            set c[39]="'"
            set c[40]="("
            set c[41]=")"
            set c[42]="*"
            set c[43]="+"
            set c[44]=","
            set c[45]="-"
            set c[46]="."
            set c[47]="/"
            set c[48]="0"
            set c[49]="1"
            set c[50]="2"
            set c[51]="3"
            set c[52]="4"
            set c[53]="5"
            set c[54]="6"
            set c[55]="7"
            set c[56]="8"
            set c[57]="9"
            set c[58]=":"
            set c[59]=";"
            set c[60]="<"
            set c[61]="="
            set c[62]=">"
            set c[63]="?"
            set c[64]="@"
            set c[65]="A"
            set c[66]="B"
            set c[67]="C"
            set c[68]="D"
            set c[69]="E"
            set c[70]="F"
            set c[71]="G"
            set c[72]="H"
            set c[73]="I"
            set c[74]="J"
            set c[75]="K"
            set c[76]="L"
            set c[77]="M"
            set c[78]="N"
            set c[79]="O"
            set c[80]="P"
            set c[81]="Q"
            set c[82]="R"
            set c[83]="S"
            set c[84]="T"
            set c[85]="U"
            set c[86]="V"
            set c[87]="W"
            set c[88]="X"
            set c[89]="Y"
            set c[90]="Z"
            set c[92]="\\"
            set c[97]="a"
            set c[98]="b"
            set c[99]="c"
            set c[100]="d"
            set c[101]="e"
            set c[102]="f"
            set c[103]="g"
            set c[104]="h"
            set c[105]="i"
            set c[106]="j"
            set c[107]="k"
            set c[108]="l"
            set c[109]="m"
            set c[110]="n"
            set c[111]="o"
            set c[112]="p"
            set c[113]="q"
            set c[114]="r"
            set c[115]="s"
            set c[116]="t"
            set c[117]="u"
            set c[118]="v"
            set c[119]="w"
            set c[120]="x"
            set c[121]="y"
            set c[122]="z"
            set c[91]="["
            set c[93]="]"
            set c[94]="^"
            set c[95]="_"
            set c[96]="`"
            set c[123]="{"
            set c[124]="|"
            set c[125]="}"
            set c[126]="~"
        endmethod
    endmodule
    private struct Inits extends array
        implement Init
    endstruct
endlibrary



And keep in mind that in your Zinc version, Ascii doesn't necessarily only deal with raw codes.


I should also mention that your new onInit is significantly slower =).
 
Last edited:

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
Nes, you missed the entire point of what I did.

Take what's common knowledge or assumptions:

1. Array is faster than a simple +1 operation
2. Hashtable takes twice as long to compute as an array

With these, hashtable would clearly be faster than an array + three math operations.

Factors of doing it with hashtable:

Much shorter code length
Faster execution in-game
Insignificantly slower init time (seriously not worth talking about)

You are also still doing the useless integer checks that TheDamien added.

JASS:
    function Char2Ascii takes string s returns integer
        local integer a
        if ("\\"==s) then
            return 92 // "/" and "\\" have same hash
        endif
        set a=i[StringHash(s)/0x1F0748+0x3EA]
        if (64<a and 91>a and s!=c[a]) then
            return a+32 //shift to lowercase, upper/lower have same hash
        endif
        debug if ((7<a and 14>a) or (31<a and 127>a)) then
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII CONVERSION ERROR: INVALID CHAR "+s)
        debug return 0
    endfunction

Compared to mine:

JASS:
        function Char2Ascii(string c) -> integer { //OrdFromChar
            integer a = data.ord[StringHash(c)];
            if (c == "\\")
                return 92; //'\\' does not compile with JassHelper but works fine in vanilla JASS
            else if (c != data.char[a]) {
                if (a == 0) {
                    debug BJDebugMsg("TypeError: Tried to get the ord value of invalid char: " + I2S(c));
                    return 0;
                }
                return a + 32; //Switch to lowercase
            }
            return a;
        }
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Actually, if c not the character, then add 32. If c still not the character, then invalid (if not doing a hash read).


I'd like to see the math stuff benched against the hashtable stuff is possible ^)^. Keep in mind that as a hashtable gets more data, it does get slower =P.

edit
Your method fails..
JASS:
//set a=i
globals
    constant boolean F=true
    integer array i
    hashtable t=InitHashtable()
    integer h=0
    string s="c"
    integer m
endglobals
function r takes nothing returns nothing
    local integer x=5000
    loop
        //20 frames per second
        static if F then
            set m=LoadInteger(t,h,StringHash(s))
        //36 frames per second
        else
            set m=i[StringHash(s)/0x1F0748+0x3EA]
        endif
        
        set x=x-1
        exitwhen 0==x
    endloop
    //call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"Done")
endfunction
struct tester extends array
    private static method onInit takes nothing returns nothing
        call TimerStart(CreateTimer(),.015625000,true,function r)
    endmethod
endstruct

gg
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
I've seen tests that show if the hashtable has more data, it operates at the same speed. Maybe the tests were not thorough as this was done on wc3c.net when hashtables first came out. They filled the entries of the hashtable (needed a lot of entries to do so) then compared the speed.

Since you did this test with hashtables I recommend you use it as argument against anyone who says hashtable is only 2x slower than an array.

I don't know what you mean by this:

>> Actually, if c not the character, then add 32.
>> If c still not the character, then invalid (if not doing a hash read).

A bad char would just load 0 from that hashtable, so you only need to check if the char's ord is not equal to 0 (invalid) and not equal to its output (uppercase).

I also don't like the array method because:

1. All the unhashing work is stolen from TheDamien. Open source, lack of credit or whatever you use to try to justify pretty much copying an entire project, it's something that doesn't sit with me.

2. A lot more lines of code to achieve a total speed difference of an entire 5-10% when compared to the size of the Ord function. Having a smaller map size is much more important than such a pathetic speed increase.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
All the unhashing work is stolen from TheDamien. Open source, lack of credit or whatever you use to try to justify pretty much copying an entire project, it's something that doesn't sit with me.

He was credited..

A lot more lines of code to achieve a total speed difference of an entire 5-10% when compared to the size of the Ord function. Having a smaller map size is much more important than such a pathetic speed increase.

Different pros/cons, and virtually nobody cares about the map size resulting from code anyways.

If two resources have very different worthwhile pros, then there should be 2 versions of that resource up.


The next thing I'll check today is comparing strings vs comparing integers (single characters in strings). I want to see if many integer comparisons is slower than 1 string comparison : P. If the string comparison wins, then it could easily be this-
JASS:
if ("\\"==s) then
    return 92
endif
set i=h[StringHash(s)/0x1F0748+0x3EA]
if (s!=c[i]) then
    if (s==c[i+32]) then
        return i+32
    endif
    return 0 //invalid
endif
return i

edit
Results are in
JASS:
globals
    constant boolean F=true
    string array i
endglobals
function r takes nothing returns nothing
    local integer x=5000
    local string s="c"
    local integer c=95
    loop
        //36 frames per second w/o debug mode
        //16 frames per second w/ debug mode
        static if F then
            if (64<c and 91>c) then
                //uppercase
            elseif ("\\"==s) then
                // \\ character
            debug elseif (7>c or 14<c) and (31>c or 127<c) then
                //invalid
            endif
            //valid
            
        //20 frames per second
        else
            if (s!=i[c]) then
                if (s!=i[c+32]) then
                    if ("\\"==s) then
                        // \\ character
                    endif
                    //invalid
                endif
                //uppercase
            endif
            //valid, non uppercase, non \\
        endif
        
        set x=x-1
        exitwhen 0==x
    endloop
    //call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"Done")
endfunction
struct tester extends array
    private static method onInit takes nothing returns nothing
        call TimerStart(CreateTimer(),.015625000,true,function r)
    endmethod
endstruct

So here is the final solution
JASS:
globals
    string array i
endglobals
function r takes nothing returns nothing
    local integer x=5000
    local string s="c"
    local integer c=95
    loop
        static if not DEBUG_MODE then
            //20 fps
            if (s==i[c]) then
                //valid, non uppercase, non \\
            elseif (s==i[c+32]) then
                //uppercase
            elseif ("\\"==s) then
                // \\
            endif
            //invalid
        else
            //43 fps
            if (64<c and 91>c) then
                //uppercase
            elseif ("\\"==s) then
                // \\ character
            endif
            //valid
        endif
        set x=x-1
        exitwhen 0==x
    endloop
    //call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"Done")
endfunction
struct tester extends array
    private static method onInit takes nothing returns nothing
        call TimerStart(CreateTimer(),.015625000,true,function r)
    endmethod
endstruct
 
Last edited:

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
If you shift the hash bias by 1 and subsequently shift the arrays by one (turning 0x3EA into 0x3EB and array index 0 to array index 1) You could simplify this:

JASS:
if (s!=c[i]) then
    if (s==c[i+32]) then
        return i+32
    endif
    return 0 //invalid
endif

To just this:

JASS:
if s != c[i] then
    if 0 == i then
        return 0 //invalid
    endif
    return i + 32
endif

Although, the way he hashes things there may a potential to cause some conflicts in bad strings (longer than 1 char). So using a hashtable in this way would be safer.

I also challenge you to pull from the top of the array (closer to TheDamien's max value of 1979) and see the benchmarks then. You'll of course want to initialize the array from an onInit in order for the benchmarks to be fair. And possibly fill that array up with all the indices TheDamien uses.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
As you wish. The ! is that 1979 value you were eying.

First one is my final solution
JASS:
globals
    constant boolean F=false
    string array i
    integer array h
    hashtable t=InitHashtable()
    integer l=0
endglobals
function r takes nothing returns nothing
    local integer x=5000
    local string s="!"
    local integer c
    loop
        if ("\\"==s) then
            //return 92
        endif
        
        //17 fps
        static if F then
            //hash
            set c=h[StringHash(s)/0x1F0748+0x3EA]
            
            if (64<c and 91>c) then
                //return c+32
            endif
            //return c (will be null if invalid)
            
        //0, can't keep up with timer
        else
            set c=LoadInteger(t,l,StringHash(s))
            if (64<c and 91>c) then
                //return c+32
            endif
            //return c (will be null if invalid)
        endif
        
        set x=x-1
        exitwhen 0==x
    endloop
    //call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"Done")
endfunction
struct tester extends array
    private static method onInit takes nothing returns nothing
        call SaveInteger(t,0,0,0)
        call TimerStart(CreateTimer(),.015625000,true,function r)
    endmethod
endstruct


Tx to you and TheDamien, I've once again come up with the perfect algorithm to a problem >.>.


edit
Updated script (couldn't use integer comparison, I was being stupid ; P). This one ofc outperforms yours =).
JASS:
library Ascii
///////////////////////////////////////////////////////////////////
//      function Char2Ascii takes string s returns integer
//          integer ascii = Char2Ascii("F")
//
//      function Ascii2Char takes integer a returns string
//          string char = Ascii2Char('F')
//
//      function A2S takes integer a returns string
//          string rawcode = A2S('CODE')
//
//      function S2A takes string s returns integer
//          integer rawcode = S2A("CODE")
//
///////////////////////////////////////////////////////////////////
    globals
        private integer array i //hash
        private string array c  //char
    endglobals
    function Char2Ascii takes string s returns integer
        local integer a
        if ("\\"==s) then
            return 92
        endif
        set a=i[StringHash(s)/0x1F0748+0x3EA]
        if (s!=c[a]) then
            debug if (0==a) then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII ERROR: INVALID CHARACTER")
                debug return 0
            debug endif
            return a+32
        endif
        return a
    endfunction
    function Ascii2Char takes integer a returns string
        return c[a]
    endfunction
    function A2S takes integer a returns string
        local string s=""
        loop
            set s=c[a-a/256*256]+s
            set a=a/256
            exitwhen 0==a
        endloop
        return s
    endfunction
    function S2A takes string s returns integer
        local integer a=0
        local integer l=StringLength(s)
        local integer j=0
        local string m
        local integer h
        loop
            exitwhen j==l
            set m=SubString(s,j,j+1)
            if ("\\"==m) then
                set a=a*256+92
            else
                set h=i[StringHash(m)/0x1F0748+0x3EA]
                if (m!=c[h]) then
                    debug if (0==a) then
                        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII ERROR: INVALID CHARACTER")
                        debug return 0
                    debug endif
                    set a=a*256+h+32
                else
                    set a=a*256+h
                endif
            endif
            set j=j+1
        endloop
        return a
    endfunction
    private module Init
        private static method onInit takes nothing returns nothing
            set i[931]=8
            set i[1075]=9
            set i[1586]=10
            set i[1340]=12
            set i[412]=13
            set i[198]=32
            set i[1979]=33
            set i[1313]=34
            set i[1003]=35
            set i[1264]=36
            set i[983]=37
            set i[1277]=38
            set i[306]=39
            set i[904]=40
            set i[934]=41
            set i[917]=42
            set i[1972]=43
            set i[1380]=44
            set i[1985]=45
            set i[869]=46
            set i[1906]=47
            set i[883]=48
            set i[1558]=49
            set i[684]=50
            set i[582]=51
            set i[668]=52
            set i[538]=53
            set i[672]=54
            set i[1173]=55
            set i[71]=56
            set i[277]=57
            set i[89]=58
            set i[1141]=59
            set i[39]=60
            set i[1171]=61
            set i[51]=62
            set i[305]=63
            set i[0]=64
            set i[222]=65
            set i[178]=66
            set i[236] =67
            set i[184]=68
            set i[1295]=69
            set i[1390]=70
            set i[1276]=71
            set i[203]=72
            set i[1314]=73
            set i[209]=74
            set i[1315]=75
            set i[170]=76
            set i[1357]=77
            set i[1343]=78
            set i[1397]=79
            set i[1420]=80
            set i[1419]=81
            set i[1396]=82
            set i[1374]=83
            set i[1407]=84
            set i[499]=85
            set i[1465]=86
            set i[736]=87
            set i[289]=88
            set i[986]=89
            set i[38]=90
            set i[1230]=91
            set i[1636]=93
            set i[1416]=94
            set i[1917]=95
            set i[217]=96
            set i[833]=123
            set i[1219]=124
            set i[553]=125
            set i[58]=126
            set c[8]="\b"
            set c[9]="\t"
            set c[10]="\n"
            set c[12]="\f"
            set c[13]="\r"
            set c[32]=" "
            set c[33]="!"
            set c[34]="\""
            set c[35]="#"
            set c[36]="$"
            set c[37]="%"
            set c[38]="&"
            set c[39]="'"
            set c[40]="("
            set c[41]=")"
            set c[42]="*"
            set c[43]="+"
            set c[44]=","
            set c[45]="-"
            set c[46]="."
            set c[47]="/"
            set c[48]="0"
            set c[49]="1"
            set c[50]="2"
            set c[51]="3"
            set c[52]="4"
            set c[53]="5"
            set c[54]="6"
            set c[55]="7"
            set c[56]="8"
            set c[57]="9"
            set c[58]=":"
            set c[59]=";"
            set c[60]="<"
            set c[61]="="
            set c[62]=">"
            set c[63]="?"
            set c[64]="@"
            set c[65]="A"
            set c[66]="B"
            set c[67]="C"
            set c[68]="D"
            set c[69]="E"
            set c[70]="F"
            set c[71]="G"
            set c[72]="H"
            set c[73]="I"
            set c[74]="J"
            set c[75]="K"
            set c[76]="L"
            set c[77]="M"
            set c[78]="N"
            set c[79]="O"
            set c[80]="P"
            set c[81]="Q"
            set c[82]="R"
            set c[83]="S"
            set c[84]="T"
            set c[85]="U"
            set c[86]="V"
            set c[87]="W"
            set c[88]="X"
            set c[89]="Y"
            set c[90]="Z"
            set c[92]="\\"
            set c[97]="a"
            set c[98]="b"
            set c[99]="c"
            set c[100]="d"
            set c[101]="e"
            set c[102]="f"
            set c[103]="g"
            set c[104]="h"
            set c[105]="i"
            set c[106]="j"
            set c[107]="k"
            set c[108]="l"
            set c[109]="m"
            set c[110]="n"
            set c[111]="o"
            set c[112]="p"
            set c[113]="q"
            set c[114]="r"
            set c[115]="s"
            set c[116]="t"
            set c[117]="u"
            set c[118]="v"
            set c[119]="w"
            set c[120]="x"
            set c[121]="y"
            set c[122]="z"
            set c[91]="["
            set c[93]="]"
            set c[94]="^"
            set c[95]="_"
            set c[96]="`"
            set c[123]="{"
            set c[124]="|"
            set c[125]="}"
            set c[126]="~"
        endmethod
    endmodule
    private struct Inits extends array
        implement Init
    endstruct
endlibrary
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
Updated with logical error fix
JASS:
library Ascii
///////////////////////////////////////////////////////////////////
//      function Char2Ascii takes string s returns integer
//          integer ascii = Char2Ascii("F")
//
//      function Ascii2Char takes integer a returns string
//          string char = Ascii2Char('F')
//
//      function A2S takes integer a returns string
//          string rawcode = A2S('CODE')
//
//      function S2A takes string s returns integer
//          integer rawcode = S2A("CODE")
//
///////////////////////////////////////////////////////////////////
    globals
        private integer array i //hash
        private string array c  //char
    endglobals
    function Char2Ascii takes string s returns integer
        local integer a
        if ("\\"==s) then
            return 92
        endif
        set a=i[StringHash(s)/0x1F0748+0x3EA]
        if (s!=c[a]) then
            debug if (0==a) then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII ERROR: INVALID CHARACTER")
                debug return 0
            debug endif
            return a+32
        endif
        return a
    endfunction
    function Ascii2Char takes integer a returns string
        return c[a]
    endfunction
    function A2S takes integer a returns string
        local string s=""
        loop
            set s=c[a-a/256*256]+s
            set a=a/256
            exitwhen 0==a
        endloop
        return s
    endfunction
    function S2A takes string s returns integer
        local integer a=0
        local integer l=StringLength(s)
        local integer j=0
        local string m
        local integer h
        loop
            exitwhen j==l
            set m=SubString(s,j,j+1)
            if ("\\"==m) then
                set a=a*256+92
            else
                set h=i[StringHash(m)/0x1F0748+0x3EA]
                if (m!=c[h]) then
                    debug if (0==h) then
                        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ASCII ERROR: INVALID CHARACTER")
                        debug return 0
                    debug endif
                    set a=a*256+h+32
                else
                    set a=a*256+h
                endif
            endif
            set j=j+1
        endloop
        return a
    endfunction
    private module Init
        private static method onInit takes nothing returns nothing
            set i[931]=8
            set i[1075]=9
            set i[1586]=10
            set i[1340]=12
            set i[412]=13
            set i[198]=32
            set i[1979]=33
            set i[1313]=34
            set i[1003]=35
            set i[1264]=36
            set i[983]=37
            set i[1277]=38
            set i[306]=39
            set i[904]=40
            set i[934]=41
            set i[917]=42
            set i[1972]=43
            set i[1380]=44
            set i[1985]=45
            set i[869]=46
            set i[1906]=47
            set i[883]=48
            set i[1558]=49
            set i[684]=50
            set i[582]=51
            set i[668]=52
            set i[538]=53
            set i[672]=54
            set i[1173]=55
            set i[71]=56
            set i[277]=57
            set i[89]=58
            set i[1141]=59
            set i[39]=60
            set i[1171]=61
            set i[51]=62
            set i[305]=63
            set i[0]=64
            set i[222]=65
            set i[178]=66
            set i[236] =67
            set i[184]=68
            set i[1295]=69
            set i[1390]=70
            set i[1276]=71
            set i[203]=72
            set i[1314]=73
            set i[209]=74
            set i[1315]=75
            set i[170]=76
            set i[1357]=77
            set i[1343]=78
            set i[1397]=79
            set i[1420]=80
            set i[1419]=81
            set i[1396]=82
            set i[1374]=83
            set i[1407]=84
            set i[499]=85
            set i[1465]=86
            set i[736]=87
            set i[289]=88
            set i[986]=89
            set i[38]=90
            set i[1230]=91
            set i[1636]=93
            set i[1416]=94
            set i[1917]=95
            set i[217]=96
            set i[833]=123
            set i[1219]=124
            set i[553]=125
            set i[58]=126
            set c[8]="\b"
            set c[9]="\t"
            set c[10]="\n"
            set c[12]="\f"
            set c[13]="\r"
            set c[32]=" "
            set c[33]="!"
            set c[34]="\""
            set c[35]="#"
            set c[36]="$"
            set c[37]="%"
            set c[38]="&"
            set c[39]="'"
            set c[40]="("
            set c[41]=")"
            set c[42]="*"
            set c[43]="+"
            set c[44]=","
            set c[45]="-"
            set c[46]="."
            set c[47]="/"
            set c[48]="0"
            set c[49]="1"
            set c[50]="2"
            set c[51]="3"
            set c[52]="4"
            set c[53]="5"
            set c[54]="6"
            set c[55]="7"
            set c[56]="8"
            set c[57]="9"
            set c[58]=":"
            set c[59]=";"
            set c[60]="<"
            set c[61]="="
            set c[62]=">"
            set c[63]="?"
            set c[64]="@"
            set c[65]="A"
            set c[66]="B"
            set c[67]="C"
            set c[68]="D"
            set c[69]="E"
            set c[70]="F"
            set c[71]="G"
            set c[72]="H"
            set c[73]="I"
            set c[74]="J"
            set c[75]="K"
            set c[76]="L"
            set c[77]="M"
            set c[78]="N"
            set c[79]="O"
            set c[80]="P"
            set c[81]="Q"
            set c[82]="R"
            set c[83]="S"
            set c[84]="T"
            set c[85]="U"
            set c[86]="V"
            set c[87]="W"
            set c[88]="X"
            set c[89]="Y"
            set c[90]="Z"
            set c[92]="\\"
            set c[97]="a"
            set c[98]="b"
            set c[99]="c"
            set c[100]="d"
            set c[101]="e"
            set c[102]="f"
            set c[103]="g"
            set c[104]="h"
            set c[105]="i"
            set c[106]="j"
            set c[107]="k"
            set c[108]="l"
            set c[109]="m"
            set c[110]="n"
            set c[111]="o"
            set c[112]="p"
            set c[113]="q"
            set c[114]="r"
            set c[115]="s"
            set c[116]="t"
            set c[117]="u"
            set c[118]="v"
            set c[119]="w"
            set c[120]="x"
            set c[121]="y"
            set c[122]="z"
            set c[91]="["
            set c[93]="]"
            set c[94]="^"
            set c[95]="_"
            set c[96]="`"
            set c[123]="{"
            set c[124]="|"
            set c[125]="}"
            set c[126]="~"
        endmethod
    endmodule
    private struct Inits extends array
        implement Init
    endstruct
endlibrary
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Well, I believe in credit for code/implementations that were taken out of other resources, not credit for ideas that spawned your own things ; ). Notice that I gave credit to Bribe in my Dummy resource for his thing on delayed recycling ^)^.


As this is TheDamien's code and TheDamien's design, TheDamien deserves credit for it ;o.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
I don't take the code though. If I wrote all of the code as my own, I see no reason to credit other people. After all, if I don't use any of someone eles's implementations, why should I credit them for anything?

Yes, what's his face from wc3c should be credited for undefend for unit indexing, but when I started on it, that was public knowledge already.

Did my unit event stuff take from wc3c's stuff in any way? Not in the slightest, I didn't even look at the code.

Did Table take from wc3c's Table? They may have looked identical, but as you realized in the argument, I didn't even read Table's lib, so it was impossible for me to take any of the implementations done in it, thus no credit was due no matter how similar the resources were, after all it was all my own code, my own ideas, my own designs, and my own implementations.

Why should I have to credit someone else for my own code? That makes 0 sense.

I gave Bribe credit because the timestamp implementation for recycling was something that I specifically took from him. That is pretty much the first time I've ever taken code from someone else, which is why that's pretty much the first time I've ever given credit for someone else's code.

Me and Phtes were having a whole debate on this as I was somewhat confused on what to credit Bribe for, but everything appears to be clear now and I am resolved to only give credit for code/designs I specifically take again, not ideas ^_^.

edit
-> "rewrite"

keyword rewrite from scratch. Why should I have to give other people credit for my own work when they didn't write any of it? Light bulb companies don't give credit to Thomas Edison. People don't give credit to the first person who did the queue. I shouldn't need to give credit to some douche just because they were the first ones to write the lib : \. My implementation is my implementation, not their's.
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
537
Know what's more readable? an obvious plus sign. But you know, PHP apparently knows better. Worst language ever.
You know, normally i don't add strings but concat them. I'm all for (++) or (<>) defined under something like Haskells Data.Monoid.

But Bribe did not finished his project anyway so i see no point in your comment.

Now, what is the point of this code in the first place?
With "this code" i assume you mean the ASCII-library this thread is about?
I bet you can imagine usecases where you want to know the ascii-value of a character ;)
 
Level 25
Joined
Jun 5, 2008
Messages
2,572
A question about this system.

I want to encode "weapon" into integer and put it in a variable.
Now i attached what i get if i do:
JASS:
            call BJDebugMsg("going to encode: weapon")
            call BJDebugMsg("Encode should be:"+I2S(S2A("weapon")))
            set i = S2A("weapon")
            call BJDebugMsg("Encoded is:"+ I2S(i))
            call BJDebugMsg("Decode into:" + A2S(i))

I asume this shouldn't happen or am i using the system improperly?
Or is the integer too little to hold more than 4 chars?
If so the S2A function is kind of useless because it encodes only the last 4 chars.
 

Attachments

  • bug.JPG
    bug.JPG
    12.6 KB · Views: 146
Top