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