• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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_FXCRT_FX_EXTENSION_H_
8 #define CORE_FXCRT_FX_EXTENSION_H_
9 
10 #include <ctype.h>
11 #include <math.h>
12 #include <time.h>
13 #include <wctype.h>
14 
15 #include "build/build_config.h"
16 #include "core/fxcrt/compiler_specific.h"
17 #include "core/fxcrt/widestring.h"
18 
19 #if defined(USE_SYSTEM_ICUUC)
20 #include <unicode/uchar.h>
21 #else
22 #include "third_party/icu/source/common/unicode/uchar.h"
23 #endif
24 
25 #define FX_INVALID_OFFSET static_cast<uint32_t>(-1)
26 
27 #define FX_IsOdd(a) ((a)&1)
28 
29 float FXSYS_wcstof(WideStringView pwsStr, size_t* pUsedLen);
30 
31 UNSAFE_BUFFER_USAGE wchar_t* FXSYS_wcsncpy(wchar_t* dstStr,
32                                            const wchar_t* srcStr,
33                                            size_t count);
34 
FXSYS_iswlower(int32_t c)35 inline bool FXSYS_iswlower(int32_t c) {
36 #ifndef ANDROID_R_COMPATIBLE
37     if (__builtin_available(android 31, *)) return u_islower(c);
38     else return iswlower(c);
39 #else
40     return iswlower(c);
41 #endif
42 }
43 
FXSYS_iswupper(int32_t c)44 inline bool FXSYS_iswupper(int32_t c) {
45 #ifndef ANDROID_R_COMPATIBLE
46     if (__builtin_available(android 31, *)) return u_isupper(c);
47     else return iswupper(c);
48 #else
49     return iswupper(c);
50 #endif
51 }
52 
FXSYS_towlower(wchar_t c)53 inline int32_t FXSYS_towlower(wchar_t c) {
54 #ifndef ANDROID_R_COMPATIBLE
55     if (__builtin_available(android 31, *)) return u_tolower(c);
56     else return towlower(c);
57 #else
58     return towlower(c);
59 #endif
60 }
61 
FXSYS_towupper(wchar_t c)62 inline int32_t FXSYS_towupper(wchar_t c) {
63 #ifndef ANDROID_R_COMPATIBLE
64     if (__builtin_available(android 31, *)) return u_toupper(c);
65     else return towupper(c);
66 #else
67     return towupper(c);
68 #endif
69 }
70 
FXSYS_IsLowerASCII(int32_t c)71 inline bool FXSYS_IsLowerASCII(int32_t c) {
72   return c >= 'a' && c <= 'z';
73 }
74 
FXSYS_IsUpperASCII(int32_t c)75 inline bool FXSYS_IsUpperASCII(int32_t c) {
76   return c >= 'A' && c <= 'Z';
77 }
78 
FXSYS_ToUpperASCII(char c)79 inline char FXSYS_ToUpperASCII(char c) {
80   return FXSYS_IsLowerASCII(c) ? (c + ('A' - 'a')) : c;
81 }
82 
FXSYS_iswalpha(wchar_t c)83 inline bool FXSYS_iswalpha(wchar_t c) {
84 #ifndef ANDROID_R_COMPATIBLE
85     if (__builtin_available(android 31, *)) return u_isalpha(c);
86     else return iswalpha(c);
87 #else
88     return iswalpha(c);
89 #endif
90 }
91 
FXSYS_iswalnum(wchar_t c)92 inline bool FXSYS_iswalnum(wchar_t c) {
93 #ifndef ANDROID_R_COMPATIBLE
94     if (__builtin_available(android 31, *)) return u_isalnum(c);
95     else return iswalnum(c);
96 #else
97     return iswalnum(c);
98 #endif
99 }
100 
FXSYS_iswspace(wchar_t c)101 inline bool FXSYS_iswspace(wchar_t c) {
102 #ifndef ANDROID_R_COMPATIBLE
103     if (__builtin_available(android 31, *)) return u_isspace(c);
104     else return iswspace(c);
105 #else
106     return iswspace(c);
107 #endif
108 }
109 
FXSYS_IsOctalDigit(char c)110 inline bool FXSYS_IsOctalDigit(char c) {
111   return c >= '0' && c <= '7';
112 }
113 
FXSYS_IsHexDigit(char c)114 inline bool FXSYS_IsHexDigit(char c) {
115   return !((c & 0x80) || !isxdigit(c));
116 }
117 
FXSYS_IsWideHexDigit(wchar_t c)118 inline bool FXSYS_IsWideHexDigit(wchar_t c) {
119   return !((c & 0xFFFFFF80) || !isxdigit(c));
120 }
121 
FXSYS_HexCharToInt(char c)122 inline int FXSYS_HexCharToInt(char c) {
123   if (!FXSYS_IsHexDigit(c))
124     return 0;
125   char upchar = FXSYS_ToUpperASCII(c);
126   return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
127 }
128 
FXSYS_WideHexCharToInt(wchar_t c)129 inline int FXSYS_WideHexCharToInt(wchar_t c) {
130   if (!FXSYS_IsWideHexDigit(c))
131     return 0;
132   char upchar = toupper(static_cast<char>(c));
133   return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
134 }
135 
FXSYS_IsDecimalDigit(char c)136 inline bool FXSYS_IsDecimalDigit(char c) {
137   return !((c & 0x80) || !isdigit(c));
138 }
139 
FXSYS_IsDecimalDigit(wchar_t c)140 inline bool FXSYS_IsDecimalDigit(wchar_t c) {
141   return !((c & 0xFFFFFF80) || !iswdigit(c));
142 }
143 
FXSYS_DecimalCharToInt(char c)144 inline int FXSYS_DecimalCharToInt(char c) {
145   return FXSYS_IsDecimalDigit(c) ? c - '0' : 0;
146 }
147 
FXSYS_DecimalCharToInt(wchar_t c)148 inline int FXSYS_DecimalCharToInt(wchar_t c) {
149   return FXSYS_IsDecimalDigit(c) ? c - L'0' : 0;
150 }
151 
152 void FXSYS_IntToTwoHexChars(uint8_t n, char* buf);
153 void FXSYS_IntToFourHexChars(uint16_t n, char* buf);
154 
155 size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf);
156 
157 // Strict order over floating types where NaNs may be present.
158 // All NaNs are treated as equal to each other and greater than infinity.
159 template <typename T>
FXSYS_SafeEQ(const T & lhs,const T & rhs)160 bool FXSYS_SafeEQ(const T& lhs, const T& rhs) {
161   return (isnan(lhs) && isnan(rhs)) ||
162          (!isnan(lhs) && !isnan(rhs) && lhs == rhs);
163 }
164 
165 template <typename T>
FXSYS_SafeLT(const T & lhs,const T & rhs)166 bool FXSYS_SafeLT(const T& lhs, const T& rhs) {
167   if (isnan(lhs) && isnan(rhs))
168     return false;
169   if (isnan(lhs) || isnan(rhs))
170     return isnan(lhs) < isnan(rhs);
171   return lhs < rhs;
172 }
173 
174 // Override time/localtime functions for test consistency.
175 void FXSYS_SetTimeFunction(time_t (*func)());
176 void FXSYS_SetLocaltimeFunction(struct tm* (*func)(const time_t*));
177 
178 // Replacements for time/localtime that respect overrides.
179 time_t FXSYS_time(time_t* tloc);
180 struct tm* FXSYS_localtime(const time_t* tp);
181 
182 #endif  // CORE_FXCRT_FX_EXTENSION_H_
183