1 // Copyright 2017 PDFium 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 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef XFA_FGAS_CRT_CFGAS_STRINGFORMATTER_H_
8 #define XFA_FGAS_CRT_CFGAS_STRINGFORMATTER_H_
9 
10 #include <vector>
11 
12 #include "core/fxcrt/unowned_ptr.h"
13 #include "third_party/base/span.h"
14 #include "xfa/fgas/crt/locale_iface.h"
15 #include "xfa/fgas/crt/locale_mgr_iface.h"
16 
17 bool FX_DateFromCanonical(pdfium::span<const wchar_t> wsTime,
18                           CFX_DateTime* datetime);
19 bool FX_TimeFromCanonical(const LocaleIface* pLocale,
20                           pdfium::span<const wchar_t> wsTime,
21                           CFX_DateTime* datetime);
22 
23 class CFGAS_StringFormatter {
24  public:
25   CFGAS_StringFormatter(LocaleMgrIface* pLocaleMgr,
26                         const WideString& wsPattern);
27   ~CFGAS_StringFormatter();
28 
29   static std::vector<WideString> SplitOnBars(const WideString& wsFormatString);
30 
31   FX_LOCALECATEGORY GetCategory() const;
32 
33   bool ParseText(const WideString& wsSrcText,
34                  WideString* wsValue) const;
35   bool ParseNum(const WideString& wsSrcNum,
36                 WideString* wsValue) const;
37   bool ParseDateTime(const WideString& wsSrcDateTime,
38                      FX_DATETIMETYPE eDateTimeType,
39                      CFX_DateTime* dtValue) const;
40   bool ParseZero(const WideString& wsSrcText) const;
41   bool ParseNull(const WideString& wsSrcText) const;
42 
43   bool FormatText(const WideString& wsSrcText,
44                   WideString* wsOutput) const;
45   bool FormatNum(const WideString& wsSrcNum,
46                  WideString* wsOutput) const;
47   bool FormatDateTime(const WideString& wsSrcDateTime,
48                       FX_DATETIMETYPE eDateTimeType,
49                       WideString* wsOutput) const;
50   bool FormatZero(WideString* wsOutput) const;
51   bool FormatNull(WideString* wsOutput) const;
52 
53  private:
54   WideString GetTextFormat(WideStringView wsCategory) const;
55   LocaleIface* GetNumericFormat(size_t* iDotIndex,
56                                 uint32_t* dwStyle,
57                                 WideString* wsPurgePattern) const;
58   FX_DATETIMETYPE GetDateTimeFormat(LocaleIface** pLocale,
59                                     WideString* wsDatePattern,
60                                     WideString* wsTimePattern) const;
61 
62   UnownedPtr<LocaleMgrIface> const m_pLocaleMgr;
63   const WideString m_wsPattern;                   // keep pattern string alive.
64   const pdfium::span<const wchar_t> m_spPattern;  // span into |m_wsPattern|.
65 };
66 
67 #endif  // XFA_FGAS_CRT_CFGAS_STRINGFORMATTER_H_
68