1. The contestants were to create water structures for the 20th Terraining Contest. Choose one in the public poll!
    Dismiss Notice
  2. Join other hivers in a friendly concept-art contest. The contestants have to create a genie coming out of its container. We wish you the best of luck!
    Dismiss Notice
  3. The Melee Mapping Contest #4: 2v2 - Results are out! Step by to congratulate the winners!
    Dismiss Notice
  4. We're hosting the 15th Mini-Mapping Contest with YouTuber Abelhawk! The contestants are to create a custom map that uses the hidden content within Warcraft 3 or is inspired by any of the many secrets within the game.
    Dismiss Notice
  5. Check out the Staff job openings thread.
    Dismiss Notice

[Snippet] Ascii

Discussion in 'JASS Resources' started by Bribe, Mar 1, 2011.

  1. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Ascii - your resource for getting the ordinal value of a char or the char value of an integer.

    Code (vJASS):

    library Ascii /* v1.1.0.0         Nestharus/Bribe
    ************************************************************************************
    *
    *   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 by a moderator: May 27, 2013
  2. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Fixed the names for the aforementioned reasons. If anyone still has a problem with A2S and S2A, especially Nestharus whose naming convention I borrowed for them, let me know.
     
  3. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    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 -.-.

    Code (vJASS):

        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
     
     
  4. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    And if the input integer is below 0, your modulo will fail. Hence ModuloInteger.

    And you who obsesses over short variable names should know, mine has a speed advantage just because of that. Of course, one could use the optimizer, but, oh ;)
     
  5. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Ahem, you'd never use a negative with ascii...
     
  6. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Appended with those changes.
     
  7. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Good, I believe it's ready for approval ;p
     
  8. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Going to check this for any possible syntax errors it might have when I get home, as I made some changes to the script (small changes, likely no errors, but I gotta check!)
     
  9. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    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.

    Code (vJASS):

    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: Mar 6, 2011
  10. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    With those updates, I don't see why this shouldn't be approved, unless TheDamien comes back from the dead and actually updates his resource.
     
  11. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Updated thanks to Nestharus - modululo converts ASCII with 256 now instead of 128.
     
  12. The General

    The General

    Joined:
    Feb 16, 2011
    Messages:
    581
    Resources:
    0
    Resources:
    0
    what is jass?
     
  13. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,420
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    http://wiki.thehelper.net/wc3/jass
     
  14. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Updated this thing to the one that will be featured when Luck is published.
     
  15. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,008
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Don't you just love zinc?
     
    Last edited: Mar 19, 2012
  16. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    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.

    Luck Ascii

    Hex is going to be a seperate resource, I originally combined them in the development process.
    Code (Text):

    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[p]]
                        debug:
                            if not VAL.contains(this[p]) 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: Jun 20, 2011
  17. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,008
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    String concatenation is done with a "."? Can it be done with ".."? (Similar to LUA)
     
    Last edited: Mar 19, 2012
  18. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    7,859
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    PHP does it with one dot, but I like your thinking here, two dots is probably more readable.
     
  19. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Here is an optimized Ascii. Extra space is removed and the comparisons are more optimal.

    Code (vJASS):

    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: Jun 28, 2011
  20. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,008
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    You WIN Nestharus.
    I'm optimizing all my scripts as we speak.
    By the way, learn Zinc!
     
    Last edited: Mar 19, 2012