Search the Community
Showing results for tags 'whitespace'.
Found 1 result
-
Removing whitespace script release
das attorney posted a topic in ARMA 3 - MISSION EDITING & SCRIPTING
Preamble: From the wiki, we can use splitstring and joinstring to remove white space from code. So if we have a function like this one (from the wiki): fnc_deleteTestObj = { _this addMPEventHandler ["MPKilled", { _this = _this select 0; { deleteVehicle _x; } forEach (_this getVariable ["effects", []]); if (isServer) then { deleteVehicle _this; }; }]; _this setDamage 1; }; We can do: copyToClipboard(([fnc_deleteTestObj] joinString "") splitString toString [9,13,10] joinString "") and it returns: { _this addMPEventHandler ["MPKilled", { _this = _this select 0; { deleteVehicle _x; } forEach (_this getVariable ["effects", []]); if (isServer) then { deleteVehicle _this; }; }]; _this setDamage 1; } Which is quite nice. But I wanted a way to remove as much white space as I can (for sending code/variables over the network etc), plus I just fancied playing about with recursion. So with this function, we can use our "fnc_deleteTestObj" function again and plug it in: copyToClipboard(([fnc_deleteTestObj] joinString "") call fnc_whitespace) This returns: {_this addMPEventHandler["MPKilled",{_this=_this select 0;{deleteVehicle _x;}forEach(_this getVariable["effects",[]]);if(isServer)then{deleteVehicle _this;};}];_this setDamage 1;} I'm using joinstring in these examples as opposed to format as it doesn't have the 8k limit. Apparently formatText isn't limited and I'm not sure about str. But you could do this I suppose - just be aware it might have a limit: (str fnc_deleteTestObj) call fnc_whitespace Uses As mentioned, you could shorten variables prior to sending them over the network (then compileFinal at other end). I'm aware that programs like these exist already, but I wanted to do it in sqf (like within the engine). It should be ok but will have a problem if you do silly things like putting spaces into variables ex: player setVariable ["there are spaces in this",true]; which is a bit of a no-no imo and bad practice. You could probably obfuscate your code with this if you want but there are already good online obfuscators available anyway, and also good online de-obfuscators, like this one: http://unminify.com/ I think Dwarden said he used to do this to his mission files to make them smaller so they wouldn't suck up as much bandwidth, so theres a use for it there I suppose. Obv, I haven't tested this much but it seems ok so far - ymmv. Be aware if you use it on very large functions, then it is recursive so it might freeze your game for a few seconds. If you rename it, make sure you rename the call to it in the code as well or you will break it. Function: Here it is for any interested: fnc_whitespace = { private _noWhitespace = toArray(_this); private _anyWS = false; { if (0 isEqualType _x) then { if ([9,13,10] find _x > -1) then { _noWhitespace set [_forEachIndex,objNull]; _anyWS = true; } else { if ([34,39,94,42,40,41,123,125,91,93,59,58,62,47,43,45,61,46] find _x > -1) then { if (_forEachIndex - 1 > -1) then { if ([32,9,13,10] find (_noWhitespace select (_forEachIndex - 1)) > -1) then { _noWhitespace set [_forEachIndex - 1,objNull]; _anyWS = true; } }; if (_forEachIndex + 1 < count _noWhitespace) then { if ([32,9,13,10] find (_noWhitespace select (_forEachIndex + 1)) > -1) then { _noWhitespace set [_forEachIndex + 1,objNull]; _anyWS = true; } }; } } }; } forEach _noWhitespace; _noWhitespace = _noWhitespace - [objNull]; if (_anyWS) then { _noWhitespace = (toString _noWhitespace) call fnc_whitespace; _noWhitespace = toArray _noWhitespace; }; toString(_noWhitespace) }; And here it is compacted (ho ho ho): fnc_whitespace={private _noWhitespace=toArray(_this);private _anyWS=false;{if(0 isEqualType _x)then{if([9,13,10]find _x>-1)then{_noWhitespace set[_forEachIndex,objNull];_anyWS=true;}else{if([34,39,94,42,40,41,123,125,91,93,59,58,62,47,43,45,61,46]find _x>-1)then{if(_forEachIndex-1>-1)then{if([32,9,13,10]find(_noWhitespace select(_forEachIndex-1))>-1)then{_noWhitespace set[_forEachIndex-1,objNull];_anyWS=true;}};if(_forEachIndex+1 < count _noWhitespace)then{if([32,9,13,10]find(_noWhitespace select(_forEachIndex+1))>-1)then{_noWhitespace set[_forEachIndex+1,objNull];_anyWS=true;}};}}};}forEach _noWhitespace;_noWhitespace=_noWhitespace-[objNull];if(_anyWS)then{_noWhitespace=(toString _noWhitespace)call fnc_whitespace;_noWhitespace=toArray _noWhitespace;};toString(_noWhitespace)} Credit to @dreadedentity for his initial code HERE Enjoy!- 11 replies
-
- 6
-
- remove
- whitespace
-
(and 5 more)
Tagged with: