blob: ffdb7a28b083d5826b4c6e14ecc330ce6fd1045d [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2014 The PDFium Authors
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002// 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
Lei Zhangd87b4542021-08-05 21:55:51 +000010#include <ctype.h>
Lei Zhang15fef792021-08-04 17:52:39 +000011#include <math.h>
Tom Sepez706e1872018-10-22 19:34:53 +000012#include <time.h>
Lei Zhangd87b4542021-08-05 21:55:51 +000013#include <wctype.h>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
Miklos Vajnafc0bbda2018-05-16 16:54:12 +000015#if defined(USE_SYSTEM_ICUUC)
16#include <unicode/uchar.h>
17#else
Ryan Harrison15c0fcc2018-03-12 15:20:04 +000018#include "third_party/icu/source/common/unicode/uchar.h"
Miklos Vajnafc0bbda2018-05-16 16:54:12 +000019#endif
Tom Sepez7386e292015-11-20 09:35:06 -080020
Wei Li89409932016-03-28 10:33:33 -070021#define FX_INVALID_OFFSET static_cast<uint32_t>(-1)
22
Dan Sinclairbcd1e702017-08-31 13:19:18 -040023#define FX_IsOdd(a) ((a)&1)
Dan Sinclairbcd1e702017-08-31 13:19:18 -040024
Tom Sepezfac0f282021-11-09 20:48:16 +000025float FXSYS_wcstof(const wchar_t* pwsStr, size_t nLength, size_t* pUsedLen);
Dan Sinclair812e96c2017-03-13 16:43:37 -040026wchar_t* FXSYS_wcsncpy(wchar_t* dstStr, const wchar_t* srcStr, size_t count);
27int32_t FXSYS_wcsnicmp(const wchar_t* s1, const wchar_t* s2, size_t count);
Tom Sepez0570b8a2015-04-06 13:49:25 -070028
Ryan Harrison7a1aa5f2018-03-13 15:16:00 +000029inline bool FXSYS_iswlower(int32_t c) {
30 return u_islower(c);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
Dan Sinclair72c6b182017-04-03 13:39:26 -040032
Ryan Harrison7a1aa5f2018-03-13 15:16:00 +000033inline bool FXSYS_iswupper(int32_t c) {
34 return u_isupper(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 int32_t FXSYS_towlower(wchar_t c) {
38 return u_tolower(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_towupper(wchar_t c) {
42 return u_toupper(c);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
Dan Sinclair72c6b182017-04-03 13:39:26 -040044
Hui Yingstbfc82af2020-03-05 19:11:06 +000045inline bool FXSYS_IsLowerASCII(int32_t c) {
46 return c >= 'a' && c <= 'z';
47}
48
Hui Yingst946be342020-03-04 14:47:35 +000049inline bool FXSYS_IsUpperASCII(int32_t c) {
50 return c >= 'A' && c <= 'Z';
51}
52
Lei Zhangfbcc18a2018-11-17 00:47:12 +000053inline char FXSYS_ToUpperASCII(char c) {
Hui Yingstbfc82af2020-03-05 19:11:06 +000054 return FXSYS_IsLowerASCII(c) ? (c + ('A' - 'a')) : c;
Lei Zhangfbcc18a2018-11-17 00:47:12 +000055}
56
Ryan Harrison735eda92018-03-12 15:44:36 +000057inline bool FXSYS_iswalpha(wchar_t c) {
58 return u_isalpha(c);
Wei Li60eac0f2015-12-17 18:16:23 -080059}
Dan Sinclair72c6b182017-04-03 13:39:26 -040060
Ryan Harrison15c0fcc2018-03-12 15:20:04 +000061inline bool FXSYS_iswalnum(wchar_t c) {
62 return u_isalnum(c);
Wei Li60eac0f2015-12-17 18:16:23 -080063}
Dan Sinclair72c6b182017-04-03 13:39:26 -040064
Ryan Harrison0c6b9812018-03-12 19:25:55 +000065inline bool FXSYS_iswspace(wchar_t c) {
66 return u_isspace(c);
dsinclairce565572016-06-23 14:00:32 -070067}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Lei Zhang3407afd2018-11-16 22:54:06 +000069inline bool FXSYS_IsOctalDigit(char c) {
70 return c >= '0' && c <= '7';
71}
72
73inline bool FXSYS_IsHexDigit(char c) {
Lei Zhangd87b4542021-08-05 21:55:51 +000074 return !((c & 0x80) || !isxdigit(c));
Nicolas Penaecd36462017-04-18 13:12:41 -040075}
76
Tom Sepez3e2cdea2018-11-28 00:41:15 +000077inline bool FXSYS_IsWideHexDigit(wchar_t c) {
Lei Zhangd87b4542021-08-05 21:55:51 +000078 return !((c & 0xFFFFFF80) || !isxdigit(c));
Tom Sepez3e2cdea2018-11-28 00:41:15 +000079}
80
Lei Zhang3407afd2018-11-16 22:54:06 +000081inline int FXSYS_HexCharToInt(char c) {
82 if (!FXSYS_IsHexDigit(c))
Dan Sinclair10cfea12015-11-16 13:09:00 -050083 return 0;
Lei Zhangfbcc18a2018-11-17 00:47:12 +000084 char upchar = FXSYS_ToUpperASCII(c);
Dan Sinclair10cfea12015-11-16 13:09:00 -050085 return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
86}
87
Tom Sepez3e2cdea2018-11-28 00:41:15 +000088inline int FXSYS_WideHexCharToInt(wchar_t c) {
89 if (!FXSYS_IsWideHexDigit(c))
90 return 0;
Lei Zhangd87b4542021-08-05 21:55:51 +000091 char upchar = toupper(static_cast<char>(c));
Tom Sepez3e2cdea2018-11-28 00:41:15 +000092 return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
93}
94
Lei Zhangf2635302018-11-16 23:47:51 +000095inline bool FXSYS_IsDecimalDigit(char c) {
Lei Zhangd87b4542021-08-05 21:55:51 +000096 return !((c & 0x80) || !isdigit(c));
Dan Sinclair1c915372016-03-03 17:12:58 -050097}
98
Lei Zhangf2635302018-11-16 23:47:51 +000099inline bool FXSYS_IsDecimalDigit(wchar_t c) {
Lei Zhangd87b4542021-08-05 21:55:51 +0000100 return !((c & 0xFFFFFF80) || !iswdigit(c));
Dan Sinclair1c915372016-03-03 17:12:58 -0500101}
102
Lei Zhang3407afd2018-11-16 22:54:06 +0000103inline int FXSYS_DecimalCharToInt(char c) {
Lei Zhangf2635302018-11-16 23:47:51 +0000104 return FXSYS_IsDecimalDigit(c) ? c - '0' : 0;
Dan Sinclair10cfea12015-11-16 13:09:00 -0500105}
106
Lei Zhang3407afd2018-11-16 22:54:06 +0000107inline int FXSYS_DecimalCharToInt(wchar_t c) {
Lei Zhanga282cf62018-12-07 19:07:20 +0000108 return FXSYS_IsDecimalDigit(c) ? c - L'0' : 0;
Dan Sinclair10cfea12015-11-16 13:09:00 -0500109}
110
Andrew Weintraub43d760a2019-06-24 17:45:20 +0000111void FXSYS_IntToTwoHexChars(uint8_t n, char* buf);
112void FXSYS_IntToFourHexChars(uint16_t n, char* buf);
Nicolas Pena54b91662017-05-05 16:49:30 -0400113
114size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf);
115
Tom Sepezed1c5802018-06-19 16:23:52 +0000116// Strict order over floating types where NaNs may be present.
Bruce Dawsonb1e80542020-05-02 01:06:13 +0000117// All NaNs are treated as equal to each other and greater than infinity.
Tom Sepezed1c5802018-06-19 16:23:52 +0000118template <typename T>
119bool FXSYS_SafeEQ(const T& lhs, const T& rhs) {
Lei Zhang15fef792021-08-04 17:52:39 +0000120 return (isnan(lhs) && isnan(rhs)) ||
121 (!isnan(lhs) && !isnan(rhs) && lhs == rhs);
Tom Sepezed1c5802018-06-19 16:23:52 +0000122}
123
124template <typename T>
125bool FXSYS_SafeLT(const T& lhs, const T& rhs) {
Lei Zhang15fef792021-08-04 17:52:39 +0000126 if (isnan(lhs) && isnan(rhs))
Tom Sepezed1c5802018-06-19 16:23:52 +0000127 return false;
Lei Zhang15fef792021-08-04 17:52:39 +0000128 if (isnan(lhs) || isnan(rhs))
129 return isnan(lhs) < isnan(rhs);
Tom Sepezed1c5802018-06-19 16:23:52 +0000130 return lhs < rhs;
131}
132
Tom Sepez706e1872018-10-22 19:34:53 +0000133// Override time/localtime functions for test consistency.
Ryan Harrison70cca362018-08-10 18:55:46 +0000134void FXSYS_SetTimeFunction(time_t (*func)());
Tom Sepez706e1872018-10-22 19:34:53 +0000135void FXSYS_SetLocaltimeFunction(struct tm* (*func)(const time_t*));
136
137// Replacements for time/localtime that respect overrides.
Ryan Harrison70cca362018-08-10 18:55:46 +0000138time_t FXSYS_time(time_t* tloc);
Tom Sepez706e1872018-10-22 19:34:53 +0000139struct tm* FXSYS_localtime(const time_t* tp);
Ryan Harrison70cca362018-08-10 18:55:46 +0000140
Dan Sinclaircfb19442017-04-20 13:13:04 -0400141#endif // CORE_FXCRT_FX_EXTENSION_H_