blob: f747ee42f03c3cfd9ba3f50757980daea7edc9ef [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhang95e854f2015-06-13 00:58:06 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclaircfb19442017-04-20 13:13:04 -04007#ifndef CORE_FXCRT_FX_EXTENSION_H_
8#define CORE_FXCRT_FX_EXTENSION_H_
Tom Sepez0570b8a2015-04-06 13:49:25 -07009
Tom Sepez706e1872018-10-22 19:34:53 +000010#include <time.h>
11
Dan Sinclair10cfea12015-11-16 13:09:00 -050012#include <cctype>
Tom Sepezed1c5802018-06-19 16:23:52 +000013#include <cmath>
Dan Sinclair10cfea12015-11-16 13:09:00 -050014#include <cwctype>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -050015#include <memory>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Miklos Vajnafc0bbda2018-05-16 16:54:12 +000017#if defined(USE_SYSTEM_ICUUC)
18#include <unicode/uchar.h>
19#else
Ryan Harrison15c0fcc2018-03-12 15:20:04 +000020#include "third_party/icu/source/common/unicode/uchar.h"
Miklos Vajnafc0bbda2018-05-16 16:54:12 +000021#endif
Tom Sepez7386e292015-11-20 09:35:06 -080022
Wei Li89409932016-03-28 10:33:33 -070023#define FX_INVALID_OFFSET static_cast<uint32_t>(-1)
24
Dan Sinclairbcd1e702017-08-31 13:19:18 -040025#ifdef PDF_ENABLE_XFA
26#define FX_IsOdd(a) ((a)&1)
27#endif // PDF_ENABLE_XFA
28
Tom Sepez6fdfe3872018-08-10 23:43:26 +000029float FXSYS_wcstof(const wchar_t* pwsStr, int32_t iLength, int32_t* pUsedLen);
Dan Sinclair812e96c2017-03-13 16:43:37 -040030wchar_t* FXSYS_wcsncpy(wchar_t* dstStr, const wchar_t* srcStr, size_t count);
31int32_t FXSYS_wcsnicmp(const wchar_t* s1, const wchar_t* s2, size_t count);
Tom Sepez0570b8a2015-04-06 13:49:25 -070032
Ryan Harrison7a1aa5f2018-03-13 15:16:00 +000033inline bool FXSYS_iswlower(int32_t c) {
34 return u_islower(c);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
Dan Sinclair72c6b182017-04-03 13:39:26 -040036
Ryan Harrison7a1aa5f2018-03-13 15:16:00 +000037inline bool FXSYS_iswupper(int32_t c) {
38 return u_isupper(c);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
Dan Sinclair72c6b182017-04-03 13:39:26 -040040
Ryan Harrison7a1aa5f2018-03-13 15:16:00 +000041inline int32_t FXSYS_towlower(wchar_t c) {
42 return u_tolower(c);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
Dan Sinclair72c6b182017-04-03 13:39:26 -040044
Ryan Harrison7a1aa5f2018-03-13 15:16:00 +000045inline int32_t FXSYS_towupper(wchar_t c) {
46 return u_toupper(c);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
Dan Sinclair72c6b182017-04-03 13:39:26 -040048
Hui Yingstbfc82af2020-03-05 19:11:06 +000049inline bool FXSYS_IsLowerASCII(int32_t c) {
50 return c >= 'a' && c <= 'z';
51}
52
Hui Yingst946be342020-03-04 14:47:35 +000053inline bool FXSYS_IsUpperASCII(int32_t c) {
54 return c >= 'A' && c <= 'Z';
55}
56
Lei Zhangfbcc18a2018-11-17 00:47:12 +000057inline char FXSYS_ToUpperASCII(char c) {
Hui Yingstbfc82af2020-03-05 19:11:06 +000058 return FXSYS_IsLowerASCII(c) ? (c + ('A' - 'a')) : c;
Lei Zhangfbcc18a2018-11-17 00:47:12 +000059}
60
Ryan Harrison735eda92018-03-12 15:44:36 +000061inline bool FXSYS_iswalpha(wchar_t c) {
62 return u_isalpha(c);
Wei Li60eac0f2015-12-17 18:16:23 -080063}
Dan Sinclair72c6b182017-04-03 13:39:26 -040064
Ryan Harrison15c0fcc2018-03-12 15:20:04 +000065inline bool FXSYS_iswalnum(wchar_t c) {
66 return u_isalnum(c);
Wei Li60eac0f2015-12-17 18:16:23 -080067}
Dan Sinclair72c6b182017-04-03 13:39:26 -040068
Ryan Harrison0c6b9812018-03-12 19:25:55 +000069inline bool FXSYS_iswspace(wchar_t c) {
70 return u_isspace(c);
dsinclairce565572016-06-23 14:00:32 -070071}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Lei Zhang3407afd2018-11-16 22:54:06 +000073inline bool FXSYS_IsOctalDigit(char c) {
74 return c >= '0' && c <= '7';
75}
76
77inline bool FXSYS_IsHexDigit(char c) {
Nicolas Penaecd36462017-04-18 13:12:41 -040078 return !((c & 0x80) || !std::isxdigit(c));
79}
80
Tom Sepez3e2cdea2018-11-28 00:41:15 +000081inline bool FXSYS_IsWideHexDigit(wchar_t c) {
82 return !((c & 0xFFFFFF80) || !std::isxdigit(c));
83}
84
Lei Zhang3407afd2018-11-16 22:54:06 +000085inline int FXSYS_HexCharToInt(char c) {
86 if (!FXSYS_IsHexDigit(c))
Dan Sinclair10cfea12015-11-16 13:09:00 -050087 return 0;
Lei Zhangfbcc18a2018-11-17 00:47:12 +000088 char upchar = FXSYS_ToUpperASCII(c);
Dan Sinclair10cfea12015-11-16 13:09:00 -050089 return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
90}
91
Tom Sepez3e2cdea2018-11-28 00:41:15 +000092inline int FXSYS_WideHexCharToInt(wchar_t c) {
93 if (!FXSYS_IsWideHexDigit(c))
94 return 0;
95 char upchar = std::toupper(static_cast<char>(c));
96 return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
97}
98
Lei Zhangf2635302018-11-16 23:47:51 +000099inline bool FXSYS_IsDecimalDigit(char c) {
Nicolas Penaecd36462017-04-18 13:12:41 -0400100 return !((c & 0x80) || !std::isdigit(c));
Dan Sinclair1c915372016-03-03 17:12:58 -0500101}
102
Lei Zhangf2635302018-11-16 23:47:51 +0000103inline bool FXSYS_IsDecimalDigit(wchar_t c) {
Lei Zhanga282cf62018-12-07 19:07:20 +0000104 return !((c & 0xFFFFFF80) || !std::iswdigit(c));
Dan Sinclair1c915372016-03-03 17:12:58 -0500105}
106
Lei Zhang3407afd2018-11-16 22:54:06 +0000107inline int FXSYS_DecimalCharToInt(char c) {
Lei Zhangf2635302018-11-16 23:47:51 +0000108 return FXSYS_IsDecimalDigit(c) ? c - '0' : 0;
Dan Sinclair10cfea12015-11-16 13:09:00 -0500109}
110
Lei Zhang3407afd2018-11-16 22:54:06 +0000111inline int FXSYS_DecimalCharToInt(wchar_t c) {
Lei Zhanga282cf62018-12-07 19:07:20 +0000112 return FXSYS_IsDecimalDigit(c) ? c - L'0' : 0;
Dan Sinclair10cfea12015-11-16 13:09:00 -0500113}
114
Andrew Weintraub43d760a2019-06-24 17:45:20 +0000115void FXSYS_IntToTwoHexChars(uint8_t n, char* buf);
116void FXSYS_IntToFourHexChars(uint16_t n, char* buf);
Nicolas Pena54b91662017-05-05 16:49:30 -0400117
118size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf);
119
Tom Sepezed1c5802018-06-19 16:23:52 +0000120// Strict order over floating types where NaNs may be present.
Bruce Dawsonb1e80542020-05-02 01:06:13 +0000121// All NaNs are treated as equal to each other and greater than infinity.
Tom Sepezed1c5802018-06-19 16:23:52 +0000122template <typename T>
123bool FXSYS_SafeEQ(const T& lhs, const T& rhs) {
124 return (std::isnan(lhs) && std::isnan(rhs)) ||
125 (!std::isnan(lhs) && !std::isnan(rhs) && lhs == rhs);
126}
127
128template <typename T>
129bool FXSYS_SafeLT(const T& lhs, const T& rhs) {
130 if (std::isnan(lhs) && std::isnan(rhs))
131 return false;
132 if (std::isnan(lhs) || std::isnan(rhs))
133 return std::isnan(lhs) < std::isnan(rhs);
134 return lhs < rhs;
135}
136
Tom Sepez706e1872018-10-22 19:34:53 +0000137// Override time/localtime functions for test consistency.
Ryan Harrison70cca362018-08-10 18:55:46 +0000138void FXSYS_SetTimeFunction(time_t (*func)());
Tom Sepez706e1872018-10-22 19:34:53 +0000139void FXSYS_SetLocaltimeFunction(struct tm* (*func)(const time_t*));
140
141// Replacements for time/localtime that respect overrides.
Ryan Harrison70cca362018-08-10 18:55:46 +0000142time_t FXSYS_time(time_t* tloc);
Tom Sepez706e1872018-10-22 19:34:53 +0000143struct tm* FXSYS_localtime(const time_t* tp);
Ryan Harrison70cca362018-08-10 18:55:46 +0000144
Dan Sinclaircfb19442017-04-20 13:13:04 -0400145#endif // CORE_FXCRT_FX_EXTENSION_H_