1 // Copyright 2017 The PDFium Authors 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/widetext_buffer.h" 8 9 #include "core/fxcrt/fx_safe_types.h" 10 #include "core/fxcrt/fx_system.h" 11 12 namespace fxcrt { 13 GetLength() const14size_t WideTextBuffer::GetLength() const { 15 return GetSize() / sizeof(wchar_t); 16 } 17 GetWideSpan()18pdfium::span<wchar_t> WideTextBuffer::GetWideSpan() { 19 return pdfium::make_span(reinterpret_cast<wchar_t*>(m_buffer.data()), 20 GetLength()); 21 } 22 GetWideSpan() const23pdfium::span<const wchar_t> WideTextBuffer::GetWideSpan() const { 24 return pdfium::make_span(reinterpret_cast<const wchar_t*>(m_buffer.data()), 25 GetLength()); 26 } 27 AsStringView() const28WideStringView WideTextBuffer::AsStringView() const { 29 return WideStringView(GetWideSpan()); 30 } 31 MakeString() const32WideString WideTextBuffer::MakeString() const { 33 return WideString(AsStringView()); 34 } 35 AppendChar(wchar_t ch)36void WideTextBuffer::AppendChar(wchar_t ch) { 37 pdfium::span<wchar_t> new_span = ExpandWideBuf(1); 38 new_span[0] = ch; 39 } 40 Delete(size_t start_index,size_t count)41void WideTextBuffer::Delete(size_t start_index, size_t count) { 42 DeleteBuf(start_index * sizeof(wchar_t), count * sizeof(wchar_t)); 43 } 44 AppendWideString(WideStringView str)45void WideTextBuffer::AppendWideString(WideStringView str) { 46 AppendSpan(pdfium::as_bytes(str.span())); 47 } 48 operator <<(ByteStringView ascii)49WideTextBuffer& WideTextBuffer::operator<<(ByteStringView ascii) { 50 pdfium::span<wchar_t> new_span = ExpandWideBuf(ascii.GetLength()); 51 for (size_t i = 0; i < ascii.GetLength(); ++i) 52 new_span[i] = ascii[i]; 53 return *this; 54 } 55 operator <<(WideStringView str)56WideTextBuffer& WideTextBuffer::operator<<(WideStringView str) { 57 AppendWideString(str); 58 return *this; 59 } 60 operator <<(const WideString & str)61WideTextBuffer& WideTextBuffer::operator<<(const WideString& str) { 62 AppendWideString(str.AsStringView()); 63 return *this; 64 } 65 operator <<(const wchar_t * lpsz)66WideTextBuffer& WideTextBuffer::operator<<(const wchar_t* lpsz) { 67 AppendWideString(WideStringView(lpsz)); 68 return *this; 69 } 70 operator <<(const WideTextBuffer & buf)71WideTextBuffer& WideTextBuffer::operator<<(const WideTextBuffer& buf) { 72 AppendWideString(buf.AsStringView()); 73 return *this; 74 } 75 ExpandWideBuf(size_t char_count)76pdfium::span<wchar_t> WideTextBuffer::ExpandWideBuf(size_t char_count) { 77 size_t original_count = GetLength(); 78 FX_SAFE_SIZE_T safe_bytes = char_count; 79 safe_bytes *= sizeof(wchar_t); 80 size_t bytes = safe_bytes.ValueOrDie(); 81 ExpandBuf(bytes); 82 m_DataSize += bytes; 83 return GetWideSpan().subspan(original_count); 84 } 85 86 } // namespace fxcrt 87