• 🏆 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!

[JASS] Iterate through integers separated by spaces in a string

Level 3
Joined
Sep 28, 2018
Messages
38
Hi! The title sums up my query. We're utilizing an ASCII/ANSI to text library (credits in lib) and want to expand it's use from single character calls to sending in a string of characters. In its original use case if '65' is passed in (ex: A2S(65), the character 'A' is returned as this is the equivalent text character for the integer 65 in ANSI. What we'd like to do is be able to send in multiple integers, ex: "97 98 99", and get the correct corresponding characters in return, in this case "abc".

Reference: ANSI and equivalent character set

Again our function, callA2S(string s), takes in a string, ex: "97 98 99", and for each integer (separated by spaces) a call is made to the function A2S(integer i) which returns a character. Finishing this example what would be returned is "abc".

We mocked up a simple second function, callA2S() that ideally iterates through a passed in string and calls A2S() for each integer. Any help is appreciated and our thanks in advance!

Best always,
j

JASS:
function callA2S takes string str returns string
    local string s=""
    local integer i=0
    loop
        s=s+A2S(i)
        i=i+1
    endloop
    return s
endfunction

function Trig_Melee_Initialization_Actions takes nothing returns nothing
    call DisplayTextToForce( GetPlayersAll(), callA2S("97 98 99") )
endfunction

JASS:
library Ascii
///////////////////////////////////////////////////////////////////
//
//      credits: Bribe/TheDamian/Nestharus @ HiveWorkshop
//
//      function A2S takes integer a returns string
//          string rawcode = A2S('CODE')
//      ex: A2S(97) returns a
//
///////////////////////////////////////////////////////////////////
    globals
        private string array c  //char
    endglobals

    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

    private module Init
        private static method onInit takes nothing returns nothing
            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
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,543
Hi! The title sums up my query. We're utilizing an ASCII/ANSI to text library (credits in lib) and want to expand it's use from single character calls to sending in a string of characters. In its original use case if '65' is passed in (ex: A2S(65), the character 'A' is returned as this is the equivalent text character for the integer 65 in ANSI. What we'd like to do is be able to send in multiple integers, ex: "97 98 99", and get the correct corresponding characters in return, in this case "abc".

Reference: ANSI and equivalent character set

Again our function, callA2S(string s), takes in a string, ex: "97 98 99", and for each integer (separated by spaces) a call is made to the function A2S(integer i) which returns a character. Finishing this example what would be returned is "abc".

We mocked up a simple second function, callA2S() that ideally iterates through a passed in string and calls A2S() for each integer. Any help is appreciated and our thanks in advance!

Best always,
j
vJASS:
function A2S_Ex takes string str returns string
    local string splitStr = ""
    local string finalStr = ""
    local integer i = 0
    local integer startIndex = 1
    local integer lastIndex = StringLength(str)

    loop
        set i = i + 1
        if SubStringBJ(str, i, i) == " " then
            set splitStr = SubStringBJ(str, startIndex, i - 1)
            set finalStr = finalStr + A2S(S2I(splitStr))
            set startIndex = i + 1
        elseif i == lastIndex then
            set splitStr = SubStringBJ(str, startIndex, i)
            set finalStr = finalStr + A2S(S2I(splitStr))
        endif 
        exitwhen i == lastIndex
    endloop

    return finalStr
endfunction
This should do what you want, assuming I understood correctly.
 
Level 3
Joined
Sep 28, 2018
Messages
38
vJASS:
function A2S_Ex takes string str returns string
...
endfunction
This should do what you want, assuming I understood correctly.
Perfection @Uncle thank you!
@Pyrogasm tested with i and i+1 and just i itself for both bounds is correct :)! I believe this is because Uncle's script establishes the initial bound at the first character and continues checking each character individually until it finds a space, " ", and uses said spaces location as the endingIndex for the bounds of the substring. Just my intuition, I could well be wrong!

*EDIT: Massive thanks to the both of you! I've truly appreciated all your help. I was a programmer by trade many moons ago and though some stuff remains in the brain converting it into JASS is always a game of patience and your guys help has always been monumental. Thank you :)
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,543
I tested it thoroughly before posting, it should definitely work. But I believe the non-BJ version of SubString() would NOT work with how I've set it up.

I don't think it really matters unless you're going for maximum efficiency - non-BJs are always the preferred choice but this design made more sense to me.
 
Top