pdfium_api_string_buffer_adapter.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 The Chromium 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. #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
  5. #include <stddef.h>
  6. #include "base/check_op.h"
  7. #include "base/strings/string_util.h"
  8. namespace chrome_pdf {
  9. namespace internal {
  10. template <class StringType>
  11. PDFiumAPIStringBufferAdapter<StringType>::PDFiumAPIStringBufferAdapter(
  12. StringType* str,
  13. size_t expected_size,
  14. bool check_expected_size)
  15. : str_(str),
  16. data_(base::WriteInto(str, expected_size + 1)),
  17. expected_size_(expected_size),
  18. check_expected_size_(check_expected_size),
  19. is_closed_(false) {}
  20. template <class StringType>
  21. PDFiumAPIStringBufferAdapter<StringType>::~PDFiumAPIStringBufferAdapter() {
  22. DCHECK(is_closed_);
  23. }
  24. template <class StringType>
  25. void* PDFiumAPIStringBufferAdapter<StringType>::GetData() {
  26. DCHECK(!is_closed_);
  27. return data_;
  28. }
  29. template <class StringType>
  30. void PDFiumAPIStringBufferAdapter<StringType>::Close(size_t actual_size) {
  31. DCHECK(!is_closed_);
  32. is_closed_ = true;
  33. if (check_expected_size_)
  34. DCHECK_EQ(expected_size_, actual_size);
  35. if (actual_size > 0) {
  36. DCHECK((*str_)[actual_size - 1] == 0);
  37. str_->resize(actual_size - 1);
  38. } else {
  39. str_->clear();
  40. }
  41. }
  42. PDFiumAPIStringBufferSizeInBytesAdapter::
  43. PDFiumAPIStringBufferSizeInBytesAdapter(std::u16string* str,
  44. size_t expected_size,
  45. bool check_expected_size)
  46. : adapter_(str, expected_size / sizeof(char16_t), check_expected_size) {
  47. DCHECK(expected_size % sizeof(char16_t) == 0);
  48. }
  49. PDFiumAPIStringBufferSizeInBytesAdapter::
  50. ~PDFiumAPIStringBufferSizeInBytesAdapter() = default;
  51. void* PDFiumAPIStringBufferSizeInBytesAdapter::GetData() {
  52. return adapter_.GetData();
  53. }
  54. void PDFiumAPIStringBufferSizeInBytesAdapter::Close(size_t actual_size) {
  55. DCHECK(actual_size % sizeof(char16_t) == 0);
  56. adapter_.Close(actual_size / sizeof(char16_t));
  57. }
  58. // explicit instantiations
  59. template class PDFiumAPIStringBufferAdapter<std::string>;
  60. template class PDFiumAPIStringBufferAdapter<std::u16string>;
  61. } // namespace internal
  62. } // namespace chrome_pdf