• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[Snippet] [Needs Mythbusting] Concat

Level 31
Joined
Jul 10, 2007
Messages
6,306
Simple concat textmacro since I don't want to continue flooding my resources with this snippet of code =p.

JASS:
//! textmacro CONCAT takes VAR, ARRAY
    set $VAR$ = $ARRAY$[220] + $ARRAY$[219] + $ARRAY$[218] + $ARRAY$[217] + $ARRAY$[216] + $ARRAY$[215] + $ARRAY$[214] + $ARRAY$[213] + $ARRAY$[212] + $ARRAY$[211] + $ARRAY$[210] + $ARRAY$[209] + $ARRAY$[208] + $ARRAY$[207] + $ARRAY$[206] + $ARRAY$[205] + $ARRAY$[204] + $ARRAY$[203] + $ARRAY$[202] + $ARRAY$[201] + $ARRAY$[200] + /*
    */$ARRAY$[199] + $ARRAY$[198] + $ARRAY$[197] + $ARRAY$[196] + $ARRAY$[195] + $ARRAY$[194] + $ARRAY$[193] + $ARRAY$[192] + $ARRAY$[191] + $ARRAY$[190] + $ARRAY$[189] + $ARRAY$[188] + $ARRAY$[187] + $ARRAY$[186] + $ARRAY$[185] + $ARRAY$[184] + $ARRAY$[183] + $ARRAY$[182] + $ARRAY$[181] + $ARRAY$[180] + $ARRAY$[179] + $ARRAY$[178] + $ARRAY$[177] + $ARRAY$[176] + $ARRAY$[175] + $ARRAY$[174] + $ARRAY$[173] + $ARRAY$[172] + $ARRAY$[171] + $ARRAY$[170] + $ARRAY$[169] + $ARRAY$[168] + $ARRAY$[167] + $ARRAY$[166] + $ARRAY$[165] + $ARRAY$[164] + $ARRAY$[163] + $ARRAY$[162] + $ARRAY$[161] + $ARRAY$[160] + $ARRAY$[159] + $ARRAY$[158] + $ARRAY$[157] + $ARRAY$[156] + $ARRAY$[155] + $ARRAY$[154] + $ARRAY$[153] + $ARRAY$[152] + $ARRAY$[151] + $ARRAY$[150] + $ARRAY$[149] + $ARRAY$[148] + $ARRAY$[147] + $ARRAY$[146] + $ARRAY$[145] + $ARRAY$[144] + $ARRAY$[143] + $ARRAY$[142] + $ARRAY$[141] + $ARRAY$[140] + $ARRAY$[139] + $ARRAY$[138] + $ARRAY$[137] + $ARRAY$[136] + $ARRAY$[135] + $ARRAY$[134] + $ARRAY$[133] + $ARRAY$[132] + $ARRAY$[131] + $ARRAY$[130] + $ARRAY$[129] + $ARRAY$[128] + $ARRAY$[127] + $ARRAY$[126] + $ARRAY$[125] + $ARRAY$[124] + $ARRAY$[123] + $ARRAY$[122] + $ARRAY$[121] + $ARRAY$[120] + $ARRAY$[119] + $ARRAY$[118] + $ARRAY$[117] + $ARRAY$[116] + $ARRAY$[115] + $ARRAY$[114] + $ARRAY$[113] + $ARRAY$[112] + $ARRAY$[111] + $ARRAY$[110] + $ARRAY$[109] + $ARRAY$[108] + $ARRAY$[107] + $ARRAY$[106] + $ARRAY$[105] + $ARRAY$[104] + $ARRAY$[103] + $ARRAY$[102] + $ARRAY$[101] + $ARRAY$[100] + /*
    */$ARRAY$[99] + $ARRAY$[98] + $ARRAY$[97] + $ARRAY$[96] + $ARRAY$[95] + $ARRAY$[94] + $ARRAY$[93] + $ARRAY$[92] + $ARRAY$[91] + $ARRAY$[90] + $ARRAY$[89] + $ARRAY$[88] + $ARRAY$[87] + $ARRAY$[86] + $ARRAY$[85] + $ARRAY$[84] + $ARRAY$[83] + $ARRAY$[82] + $ARRAY$[81] + $ARRAY$[80] + $ARRAY$[79] + $ARRAY$[78] + $ARRAY$[77] + $ARRAY$[76] + $ARRAY$[75] + $ARRAY$[74] + $ARRAY$[73] + $ARRAY$[72] + $ARRAY$[71] + $ARRAY$[70] + $ARRAY$[69] + $ARRAY$[68] + $ARRAY$[67] + $ARRAY$[66] + $ARRAY$[65] + $ARRAY$[64] + $ARRAY$[63] + $ARRAY$[62] + $ARRAY$[61] + $ARRAY$[60] + $ARRAY$[59] + $ARRAY$[58] + $ARRAY$[57] + $ARRAY$[56] + $ARRAY$[55] + $ARRAY$[54] + $ARRAY$[53] + $ARRAY$[52] + $ARRAY$[51] + $ARRAY$[50] + $ARRAY$[49] + $ARRAY$[48] + $ARRAY$[47] + $ARRAY$[46] + $ARRAY$[45] + $ARRAY$[44] + $ARRAY$[43] + $ARRAY$[42] + $ARRAY$[41] + $ARRAY$[40] + $ARRAY$[39] + $ARRAY$[38] + $ARRAY$[37] + $ARRAY$[36] + $ARRAY$[35] + $ARRAY$[34] + $ARRAY$[33] + $ARRAY$[32] + $ARRAY$[31] + $ARRAY$[30] + $ARRAY$[29] + $ARRAY$[28] + $ARRAY$[27] + $ARRAY$[26] + $ARRAY$[25] + $ARRAY$[24] + $ARRAY$[23] + $ARRAY$[22] + $ARRAY$[21] + $ARRAY$[20] + $ARRAY$[19] + $ARRAY$[18] + $ARRAY$[17] + $ARRAY$[16] + $ARRAY$[15] + $ARRAY$[14] + $ARRAY$[13] + $ARRAY$[12] + $ARRAY$[11] + $ARRAY$[10] + $ARRAY$[9] + $ARRAY$[8] + $ARRAY$[7] + $ARRAY$[6] + $ARRAY$[5] + $ARRAY$[4] + $ARRAY$[3] + $ARRAY$[2] + $ARRAY$[1] + $ARRAY$[0]
