--- es7.py- 2016-09-01 03:00:00.000000000 +0900 +++ es7.py 2016-09-01 03:30:00.000000000 +0900 @@ -7,12 +7,14 @@ def enc(idx): def encode(s, tbl): f = lambda t, s1: t.replace( s1, enc( tbl.index(s1) ) ) - return reduce(f, tbl, s) + kf = lambda s1: len(s1) + stbl = sorted(tbl, key=kf, reverse=True) + return reduce(f, stbl, s) def decode(s, tbl, add_spc=False): add = ' ' if add_spc else '' - f = lambda t, s1: t.replace( enc( tbl.index(s1) ), add + s1 + add) - return reduce(f, tbl, s) + f = lambda t, idx: t.replace( enc(idx), add + tbl[idx] + add ) + return reduce(f, range( len(tbl) ), s) def fidx(s, lst): idxs = [ s.index(t) for t in lst if t in s ] @@ -43,11 +45,14 @@ def div_str_cut_comment(s, tbl): (k, sta, end) = (None, None, None) (k, sta, end) = next( ( e for e in targs if s.startswith( e[1] ) ), (k, sta, end) ) (_, j) = idxs(s, sta, end) - r = [ [ 'str', decode( s[:j], tbl ) ] ] if k == 'str' else [] + + d = { '\\"' : '"', '\\n' : '\n', '\\t' : '\t', '\\\\' : '\\' } + tbl_dec_str = [ d.get(s1) if s1 in d else s1 for s1 in tbl ] + r = [ [ 'str', decode( s[:j], tbl_dec_str ) ] ] if k == 'str' else [] return r + div_str_cut_comment( s[j:], tbl ) def es_split(s): - tbl = [ '/*', '*/', '(', ')', ';', ',', '*', '"', ' ', '\t', '\n' ] + tbl = [ '/*', '*/', '(', ')', ';', ',', '*', '"', ' ', '\t', '\n', '\\"', '\\n', '\\t' '\\\\' ] s = encode(s, tbl) print('encode:\n{}\n'.format(s))