K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2014 The PDFium Authors |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | 95e854f | 2015-06-13 00:58:06 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | cfb1944 | 2017-04-20 13:13:04 -0400 | [diff] [blame] | 7 | #ifndef CORE_FXCRT_FX_EXTENSION_H_ |
| 8 | #define CORE_FXCRT_FX_EXTENSION_H_ |
Tom Sepez | 0570b8a | 2015-04-06 13:49:25 -0700 | [diff] [blame] | 9 | |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
Lei Zhang | 15fef79 | 2021-08-04 17:52:39 +0000 | [diff] [blame] | 11 | #include <math.h> |
Tom Sepez | 706e187 | 2018-10-22 19:34:53 +0000 | [diff] [blame] | 12 | #include <time.h> |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 13 | #include <wctype.h> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 14 | |
Miklos Vajna | fc0bbda | 2018-05-16 16:54:12 +0000 | [diff] [blame] | 15 | #if defined(USE_SYSTEM_ICUUC) |
| 16 | #include <unicode/uchar.h> |
| 17 | #else |
Ryan Harrison | 15c0fcc | 2018-03-12 15:20:04 +0000 | [diff] [blame] | 18 | #include "third_party/icu/source/common/unicode/uchar.h" |
Miklos Vajna | fc0bbda | 2018-05-16 16:54:12 +0000 | [diff] [blame] | 19 | #endif |
Tom Sepez | 7386e29 | 2015-11-20 09:35:06 -0800 | [diff] [blame] | 20 | |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 21 | #define FX_INVALID_OFFSET static_cast<uint32_t>(-1) |
| 22 | |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 23 | #define FX_IsOdd(a) ((a)&1) |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 24 | |
Tom Sepez | fac0f28 | 2021-11-09 20:48:16 +0000 | [diff] [blame] | 25 | float FXSYS_wcstof(const wchar_t* pwsStr, size_t nLength, size_t* pUsedLen); |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 26 | wchar_t* FXSYS_wcsncpy(wchar_t* dstStr, const wchar_t* srcStr, size_t count); |
| 27 | int32_t FXSYS_wcsnicmp(const wchar_t* s1, const wchar_t* s2, size_t count); |
Tom Sepez | 0570b8a | 2015-04-06 13:49:25 -0700 | [diff] [blame] | 28 | |
Ryan Harrison | 7a1aa5f | 2018-03-13 15:16:00 +0000 | [diff] [blame] | 29 | inline bool FXSYS_iswlower(int32_t c) { |
| 30 | return u_islower(c); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | } |
Dan Sinclair | 72c6b18 | 2017-04-03 13:39:26 -0400 | [diff] [blame] | 32 | |
Ryan Harrison | 7a1aa5f | 2018-03-13 15:16:00 +0000 | [diff] [blame] | 33 | inline bool FXSYS_iswupper(int32_t c) { |
| 34 | return u_isupper(c); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 35 | } |
Dan Sinclair | 72c6b18 | 2017-04-03 13:39:26 -0400 | [diff] [blame] | 36 | |
Ryan Harrison | 7a1aa5f | 2018-03-13 15:16:00 +0000 | [diff] [blame] | 37 | inline int32_t FXSYS_towlower(wchar_t c) { |
| 38 | return u_tolower(c); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 39 | } |
Dan Sinclair | 72c6b18 | 2017-04-03 13:39:26 -0400 | [diff] [blame] | 40 | |
Ryan Harrison | 7a1aa5f | 2018-03-13 15:16:00 +0000 | [diff] [blame] | 41 | inline int32_t FXSYS_towupper(wchar_t c) { |
| 42 | return u_toupper(c); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 43 | } |
Dan Sinclair | 72c6b18 | 2017-04-03 13:39:26 -0400 | [diff] [blame] | 44 | |
Hui Yingst | bfc82af | 2020-03-05 19:11:06 +0000 | [diff] [blame] | 45 | inline bool FXSYS_IsLowerASCII(int32_t c) { |
| 46 | return c >= 'a' && c <= 'z'; |
| 47 | } |
| 48 | |
Hui Yingst | 946be34 | 2020-03-04 14:47:35 +0000 | [diff] [blame] | 49 | inline bool FXSYS_IsUpperASCII(int32_t c) { |
| 50 | return c >= 'A' && c <= 'Z'; |
| 51 | } |
| 52 | |
Lei Zhang | fbcc18a | 2018-11-17 00:47:12 +0000 | [diff] [blame] | 53 | inline char FXSYS_ToUpperASCII(char c) { |
Hui Yingst | bfc82af | 2020-03-05 19:11:06 +0000 | [diff] [blame] | 54 | return FXSYS_IsLowerASCII(c) ? (c + ('A' - 'a')) : c; |
Lei Zhang | fbcc18a | 2018-11-17 00:47:12 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Ryan Harrison | 735eda9 | 2018-03-12 15:44:36 +0000 | [diff] [blame] | 57 | inline bool FXSYS_iswalpha(wchar_t c) { |
| 58 | return u_isalpha(c); |
Wei Li | 60eac0f | 2015-12-17 18:16:23 -0800 | [diff] [blame] | 59 | } |
Dan Sinclair | 72c6b18 | 2017-04-03 13:39:26 -0400 | [diff] [blame] | 60 | |
Ryan Harrison | 15c0fcc | 2018-03-12 15:20:04 +0000 | [diff] [blame] | 61 | inline bool FXSYS_iswalnum(wchar_t c) { |
| 62 | return u_isalnum(c); |
Wei Li | 60eac0f | 2015-12-17 18:16:23 -0800 | [diff] [blame] | 63 | } |
Dan Sinclair | 72c6b18 | 2017-04-03 13:39:26 -0400 | [diff] [blame] | 64 | |
Ryan Harrison | 0c6b981 | 2018-03-12 19:25:55 +0000 | [diff] [blame] | 65 | inline bool FXSYS_iswspace(wchar_t c) { |
| 66 | return u_isspace(c); |
dsinclair | ce56557 | 2016-06-23 14:00:32 -0700 | [diff] [blame] | 67 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 68 | |
Lei Zhang | 3407afd | 2018-11-16 22:54:06 +0000 | [diff] [blame] | 69 | inline bool FXSYS_IsOctalDigit(char c) { |
| 70 | return c >= '0' && c <= '7'; |
| 71 | } |
| 72 | |
| 73 | inline bool FXSYS_IsHexDigit(char c) { |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 74 | return !((c & 0x80) || !isxdigit(c)); |
Nicolas Pena | ecd3646 | 2017-04-18 13:12:41 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Tom Sepez | 3e2cdea | 2018-11-28 00:41:15 +0000 | [diff] [blame] | 77 | inline bool FXSYS_IsWideHexDigit(wchar_t c) { |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 78 | return !((c & 0xFFFFFF80) || !isxdigit(c)); |
Tom Sepez | 3e2cdea | 2018-11-28 00:41:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Lei Zhang | 3407afd | 2018-11-16 22:54:06 +0000 | [diff] [blame] | 81 | inline int FXSYS_HexCharToInt(char c) { |
| 82 | if (!FXSYS_IsHexDigit(c)) |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 83 | return 0; |
Lei Zhang | fbcc18a | 2018-11-17 00:47:12 +0000 | [diff] [blame] | 84 | char upchar = FXSYS_ToUpperASCII(c); |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 85 | return upchar > '9' ? upchar - 'A' + 10 : upchar - '0'; |
| 86 | } |
| 87 | |
Tom Sepez | 3e2cdea | 2018-11-28 00:41:15 +0000 | [diff] [blame] | 88 | inline int FXSYS_WideHexCharToInt(wchar_t c) { |
| 89 | if (!FXSYS_IsWideHexDigit(c)) |
| 90 | return 0; |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 91 | char upchar = toupper(static_cast<char>(c)); |
Tom Sepez | 3e2cdea | 2018-11-28 00:41:15 +0000 | [diff] [blame] | 92 | return upchar > '9' ? upchar - 'A' + 10 : upchar - '0'; |
| 93 | } |
| 94 | |
Lei Zhang | f263530 | 2018-11-16 23:47:51 +0000 | [diff] [blame] | 95 | inline bool FXSYS_IsDecimalDigit(char c) { |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 96 | return !((c & 0x80) || !isdigit(c)); |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Lei Zhang | f263530 | 2018-11-16 23:47:51 +0000 | [diff] [blame] | 99 | inline bool FXSYS_IsDecimalDigit(wchar_t c) { |
Lei Zhang | d87b454 | 2021-08-05 21:55:51 +0000 | [diff] [blame] | 100 | return !((c & 0xFFFFFF80) || !iswdigit(c)); |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Lei Zhang | 3407afd | 2018-11-16 22:54:06 +0000 | [diff] [blame] | 103 | inline int FXSYS_DecimalCharToInt(char c) { |
Lei Zhang | f263530 | 2018-11-16 23:47:51 +0000 | [diff] [blame] | 104 | return FXSYS_IsDecimalDigit(c) ? c - '0' : 0; |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Lei Zhang | 3407afd | 2018-11-16 22:54:06 +0000 | [diff] [blame] | 107 | inline int FXSYS_DecimalCharToInt(wchar_t c) { |
Lei Zhang | a282cf6 | 2018-12-07 19:07:20 +0000 | [diff] [blame] | 108 | return FXSYS_IsDecimalDigit(c) ? c - L'0' : 0; |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Andrew Weintraub | 43d760a | 2019-06-24 17:45:20 +0000 | [diff] [blame] | 111 | void FXSYS_IntToTwoHexChars(uint8_t n, char* buf); |
| 112 | void FXSYS_IntToFourHexChars(uint16_t n, char* buf); |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 113 | |
| 114 | size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf); |
| 115 | |
Tom Sepez | ed1c580 | 2018-06-19 16:23:52 +0000 | [diff] [blame] | 116 | // Strict order over floating types where NaNs may be present. |
Bruce Dawson | b1e8054 | 2020-05-02 01:06:13 +0000 | [diff] [blame] | 117 | // All NaNs are treated as equal to each other and greater than infinity. |
Tom Sepez | ed1c580 | 2018-06-19 16:23:52 +0000 | [diff] [blame] | 118 | template <typename T> |
| 119 | bool FXSYS_SafeEQ(const T& lhs, const T& rhs) { |
Lei Zhang | 15fef79 | 2021-08-04 17:52:39 +0000 | [diff] [blame] | 120 | return (isnan(lhs) && isnan(rhs)) || |
| 121 | (!isnan(lhs) && !isnan(rhs) && lhs == rhs); |
Tom Sepez | ed1c580 | 2018-06-19 16:23:52 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | template <typename T> |
| 125 | bool FXSYS_SafeLT(const T& lhs, const T& rhs) { |
Lei Zhang | 15fef79 | 2021-08-04 17:52:39 +0000 | [diff] [blame] | 126 | if (isnan(lhs) && isnan(rhs)) |
Tom Sepez | ed1c580 | 2018-06-19 16:23:52 +0000 | [diff] [blame] | 127 | return false; |
Lei Zhang | 15fef79 | 2021-08-04 17:52:39 +0000 | [diff] [blame] | 128 | if (isnan(lhs) || isnan(rhs)) |
| 129 | return isnan(lhs) < isnan(rhs); |
Tom Sepez | ed1c580 | 2018-06-19 16:23:52 +0000 | [diff] [blame] | 130 | return lhs < rhs; |
| 131 | } |
| 132 | |
Tom Sepez | 706e187 | 2018-10-22 19:34:53 +0000 | [diff] [blame] | 133 | // Override time/localtime functions for test consistency. |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 134 | void FXSYS_SetTimeFunction(time_t (*func)()); |
Tom Sepez | 706e187 | 2018-10-22 19:34:53 +0000 | [diff] [blame] | 135 | void FXSYS_SetLocaltimeFunction(struct tm* (*func)(const time_t*)); |
| 136 | |
| 137 | // Replacements for time/localtime that respect overrides. |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 138 | time_t FXSYS_time(time_t* tloc); |
Tom Sepez | 706e187 | 2018-10-22 19:34:53 +0000 | [diff] [blame] | 139 | struct tm* FXSYS_localtime(const time_t* tp); |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 140 | |
Dan Sinclair | cfb1944 | 2017-04-20 13:13:04 -0400 | [diff] [blame] | 141 | #endif // CORE_FXCRT_FX_EXTENSION_H_ |