Skip to content

Commit 27b4c68

Browse files
committed
add array_is_subset and file_jsonc_parse
1 parent 806fe8b commit 27b4c68

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

current-scripts/Demos/useful-scripts/scripts/data_structures/data_structures.gml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,87 @@ function grids_are_equal(_grid1, _grid2) {
1212
/// @param {Array} array
1313
function choose_from_array(_array) {
1414
return _array[irandom(array_length(_array)-1)];
15+
}
16+
17+
/// @func array_is_subset(array)
18+
/// @desc Checks if Array a is a subset of array b
19+
/// @param {Array} a
20+
/// @param {Array} b
21+
function array_is_subset(_a, _b) {
22+
var _a_len = array_length(_a);
23+
var _b_len = array_length(_b);
24+
if (_a_len > _b_len) return false;
25+
26+
var _match = false;
27+
28+
for (var i=0; i<_a_len; i++) {
29+
_match = false;
30+
for (var j=0; j<_b_len; j++) {
31+
if (_a[i] == _b[i]) {
32+
_match = true;
33+
break;
34+
}
35+
}
36+
37+
if (!_match) return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
/// @func file_jsonc_parse(filename)
44+
/// @desc Parses a JSON with Comments from file, stripping out the comments
45+
/// @param {Array} filename
46+
function file_jsonc_parse(_filename) {
47+
var _jsonc_file = file_text_open_read(_filename);
48+
if (_jsonc_file == -1) return {};
49+
50+
var _json_str = "";
51+
var _is_in_multiline_comment = false;
52+
53+
while (!file_text_eof(_jsonc_file)) {
54+
var _line = file_text_readln(_jsonc_file);
55+
56+
// Strip comments
57+
if (!_is_in_multiline_comment) {
58+
var _single_line_comment_pos = string_pos("\/\/", _line);
59+
var _multi_line_comment_start_pos = string_pos("\/*", _line);
60+
var _new_line = _line;
61+
62+
if (_single_line_comment_pos != 0
63+
&& (_multi_line_comment_start_pos == 0 || _single_line_comment_pos < _multi_line_comment_start_pos)) {
64+
// Single-line comment
65+
_new_line = string_copy(_line, 1, _single_line_comment_pos-1);
66+
} else if (_multi_line_comment_start_pos != 0) {
67+
// Start of multi-line comment
68+
_new_line = string_copy(_line, 1, _multi_line_comment_start_pos-1);
69+
70+
// Does it end on the same line?
71+
var _multi_line_comment_end_pos = string_pos("*\/", _line);
72+
if (_multi_line_comment_end_pos != 0 && _multi_line_comment_end_pos > _multi_line_comment_start_pos) {
73+
var _str_len = string_length(_line);
74+
var _length_left = _str_len - (_multi_line_comment_end_pos+1);
75+
_new_line += string_copy(_line, _multi_line_comment_end_pos+2, _length_left);
76+
} else {
77+
_is_in_multiline_comment = true;
78+
}
79+
}
80+
81+
_json_str += _new_line;
82+
} else {
83+
var _multi_line_comment_end_pos = string_pos("*\/", _line);
84+
85+
if (_multi_line_comment_end_pos != 0) {
86+
var _str_len = string_length(_line);
87+
var _length_left = _str_len - (_multi_line_comment_end_pos+1);
88+
var _new_line = string_copy(_line, _multi_line_comment_end_pos+2, _length_left);
89+
_is_in_multiline_comment = false;
90+
_json_str += _new_line;
91+
}
92+
}
93+
}
94+
95+
file_text_close(_jsonc_file);
96+
97+
return json_parse(_json_str);
1598
}

current-scripts/Demos/useful-scripts/useful-scripts.yyp

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)