forked from dflook/python-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathministring.py
More file actions
175 lines (135 loc) · 4.13 KB
/
ministring.py
File metadata and controls
175 lines (135 loc) · 4.13 KB
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
BACKSLASH = '\\'
class MiniString(object):
"""
Create a representation of a string object
:param str string: The string to minify
"""
def __init__(self, string, quote="'"):
self._s = string
self.safe_mode = False
self.quote = quote
def __str__(self):
"""
The smallest python literal representation of a string
:rtype: str
"""
if self._s == '':
return ''
if len(self.quote) == 1:
s = self.to_short()
else:
s = self.to_long()
try:
eval(self.quote + s + self.quote)
except UnicodeDecodeError:
if self._safe_mode:
raise
self._safe_mode = True
assert eval(self.quote + s + self.quote) == self._s
return s
def to_short(self):
s = ''
escaped = {
'\n': BACKSLASH + 'n',
'\\': BACKSLASH + BACKSLASH,
'\a': BACKSLASH + 'a',
'\b': BACKSLASH + 'b',
'\f': BACKSLASH + 'f',
'\r': BACKSLASH + 'r',
'\t': BACKSLASH + 't',
'\v': BACKSLASH + 'v',
'\0': BACKSLASH + 'x00',
self.quote: BACKSLASH + self.quote,
}
for c in self._s:
if c in escaped.keys():
s += escaped[c]
else:
if self.safe_mode:
unicode_value = ord(c)
if unicode_value <= 0x7F:
s += c
elif unicode_value <= 0xFFFF:
s += BACKSLASH + 'u' + format(unicode_value, '04x')
else:
s += BACKSLASH + 'U' + format(unicode_value, '08x')
else:
s += c
return s
def to_long(self):
s = ''
escaped = {
'\\': BACKSLASH + BACKSLASH,
'\a': BACKSLASH + 'a',
'\b': BACKSLASH + 'b',
'\f': BACKSLASH + 'f',
'\r': BACKSLASH + 'r',
'\t': BACKSLASH + 't',
'\v': BACKSLASH + 'v',
'\0': BACKSLASH + 'x00',
self.quote[0]: BACKSLASH + self.quote[0],
}
for c in self._s:
if c in escaped.keys():
s += escaped[c]
else:
if self.safe_mode:
unicode_value = ord(c)
if unicode_value <= 0x7F:
s += c
elif unicode_value <= 0xFFFF:
s += BACKSLASH + 'u' + format(unicode_value, '04x')
else:
s += BACKSLASH + 'U' + format(unicode_value, '08x')
else:
s += c
return s
class MiniBytes(object):
"""
Create a representation of a bytes object
:param bytes string: The string to minify
"""
def __init__(self, string, quote="'"):
self._b = string
self.quote = quote
def __str__(self):
"""
The smallest python literal representation of a string
:rtype: str
"""
if self._b == b'':
return ''
if len(self.quote) == 1:
s = self.to_short()
else:
s = self.to_long()
assert eval('b' + self.quote + s + self.quote) == self._b
return s
def to_short(self):
b = ''
for c in self._b:
if c == b'\\':
b += BACKSLASH
elif c == b'\n':
b += BACKSLASH + 'n'
elif c == self.quote:
b += BACKSLASH + self.quote
else:
if c >= 128:
b += BACKSLASH + chr(c)
else:
b += chr(c)
return b
def to_long(self):
b = ''
for c in self._b:
if c == b'\\':
b += BACKSLASH
elif c == self.quote:
b += BACKSLASH + self.quote
else:
if c >= 128:
b += BACKSLASH + chr(c)
else:
b += chr(c)
return b