//! endtextmacro
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
It's just used to concatenate a string like "s" + "a" = "sa", that's all =p.

If you have a list of characters, you want to concatenate them all at once instead of 1 at a time to prevent leaks. Bribe demanded I make this resource so his browser would stop crashing whenever he read my File resource ; P.


Even if this is fairly useless, it will be approved simply because it is a necessity, that is, unless Troll-Brain proves otherwise with his string leak test ; ).


Any arguments against this are moot until Troll-Brain comes out with results ;o.

I may be sounding like an arrogant bastard, but that's just the way it is :\.
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,554
You should call it SafeConcatStrings then, or something besides concat. Concat isn't supposed to strictly work with strings.

Concat in jass would look something like this:

JASS:
library concat
    //! textmacro nullable takes TYPE
        function concat_$TYPE$ takes $TYPE$ array as, $TYPE$ array bs returns $TYPE$ array
            local integer index=0 //we must assume the array index starts with 0, and all values are non-null until the last value, which is null
            local integer bIndex=0
            local integer tertiaryIndex=0
            loop
                exitwhen as[index]==null
                set index=index+1
            endloop
            loop
                exitwhen bs[bIndex]==null
                set bIndex=bIndex+1
            endloop
            loop
                exitwhen tertiaryIndex==bIndex
                set as[index+tertiaryIndex]=bs[tertiaryIndex]
                set tertiaryIndex=tertiaryIndex+1
            endloop
            return as
        endfunction
    //! endtextmacro
    
    //! runtextmacro nullable("unit")
    //! runtextmacro nullable("trigger")
endlibrary

However that script is both incomplete and doesn't work.

