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