from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import re
import tokenize


double_quote_starts = tuple(s for s in tokenize.single_quoted if '"' in s)
compiled_tokenize_string = re.compile(tokenize.String)


def handle_match(m):
    string = m.group(0)

    for double_quote_start in double_quote_starts:
        if string.startswith(double_quote_start):
            meat = string[len(double_quote_start):-1]
            if '"' in meat or "'" in meat:
                break
            return (
                double_quote_start.replace('"', "'") +
                string[len(double_quote_start):-1] +
                "'"
            )
    return string


def fix_strings(filename):
    return_value = 0

    lines = []

    with open(filename, 'r') as read_handle:
        for line in read_handle:
            if '"""' in line:
                # Docstrings are hard, fuck it
                lines.append(line)
            else:
                result = re.sub(compiled_tokenize_string, handle_match, line)
                lines.append(result)
                return_value |= int(result != line)

    with open(filename, 'w') as write_handle:
        for line in lines:
            write_handle.write(line)

    return return_value


def main(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('filenames', nargs='*', help='Filenames to fix')
    args = parser.parse_args(argv)

    retv = 0

    for filename in args.filenames:
        return_value = fix_strings(filename)
        if return_value != 0:
            print('Fixing strings in {0}'.format(filename))
        retv |= return_value

    return retv

@@ -268,16 +268,27 @@
 compile(
+'(?%3C!%22)' + 
 tokenize
@@ -294,16 +294,26 @@
 e.String
+ + '(?!%22)'
 )%0A%0A%0Adef 
@@ -597,26 +597,8 @@
 urn 
-(%0A                
 doub
@@ -635,421 +635,123 @@
 %22) +
-%0A                string%5Blen(double_quote_start):-1%5D +%0A                %22'%22%0A            )%0A    return string%0A%0A%0Adef fix_strings(filename):%0A    return_value = 0%0A%0A    lines = %5B%5D%0A%0A    with open(filename, 'r') as read_handle:%0A        for line in read_handle:%0A            if '%22%22%22' in line:%0A                # Docstrings are hard, fuck it%0A                lines.append(line)%0A            else:%0A                result = re.sub(
+ meat + %22'%22%0A    return string%0A%0A%0Adef fix_strings(filename):%0A    contents = open(filename).read()%0A    new_contents = 
 comp
@@ -770,18 +770,21 @@
 e_string
-, 
+.sub(
 handle_m
@@ -793,104 +793,80 @@
 ch, 
-line
+contents
 )%0A    
- 
-           lines.append(result
+retval = int(new_contents != contents
 )%0A    
- 
-           return_value %7C= int(result != line)%0A%0A
+if retval:%0A    
     
@@ -911,35 +911,8 @@
 le:%0A
-        for line in lines:%0A
     
@@ -942,14 +942,21 @@
 ite(
-line)%0A
+new_contents)
 %0A   
@@ -966,25 +966,19 @@
 turn ret
-urn_
 val
-ue
 %0A%0A%0Adef m

Simplify string-fixer