Advertisement
DeTrix

KRON_Strings.sqf

Apr 20th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. // =========================================================================================================
  2. //
  3. // String Functions Library
  4. // Version: 2.2.1
  5. // Author: Kronzky
  6. //
  7. // =========================================================================================================
  8. //
  9. // Usage:
  10. //
  11. // • KRON_StrToArray - Converts a string into an array of characters:
  12. // _array=[_str] call KRON_StrToArray
  13. //
  14. // • KRON_StrLen - Returns the length of the string
  15. // _len=[_str] call KRON_StrLen
  16. //
  17. // • KRON_StrLeft - Returns l characters from the left side of the string
  18. // _left=[_str,l] call KRON_StrLeft
  19. //
  20. // • KRON_StrRight - Returns l characters from the right side of the string
  21. // _right=[_str,l] call KRON_StrRight
  22. //
  23. // • KRON_StrMid - Returns l characters from the string, starting at position p (zero-based)
  24. // If l is not defined, the rest of the string is returned
  25. // _mid=[_str,p,(l)] call KRON_StrMid
  26. //
  27. // • KRON_StrInStr - Tests whether string b is present in string a
  28. // _found=[a,b] call KRON_StrInStr
  29. //
  30. // • KRON_StrIndex - Returns the position of string b in string a
  31. // _index=[a,b] call KRON_StrIndex
  32. //
  33. // • KRON_StrUpper - Converts a string to uppercase characters
  34. // _upper=[_str] call KRON_StrUpper
  35. //
  36. // • KRON_StrLower - Converts a string to lowercase characters
  37. // _lower=[_str] call KRON_StrLower
  38. //
  39. // • KRON_Replace - Replaces every occurrence of string _old in string _str with string _new
  40. // _index=[_str,_old,_new] call KRON_Replace
  41. //
  42. // • KRON_FindFlag - Checks a mixed array (_this) for the presence of a string (_str)
  43. // _flg=[_this,_str] call KRON_FindFlag
  44. //
  45. // • KRON_getArg - Searches a mixed array (_this) for a matching string beginning with (_t), and returns the part after a separator (s)
  46. // A default value can be defined as (_d).
  47. // _arg=[_this,_t,(_d)] call KRON_getArg
  48. //
  49. // • KRON_getArgRev - Works like getArg, but search for the part *after* the colon, and return the part in front of it
  50. // A default value can be defined as (_d).
  51. // _arg=[_this,_t,(_d)] call KRON_getArgRev
  52. //
  53. // • KRON_Compare - Compares two elements and returns -1 if first is smaller, 1 if second is smaller, and 0 if equal
  54. // If optional parameter "case" is given, capitalization is considered (upper before lowercase)
  55. // _cmp=[_str1,_str2,("case")] call KRON_Compare
  56. //
  57. // • KRON_ArraySort - Sorts an array of strings in acsending order (Numbers before letters, uppercase before lowercase)
  58. // If array is multi-dimensional, optional parameter (_idx) specifies which column is used for sorting
  59. // If optional parameter "desc" is given, order is reversed
  60. // If optional parameter "case" is given, capitalization is considered (upper before lowercase)
  61. // _srt=[_arr,(_idx),("desc"),("case")] call KRON_ArraySort
  62. //
  63. // =========================================================================================================
  64.  
  65. if !(requiredVersion '1.09') exitWith {};
  66.  
  67.  
  68. KRON_StrToArray = {
  69. private["_in","_i","_arr","_out"];
  70. _in=_this select 0;
  71. _arr = toArray(_in);
  72. _out=[];
  73. for "_i" from 0 to (count _arr)-1 do {
  74. _out=_out+[toString([_arr select _i])];
  75. };
  76. _out
  77. };
  78.  
  79. KRON_StrLeft = {
  80. private["_in","_len","_arr","_out"];
  81. _in=_this select 0;
  82. _len=(_this select 1)-1;
  83. _arr=[_in] call KRON_StrToArray;
  84. _out="";
  85. if (_len>=(count _arr)) then {
  86. _out=_in;
  87. } else {
  88. for "_i" from 0 to _len do {
  89. _out=_out + (_arr select _i);
  90. };
  91. };
  92. _out
  93. };
  94.  
  95. KRON_StrLen = {
  96. private["_in","_arr","_len"];
  97. _in=_this select 0;
  98. _arr=[_in] call KRON_StrToArray;
  99. _len=count (_arr);
  100. _len
  101. };
  102.  
  103. KRON_StrRight = {
  104. private["_in","_len","_arr","_i","_out"];
  105. _in=_this select 0;
  106. _len=_this select 1;
  107. _arr=[_in] call KRON_StrToArray;
  108. _out="";
  109. if (_len>(count _arr)) then {_len=count _arr};
  110. for "_i" from ((count _arr)-_len) to ((count _arr)-1) do {
  111. _out=_out + (_arr select _i);
  112. };
  113. _out
  114. };
  115.  
  116. KRON_StrMid = {
  117. private["_in","_pos","_len","_arr","_i","_out"];
  118. _in=_this select 0;
  119. _pos=abs(_this select 1);
  120. _arr=[_in] call KRON_StrToArray;
  121. _len=count(_arr);
  122. if ((count _this)>2) then {_len=(_this select 2)};
  123. _out="";
  124. if ((_pos+_len)>=(count _arr)) then {_len=(count _arr)-_pos};
  125. if (_len>0) then {
  126. for "_i" from _pos to (_pos+_len-1) do {
  127. _out=_out + (_arr select _i);
  128. };
  129. };
  130. _out
  131. };
  132.  
  133. KRON_StrIndex = {
  134. private["_hay","_ndl","_lh","_ln","_arr","_tmp","_i","_j","_out"];
  135. _hay=_this select 0;
  136. _ndl=_this select 1;
  137. _out=-1;
  138. _i=0;
  139. if (_hay == _ndl) exitWith {0};
  140. _lh=[_hay] call KRON_StrLen;
  141. _ln=[_ndl] call KRON_StrLen;
  142. if (_lh < _ln) exitWith {-1};
  143. _arr=[_hay] call KRON_StrToArray;
  144. for "_i" from 0 to (_lh-_ln) do {
  145. _tmp="";
  146. for "_j" from _i to (_i+_ln-1) do {
  147. _tmp=_tmp + (_arr select _j);
  148. };
  149. if (_tmp==_ndl) exitWith {_out=_i};
  150. };
  151. _out
  152. };
  153.  
  154. KRON_StrInStr = {
  155. private["_out"];
  156. _in=_this select 0;
  157. _out=if (([_this select 0,_this select 1] call KRON_StrIndex)==-1) then {false} else {true};
  158. _out
  159. };
  160.  
  161. KRON_Replace = {
  162. private["_str","_old","_new","_out","_tmp","_jm","_la","_lo","_ln","_i"];
  163. _str=_this select 0;
  164. _arr=toArray(_str);
  165. _la=count _arr;
  166. _old=_this select 1;
  167. _new=_this select 2;
  168. _na=[_new] call KRON_StrToArray;
  169. _lo=[_old] call KRON_StrLen;
  170. _ln=[_new] call KRON_StrLen;
  171. _out="";
  172. for "_i" from 0 to (count _arr)-1 do {
  173. _tmp="";
  174. if (_i <= _la-_lo) then {
  175. for "_j" from _i to (_i+_lo-1) do {
  176. _tmp=_tmp + toString([_arr select _j]);
  177. };
  178. };
  179. if (_tmp==_old) then {
  180. _out=_out+_new;
  181. _i=_i+_lo-1;
  182. } else {
  183. _out=_out+toString([_arr select _i]);
  184. };
  185. };
  186. _out
  187. };
  188.  
  189. KRON_StrUpper = {
  190. private["_in","_out"];
  191. _in=_this select 0;
  192. _out=toUpper(_in);
  193. _out
  194. };
  195.  
  196. KRON_StrLower = {
  197. private["_in","_out"];
  198. _in=_this select 0;
  199. _out=toLower(_in);
  200. _out
  201. };
  202.  
  203. KRON_ArrayToUpper = {
  204. private["_in","_i","_e","_out"];
  205. _in=_this select 0;
  206. _out=[];
  207. if ((count _in)>0) then {
  208. for "_i" from 0 to (count _in)-1 do {
  209. _e=_in select _i;
  210. if (typeName _e=="STRING") then {
  211. _e=toUpper(_e);
  212. };
  213. _out set [_i,_e];
  214. };
  215. };
  216. _out
  217. };
  218.  
  219. KRON_Compare = {
  220. private["_k","_n","_s","_i","_c","_t","_s1","_s2","_l1","_l2","_l"];
  221. _k=[_this,"CASE"] call KRON_findFlag;
  222. _n=0;
  223. _s=0;
  224. for "_i" from 0 to 1 do {
  225. _t=_this select _i;
  226. switch (typeName _t) do {
  227. case "SCALAR": {_n=1};
  228. case "BOOL": {_this set [_i,str(_t)]};
  229. case "SIDE": {_this set [_i,str(_t)]};
  230. case "STRING": {if !(_k) then {_this=[_this] call KRON_ArrayToUpper}};
  231. default {_n=-1};
  232. };
  233. };
  234. _s1 = _this select 0;
  235. _s2 = _this select 1;
  236. if (_n!=0) exitWith {
  237. if (_n==1) then {
  238. if (_s1<_s2) then {_s=-1} else {if (_s1>_s2) then {_s=1}};
  239. };
  240. _s
  241. };
  242. _s1 = toArray(_s1);
  243. _s2 = toArray(_s2);
  244. _l1 = count _s1;
  245. _l2 = count _s2;
  246. _l=if (_l1<_l2) then {_l1} else {_l2};
  247. for "_i" from 0 to _l-1 do {
  248. if ((_s1 select _i)<(_s2 select _i)) then {
  249. _s=-1;
  250. _i=_l;
  251. } else {
  252. if ((_s1 select _i)>(_s2 select _i)) then {
  253. _s=1;
  254. _i=_l;
  255. };
  256. };
  257. };
  258. if (_s==0) then {
  259. if (_l1<_l2) then {
  260. _s=-1;
  261. } else {
  262. if (_l1>_l2) then {_s=1};
  263. };
  264. };
  265. _s
  266. };
  267.  
  268. KRON_ArraySort = {
  269. private["_a","_d","_k","_s","_i","_vo","_v1","_v2","_j","_c","_x"];
  270. _a = +(_this select 0);
  271. _d = if ([_this,"DESC"] call KRON_findFlag) then {-1} else {1};
  272. _k = if ([_this,"CASE"] call KRON_findFlag) then {"CASE"} else {"nocase"};
  273. _s = -1;
  274. if (typeName (_a select 0)=="ARRAY") then {
  275. _s=0;
  276. if (((count _this)>1) && (typeName (_this select 1)=="SCALAR")) then {
  277. _s=_this select 1;
  278. };
  279. };
  280. for "_i" from 0 to (count _a)-1 do {
  281. _vo = _a select _i;
  282. _v1 = _vo;
  283. if (_s>-1) then {_v1=_v1 select _s};
  284. _j = 0;
  285. for [{_j=_i-1},{_j>=0},{_j=_j-1}] do {
  286. _v2 = _a select _j;
  287. if (_s>-1) then {_v2=_v2 select _s};
  288. _c=[_v2,_v1,_k] call KRON_Compare;
  289. if (_c!=_d) exitWith {};
  290. _a set [_j+1,_a select _j];
  291. };
  292. _a set [_j+1,_vo];
  293. };
  294. _a
  295. };
  296.  
  297. KRON_findFlag = {
  298. private["_in","_flg","_arr","_out"];
  299. _in=_this select 0;
  300. _flg=toUpper(_this select 1);
  301. _arr=[_in] call KRON_ArrayToUpper;
  302. _out=_flg in _arr;
  303. _out
  304. };
  305.  
  306. KRON_getArg = {
  307. private["_in","_flg","_fl","_def","_arr","_i","_j","_as","_aa","_org","_p","_out"];
  308. _in=_this select 0;
  309. _flg=toUpper(_this select 1);
  310. _fl=[_flg] call KRON_StrLen;
  311. _out="";
  312. if ((count _this)>2) then {_out=_this select 2};
  313. _arr=[_in] call KRON_ArrayToUpper;
  314. if ((count _arr)>0) then {
  315. for "_i" from 0 to (count _in)-1 do {
  316. _as = _arr select _i;
  317. if (typeName _as=="STRING") then {
  318. _aa = [_as] call KRON_StrToArray;
  319. _p = _aa find ":";
  320. if (_p==_fl) then {
  321. if (([_as,_fl] call KRON_StrLeft)==_flg) then {
  322. _org = _in select _i;
  323. _out=[_org,_p+1] call KRON_StrMid;
  324. };
  325. };
  326. };
  327. };
  328. };
  329. _out
  330. };
  331.  
  332.  
  333. KRON_getArgRev = {
  334. private["_in","_flg","_fl","_def","_arr","_i","_j","_as","_aa","_org","_p","_out"];
  335. _in=_this select 0;
  336. _flg=toUpper(_this select 1);
  337. _fl=[_flg] call KRON_StrLen;
  338. _out="";
  339. if ((count _this)>2) then {_out=_this select 2};
  340. _arr=[_in] call KRON_ArrayToUpper;
  341. if ((count _arr)>0) then {
  342. for "_i" from 0 to (count _in)-1 do {
  343. _as = _arr select _i;
  344. if (typeName _as=="STRING") then {
  345. _aa = [_as] call KRON_StrToArray;
  346. _p = _aa find ":";
  347. if (_p+1==(count _aa)-_fl) then {
  348. if (([_as,_p+1] call KRON_StrMid)==_flg) then {
  349. _org = _in select _i;
  350. _out=[_org,_p] call KRON_StrLeft;
  351. };
  352. };
  353. };
  354. };
  355. };
  356. _out
  357. };
  358.  
  359. KRON_convertPlayerUID = {
  360. private["_in","_up","_out"];
  361. _in=_this select 0;
  362. _up=[_in] call KRON_StrUpper;
  363. _out=[_up,"AX","999"] call KRON_Replace;
  364. _out
  365. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement