c_utf8_from_locale: optimize the UTF8 case on Linux
authorOlivier Goffart <ogoffart@woboq.com>
Sat, 23 Sep 2017 12:09:19 +0000 (14:09 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 5 Oct 2017 20:01:36 +0000 (22:01 +0200)
Avoid converting to a QString as an intermediate step

src/csync/std/c_utf8.cpp

index f4f5d1ac2565d99b7f08972825480f9ac821dfbb..2f862b5ced5dd4a46736270b46bca1792ade78ee 100644 (file)
@@ -59,7 +59,15 @@ QByteArray c_utf8_from_locale(const mbchar_t *wstr)
   }
   return dst;
 #else
-    QTextDecoder dec(QTextCodec::codecForLocale());
+    auto codec = QTextCodec::codecForLocale();
+#ifndef __APPLE__
+    if (codec->mibEnum() == 106) { // UTF-8
+        // Optimisation for UTF-8: no need to convert to QString.
+        // We still need to do it for mac because of normalization
+        return QByteArray(wstr);
+    }
+#endif
+    QTextDecoder dec(codec);
     QString s = dec.toUnicode(wstr, qstrlen(wstr));
     if (s.isEmpty() || dec.hasFailure()) {
         /* Conversion error: since we can't report error from this function, just return the original