KRON_Strings.sqf 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. KRON_StrToArray = {
  65. private["_in","_i","_arr","_out"];
  66. _in=_this select 0;
  67. _arr = toArray(_in);
  68. _out=[];
  69. for "_i" from 0 to (count _arr)-1 do {
  70. _out=_out+[toString([_arr select _i])];
  71. };
  72. _out
  73. };
  74. KRON_StrLeft = {
  75. private["_in","_len","_arr","_out"];
  76. _in=_this select 0;
  77. _len=(_this select 1)-1;
  78. _arr=[_in] call KRON_StrToArray;
  79. _out="";
  80. if (_len>=(count _arr)) then {
  81. _out=_in;
  82. } else {
  83. for "_i" from 0 to _len do {
  84. _out=_out + (_arr select _i);
  85. };
  86. };
  87. _out
  88. };
  89. KRON_StrLen = {
  90. private["_in","_arr","_len"];
  91. _in=_this select 0;
  92. _arr=[_in] call KRON_StrToArray;
  93. _len=count (_arr);
  94. _len
  95. };
  96. KRON_StrRight = {
  97. private["_in","_len","_arr","_i","_out"];
  98. _in=_this select 0;
  99. _len=_this select 1;
  100. _arr=[_in] call KRON_StrToArray;
  101. _out="";
  102. if (_len>(count _arr)) then {_len=count _arr};
  103. for "_i" from ((count _arr)-_len) to ((count _arr)-1) do {
  104. _out=_out + (_arr select _i);
  105. };
  106. _out
  107. };
  108. KRON_StrMid = {
  109. private["_in","_pos","_len","_arr","_i","_out"];
  110. _in=_this select 0;
  111. _pos=abs(_this select 1);
  112. _arr=[_in] call KRON_StrToArray;
  113. _len=count(_arr);
  114. if ((count _this)>2) then {_len=(_this select 2)};
  115. _out="";
  116. if ((_pos+_len)>=(count _arr)) then {_len=(count _arr)-_pos};
  117. if (_len>0) then {
  118. for "_i" from _pos to (_pos+_len-1) do {
  119. _out=_out + (_arr select _i);
  120. };
  121. };
  122. _out
  123. };
  124. KRON_StrIndex = {
  125. private["_hay","_ndl","_lh","_ln","_arr","_tmp","_i","_j","_out"];
  126. _hay=_this select 0;
  127. _ndl=_this select 1;
  128. _out=-1;
  129. _i=0;
  130. if (_hay == _ndl) exitWith {0};
  131. _lh=[_hay] call KRON_StrLen;
  132. _ln=[_ndl] call KRON_StrLen;
  133. if (_lh < _ln) exitWith {-1};
  134. _arr=[_hay] call KRON_StrToArray;
  135. for "_i" from 0 to (_lh-_ln) do {
  136. _tmp="";
  137. for "_j" from _i to (_i+_ln-1) do {
  138. _tmp=_tmp + (_arr select _j);
  139. };
  140. if (_tmp==_ndl) exitWith {_out=_i};
  141. };
  142. _out
  143. };
  144. KRON_StrInStr = {
  145. private["_out"];
  146. _in=_this select 0;
  147. _out=if (([_this select 0,_this select 1] call KRON_StrIndex)==-1) then {false} else {true};
  148. _out
  149. };
  150. KRON_Replace = {
  151. private["_str","_old","_new","_out","_tmp","_jm","_la","_lo","_ln","_i"];
  152. _str=_this select 0;
  153. _arr=toArray(_str);
  154. _la=count _arr;
  155. _old=_this select 1;
  156. _new=_this select 2;
  157. _na=[_new] call KRON_StrToArray;
  158. _lo=[_old] call KRON_StrLen;
  159. _ln=[_new] call KRON_StrLen;
  160. _out="";
  161. for "_i" from 0 to (count _arr)-1 do {
  162. _tmp="";
  163. if (_i <= _la-_lo) then {
  164. for "_j" from _i to (_i+_lo-1) do {
  165. _tmp=_tmp + toString([_arr select _j]);
  166. };
  167. };
  168. if (_tmp==_old) then {
  169. _out=_out+_new;
  170. _i=_i+_lo-1;
  171. } else {
  172. _out=_out+toString([_arr select _i]);
  173. };
  174. };
  175. _out
  176. };
  177. KRON_StrUpper = {
  178. private["_in","_out"];
  179. _in=_this select 0;
  180. _out=toUpper(_in);
  181. _out
  182. };
  183. KRON_StrLower = {
  184. private["_in","_out"];
  185. _in=_this select 0;
  186. _out=toLower(_in);
  187. _out
  188. };
  189. KRON_ArrayToUpper = {
  190. private["_in","_i","_e","_out"];
  191. _in=_this select 0;
  192. _out=[];
  193. if ((count _in)>0) then {
  194. for "_i" from 0 to (count _in)-1 do {
  195. _e=_in select _i;
  196. if (typeName _e=="STRING") then {
  197. _e=toUpper(_e);
  198. };
  199. _out set [_i,_e];
  200. };
  201. };
  202. _out
  203. };
  204. KRON_Compare = {
  205. private["_k","_n","_s","_i","_c","_t","_s1","_s2","_l1","_l2","_l"];
  206. _k=[_this,"CASE"] call KRON_findFlag;
  207. _n=0;
  208. _s=0;
  209. for "_i" from 0 to 1 do {
  210. _t=_this select _i;
  211. switch (typeName _t) do {
  212. case "SCALAR": {_n=1};
  213. case "BOOL": {_this set [_i,str(_t)]};
  214. case "SIDE": {_this set [_i,str(_t)]};
  215. case "STRING": {if !(_k) then {_this=[_this] call KRON_ArrayToUpper}};
  216. default {_n=-1};
  217. };
  218. };
  219. _s1 = _this select 0;
  220. _s2 = _this select 1;
  221. if (_n!=0) exitWith {
  222. if (_n==1) then {
  223. if (_s1<_s2) then {_s=-1} else {if (_s1>_s2) then {_s=1}};
  224. };
  225. _s
  226. };
  227. _s1 = toArray(_s1);
  228. _s2 = toArray(_s2);
  229. _l1 = count _s1;
  230. _l2 = count _s2;
  231. _l=if (_l1<_l2) then {_l1} else {_l2};
  232. for "_i" from 0 to _l-1 do {
  233. if ((_s1 select _i)<(_s2 select _i)) then {
  234. _s=-1;
  235. _i=_l;
  236. } else {
  237. if ((_s1 select _i)>(_s2 select _i)) then {
  238. _s=1;
  239. _i=_l;
  240. };
  241. };
  242. };
  243. if (_s==0) then {
  244. if (_l1<_l2) then {
  245. _s=-1;
  246. } else {
  247. if (_l1>_l2) then {_s=1};
  248. };
  249. };
  250. _s
  251. };
  252. KRON_ArraySort = {
  253. private["_a","_d","_k","_s","_i","_vo","_v1","_v2","_j","_c","_x"];
  254. _a = +(_this select 0);
  255. _d = if ([_this,"DESC"] call KRON_findFlag) then {-1} else {1};
  256. _k = if ([_this,"CASE"] call KRON_findFlag) then {"CASE"} else {"nocase"};
  257. _s = -1;
  258. if (typeName (_a select 0)=="ARRAY") then {
  259. _s=0;
  260. if (((count _this)>1) && (typeName (_this select 1)=="SCALAR")) then {
  261. _s=_this select 1;
  262. };
  263. };
  264. for "_i" from 0 to (count _a)-1 do {
  265. _vo = _a select _i;
  266. _v1 = _vo;
  267. if (_s>-1) then {_v1=_v1 select _s};
  268. _j = 0;
  269. for [{_j=_i-1},{_j>=0},{_j=_j-1}] do {
  270. _v2 = _a select _j;
  271. if (_s>-1) then {_v2=_v2 select _s};
  272. _c=[_v2,_v1,_k] call KRON_Compare;
  273. if (_c!=_d) exitWith {};
  274. _a set [_j+1,_a select _j];
  275. };
  276. _a set [_j+1,_vo];
  277. };
  278. _a
  279. };
  280. KRON_findFlag = {
  281. private["_in","_flg","_arr","_out"];
  282. _in=_this select 0;
  283. _flg=toUpper(_this select 1);
  284. _arr=[_in] call KRON_ArrayToUpper;
  285. _out=_flg in _arr;
  286. _out
  287. };
  288. KRON_getArg = {
  289. private["_in","_flg","_fl","_def","_arr","_i","_j","_as","_aa","_org","_p","_out"];
  290. _in=_this select 0;
  291. _flg=toUpper(_this select 1);
  292. _fl=[_flg] call KRON_StrLen;
  293. _out="";
  294. if ((count _this)>2) then {_out=_this select 2};
  295. _arr=[_in] call KRON_ArrayToUpper;
  296. if ((count _arr)>0) then {
  297. for "_i" from 0 to (count _in)-1 do {
  298. _as = _arr select _i;
  299. if (typeName _as=="STRING") then {
  300. _aa = [_as] call KRON_StrToArray;
  301. _p = _aa find ":";
  302. if (_p==_fl) then {
  303. if (([_as,_fl] call KRON_StrLeft)==_flg) then {
  304. _org = _in select _i;
  305. _out=[_org,_p+1] call KRON_StrMid;
  306. };
  307. };
  308. };
  309. };
  310. };
  311. _out
  312. };
  313. KRON_getArgRev = {
  314. private["_in","_flg","_fl","_def","_arr","_i","_j","_as","_aa","_org","_p","_out"];
  315. _in=_this select 0;
  316. _flg=toUpper(_this select 1);
  317. _fl=[_flg] call KRON_StrLen;
  318. _out="";
  319. if ((count _this)>2) then {_out=_this select 2};
  320. _arr=[_in] call KRON_ArrayToUpper;
  321. if ((count _arr)>0) then {
  322. for "_i" from 0 to (count _in)-1 do {
  323. _as = _arr select _i;
  324. if (typeName _as=="STRING") then {
  325. _aa = [_as] call KRON_StrToArray;
  326. _p = _aa find ":";
  327. if (_p+1==(count _aa)-_fl) then {
  328. if (([_as,_p+1] call KRON_StrMid)==_flg) then {
  329. _org = _in select _i;
  330. _out=[_org,_p] call KRON_StrLeft;
  331. };
  332. };
  333. };
  334. };
  335. };
  336. _out
  337. };