diff --git a/dir.c b/dir.c
index ad6793ffdc..f42a769d71 100644
--- a/dir.c
+++ b/dir.c
@@ -627,14 +627,13 @@ dir_each(VALUE dir)
 	size_t namlen = NAMLEN(dp);
 	VALUE path;
 #if HAVE_HFS
-	VALUE utf8str = Qnil;
 	if (hfs_p && has_nonascii(name, namlen) &&
-	    !NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
-	    RSTRING_GETMEM(utf8str, name, namlen);
+	    !NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
+	    path = rb_external_str_with_enc(path, dirp->enc);
 	}
+	else
 #endif
 	path = rb_external_str_new_with_enc(name, namlen, dirp->enc);
-	IF_HAVE_HFS(if (!NIL_P(utf8str)) rb_str_resize(utf8str, 0));
 	rb_yield(path);
 	if (dirp->dir == NULL) dir_closed();
     }
diff --git a/internal.h b/internal.h
index e900c42271..377baa09ee 100644
--- a/internal.h
+++ b/internal.h
@@ -434,6 +434,9 @@ VALUE rb_id_quote_unprintable(ID);
 #define QUOTE_ID(id) rb_id_quote_unprintable(id)
 void rb_str_fill_terminator(VALUE str, const int termlen);
 VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg);
+#ifdef RUBY_ENCODING_H
+VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc);
+#endif
 
 /* struct.c */
 VALUE rb_struct_init_copy(VALUE copy, VALUE s);
diff --git a/string.c b/string.c
index 2d328e45d5..7f8707ae5c 100644
--- a/string.c
+++ b/string.c
@@ -576,6 +576,12 @@ rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
     VALUE str;
 
     str = rb_tainted_str_new(ptr, len);
+    return rb_external_str_with_enc(str, eenc);
+}
+
+VALUE
+rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
+{
     if (eenc == rb_usascii_encoding() &&
 	rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
 	rb_enc_associate(str, rb_ascii8bit_encoding());