def _fixencoding(input, encoding, final=False):
 prefix = '@charset "'
 if (len(input) > len(prefix)):
  if input.startswith(prefix):
   pos = input.find('"', len(prefix))
   if (pos >= 0):
    if (encoding.replace('_', '-').lower() == 'utf-8-sig'):
     encoding = 'utf-8'
    return ((prefix + encoding) + input[pos:])
  else:
   return input
 elif ((not prefix.startswith(input)) or final):
  return input
 if final:
  return input
 return None

def _fixencoding ( input , encoding , final = False ) : prefix = '@charset "' if ( len ( input ) > len ( prefix ) ) : if input . startswith ( prefix ) : pos = input . find ( '"' , len ( prefix ) ) if ( pos >= 0 ) : if ( encoding . replace ( '_' , '-' ) . lower ( ) == 'utf-8-sig' ) : encoding = 'utf-8' return ( ( prefix + encoding ) + input [ pos : ] ) else : return input elif ( ( not prefix . startswith ( input ) ) or final ) : return input if final : return input return None
replace the name of the encoding in the charset rule at the beginning of input with encoding .