Edit: So "s"+"a" -> "sa" leaks? How was the conclusion derived?
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Well, i tested it again, but this time with variables instead of litteral strings.
This doesn't prevent strings leaks in any way, it acts like i said previously.
One string is created for each string concatenation.

JASS:
library TestStringLeak initializer init

    globals
        private constant integer STRING_TABLE_SIZE = 100
    endglobals

    private function Int2S takes integer i returns string
        return i
        return ""
    endfunction

    private function S2Int takes string s returns integer
        return s
        return 0
    endfunction

    private function init takes nothing returns nothing
        local integer i = 0
        local integer count1 = 0
        local integer count2 = 0
        local string s = null
        local string str1 = "NAD1"
        local string str2 = "NAD2"
        local string str3 = "NAD3"
    
        call TriggerSleepAction(0)
    
        loop
        set s = Int2S(i)
        exitwhen i == STRING_TABLE_SIZE
        set i = i+1
            if s != null then
                set count1 = count1+1
            endif
        endloop
        set s = I2S(GetRandomInt(1,1))+I2S(GetRandomInt(2,2))+I2S(GetRandomInt(3,3))
        set s = str1+str2+str3
    
        set i = 0
        loop
        set s = Int2S(i)
        exitwhen i == STRING_TABLE_SIZE
        set i = i+1
            if s != null then
                set count2 = count2+1
                call BJDebugMsg(s)
            endif
        endloop
    
        call BJDebugMsg("count1 == "+I2S(count1))
        call BJDebugMsg("count2 == "+I2S(count2))
    
    endfunction
    
endlibrary

It displays (with other strings) :

"1" , "2" , "3" , "12" , "123", "NAD1", "NAD2", "NAD3", "NAD1NAD2" , "NAD1NAD2NAD3".

If it worked like you said "12" and "NAD1NAD2" shouldn't be created.

Now there is still the possibility that strings are handle differently in the newest patches but i'm doubtful about it, it's Blizzard, they don't improve jass so easily.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Probably the cost of the loop, since jass is slowly interpreted.

EDIT : Now, it's at you to prove something, i don't bother to do more, i'm not your monkey.
Plus when you're doing such jass abuses you have to justify it, not just because you can do it.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Hmm, as said maybe strings are handled differently now.
Any way to typecast integer <-> string with the newest wc3 versions ?

EDIT : Since you're an human after all, make sure your loop worked as expected in the range you wanted (and not with an "infinite" loop, where the exitwhen is always false)
 
Level 14
Joined
Nov 18, 2007
Messages
816
Try using this.

It combines 1 with 2, 3 with 4, 5 with 6 and so on.
Then it combines 12 with 34, 56 with 78, and so on.
Then it combines 1234 with 5678, and so on.
Repeat until have only one string left.

JASS:
function BinaryConcat takes nothing returns nothing
local string array data
local integer i=4096
local integer j=0
    loop
        exitwhen i==1
        set i=(i+1)/2
        set j=0
        loop
            exitwhen j==i
            set data[j]=data[j*2]+data[j*2+1]
            set j=j+1
        endloop
    endloop
endfunction

A recursive solution can be found here (by Anitarf).
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
That would create the same amount of strings of a simple loop when you will concatenate the full string or i'm missing something.
number of created strings : size of the string subdivided - 2 (assuming all strings of size "1" are already created)

If we take the string "12345678" :

Assuming the strings "1","2","...","8" already exist.

your loop would create the strings :

"12" , "34" , "56" , "78" , "1234" , "5678"

While with a simple loop to concatenate the previous string with the next one :

"12" ,"123","1234","12345","123456","1234567".

But meh, because the average size of the created strings is smaller maybe it's worth it.
I just don't know if the jass vm will handle better smaller strings, it should be the case but since i don't know the algorithm which handle strings it's worth a test.

EDIT : Also since the average size of strings created is smaller, because of probabilities it has more chance that the string is already created, so less string "leaks".

So it should be a win-win, however a test would be good. It's jass after all.
 
Last edited:
Top