]> git.saurik.com Git - wxWidgets.git/commitdiff
rewrote wxHtmlEntitiesParser::Parse() using iterators, optimized for the common case...
authorVáclav Slavík <vslavik@fastmail.fm>
Wed, 22 Aug 2007 06:18:12 +0000 (06:18 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Wed, 22 Aug 2007 06:18:12 +0000 (06:18 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48318 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/html/htmlpars.cpp

index 1ea5e979ed529422635e0c96494ea670e85ba5a1..001e547fa28b312dbddc597673bffe179d24c116 100644 (file)
@@ -476,46 +476,52 @@ void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding)
 
 wxString wxHtmlEntitiesParser::Parse(const wxString& input) const
 {
-    const wxChar *c, *last;
-    const wxChar *in_str = input.c_str();
     wxString output;
 
-    output.reserve(input.length());
+    const wxString::const_iterator end(input.end());
+    wxString::const_iterator c(input.begin());
+    wxString::const_iterator last(c);
 
-    for (c = in_str, last = in_str; *c != wxT('\0'); c++)
+    for ( ; c < end; ++c )
     {
         if (*c == wxT('&'))
         {
+            if ( output.empty() )
+                output.reserve(input.length());
+
             if (c - last > 0)
-                output.append(last, c - last);
-            if ( *++c == wxT('\0') )
+                output.append(last, c);
+            if ( ++c == end )
                 break;
 
             wxString entity;
-            const wxChar *ent_s = c;
+            const wxString::const_iterator ent_s = c;
             wxChar entity_char;
 
-            for (; (*c >= wxT('a') && *c <= wxT('z')) ||
-                   (*c >= wxT('A') && *c <= wxT('Z')) ||
-                   (*c >= wxT('0') && *c <= wxT('9')) ||
-                   *c == wxT('_') || *c == wxT('#'); c++) {}
-            entity.append(ent_s, c - ent_s);
-            if (*c != wxT(';')) c--;
+            for (; c != end &&
+                   ((*c >= wxT('a') && *c <= wxT('z')) ||
+                    (*c >= wxT('A') && *c <= wxT('Z')) ||
+                    (*c >= wxT('0') && *c <= wxT('9')) ||
+                    *c == wxT('_') || *c == wxT('#')); ++c) {}
+            entity.append(ent_s, c);
+            if (c == end || *c != wxT(';')) --c;
             last = c+1;
             entity_char = GetEntityChar(entity);
             if (entity_char)
                 output << entity_char;
             else
             {
-                output.append(ent_s-1, c-ent_s+2);
+                output.append(ent_s-1, c+1);
                 wxLogTrace(wxTRACE_HTML_DEBUG,
-                           wxT("Unrecognized HTML entity: '%s'"),
-                           entity.c_str());
+                           "Unrecognized HTML entity: '%s'",
+                           entity);
             }
         }
     }
-    if (*last != wxT('\0'))
-        output.append(last);
+    if ( last == input.begin() ) // common case: no entity
+        return input;
+    if ( last != end )
+        output.append(last, end);
     return output;
 }