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/span_util.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 reinterpret_span<wchar_t>(GetMutableSpan()); 20 } 21 GetWideSpan() const22pdfium::span<const wchar_t> WideTextBuffer::GetWideSpan() const { 23 return reinterpret_span<const wchar_t>(GetSpan()); 24 } 25 AsStringView() const26WideStringView WideTextBuffer::AsStringView() const { 27 return WideStringView(GetWideSpan()); 28 } 29 MakeString() const30WideString WideTextBuffer::MakeString() const { 31 return WideString(AsStringView()); 32 } 33 AppendChar(wchar_t ch)34void WideTextBuffer::AppendChar(wchar_t ch) { 35 pdfium::span<wchar_t> new_span = ExpandWideBuf(1); 36 new_span[0] = ch; 37 } 38 Delete(size_t start_index,size_t count)39void WideTextBuffer::Delete(size_t start_index, size_t count) { 40 DeleteBuf(start_index * sizeof(wchar_t), count * sizeof(wchar_t)); 41 } 42 AppendWideString(WideStringView str)43void WideTextBuffer::AppendWideString(WideStringView str) { 44 AppendSpan(pdfium::as_bytes(str.span())); 45 } 46 operator <<(ByteStringView ascii)47WideTextBuffer& WideTextBuffer::operator<<(ByteStringView ascii) { 48 pdfium::span<wchar_t> new_span = ExpandWideBuf(ascii.GetLength()); 49 for (size_t i = 0; i < ascii.GetLength(); ++i) 50 new_span[i] = ascii[i]; 51 return *this; 52 } 53 operator <<(WideStringView str)54WideTextBuffer& WideTextBuffer::operator<<(WideStringView str) { 55 AppendWideString(str); 56 return *this; 57 } 58 operator <<(const WideString & str)59WideTextBuffer& WideTextBuffer::operator<<(const WideString& str) { 60 AppendWideString(str.AsStringView()); 61 return *this; 62 } 63 operator <<(const wchar_t * lpsz)64WideTextBuffer& WideTextBuffer::operator<<(const wchar_t* lpsz) { 65 AppendWideString(WideStringView(lpsz)); 66 return *this; 67 } 68 operator <<(const WideTextBuffer & buf)69WideTextBuffer& WideTextBuffer::operator<<(const WideTextBuffer& buf) { 70 AppendWideString(buf.AsStringView()); 71 return *this; 72 } 73 ExpandWideBuf(size_t char_count)74pdfium::span<wchar_t> WideTextBuffer::ExpandWideBuf(size_t char_count) { 75 size_t original_count = GetLength(); 76 FX_SAFE_SIZE_T safe_bytes = char_count; 77 safe_bytes *= sizeof(wchar_t); 78 size_t bytes = safe_bytes.ValueOrDie(); 79 ExpandBuf(bytes); 80 m_DataSize += bytes; 81 return GetWideSpan().subspan(original_count); 82 } 83 84 } // namespace fxcrt 85