Home | History | Annotate | Download | only in css
      1 // Copyright 2017 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "core/fxcrt/css/cfx_csstextbuf.h"
      8 
      9 #include "core/fxcrt/fx_memory.h"
     10 
     11 CFX_CSSTextBuf::CFX_CSSTextBuf()
     12     : m_pBuffer(nullptr), m_iBufLen(0), m_iDatLen(0) {}
     13 
     14 CFX_CSSTextBuf::~CFX_CSSTextBuf() {
     15   FX_Free(m_pBuffer);
     16   m_pBuffer = nullptr;
     17   m_iDatLen = m_iBufLen;
     18 }
     19 
     20 void CFX_CSSTextBuf::InitWithSize(int32_t iAllocSize) {
     21   ExpandBuf(iAllocSize);
     22 }
     23 
     24 void CFX_CSSTextBuf::AppendChar(wchar_t wch) {
     25   if (m_iDatLen >= m_iBufLen)
     26     ExpandBuf(m_iBufLen * 2);
     27 
     28   m_pBuffer[m_iDatLen++] = wch;
     29 }
     30 
     31 int32_t CFX_CSSTextBuf::TrimEnd() {
     32   while (m_iDatLen > 0 && m_pBuffer[m_iDatLen - 1] <= ' ')
     33     --m_iDatLen;
     34   AppendChar(0);
     35   return --m_iDatLen;
     36 }
     37 
     38 void CFX_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) {
     39   ASSERT(iDesiredSize > 0);
     40   if (m_pBuffer && m_iBufLen == iDesiredSize)
     41     return;
     42 
     43   if (m_pBuffer)
     44     m_pBuffer = FX_Realloc(wchar_t, m_pBuffer, iDesiredSize);
     45   else
     46     m_pBuffer = FX_Alloc(wchar_t, iDesiredSize);
     47 
     48   m_iBufLen = iDesiredSize;
     49 }
     50