Parcourir la source

[bugfix][Fix #28] refactored unicode parsing

B Thibault il y a 9 ans
Parent
commit
ae46a7752f
3 fichiers modifiés avec 8 ajouts et 8 suppressions
  1. 5 5
      src/streamConsumer.cpp
  2. 1 1
      test/src/unicode.cpp
  3. 2 2
      test/testUnicode.json

+ 5 - 5
src/streamConsumer.cpp

@@ -6,6 +6,8 @@
 
 
 #include <iostream>
 #include <iostream>
 #include <sstream>
 #include <sstream>
+#include <codecvt>
+#include <locale>
 #include "jsonElement.hh"
 #include "jsonElement.hh"
 #include "streamConsumer.hh"
 #include "streamConsumer.hh"
 #include "unicode.hpp"
 #include "unicode.hpp"
@@ -339,11 +341,9 @@ JSonElement *StreamConsumer::consumeToken(JSonContainer *parent, std::stringstre
 void StreamConsumer::appendUnicode(const char unicode[4], std::stringstream &buf)
 void StreamConsumer::appendUnicode(const char unicode[4], std::stringstream &buf)
 {
 {
     unsigned short uni = hexbyte<unsigned short>(unicode, 4);
     unsigned short uni = hexbyte<unsigned short>(unicode, 4);
-    char test[5];
-
-    bzero(test, sizeof(*test) *5);
-    snprintf(test, 4, "%lc", uni);
-    buf.write(test, 2);
+    std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
+    std::string unichar = converter.to_bytes(uni);
+    buf.write(unichar.c_str(), unichar.size());
 }
 }
 
 
 std::string StreamConsumer::extractUnicode(const char *buf)
 std::string StreamConsumer::extractUnicode(const char *buf)

+ 1 - 1
test/src/unicode.cpp

@@ -19,7 +19,7 @@ class StreamConsumerTester: public StreamConsumer
         {
         {
             std::string s = getStringFromUnicode("00e8");
             std::string s = getStringFromUnicode("00e8");
             if (s != "è")
             if (s != "è")
-                FAILED(s, "!=", "è");
+                FAILED((int)(s.c_str()[0]), "!=", (int)L'è');
             return true;
             return true;
         };
         };
 };
 };

+ 2 - 2
test/testUnicode.json

@@ -1,9 +1,9 @@
 [
 [
-    "_\u20ac_\u20ac_",
+    "_\u20ac_\u20ac_ is _euro_euro_",
     "Kæmi ný öxi hér ykist þjófum nú bæði víl og ádrepa",
     "Kæmi ný öxi hér ykist þjófum nú bæði víl og ádrepa",
     "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!",
     "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!",
     "ascii-only",
     "ascii-only",
-    "\u058e",
+    "\u058e is 'LEFT-FACING ARMENIAN ETERNITY SIGN'",
     {
     {
         "eurooooooooooooooooooooooooooo": "\u20ac coucou"
         "eurooooooooooooooooooooooooooo": "\u20ac coucou"
     }
     }