MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / parsePhpArray

Function parsePhpArray

packages/migrate/scripts/jnoj.ts:15–51  ·  view source on GitHub ↗
(serialized: string)

Source from the content-addressed store, hash-verified

13});
14
15function parsePhpArray(serialized: string) {
16 const initial = serialized;
17 const readString = (length: number) => {
18 const buf = new TextEncoder().encode(serialized).slice(0, length);
19 const result = new TextDecoder().decode(buf);
20 serialized = serialized.slice(result.length);
21 return result;
22 };
23 const readInt = () => {
24 let val = 0;
25 if (!'0123456789'.includes(serialized[0])) throw new Error(`Expected number, got "${serialized[0]}", initial "${initial}"`);
26 while ('0123456789'.includes(serialized[0])) {
27 val = val * 10 + +serialized[0];
28 serialized = serialized.slice(1);
29 }
30 return val;
31 };
32 const readToken = (token: string) => {
33 if (serialized.startsWith(token)) serialized = serialized.slice(token.length);
34 else throw new Error(`Expected token "${token}", got "${serialized.slice(0, token.length)}", initial "${initial}"`);
35 };
36 readToken('a:');
37 const size = readInt();
38 readToken(':{');
39 const result = [...Array.from({ length: size }, () => '')];
40 for (let i = 0; i < size; i++) {
41 readToken('i:');
42 const key = readInt();
43 readToken(';s:');
44 const length = readInt();
45 readToken(':"');
46 const value = readString(length);
47 result[key] = value;
48 readToken('";');
49 }
50 return result;
51}
52
53async function addContestFile(domainId: string, tid: ObjectId, filename: string, filepath: string) {
54 const tdoc = await ContestModel.get(domainId, tid);

Callers 1

runFunction · 0.85

Calls 3

readTokenFunction · 0.85
readIntFunction · 0.85
readStringFunction · 0.85

Tested by

no test coverage detected