added check for strings already encoded utf8
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 21 Mar 2020 02:46:26 +0000 (19:46 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 21 Mar 2020 02:46:26 +0000 (19:46 -0700)
lib/Perl/Tidy.pm

index 12e4a2b9fcba106b109ee498c3ae05f7cf93230c..bc2dc9d7e567a0bfafc82ecc4b9afcb5207bd95f 100644 (file)
@@ -959,18 +959,20 @@ EOM
         # Case 3. Decode with a specific encoding
         else {
             $encoding_in = $rOpts->{'character-encoding'};
-            eval {
-                $buf = Encode::decode( $encoding_in, $buf,
-                    Encode::FB_CROAK | Encode::LEAVE_SRC );
-            };
-            if ($@) {
-
-                # Quit if we cannot decode by the requested encoding;
-                # Something is not right.
-                Warn(
+            if ( !utf8::is_utf8($buf) ) {
+                eval {
+                    $buf = Encode::decode( $encoding_in, $buf,
+                        Encode::FB_CROAK | Encode::LEAVE_SRC );
+                };
+                if ($@) {
+
+                    # Quit if we cannot decode by the requested encoding;
+                    # Something is not right.
+                    Warn(
 "skipping file: $input_file: Unable to decode source as $encoding_in\n"
-                );
-                next;
+                    );
+                    next;
+                }
             }
         }