Death to std::wstringstream!
authorAdam Borowski <kilobyte@angband.pl>
Sun, 12 Aug 2012 18:08:25 +0000 (20:08 +0200)
committerAdam Borowski <kilobyte@angband.pl>
Sun, 12 Aug 2012 19:42:32 +0000 (21:42 +0200)
There's no point in accumulating a string of wchar_t and then immediately
converting it to utf8 if we can accumulate it in utf8.  This solves problems
on Windows and Android (neither of whom can run webtiles servers currently)
and reduces the code size somewhat.

crawl-ref/source/tileweb-text.cc
crawl-ref/source/unicode.cc
crawl-ref/source/unicode.h

index c17ebac..98c3fce 100644 (file)
@@ -1,12 +1,11 @@
 
 #include "AppHdr.h"
 
+#include "libutil.h"
 #include "tileweb-text.h"
 #include "tileweb.h"
 #include "unicode.h"
 
-#include <sstream>
-
 WebTextArea::WebTextArea(std::string name) :
     mx(0),
     my(0),
@@ -97,7 +96,7 @@ void WebTextArea::send(bool force)
     int last_col = -1;
     int space_count = 0;
     bool dirty = false;
-    std::wstringstream html;
+    std::string html;
 
     bool sending = false;
 
@@ -106,7 +105,7 @@ void WebTextArea::send(bool force)
         last_col = -1;
         space_count = 0;
         dirty = false;
-        html.str(L"");
+        html.clear();
 
         for (int x = 0; x < mx; ++x)
         {
@@ -125,7 +124,7 @@ void WebTextArea::send(bool force)
             {
                 while (space_count)
                 {
-                    html << " ";
+                    html.push_back(' ');
                     space_count--;
                 }
             }
@@ -133,9 +132,9 @@ void WebTextArea::send(bool force)
             if (col != last_col && chr != ' ')
             {
                 if (last_col != -1)
-                    html << "</span>";
-                html << "<span class=\\\"fg" << (col & 0xf)
-                     << " bg" << ((col >> 4) & 0xf) << "\\\">";
+                    html += "</span>";
+                html += make_stringf("<span class=\\\"fg%d bg%d\\\">",
+                                     col & 0xf, (col >> 4) & 0xf);
                 last_col = col;
             }
 
@@ -146,28 +145,30 @@ void WebTextArea::send(bool force)
                 switch (chr)
                 {
                 case '<':
-                    html << "&lt;";
+                    html += "&lt;";
                     break;
                 case '>':
-                    html << "&gt;";
+                    html += "&gt;";
                     break;
                 case '&':
-                    html << "&amp;";
+                    html += "&amp;";
                     break;
                 case '\\':
-                    html << "\\\\";
+                    html += "\\\\";
                     break;
                 case '"':
-                    html << "&quot;";
+                    html += "&quot;";
                     break;
                 default:
-                    html.put(chr);
+                    char buf[5];
+                    buf[wctoutf8(buf, chr)] = 0;
+                    html += buf;
                     break;
                 }
             }
         }
 
-        if (dirty || (force && !html.str().empty()))
+        if (dirty || (force && !html.empty()))
         {
             if (!sending)
             {
@@ -179,7 +180,7 @@ void WebTextArea::send(bool force)
                 sending = true;
             }
 
-            tiles.write_message("%u:\"%s\",", y, wcstoutf8(html.str()).c_str());
+            tiles.write_message("%u:\"%s\",", y, html.c_str());
         }
     }
     if (sending)
index 357ec1d..1d83f65 100644 (file)
@@ -233,23 +233,6 @@ static std::string utf8_validate(const char *s)
     return d;
 }
 
-#ifdef USE_TILE_WEB
-std::string wcstoutf8(const std::wstring &s)
-{
-    std::string out;
-
-    for (size_t j = 0; j < s.length(); j++)
-    {
-        char buf[4];
-        int r = wctoutf8(buf, s[j]);
-        for (int i = 0; i < r; i++)
-            out.push_back(buf[i]);
-    }
-
-    return out;
-}
-#endif
-
 static bool _check_trail(FILE *f, const char* bytes, int len)
 {
     while (len--)
index 8ac74a6..f0d8c93 100644 (file)
@@ -45,10 +45,6 @@ char *next_glyph(char *s);
 #define OUTS(x) utf8_to_mb(x).c_str()
 #define OUTW(x) utf8_to_16(x).c_str()
 
-#ifdef USE_TILE_WEB
-std::string wcstoutf8(const std::wstring &s);
-#endif
-
 class LineInput
 {
 public: