Skip to content

Commit 2dca726

Browse files
committed
Cleanup formatting of CBA_fnc_parseJSON
1 parent 8148521 commit 2dca726

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

addons/hashes/fnc_parseJSON.sqf

+56-56
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ private _createObject = {
4242
switch (_objectType) do {
4343
case false;
4444
case 0: {
45-
[] call CBA_fnc_createNamespace;
45+
[] call CBA_fnc_createNamespace
4646
};
4747

4848
case true;
4949
case 1: {
50-
[] call CBA_fnc_hashCreate;
50+
[] call CBA_fnc_hashCreate
5151
};
5252

5353
case 2: {
54-
createHashMap;
54+
createHashMap
5555
};
5656
};
5757
};
@@ -88,15 +88,15 @@ private _objectSet = {
8888
private _tokenize = {
8989
params [["_jsonStr", objNull, [""]]];
9090

91-
if (!(_jsonStr isEqualType "")) exitWith {
92-
objNull;
91+
if !(_jsonStr isEqualType "") exitWith {
92+
objNull
9393
};
9494

9595
private _tokens = [];
9696

9797
private _symbolsArr = "{}[]:," splitString "";
9898
private _numberArr = "-0123456789" splitString "";
99-
private _whitespaceArr = [0x9, 0xA, 0xD, 0x20] apply { toString [_x] };
99+
private _whitespaceChars = toString [0x9, 0xA, 0xD, 0x20];
100100

101101
private _constRegex = "true|false|null/o";
102102
private _numberRegex = "(-?(?:(?:[1-9]\d*)|\d)(?:\.\d+)?(?:[eE][+-]?\d+)?).*/o";
@@ -123,12 +123,12 @@ private _tokenize = {
123123
private _rowIndex = 0;
124124
private _columnIndex = 0;
125125

126-
while {(count _codePoints > 0)} do {
126+
while {count _codePoints > 0} do {
127127
// Match against constants
128128
private _rmatch = flatten (((_codePoints select [0, 5]) joinString "") regexFind [_constRegex]);
129129
if (count _rmatch > 0) then {
130130
// Ensure match starts at beginning
131-
if ((_rmatch # 1) == 0) then {
131+
if (_rmatch # 1 == 0) then {
132132
switch (_rmatch # 0) do {
133133
case "true": {
134134
_tokens pushBack [true, [_rowIndex, _columnIndex]];
@@ -158,7 +158,7 @@ private _tokenize = {
158158
private _cp = _codePoints # 0;
159159

160160
// Match against whitespace
161-
if (_cp in _whitespaceArr) then {
161+
if (_cp in _whitespaceChars) then {
162162
// Omit whitespace from tokenized output
163163
_codePoints deleteAt 0;
164164
_columnIndex = _columnIndex + 1;
@@ -195,18 +195,18 @@ private _tokenize = {
195195
};
196196

197197
private _rmatch = ((_numberSym + _ncps) regexFind [_numberRegex, 0]);
198-
if ((count _rmatch) == 0) then {
199-
throw (format ["invalid number token at %1:%2", _rowIndex + 1, _columnIndex + 1]);
198+
if (count _rmatch == 0) then {
199+
throw format ["invalid number token at %1:%2", _rowIndex + 1, _columnIndex + 1];
200200
};
201201

202202
_rmatch = _rmatch # 0 # 1;
203203
if (_rmatch # 1 != 0) then {
204204
// Regex match must be at the beginning to denote a valid number
205-
throw (format ["invalid number token at %1:%2", _rowIndex + 1, _columnIndex + 1]);
205+
throw format ["invalid number token at %1:%2", _rowIndex + 1, _columnIndex + 1];
206206
};
207207

208208
private _nextNumberSym = _rmatch # 0;
209-
private _newCodePointCount = (count _nextNumberSym) - _numberExtent;
209+
private _newCodePointCount = count _nextNumberSym - _numberExtent;
210210

211211
if (_newCodePointCount == 0) then {
212212
// We're at the end of our number since we haven't made any forward progress
@@ -219,7 +219,7 @@ private _tokenize = {
219219

220220
// Reject case where the match is a single minus sign
221221
if (_numberSym == "-") then {
222-
throw (format ["invalid number token at %1:%2", _rowIndex + 1, _columnIndex + 1]);
222+
throw format ["invalid number token at %1:%2", _rowIndex + 1, _columnIndex + 1];
223223
};
224224

225225
_tokens pushBack [parseNumber _numberSym, [_rowIndex, _columnIndex]];
@@ -240,14 +240,14 @@ private _tokenize = {
240240

241241
if (isNil "_ncp") then {
242242
// End of input reached before string terminated
243-
throw (format ["unterminated string token at %1:%2", _rowIndex + 1, _columnIndex + 1]);
243+
throw format ["unterminated string token at %1:%2", _rowIndex + 1, _columnIndex + 1];
244244
};
245245

246246
_strExtent = _strExtent + 1;
247247

248248
if (_currentlyEscaping) then {
249249
if (!(_ncp in _strEscapeMap) && _ncp != "u") then {
250-
throw (format ["invalid string escape char '%1' at %2:%3", _ncp, _rowIndex + 1, _columnIndex + _strExtent]);
250+
throw format ["invalid string escape char '%1' at %2:%3", _ncp, _rowIndex + 1, _columnIndex + _strExtent];
251251
};
252252

253253
if (_ncp in _strEscapeMap) then {
@@ -256,15 +256,15 @@ private _tokenize = {
256256
private _hexStr = _codePoints select [_strExtent, 4];
257257
_strExtent = _strExtent + 4;
258258

259-
if (!((_hexStr joinString "") regexMatch _hexStrRegex)) then {
259+
if !((_hexStr joinString "") regexMatch _hexStrRegex) then {
260260
// Invalid hex string
261-
throw (format ["invalid hex string '%1' at %2:%3", _hexStr, _rowIndex + 1, _columnIndex + 1]);
261+
throw format ["invalid hex string '%1' at %2:%3", _hexStr, _rowIndex + 1, _columnIndex + 1];
262262
};
263263

264264
private _cp = 0;
265265
{
266266
private _digit = _strHexMap get (toLower _x);
267-
_cp = _cp + (16 ^ ((count _hexStr) - _forEachIndex - 1)) * _digit;
267+
_cp = _cp + (16 ^ (count _hexStr - _forEachIndex - 1)) * _digit;
268268
} forEach _hexStr;
269269

270270
_strSym = _strSym + (toString [_cp]);
@@ -285,7 +285,7 @@ private _tokenize = {
285285

286286
private _ncpValue = (toArray _ncp) # 0;
287287
if (_ncpValue < 0x20 || _ncpValue > 0x10FFFF) then {
288-
throw (format ["invalid string code point at %1:%2", _rowIndex + 1, _columnIndex + _strExtent]);
288+
throw format ["invalid string code point at %1:%2", _rowIndex + 1, _columnIndex + _strExtent];
289289
};
290290

291291
_strSym = _strSym + _ncp;
@@ -297,11 +297,11 @@ private _tokenize = {
297297
continue;
298298
};
299299

300-
throw (format ["unexpected token at %1:%2", _rowIndex + 1, _columnIndex + 1]);
300+
throw format ["unexpected token at %1:%2", _rowIndex + 1, _columnIndex + 1];
301301
};
302302

303303
// Return parsed tokens
304-
_tokens;
304+
_tokens
305305
};
306306

307307
/**
@@ -317,9 +317,9 @@ private _shift = {
317317
if (count _tokens > 0) then {
318318
// Append token reduction state on to end of each token
319319
_parseStack pushBack ((_tokens deleteAt 0) + [false]);
320-
true;
320+
true
321321
} else {
322-
false;
322+
false
323323
};
324324
};
325325

@@ -336,14 +336,14 @@ private _reduce = {
336336
params [["_parseStack", [], [[]]], ["_reducerState", createHashMap, [createHashMap]]];
337337

338338
if (count _parseStack == 0) exitWith {
339-
false;
339+
false
340340
};
341341

342342
// Initialize reducer state if not already initialized
343-
if (!("STACK_{" in _reducerState)) then {
343+
if !("STACK_{" in _reducerState) then {
344344
_reducerState set ["STACK_{", []];
345345
};
346-
if (!("STACK_[" in _reducerState)) then {
346+
if !("STACK_[" in _reducerState) then {
347347
_reducerState set ["STACK_[", []];
348348
};
349349

@@ -354,19 +354,19 @@ private _reduce = {
354354

355355
// Handle terminal symbols
356356
switch (true) do {
357-
case ((_topToken # 0) isEqualType 0): {
357+
case (_topToken # 0 isEqualType 0): {
358358
_topToken set [2, true];
359359
};
360360

361-
case ((_topToken # 0) isEqualType true): {
361+
case (_topToken # 0 isEqualType true): {
362362
_topToken set [2, true];
363363
};
364364

365-
case ((_topToken # 0) isEqualTo objNull): {
365+
case (_topToken # 0 isEqualTo objNull): {
366366
_topToken set [2, true];
367367
};
368368

369-
case (((_topToken # 0) select [0, 1]) == "$"): {
369+
case ((_topToken # 0 select [0, 1]) == "$"): {
370370
_topToken set [0, (_topToken # 0) select [1]];
371371
_topToken set [2, true];
372372
};
@@ -386,11 +386,11 @@ private _reduce = {
386386

387387
case "}": {
388388
if (count _objOpenStack == 0) then {
389-
throw (format (["invalid '}' token at %1:%2"] + ((_topToken # 1) apply { _x + 1 })));
389+
throw format (["invalid '}' token at %1:%2"] + ((_topToken # 1) apply { _x + 1 }));
390390
};
391391

392392
private _objStart = _objOpenStack deleteAt (count _objOpenStack - 1);
393-
private _objLen = (count _parseStack) - _objStart;
393+
private _objLen = count _parseStack - _objStart;
394394

395395
private _obj = [] call _createObject;
396396
private _objEmpty = true;
@@ -402,26 +402,26 @@ private _reduce = {
402402
{
403403
switch (_nextTokenType) do {
404404
case 0: {
405-
if (!(_x # 2)) then {
406-
throw (format (["invalid '%1' token at %2:%3", _x # 0] + ((_x # 1) apply { _x + 1 })));
405+
if !(_x # 2) then {
406+
throw format (["invalid '%1' token at %2:%3", _x # 0] + ((_x # 1) apply { _x + 1 }));
407407
};
408408

409-
if (!(_x # 0 isEqualType "")) then {
410-
throw (format (["invalid key token at %1:%2"] + ((_x # 1) apply { _x + 1 })));
409+
if !(_x # 0 isEqualType "") then {
410+
throw format (["invalid key token at %1:%2"] + ((_x # 1) apply { _x + 1 }));
411411
};
412412

413413
_currentKey = _x # 0;
414414
};
415415

416416
case 1: {
417417
if (_x # 2 || _x # 0 != ":") then {
418-
throw (format (["missing colon token at %1:%2"] + ((_x # 1) apply { _x + 1 })));
418+
throw format (["missing colon token at %1:%2"] + ((_x # 1) apply { _x + 1 }));
419419
};
420420
};
421421

422422
case 2: {
423-
if (!(_x # 2)) then {
424-
throw (format (["invalid '%1' token at %2:%3", _x # 0] + ((_x # 1) apply { _x + 1 })));
423+
if !(_x # 2) then {
424+
throw format (["invalid '%1' token at %2:%3", _x # 0] + ((_x # 1) apply { _x + 1 }));
425425
};
426426

427427
[_obj, _currentKey, _x # 0] call _objectSet;
@@ -430,7 +430,7 @@ private _reduce = {
430430

431431
case 3: {
432432
if (_x # 2 || _x # 0 != ",") then {
433-
throw (format (["missing comma token at %1:%2"] + ((_x # 1) apply { _x + 1 })));
433+
throw format (["missing comma token at %1:%2"] + ((_x # 1) apply { _x + 1 }));
434434
};
435435
};
436436
};
@@ -441,16 +441,16 @@ private _reduce = {
441441
// Validate object definition state machine is in correct final state
442442
if (_objEmpty) then {
443443
if (_nextTokenType != 0) then {
444-
throw (format (["incomplete object definition at %1:%2"] + ((_topToken # 1) apply { _x + 1 })));
444+
throw format (["incomplete object definition at %1:%2"] + ((_topToken # 1) apply { _x + 1 }));
445445
};
446446
} else {
447447
if (_nextTokenType == 0) then {
448448
private _commaToken = _parseStack select -2;
449-
throw (format (["extraneous comma at %1:%2"] + ((_commaToken # 1) apply { _x + 1 })));
449+
throw format (["extraneous comma at %1:%2"] + ((_commaToken # 1) apply { _x + 1 }));
450450
};
451451

452452
if (_nextTokenType != 3) then {
453-
throw (format (["incomplete object definition at %1:%2"] + ((_topToken # 1) apply { _x + 1 })));
453+
throw format (["incomplete object definition at %1:%2"] + ((_topToken # 1) apply { _x + 1 }));
454454
}
455455
};
456456

@@ -463,26 +463,26 @@ private _reduce = {
463463

464464
case "]": {
465465
if (count _arrOpenStack == 0) then {
466-
throw (format (["invalid ']' token at %1:%2"] + ((_topToken # 1) apply { _x + 1 })));
466+
throw format (["invalid ']' token at %1:%2"] + ((_topToken # 1) apply { _x + 1 }));
467467
};
468468

469469
private _arrStart = _arrOpenStack deleteAt (count _arrOpenStack - 1);
470-
private _arrLen = (count _parseStack) - _arrStart;
470+
private _arrLen = count _parseStack - _arrStart;
471471

472472
private _arr = [];
473473
private _nextTokenItem = true;
474474

475475
{
476476
if (_nextTokenItem) then {
477-
if (!(_x # 2)) then {
478-
throw (format (["invalid '%1' token at %2:%3", _x # 0] + ((_x # 1) apply { _x + 1 })));
477+
if !(_x # 2) then {
478+
throw format (["invalid '%1' token at %2:%3", _x # 0] + ((_x # 1) apply { _x + 1 }));
479479
};
480480

481481
_arr pushBack (_x # 0);
482482
_nextTokenItem = false;
483483
} else {
484484
if (_x # 2 || _x # 0 != ",") then {
485-
throw (format (["missing comma at %1:%2"] + ((_x # 1) apply { _x + 1 })));
485+
throw format (["missing comma at %1:%2"] + ((_x # 1) apply { _x + 1 }));
486486
};
487487

488488
_nextTokenItem = true;
@@ -491,7 +491,7 @@ private _reduce = {
491491

492492
if (_nextTokenItem && count _arr > 0) then {
493493
private _commaToken = _parseStack select -2;
494-
throw (format (["extraneous comma at %1:%2"] + ((_commaToken # 1) apply { _x + 1 })));
494+
throw format (["extraneous comma at %1:%2"] + ((_commaToken # 1) apply { _x + 1 }));
495495
};
496496

497497
private _arrToken = _parseStack # _arrStart;
@@ -512,8 +512,8 @@ private _reduce = {
512512
private _parse = {
513513
params [["_jsonStr", objNull, [""]]];
514514

515-
if (!(_jsonStr isEqualType "")) exitWith {
516-
objNull;
515+
if !(_jsonStr isEqualType "") exitWith {
516+
objNull
517517
};
518518

519519
private _tokens = [_jsonStr] call _tokenize;
@@ -526,7 +526,7 @@ private _parse = {
526526
private _reducerState = createHashMap;
527527

528528
while {true} do {
529-
if (!([_parseStack, _tokens] call _shift)) then {
529+
if !([_parseStack, _tokens] call _shift) then {
530530
break;
531531
};
532532

@@ -535,16 +535,16 @@ private _parse = {
535535

536536
if (count _parseStack > 1) then {
537537
private _extraneousToken = _parseStack # 1;
538-
throw (format (["extraneous '%1' token at %2:%3", _extraneousToken # 0] + ((_extraneousToken # 1) apply { _x + 1 })));
538+
throw format (["extraneous '%1' token at %2:%3", _extraneousToken # 0] + ((_extraneousToken # 1) apply { _x + 1 }));
539539
};
540540

541541
// Extract and return parsed object
542-
_parseStack # 0 # 0;
542+
_parseStack # 0 # 0
543543
};
544544

545545
private _jsonValue = [_json] call _parse;
546546

547547
// Reset unicode processing
548548
forceUnicode -1;
549549

550-
_jsonValue;
550+
_jsonValue

0 commit comments

Comments
 (0)