Last active
August 29, 2015 14:00
-
-
Save LordAro/11284445 to your computer and use it in GitHub Desktop.
PHP array(...) to [...]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import mmap | |
def deleteFromMmap(f, mmapfile, start, end): | |
length = end - start | |
size = len(mmapfile) | |
newsize = size - length | |
mmapfile.move(start, end, size - end) | |
mmapfile.flush() | |
mmapfile.close() | |
f.truncate(newsize) | |
return mmap.mmap(f.fileno(), 0) | |
def insertIntoMmap(f, mmapfile, offset, data): | |
length = len(data) | |
size = len(mmapfile) | |
newsize = size + length | |
mmapfile.flush() | |
mmapfile.close() | |
f.seek(size) | |
f.write(data) | |
f.flush() | |
mmapfile = mmap.mmap(f.fileno(), 0) | |
mmapfile.move(offset + length, offset, size - offset) | |
mmapfile.seek(offset) | |
mmapfile.write(bytes(data, "UTF-8")) | |
mmapfile.flush() | |
return mmapfile | |
def convert(path): | |
print(path) | |
size = os.path.getsize(path) | |
with open(path, "r+") as f: | |
data = mmap.mmap(f.fileno(), 0) | |
orig_index = 0 | |
comment_str = "" | |
while orig_index + 6 < size: | |
# Skip strings entirely | |
if (chr(data[orig_index]) == "'" or chr(data[orig_index]) == "\"") and chr(data[orig_index - 1]) != "\\" and not comment_str: | |
char = data[orig_index] | |
orig_index += 1 | |
while data[orig_index] != char or chr(data[orig_index - 1]) == "\\": | |
orig_index += 1 | |
orig_index += 1 | |
# Comments | |
comment_substr = data[orig_index:orig_index + 2] | |
if not comment_str and (comment_substr == bytes("//", "UTF-8") or comment_substr == bytes("/*", "UTF-8")): | |
comment_str = comment_substr | |
elif comment_str == bytes("//", "UTF-8") and chr(data[orig_index]) == "\n": | |
comment_str = "" | |
elif comment_str == bytes("/*", "UTF-8") and comment_substr == bytes("*/", "UTF-8"): | |
comment_str = "" | |
substr = data[orig_index:orig_index + 6] | |
if substr != bytes("array(", "UTF-8"): | |
orig_index += 1 | |
continue | |
# Only match array() func, not others | |
if chr(data[orig_index - 1]) not in [' ', '\n', '.', '(', '[']: | |
orig_index += 1 | |
continue | |
# Closing ')' is at least 6 characters after the start of array | |
index = orig_index + 6 | |
# Match braces | |
open_br = 1 | |
while index < size: | |
if chr(data[index]) == ')': | |
if open_br == 1: | |
break | |
else: | |
open_br -= 1 | |
elif chr(data[index]) == '(': | |
open_br += 1 | |
index += 1 | |
print("Found at", orig_index, "Ends at", index) | |
data = deleteFromMmap(f, data, orig_index, orig_index + 6) | |
data = insertIntoMmap(f, data, orig_index, "[") | |
data = deleteFromMmap(f, data, index - 6 + 1, index - 6 + 2) | |
data = insertIntoMmap(f, data, index - 6 + 1, "]") | |
size -= 5 | |
orig_index += 1 | |
for root, dirs, files in os.walk(os.getcwd()): | |
if "vendor" in root: | |
continue | |
for f in files: | |
if f.endswith('.php'): | |
path = root + '/' + f | |
convert(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment