aboutsummaryrefslogtreecommitdiffhomepage
path: root/SplitSource.php
blob: a6dfb1200ce94e98b1a7be7ac719763ec9239681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php

function disjoint_set_find(&$parents, $x) {
    if ($parents[$x] !== $x) {
        return $parents[$x] = disjoint_set_find($parents, $parents[$x]);
    }
    return $x;
}

function disjoint_set_union(&$parents, $x, $y) {
    $x = disjoint_set_find($parents, $x);
    $y = disjoint_set_find($parents, $y);

    if ($x !== $y) {
        if (rand(0, 1) == 0) {
            $parents[$x] = $y;
        } else {
            $parents[$y] = $x;
        }
    }
}

function split_file($file, $chunks, $undo) {
    $cpp_name = "$file.cpp";

    echo "Processing file $cpp_name".PHP_EOL;

    $new_files = array();
    foreach (range(0, $chunks - 1) as $n) {
        $new_files[] = "$file$n.cpp";
    }

    $cmake_file = 'CMakeLists.txt';
    $cmake = file_get_contents($cmake_file);

    $cmake_cpp_name = $cpp_name;
    $cmake_new_files = $new_files;

    if ($undo) {
        foreach ($new_files as $file) {
            if (file_exists($file)) {
                echo "Unlinking ".$file.PHP_EOL;
                unlink($file);
            }
        }

        if (strpos($cmake, $cmake_cpp_name) === false) {
            $cmake = str_replace(implode(PHP_EOL.'  ', $cmake_new_files), $cmake_cpp_name, $cmake);
            file_put_contents($cmake_file, $cmake);
        }

        return;
    }

    if (strpos($cmake, $cmake_cpp_name) !== false) {
        $cmake = str_replace($cmake_cpp_name, implode(PHP_EOL.'  ', $cmake_new_files), $cmake);
        file_put_contents($cmake_file, $cmake);
    }

    if (!file_exists($cpp_name)) {
        echo "ERROR: skip nonexistent file $cpp_name".PHP_EOL;
        return;
    }

    $lines = file($cpp_name);
    $depth = 0;
    $target_depth = 1;
    $is_static = false;
    $in_define = false;
    $in_comment = false;
    $current = '';
    $common = '';
    $functions = array();
    $namespace_begin = '';
    $namespace_end = '';
    foreach ($lines as $line) {
        $add_depth = strpos($line, 'namespace ') === 0 ? 1 : (strpos($line, '}  // namespace') === 0 ? -1 : 0);
        if ($add_depth) {
            # namespace begin/end
            if ($add_depth > 0) {
              $depth += $add_depth;
            }
            if ($depth <= $target_depth) {
                if ($add_depth > 0) {
                    $namespace_begin .= $line;
                } else {
                    $namespace_end .= $line;
                }
            }
            if ($add_depth < 0) {
              $depth += $add_depth;
            }
            if ($is_static) {
                $common .= $current;
            } else {
                $functions[] = $current;
            }
            $common .= $line;
            $current = '';
            $is_static = false;
            $in_define = false;
            continue;
        }

        if (strpos($line, '#undef') === 0 && !trim($current)) {
            continue;
        }

        if ($in_comment && strpos($line, '*/') === 0) {
            $in_comment = false;
            continue;
        }
        if (strpos($line, '/*') === 0) {
            $in_comment = true;
        }
        if ($in_comment) {
            continue;
        }

        if ($depth !== $target_depth) {
            $common .= $line;
            continue;
        }

        if (strpos($line, 'static ') === 0 && $depth === $target_depth) {
            $is_static = true;
        }
        if (!trim($current) && strpos($line, '#define ') === 0) {
            $is_static = true;
            $in_define = true;
        }

        $current .= $line;
        if ((strpos($line, '}') === 0 || ($in_define && !trim($line)) || preg_match('/^[a-z].*;\s*$/i', $line)) && $depth === $target_depth) {
            # block end
            if ($is_static) {
                $common .= $current;
            } else {
                $functions[] = $current;
            }
            $current = '';
            $is_static = false;
            $in_define = false;
        }
    }
    $current = trim($current);
    if (!empty($current)) {
        fwrite(STDERR, "ERROR: $current".PHP_EOL);
        exit();
    }

    if (count($functions) < $chunks) {
        fwrite(STDERR, "ERROR: file is too small to be split more".PHP_EOL);
        return;
    }

    $deps = array();  // all functions from the same subarray must be in the same file
    $parents = array();
    foreach ($functions as $i => $f) {
        if (preg_match_all('/(?J)create_handler<(?<name>[A-Z][A-Za-z]*)>|'.
                           '(?<name>[A-Z][A-Za-z]*) (final )?: public (Td::ResultHandler|Request)|'.
                           '(CREATE_REQUEST|CREATE_NO_ARGS_REQUEST)[(](?<name>[A-Z][A-Za-z]*)|'.
                           '(?<name>complete_pending_preauthentication_requests)|'.
                           '(?<name>get_message_history_slice)|'.
                           '(Up|Down)load(?!ManagerCallback)[a-zA-Z]+C(?<name>allback)|(up|down)load_[a-z_]*_c(?<name>allback)_|'.
                           '(?<name>LogEvent)[^sA]|'.
                           '(?<name>parse)[(]|'.
                           '(?<name>store)[(]/', $f, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $name = $match['name'];
                if ($name === 'parse' || $name === 'store') {
                    $name = 'LogEvent';
                }
                $deps[$name][] = $i;
            }
        }
        $parents[$i] = $i;
    }

    foreach ($deps as $func_ids) {
        foreach ($func_ids as $func_id) {
            disjoint_set_union($parents, $func_ids[0], $func_id);
        }
    }
    $sets = array();
    $set_sizes = array();
    foreach ($functions as $i => $f) {
        $parent = disjoint_set_find($parents, $i);
        if (!isset($sets[$parent])) {
            $sets[$parent] = '';
            $set_sizes[$parent] = 0;
        }
        $sets[$parent] .= $f;
        $set_sizes[$parent] += strlen($f);
    }
    arsort($set_sizes);

    $files = array_fill(0, $chunks, '');
    $file_sizes = array_fill(0, $chunks, 0);
    foreach ($set_sizes as $parent => $size) {
        $file_id = array_search(min($file_sizes), $file_sizes);
        $files[$file_id] .= $sets[$parent];
        $file_sizes[$file_id] += $size;
    }

    foreach ($files as $n => $f) {
        $new_content = $common.$namespace_begin.$f.$namespace_end;

        $std_methods = array();
        preg_match_all('/std::[a-z_0-9]*|td::unique(?!_)/', $new_content, $std_methods);
        $std_methods = array_unique($std_methods[0]);

        $needed_std_headers = array();
        $type_headers = array(
            'std::move' => '',
            'std::vector' => '',
            'std::string' => '',
            'std::uint32_t' => '',
            'std::int32_t' => '',
            'std::int64_t' => '',
            'td::unique' => 'algorithm',
            'std::count_if' => 'algorithm',
            'std::fill' => 'algorithm',
            'std::find' => 'algorithm',
            'std::is_sorted' => 'algorithm',
            'std::lower_bound' => 'algorithm',
            'std::max' => 'algorithm',
            'std::merge' => 'algorithm',
            'std::min' => 'algorithm',
            'std::partial_sort' => 'algorithm',
            'std::partition' => 'algorithm',
            'std::remove' => 'algorithm',
            'std::reverse' => 'algorithm',
            'std::rotate' => 'algorithm',
            'std::sort' => 'algorithm',
            'std::stable_sort' => 'algorithm',
            'std::upper_bound' => 'algorithm',
            'std::abs' => 'cmath',
            'std::isfinite' => 'cmath',
            'std::function' => 'functional',
            'std::greater' => 'functional',
            'std::reference_wrapper' => 'functional',
            'std::make_move_iterator' => 'iterator',
            'std::numeric_limits' => 'limits',
            'std::map' => 'map',
            'std::multimap' => 'map',
            'std::make_shared' => 'memory',
            'std::shared_ptr' => 'memory',
            'std::multiset' => 'set',
            'std::set' => 'set',
            'std::get' => 'tuple',
            'std::make_tuple' => 'tuple',
            'std::tie' => 'tuple',
            'std::tuple' => 'tuple',
            'std::decay_t' => 'type_traits',
            'std::is_same' => 'type_traits',
            'std::unordered_map' => 'unordered_map',
            'std::unordered_set' => 'unordered_set',
            'std::make_pair' => 'utility',
            'std::pair' => 'utility',
            'std::swap' => 'utility');
        foreach ($type_headers as $type => $header) {
            if (in_array($type, $std_methods)) {
                $std_methods = array_diff($std_methods, array($type));
                if ($header && !in_array($header, $needed_std_headers)) {
                    $needed_std_headers[] = $header;
                }
            }
        }

        if (!$std_methods) { // know all needed std headers
            $new_content = preg_replace_callback(
                '/#include <([a-z_]*)>/',
                function ($matches) use ($needed_std_headers) {
                    if (in_array($matches[1], $needed_std_headers)) {
                        return $matches[0];
                    }
                    return '';
                },
                $new_content
            );
        }

        $td_methods = array(
            'AccentColorId' => 'AccentColorId',
            'account_manager[_(-](?![.]get[(][)])|AccountManager[^;>]' => 'AccountManager',
            'ActiveStoryState' => 'ActiveStoryState',
            'AffiliateType' => 'AffiliateType',
            'AgeVerificationParameters' => 'AgeVerificationParameters',
            'alarm_manager[_(-](?![.]get[(][)])|AlarmManager' => 'AlarmManager',
            'animations_manager[_(-](?![.]get[(][)])|AnimationsManager[^;>]' => 'AnimationsManager',
            'attach_menu_manager[_(-](?![.]get[(][)])|AttachMenuManager[^;>]' => 'AttachMenuManager',
            'AuctionBidLevel' => 'AuctionBidLevel',
            'audios_manager[_(-](?![.]get[(][)])|AudiosManager' => 'AudiosManager',
            'auth_manager[_(-](?![.]get[(][)])|AuthManager' => 'AuthManager',
            'AutoDownloadSettings|[a-z_]*auto_download_settings' => 'AutoDownloadSettings',
            'autosave_manager[_(-](?![.]get[(][)])|AutosaveManager' => 'AutosaveManager',
            'BackgroundId' => 'BackgroundId',
            'background_manager[_(-](?![.]get[(][)])|BackgroundManager' => 'BackgroundManager',
            'BackgroundType' => 'BackgroundType',
            'Birthdate' => 'Birthdate',
            'boost_manager[_(-](?![.]get[(][)])|BoostManager' => 'BoostManager',
            'bot_info_manager[_(-](?![.]get[(][)])|BotInfoManager' => 'BotInfoManager',
            'BotMenuButton|[a-z_]*_menu_button' => 'BotMenuButton',
            'send_bot_custom_query|answer_bot_custom_query|set_bot_updates_status' => 'BotQueries',
            'bot_recommendation_manager[_(-](?![.]get[(][)])|BotRecommendationManager' => 'BotRecommendationManager',
            'BotVerification' => 'BotVerification',
            'BotVerifierSettings' => 'BotVerifierSettings',
            'BusinessAwayMessage' => 'BusinessAwayMessage',
            'BusinessBotRights' => 'BusinessBotRights',
            'BusinessChatLink' => 'BusinessChatLink',
            'BusinessConnectedBot' => 'BusinessConnectedBot',
            'BusinessConnectionId' => 'BusinessConnectionId',
            'business_connection_manager[_(-](?![.]get[(][)])|BusinessConnectionManager' => 'BusinessConnectionManager',
            'BusinessGreetingMessage' => 'BusinessGreetingMessage',
            'BusinessInfo|business_info' => 'BusinessInfo',
            'BusinessIntro' => 'BusinessIntro',
            'business_manager[_(-](?![.]get[(][)])|BusinessManager' => 'BusinessManager',
            'BusinessRecipients' => 'BusinessRecipients',
            'BusinessWorkHours' => 'BusinessWorkHours',
            'callback_queries_manager[_(-](?![.]get[(][)])|CallbackQueriesManager' => 'CallbackQueriesManager',
            'CallId' => 'CallId',
            'call_manager[_(-](?![.]get[(][)])|CallManager' => 'CallManager',
            'ChannelId' => 'ChannelId',
            'channel_recommendation_manager[_(-](?![.]get[(][)])|ChannelRecommendationManager' => 'ChannelRecommendationManager',
            'ChatId' => 'ChatId',
            'chat_manager[_(-](?![.]get[(][)])|ChatManager([^ ;.]| [^*])' => 'ChatManager',
            'common_dialog_manager[_(-](?![.]get[(][)])|CommonDialogManager' => 'CommonDialogManager',
            'connection_state_manager[_(-](?![.]get[(][)])|ConnectionStateManager' => 'ConnectionStateManager',
            'country_info_manager[_(-](?![.]get[(][)])|CountryInfoManager' => 'CountryInfoManager',
            'CurrencyAmount' => 'CurrencyAmount',
            'CustomEmojiId' => 'CustomEmojiId',
            'device_token_manager[_(-](?![.]get[(][)])|DeviceTokenManager' => 'DeviceTokenManager',
            'DialogAction[^M]' => 'DialogAction',
            'dialog_action_manager[_(-](?![.]get[(][)])|DialogActionManager' => 'DialogActionManager',
            'DialogFilter[^A-Z]' => 'DialogFilter',
            'DialogFilterId' => 'DialogFilterId',
            'dialog_filter_manager[_(-](?![.]get[(][)])|DialogFilterManager' => 'DialogFilterManager',
            'DialogId' => 'DialogId',
            'dialog_invite_link_manager[_(-](?![.]get[(][)])|DialogInviteLinkManager' => 'DialogInviteLinkManager',
            'DialogListId' => 'DialogListId',
            'DialogLocation' => 'DialogLocation',
            'dialog_manager[_(-](?![.]get[(][)])|DialogManager' => 'DialogManager',
            'DialogParticipantFilter' => 'DialogParticipantFilter',
            'dialog_participant_manager[_(-](?![.]get[(][)])|DialogParticipantManager' => 'DialogParticipantManager',
            'DialogSource' => 'DialogSource',
            'DisallowedGiftsSettings' => 'DisallowedGiftsSettings',
            'documents_manager[_(-](?![.]get[(][)])|DocumentsManager' => 'DocumentsManager',
            'download_manager[_(-](?![.]get[(][)])|DownloadManager[^C]' => 'DownloadManager',
            'DownloadManagerCallback' => 'DownloadManagerCallback',
            'EmailVerification' => 'EmailVerification',
            'EmojiGroup' => 'EmojiGroup',
            'FactCheck' => 'FactCheck',
            'file_reference_manager[_(-](?![.]get[(][)])|FileReferenceManager|file_references[)]' => 'FileReferenceManager',
            'file_manager[_(-](?![.]get[(][)])|FileManager([^ ;.]| [^*])|update_file[)]' => 'files/FileManager',
            'FolderId' => 'FolderId',
            'ForumTopicFullId' => 'ForumTopicFullId',
            'ForumTopicId' => 'ForumTopicId',
            'forum_topic_manager[_(-](?![.]get[(][)])|ForumTopicManager' => 'ForumTopicManager',
            'game_manager[_(-](?![.]get[(][)])|GameManager' => 'GameManager',
            'G[(][)]|Global[^A-Za-z]' => 'Global',
            'GlobalPrivacySettings' => 'GlobalPrivacySettings',
            'GroupCallJoinParameters' => 'GroupCallJoinParameters',
            'GroupCallId' => 'GroupCallId',
            'group_call_manager[_(-](?![.]get[(][)])|GroupCallManager' => 'GroupCallManager',
            'GroupCallMessage[^A-Z]' => 'GroupCallMessage',
            'GroupCallMessageLimit' => 'GroupCallMessageLimit',
            'hashtag_hints[_(-](?![.]get[(][)])|HashtagHints' => 'HashtagHints',
            'inline_message_manager[_(-](?![.]get[(][)])|InlineMessageManager' => 'InlineMessageManager',
            'inline_queries_manager[_(-](?![.]get[(][)])|InlineQueriesManager' => 'InlineQueriesManager',
            'InputBusinessChatLink' => 'InputBusinessChatLink',
            'InputGroupCall' => 'InputGroupCall',
            'KeyboardButtonStyle' => 'KeyboardButtonStyle',
            'language_pack_manager[_(-]|LanguagePackManager' => 'LanguagePackManager',
            'link_manager[_(-](?![.]get[(][)])|LinkManager' => 'LinkManager',
            'LogeventIdWithGeneration|add_log_event|delete_log_event|get_erase_log_event_promise|parse_time|store_time' => 'logevent/LogEventHelper',
            'MessageCopyOptions' => 'MessageCopyOptions',
            'MessageCover' => 'MessageCover',
            'MessageEffectId' => 'MessageEffectId',
            'MessageForwardInfo|LastForwardedMessageInfo|forward_info' => 'MessageForwardInfo',
            'MessageFullId' => 'MessageFullId',
            'MessageId' => 'MessageId',
            'message_import_manager[_(-](?![.]get[(][)])|MessageImportManager' => 'MessageImportManager',
            'MessageLinkInfo' => 'MessageLinkInfo',
            'message_query_manager[_(-](?![.]get[(][)])|MessageQueryManager' => 'MessageQueryManager',
            'MessageQuote' => 'MessageQuote',
            'MessageReaction|UnreadMessageReaction|[a-z_]*message[a-z_]*reaction|reload_paid_reaction_privacy|get_chosen_tags' => 'MessageReaction',
            'MessageReactor' => 'MessageReactor',
            'MessageSearchOffset' => 'MessageSearchOffset',
            '[a-z_]*_message_sender' => 'MessageSender',
            'MessageSendOptions' => 'MessageSendOptions',
            'messages_manager[_(-](?![.]get[(][)])|MessagesManager' => 'MessagesManager',
            'MessageThreadInfo' => 'MessageThreadInfo',
            'MessageTopic' => 'MessageTopic',
            'MessageTtl' => 'MessageTtl',
            'MissingInvitee' => 'MissingInvitee',
            'notification_manager[_(-](?![.]get[(][)])|NotificationManager|notifications[)]' => 'NotificationManager',
            'notification_settings_manager[_(-](?![.]get[(][)])|NotificationSettingsManager' => 'NotificationSettingsManager',
            'online_manager[_(-](?![.]get[(][)])|OnlineManager' => 'OnlineManager',
            'option_manager[_(-](?![.]get[(][)])|OptionManager' => 'OptionManager',
            'PaidReactionType' => 'PaidReactionType',
            'Passkey[^A-Z]' => 'Passkey',
            'password_manager[_(-](?![.]get[(][)])|PasswordManager' => 'PasswordManager',
            'PeerColor[^A-Z]' => 'PeerColor',
            'PeerColorCollectible' => 'PeerColorCollectible',
            'people_nearby_manager[_(-](?![.]get[(][)])|PeopleNearbyManager' => 'PeopleNearbyManager',
            'phone_number_manager[_(-](?![.]get[(][)])|PhoneNumberManager' => 'PhoneNumberManager',
            'PhotoSizeSource' => 'PhotoSizeSource',
            'poll_manager[_(-](?![.]get[(][)])|PollManager' => 'PollManager',
            'privacy_manager[_(-](?![.]get[(][)])|PrivacyManager' => 'PrivacyManager',
            'ProfileTab' => 'ProfileTab',
            'promo_data_manager[_(-](?![.]get[(][)])|PromoDataManager' => 'PromoDataManager',
            'PublicDialogType|get_public_dialog_type' => 'PublicDialogType',
            'quick_reply_manager[_(-](?![.]get[(][)])|QuickReplyManager' => 'QuickReplyManager',
            'ReactionListType|[a-z_]*_reaction_list_type' => 'ReactionListType',
            'reaction_manager[_(-](?![.]get[(][)])|ReactionManager' => 'ReactionManager',
            'ReactionNotificationSettings' => 'ReactionNotificationSettings',
            'ReactionNotificationsFrom' => 'ReactionNotificationsFrom',
            'ReactionType|[a-z_]*_reaction_type' => 'ReactionType',
            'ReferralProgramInfo' => 'ReferralProgramInfo',
            'referral_program_manager[_(-](?![.]get[(][)])|ReferralProgramManager' => 'ReferralProgramManager',
            'ReferralProgramParameters' => 'ReferralProgramParameters',
            'RequestActor|RequestOnceActor' => 'RequestActor',
            'saved_messages_manager[_(-](?![.]get[(][)])|SavedMessagesManager' => 'SavedMessagesManager',
            'ScopeNotificationSettings|[a-z_]*_scope_notification_settings' => 'ScopeNotificationSettings',
            'SearchPostsFlood' => 'SearchPostsFlood',
            'SecretChatActor' => 'SecretChatActor',
            'secret_chats_manager[_(-]|SecretChatsManager' => 'SecretChatsManager',
            'secure_manager[_(-](?![.]get[(][)])|SecureManager' => 'SecureManager',
            'SentEmailCode' => 'SentEmailCode',
            'SharedDialog' => 'SharedDialog',
            'sponsored_message_manager[_(-](?![.]get[(][)])|SponsoredMessageManager' => 'SponsoredMessageManager',
            'StarAmount' => 'StarAmount',
            'StarGift[^A-Z]' => 'StarGift',
            'StarGiftAttribute[^IR]' => 'StarGiftAttribute',
            'StarGiftAttributeId' => 'StarGiftAttributeId',
            'StarGiftAttributeRarity' => 'StarGiftAttributeRarity',
            'StarGiftAuctionRound' => 'StarGiftAuctionRound',
            'StarGiftAuctionState' => 'StarGiftAuctionState',
            'StarGiftAuctionUserState' => 'StarGiftAuctionUserState',
            'StarGiftBackground' => 'StarGiftBackground',
            'StarGiftCollectionId' => 'StarGiftCollectionId',
            'StarGiftId' => 'StarGiftId',
            'star_gift_manager[_(-](?![.]get[(][)])|StarGiftManager' => 'StarGiftManager',
            'StarGiftResalePrice' => 'StarGiftResalePrice',
            'StarGiftSettings' => 'StarGiftSettings',
            'star_manager[_(-](?![.]get[(][)])|StarManager' => 'StarManager',
            'StarRating' => 'StarRating',
            'StarSubscription[^P]' => 'StarSubscription',
            'StarSubscriptionPricing' => 'StarSubscriptionPricing',
            'state_manager[_(-](?![.]get[(][)])|StateManager' => 'StateManager',
            'statistics_manager[_(-](?![.]get[(][)])|StatisticsManager' => 'StatisticsManager',
            'StickerSetId' => 'StickerSetId',
            'stickers_manager[_(-](?![.]get[(][)])|StickersManager' => 'StickersManager',
            'storage_manager[_(-](?![.]get[(][)])|StorageManager' => 'StorageManager',
            'StoryAlbum' => 'StoryAlbum',
            'StoryAlbumFullId' => 'StoryAlbumFullId',
            'StoryAlbumId' => 'StoryAlbumId',
            'StoryId' => 'StoryId',
            'StoryListId' => 'StoryListId',
            'story_manager[_(-](?![.]get[(][)])|StoryManager' => 'StoryManager',
            'SuggestedAction|[a-z_]*_suggested_action' => 'SuggestedAction',
            'suggested_action_manager[_(-](?![.]get[(][)])|SuggestedActionManager' => 'SuggestedActionManager',
            'SuggestedPost[^A-Z]' => 'SuggestedPost',
            'SuggestedPostPrice' => 'SuggestedPostPrice',
            'SynchronousRequests' => 'SynchronousRequests',
            'TargetDialogTypes' => 'TargetDialogTypes',
            'td_api' => 'td_api',
            'td_db[(][)]|TdDb[^A-Za-z]' => 'TdDb',
            'telegram_api' => 'telegram_api',
            'TempPasswordState' => 'TempPasswordState',
            'terms_of_service_manager[_(-](?![.]get[(][)])|TermsOfServiceManager' => 'TermsOfServiceManager',
            'theme_manager[_(-](?![.]get[(][)])|ThemeManager' => 'ThemeManager',
            'ThemeSettings' => 'ThemeSettings',
            'time_zone_manager[_(-](?![.]get[(][)])|TimeZoneManager' => 'TimeZoneManager',
            'ToDoCompletion' => 'ToDoCompletion',
            'ToDoItem' => 'ToDoItem',
            'ToDoList' => 'ToDoList',
            'TonAmount' => 'TonAmount',
            'TopDialogCategory|get_top_dialog_category' => 'TopDialogCategory',
            'top_dialog_manager[_(-](?![.]get[(][)])|TopDialogManager' => 'TopDialogManager',
            'translation_manager[_(-](?![.]get[(][)])|TranslationManager' => 'TranslationManager',
            'transcription_manager[_(-](?![.]get[(][)])|TranscriptionManager' => 'TranscriptionManager',
            'updates_manager[_(-](?![.]get[(][)])|UpdatesManager|get_difference[)]|updateSentMessage|dummyUpdate' => 'UpdatesManager',
            'UserId' => 'UserId',
            'user_manager[_(-](?![.]get[(][)])|UserManager([^ ;.]| [^*])' => 'UserManager',
            'UserStarGift' => 'UserStarGift',
            'video_notes_manager[_(-](?![.]get[(][)])|VideoNotesManager' => 'VideoNotesManager',
            'videos_manager[_(-](?![.]get[(][)])|VideosManager' => 'VideosManager',
            'voice_notes_manager[_(-](?![.]get[(][)])|VoiceNotesManager' => 'VoiceNotesManager',
            'web_app_manager[_(-](?![.]get[(][)])|WebAppManager' => 'WebAppManager',
            'WebAppOpenParameters' => 'WebAppOpenParameters',
            'WebPageId(Hash)?' => 'WebPageId',
            'web_pages_manager[_(-](?![.]get[(][)])|WebPagesManager' => 'WebPagesManager');

        foreach ($td_methods as $pattern => $header) {
            if (strpos($cpp_name, $header) !== false) {
                continue;
            }

            $include_name = '#include "td/telegram/'.$header.'.h"';
            if (strpos($new_content, $include_name) !== false && preg_match('/[^a-zA-Z0-9_]('.$pattern.')/', str_replace($include_name, '', $new_content)) === 0) {
                $new_content = str_replace($include_name, '', $new_content);
            }
        }

        if (!file_exists($new_files[$n]) || file_get_contents($new_files[$n]) !== $new_content) {
            echo "Writing file ".$new_files[$n].PHP_EOL;
            file_put_contents($new_files[$n], $new_content);
        }
    }
}

if (in_array('--help', $argv) || in_array('-h', $argv)) {
    echo "Usage: php SplitSource.php [OPTION]...\n".
         "Splits some source files to reduce a maximum amount of RAM needed for compiling a single file.\n".
         "  -u, --undo Undo all source code changes.\n".
         "  -h, --help Show this help.\n";
    exit(2);
}

$undo = in_array('--undo', $argv) || in_array('-u', $argv);
$files = array('td/telegram/ChatManager' => 10,
               'td/telegram/MessagesManager' => 50,
               'td/telegram/NotificationManager' => 10,
               'td/telegram/Requests' => 50,
               'td/telegram/StickersManager' => 10,
               'td/telegram/StoryManager' => 10,
               'td/telegram/UpdatesManager' => 10,
               'td/telegram/UserManager' => 10);

foreach ($files as $file => $chunks) {
    split_file($file, $chunks, $undo);
}