parser.cpp
changeset 21 3c10031b140d
parent 19 6164205b828d
child 28 dc8f2d4b515d
     1.1 --- a/parser.cpp	Tue May 12 14:13:01 2009 +0000
     1.2 +++ b/parser.cpp	Mon Jul 06 00:17:46 2009 +0000
     1.3 @@ -83,6 +83,9 @@
     1.4      int err_no;
     1.5      brk_re = pcre_compile(pattern, 0, &error, &err_no, NULL);
     1.6      brk_re_extra = pcre_study(brk_re, 0, &error);
     1.7 +    m_tostring = NULL;
     1.8 +    m_tostring_len = 0;
     1.9 +    m_tostring_maxlen = 0;
    1.10  }
    1.11  
    1.12  Stripper::~Stripper()
    1.13 @@ -99,6 +102,8 @@
    1.14          delete m_PropCache.front();
    1.15          m_PropCache.pop();
    1.16      }
    1.17 +    
    1.18 +    free(m_tostring);
    1.19  }
    1.20  
    1.21  SourceHook::String *Stripper::AllocString()
    1.22 @@ -148,9 +153,9 @@
    1.23      Clear();
    1.24      
    1.25      m_resync = false;
    1.26 -    m_tostring.assign(ents);
    1.27 +    AppendToString(ents, strlen(ents));
    1.28  
    1.29 -    size_t len = m_tostring.size();
    1.30 +    size_t len = m_tostring_len;
    1.31      char *_tmp = NULL;
    1.32      size_t _tmpsize = 0;
    1.33      size_t pos = 0;
    1.34 @@ -464,6 +469,11 @@
    1.35              in_block = false;
    1.36              m_props.push_back(cl);
    1.37              cl = NULL;
    1.38 +            if (*(s->c_str() + 1) == '{')
    1.39 +            {
    1.40 +                in_block = true;
    1.41 +                cl = new List<ent_prop *>();
    1.42 +            }
    1.43          } else {
    1.44              /* try to match our precompiled expression for "..." "..." */
    1.45              int rc = pcre_exec(brk_re, brk_re_extra, s->c_str(), s->size(), 0, 0, ovector, 30);
    1.46 @@ -745,10 +755,12 @@
    1.47  {
    1.48      if (!m_resync)
    1.49      {
    1.50 -        return m_tostring.c_str();
    1.51 +        return m_tostring;
    1.52      }
    1.53  
    1.54 -    m_tostring.clear();
    1.55 +    if (m_tostring != NULL)
    1.56 +        m_tostring[0] = '\0';
    1.57 +    m_tostring_len = 0;
    1.58  
    1.59      List<List<ent_prop *> *>::iterator iter, end=m_props.end(), begin=m_props.begin();
    1.60      List<ent_prop *>::iterator eiter, eend;
    1.61 @@ -759,25 +771,25 @@
    1.62          eend = (*iter)->end();
    1.63          if (!first)
    1.64          {
    1.65 -            m_tostring.append("\n");
    1.66 +            AppendToString("\n", 1);
    1.67          } else {
    1.68              first = true;
    1.69          }
    1.70 -        m_tostring.append("{\n");
    1.71 +        AppendToString("{\n", 2);
    1.72          for (eiter=(*iter)->begin(); eiter!=eend; eiter++)
    1.73          {
    1.74 -            m_tostring.append('"');
    1.75 -            m_tostring.append( (*eiter)->key.c_str() );
    1.76 -            m_tostring.append("\" \"");
    1.77 -            m_tostring.append( (*eiter)->val.c_str() );
    1.78 -            m_tostring.append("\"\n");
    1.79 +            AppendToString("\"", 1);
    1.80 +            AppendToString((*eiter)->key.c_str(), (*eiter)->key.size());
    1.81 +            AppendToString("\" \"", 3);
    1.82 +            AppendToString((*eiter)->val.c_str(), (*eiter)->val.size());
    1.83 +            AppendToString("\"\n", 2);
    1.84          }
    1.85 -        m_tostring.append("}");
    1.86 +        AppendToString("}", 1);
    1.87      }
    1.88  
    1.89      m_resync = false;
    1.90  
    1.91 -    return m_tostring.c_str();
    1.92 +    return m_tostring;
    1.93  }
    1.94  
    1.95  void Stripper::Clear()
    1.96 @@ -814,5 +826,23 @@
    1.97      }
    1.98  
    1.99      m_props.clear();
   1.100 -    m_tostring.clear();
   1.101 +    if (m_tostring != NULL)
   1.102 +        m_tostring[0] = '\0';
   1.103 +    m_tostring_len = 0;
   1.104  }
   1.105 +
   1.106 +void Stripper::AppendToString(const char* buf, size_t len)
   1.107 +{
   1.108 +    if (m_tostring_len + len + 1 > m_tostring_maxlen)
   1.109 +    {
   1.110 +        if (m_tostring_maxlen == 0)
   1.111 +            m_tostring_maxlen = 512;
   1.112 +        while (m_tostring_maxlen < m_tostring_len + len + 1)
   1.113 +            m_tostring_maxlen += m_tostring_maxlen / 2;
   1.114 +        m_tostring = (char*)realloc(m_tostring, m_tostring_maxlen);
   1.115 +    }
   1.116 +    memcpy(m_tostring + m_tostring_len, buf, len);
   1.117 +    m_tostring_len += len;
   1.118 +    m_tostring[m_tostring_len] = '\0';
   1.119 +}
   1.120 +