tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fphdlimp.h (3297B)


      1 // © 2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4 *******************************************************************************
      5 * Copyright (C) 2009-2015, International Business Machines Corporation and    *
      6 * others. All Rights Reserved.                                                *
      7 *******************************************************************************
      8 */
      9 
     10 #ifndef FPHDLIMP_H
     11 #define FPHDLIMP_H
     12 
     13 #include "unicode/utypes.h"
     14 
     15 #if !UCONFIG_NO_FORMATTING
     16 
     17 #include "unicode/fieldpos.h"
     18 #include "unicode/fpositer.h"
     19 #include "unicode/formattedvalue.h"
     20 
     21 U_NAMESPACE_BEGIN
     22 
     23 // utility FieldPositionHandler
     24 // base class, null implementation
     25 
     26 class U_I18N_API FieldPositionHandler: public UMemory {
     27 protected:
     28  int32_t fShift = 0;
     29 
     30 public:
     31  virtual ~FieldPositionHandler();
     32  virtual void addAttribute(int32_t id, int32_t start, int32_t limit) = 0;
     33  virtual void shiftLast(int32_t delta) = 0;
     34  virtual UBool isRecording() const = 0;
     35 
     36  void setShift(int32_t delta);
     37 };
     38 
     39 
     40 // utility subclass FieldPositionOnlyHandler
     41 
     42 class FieldPositionOnlyHandler : public FieldPositionHandler {
     43  FieldPosition& pos;
     44  UBool acceptFirstOnly = false;
     45  UBool seenFirst = false;
     46 
     47 public:
     48  FieldPositionOnlyHandler(FieldPosition& pos);
     49  virtual ~FieldPositionOnlyHandler();
     50 
     51  void addAttribute(int32_t id, int32_t start, int32_t limit) override;
     52  void shiftLast(int32_t delta) override;
     53  UBool isRecording() const override;
     54 
     55  /**
     56   * Enable this option to lock in the FieldPosition value after seeing the
     57   * first occurrence of the field. The default behavior is to take the last
     58   * occurrence.
     59   */
     60  void setAcceptFirstOnly(UBool acceptFirstOnly);
     61 };
     62 
     63 
     64 // utility subclass FieldPositionIteratorHandler
     65 // exported as U_I18N_API for tests
     66 
     67 class U_I18N_API FieldPositionIteratorHandler : public FieldPositionHandler {
     68  FieldPositionIterator* iter; // can be nullptr
     69  UVector32* vec;
     70  UErrorCode status;
     71  UFieldCategory fCategory;
     72 
     73  // Note, we keep a reference to status, so if status is on the stack, we have
     74  // to be destroyed before status goes out of scope.  Easiest thing is to
     75  // allocate us on the stack in the same (or narrower) scope as status has.
     76  // This attempts to encourage that by blocking heap allocation.
     77  static void* U_EXPORT2 operator new(size_t) noexcept = delete;
     78  static void* U_EXPORT2 operator new[](size_t) noexcept = delete;
     79  static void* U_EXPORT2 operator new(size_t, void*) noexcept = delete;
     80 
     81 public:
     82  FieldPositionIteratorHandler(FieldPositionIterator* posIter, UErrorCode& status);
     83  /** If using this constructor, you must call getError() when done formatting! */
     84  FieldPositionIteratorHandler(UVector32* vec, UErrorCode& status);
     85  ~FieldPositionIteratorHandler();
     86 
     87  void addAttribute(int32_t id, int32_t start, int32_t limit) override;
     88  void shiftLast(int32_t delta) override;
     89  UBool isRecording() const override;
     90 
     91  /** Copies a failed error code into _status. */
     92  inline void getError(UErrorCode& _status) {
     93    if (U_SUCCESS(_status) && U_FAILURE(status)) {
     94      _status = status;
     95    }
     96  }
     97 
     98  inline void setCategory(UFieldCategory category) {
     99    fCategory = category;
    100  }
    101 };
    102 
    103 U_NAMESPACE_END
    104 
    105 #endif /* !UCONFIG_NO_FORMATTING */
    106 
    107 #endif /* FPHDLIMP_H */