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.
5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
9 #include "base/logging.h"
10 #include "base/numerics/safe_math.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
14 namespace chrome_pdf
{
16 template <class StringType
>
17 PDFiumAPIStringBufferAdapter
<StringType
>::PDFiumAPIStringBufferAdapter(
20 bool check_expected_size
)
22 data_(WriteInto(str
, expected_size
+ 1)),
23 expected_size_(expected_size
),
24 check_expected_size_(check_expected_size
),
28 template <class StringType
>
29 PDFiumAPIStringBufferAdapter
<StringType
>::~PDFiumAPIStringBufferAdapter() {
33 template <class StringType
>
34 void* PDFiumAPIStringBufferAdapter
<StringType
>::GetData() {
39 template <class StringType
>
40 void PDFiumAPIStringBufferAdapter
<StringType
>::Close(int actual_size
) {
41 base::CheckedNumeric
<size_t> unsigned_size
= actual_size
;
42 Close(unsigned_size
.ValueOrDie());
45 template <class StringType
>
46 void PDFiumAPIStringBufferAdapter
<StringType
>::Close(size_t actual_size
) {
50 if (check_expected_size_
)
51 DCHECK_EQ(expected_size_
, actual_size
);
53 if (actual_size
> 0) {
54 DCHECK((*str_
)[actual_size
- 1] == 0);
55 str_
->resize(actual_size
- 1);
61 // explicit instantiations
62 template class PDFiumAPIStringBufferAdapter
<std::string
>;
63 template class PDFiumAPIStringBufferAdapter
<base::string16
>;
65 } // namespace chrome_pdf