xref: /aosp_15_r20/external/pdfium/core/fpdfapi/parser/cpdf_string.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 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 #ifndef CORE_FPDFAPI_PARSER_CPDF_STRING_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_STRING_H_
9 
10 #include "core/fpdfapi/parser/cpdf_object.h"
11 #include "core/fxcrt/fx_string.h"
12 #include "core/fxcrt/retain_ptr.h"
13 #include "core/fxcrt/string_pool_template.h"
14 #include "core/fxcrt/weak_ptr.h"
15 
16 class CPDF_String final : public CPDF_Object {
17  public:
18   CONSTRUCT_VIA_MAKE_RETAIN;
19 
20   // CPDF_Object:
21   Type GetType() const override;
22   RetainPtr<CPDF_Object> Clone() const override;
23   ByteString GetString() const override;
24   WideString GetUnicodeText() const override;
25   void SetString(const ByteString& str) override;
26   CPDF_String* AsMutableString() override;
27   bool WriteTo(IFX_ArchiveStream* archive,
28                const CPDF_Encryptor* encryptor) const override;
29 
IsHex()30   bool IsHex() const { return m_bHex; }
31   ByteString EncodeString() const;
32 
33  private:
34   CPDF_String();
35   CPDF_String(WeakPtr<ByteStringPool> pPool, const ByteString& str, bool bHex);
36   CPDF_String(WeakPtr<ByteStringPool> pPool, WideStringView str);
37   ~CPDF_String() override;
38 
39   ByteString m_String;
40   bool m_bHex = false;
41 };
42 
ToString(CPDF_Object * obj)43 inline CPDF_String* ToString(CPDF_Object* obj) {
44   return obj ? obj->AsMutableString() : nullptr;
45 }
46 
ToString(const CPDF_Object * obj)47 inline const CPDF_String* ToString(const CPDF_Object* obj) {
48   return obj ? obj->AsString() : nullptr;
49 }
50 
ToString(RetainPtr<const CPDF_Object> obj)51 inline RetainPtr<const CPDF_String> ToString(RetainPtr<const CPDF_Object> obj) {
52   return RetainPtr<const CPDF_String>(ToString(obj.Get()));
53 }
54 
55 #endif  // CORE_FPDFAPI_PARSER_CPDF_STRING_H_
56