summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2019-04-24 17:33:26 +0200
committerRefael Ackermann <refack@gmail.com>2019-06-01 10:00:42 -0400
commitf73d5c238fc7defe88a594fa2954f5c2cf5d31f0 (patch)
tree1b0afe1585eb12387d9d95c26a537b1ff3da4a2e /tools
parent951318f1adc1f1d86d39007a6dc11c526eaf25fb (diff)
downloadandroid-node-v8-f73d5c238fc7defe88a594fa2954f5c2cf5d31f0.tar.gz
android-node-v8-f73d5c238fc7defe88a594fa2954f5c2cf5d31f0.tar.bz2
android-node-v8-f73d5c238fc7defe88a594fa2954f5c2cf5d31f0.zip
deps: bump minimum icu version to 64
Bump minimum version of ICU needed to build node to 64. Refs: https://github.com/v8/v8/commit/ccbe3d07fbac807e15c524257f3e79e04492f9cf PR-URL: https://github.com/nodejs/node/pull/27375 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/icu/icu_versions.json2
-rw-r--r--tools/icu/patches/54/source/io/ufile.c360
-rw-r--r--tools/icu/patches/55/source/io/ufile.c360
-rw-r--r--tools/icu/patches/58/source/i18n/digitlst.cpp1095
-rw-r--r--tools/icu/patches/62/source/i18n/decimfmt.cpp1384
-rw-r--r--tools/icu/patches/63/source/i18n/dtptngen.cpp2752
-rw-r--r--tools/icu/patches/63/source/tools/toolutil/pkg_genc.cpp1221
7 files changed, 1 insertions, 7173 deletions
diff --git a/tools/icu/icu_versions.json b/tools/icu/icu_versions.json
index 7224e9debe..e6929ac6a3 100644
--- a/tools/icu/icu_versions.json
+++ b/tools/icu/icu_versions.json
@@ -1,3 +1,3 @@
{
- "minimum_icu": 63
+ "minimum_icu": 64
}
diff --git a/tools/icu/patches/54/source/io/ufile.c b/tools/icu/patches/54/source/io/ufile.c
deleted file mode 100644
index ab9f70a3d5..0000000000
--- a/tools/icu/patches/54/source/io/ufile.c
+++ /dev/null
@@ -1,360 +0,0 @@
-/*
-******************************************************************************
-*
-* Copyright (C) 1998-2015, International Business Machines
-* Corporation and others. All Rights Reserved.
-*
-******************************************************************************
-*
-* File ufile.c
-*
-* Modification History:
-*
-* Date Name Description
-* 11/19/98 stephen Creation.
-* 03/12/99 stephen Modified for new C API.
-* 06/16/99 stephen Changed T_LocaleBundle to u_locbund
-* 07/19/99 stephen Fixed to use ucnv's default codepage.
-******************************************************************************
-*/
-
-/*
- * fileno is not declared when building with GCC in strict mode.
- */
-#if defined(__GNUC__) && defined(__STRICT_ANSI__)
-#undef __STRICT_ANSI__
-#endif
-
-#include "locmap.h"
-#include "unicode/ustdio.h"
-
-#if !UCONFIG_NO_CONVERSION
-
-#include "ufile.h"
-#include "unicode/uloc.h"
-#include "unicode/ures.h"
-#include "unicode/ucnv.h"
-#include "unicode/ustring.h"
-#include "cstring.h"
-#include "cmemory.h"
-
-#if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
-/* Windows likes to rename Unix-like functions */
-#define fileno _fileno
-#endif
-
-static UFILE*
-finit_owner(FILE *f,
- const char *locale,
- const char *codepage,
- UBool takeOwnership
- )
-{
- UErrorCode status = U_ZERO_ERROR;
- UFILE *result;
- if(f == NULL) {
- return 0;
- }
- result = (UFILE*) uprv_malloc(sizeof(UFILE));
- if(result == NULL) {
- return 0;
- }
-
- uprv_memset(result, 0, sizeof(UFILE));
- result->fFileno = fileno(f);
-
-#if U_PLATFORM_USES_ONLY_WIN32_API && _MSC_VER < 1900
- /*
- * Below is a very old workaround (ICU ticket:231).
- *
- * Previously, 'FILE*' from inside and outside ICU's DLL
- * were different, because they pointed into local copies
- * of the io block. At least by VS 2015 the implementation
- * is something like:
- * stdio = _acrt_iob_func(0)
- * .. which is a function call, so should return the same pointer
- * regardless of call site.
- * As of _MSC_VER 1900 this patch is retired, at 16 years old.
- */
- if (0 <= result->fFileno && result->fFileno <= 2) {
- /* stdin, stdout and stderr need to be special cased for Windows 98 */
-#if _MSC_VER >= 1400
- result->fFile = &__iob_func()[_fileno(f)];
-#else
- result->fFile = &_iob[_fileno(f)];
-#endif
- }
- else
-#endif
- {
- result->fFile = f;
- }
-
- result->str.fBuffer = result->fUCBuffer;
- result->str.fPos = result->fUCBuffer;
- result->str.fLimit = result->fUCBuffer;
-
-#if !UCONFIG_NO_FORMATTING
- /* if locale is 0, use the default */
- if(u_locbund_init(&result->str.fBundle, locale) == 0) {
- /* DO NOT FCLOSE HERE! */
- uprv_free(result);
- return 0;
- }
-#endif
-
- /* If the codepage is not "" use the ucnv_open default behavior */
- if(codepage == NULL || *codepage != '\0') {
- result->fConverter = ucnv_open(codepage, &status);
- }
- /* else result->fConverter is already memset'd to NULL. */
-
- if(U_SUCCESS(status)) {
- result->fOwnFile = takeOwnership;
- }
- else {
-#if !UCONFIG_NO_FORMATTING
- u_locbund_close(&result->str.fBundle);
-#endif
- /* DO NOT fclose here!!!!!! */
- uprv_free(result);
- result = NULL;
- }
-
- return result;
-}
-
-U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_finit(FILE *f,
- const char *locale,
- const char *codepage)
-{
- return finit_owner(f, locale, codepage, FALSE);
-}
-
-U_CAPI UFILE* U_EXPORT2
-u_fadopt(FILE *f,
- const char *locale,
- const char *codepage)
-{
- return finit_owner(f, locale, codepage, TRUE);
-}
-
-U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fopen(const char *filename,
- const char *perm,
- const char *locale,
- const char *codepage)
-{
- UFILE *result;
- FILE *systemFile = fopen(filename, perm);
- if(systemFile == 0) {
- return 0;
- }
-
- result = finit_owner(systemFile, locale, codepage, TRUE);
-
- if (!result) {
- /* Something bad happened.
- Maybe the converter couldn't be opened. */
- fclose(systemFile);
- }
-
- return result; /* not a file leak */
-}
-
-U_CAPI UFILE* U_EXPORT2
-u_fopen_u(const UChar *filename,
- const char *perm,
- const char *locale,
- const char *codepage)
-{
- UFILE *result;
- char buffer[256];
-
- u_austrcpy(buffer, filename);
-
- result = u_fopen(buffer, perm, locale, codepage);
-#if U_PLATFORM_USES_ONLY_WIN32_API
- /* Try Windows API _wfopen if the above fails. */
- if (!result) {
- FILE *systemFile = _wfopen(filename, (UChar*)perm);
- if (systemFile) {
- result = finit_owner(systemFile, locale, codepage, TRUE);
- }
- if (!result) {
- /* Something bad happened.
- Maybe the converter couldn't be opened. */
- fclose(systemFile);
- }
- }
-#endif
- return result; /* not a file leak */
-}
-
-U_CAPI UFILE* U_EXPORT2
-u_fstropen(UChar *stringBuf,
- int32_t capacity,
- const char *locale)
-{
- UFILE *result;
-
- if (capacity < 0) {
- return NULL;
- }
-
- result = (UFILE*) uprv_malloc(sizeof(UFILE));
- /* Null pointer test */
- if (result == NULL) {
- return NULL; /* Just get out. */
- }
- uprv_memset(result, 0, sizeof(UFILE));
- result->str.fBuffer = stringBuf;
- result->str.fPos = stringBuf;
- result->str.fLimit = stringBuf+capacity;
-
-#if !UCONFIG_NO_FORMATTING
- /* if locale is 0, use the default */
- if(u_locbund_init(&result->str.fBundle, locale) == 0) {
- /* DO NOT FCLOSE HERE! */
- uprv_free(result);
- return 0;
- }
-#endif
-
- return result;
-}
-
-U_CAPI UBool U_EXPORT2
-u_feof(UFILE *f)
-{
- UBool endOfBuffer;
- if (f == NULL) {
- return TRUE;
- }
- endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit);
- if (f->fFile != NULL) {
- return endOfBuffer && feof(f->fFile);
- }
- return endOfBuffer;
-}
-
-U_CAPI void U_EXPORT2
-u_fflush(UFILE *file)
-{
- ufile_flush_translit(file);
- ufile_flush_io(file);
- if (file->fFile) {
- fflush(file->fFile);
- }
- else if (file->str.fPos < file->str.fLimit) {
- *(file->str.fPos++) = 0;
- }
- /* TODO: flush input */
-}
-
-U_CAPI void
-u_frewind(UFILE *file)
-{
- u_fflush(file);
- ucnv_reset(file->fConverter);
- if (file->fFile) {
- rewind(file->fFile);
- file->str.fLimit = file->fUCBuffer;
- file->str.fPos = file->fUCBuffer;
- }
- else {
- file->str.fPos = file->str.fBuffer;
- }
-}
-
-U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fclose(UFILE *file)
-{
- if (file) {
- u_fflush(file);
- ufile_close_translit(file);
-
- if(file->fOwnFile)
- fclose(file->fFile);
-
-#if !UCONFIG_NO_FORMATTING
- u_locbund_close(&file->str.fBundle);
-#endif
-
- ucnv_close(file->fConverter);
- uprv_free(file);
- }
-}
-
-U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetfile( UFILE *f)
-{
- return f->fFile;
-}
-
-#if !UCONFIG_NO_FORMATTING
-
-U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetlocale( UFILE *file)
-{
- return file->str.fBundle.fLocale;
-}
-
-U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fsetlocale(UFILE *file,
- const char *locale)
-{
- u_locbund_close(&file->str.fBundle);
-
- return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0;
-}
-
-#endif
-
-U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetcodepage(UFILE *file)
-{
- UErrorCode status = U_ZERO_ERROR;
- const char *codepage = NULL;
-
- if (file->fConverter) {
- codepage = ucnv_getName(file->fConverter, &status);
- if(U_FAILURE(status))
- return 0;
- }
- return codepage;
-}
-
-U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fsetcodepage( const char *codepage,
- UFILE *file)
-{
- UErrorCode status = U_ZERO_ERROR;
- int32_t retVal = -1;
-
- /* We use the normal default codepage for this system, and not the one for the locale. */
- if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) {
- ucnv_close(file->fConverter);
- file->fConverter = ucnv_open(codepage, &status);
- if(U_SUCCESS(status)) {
- retVal = 0;
- }
- }
- return retVal;
-}
-
-
-U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetConverter(UFILE *file)
-{
- return file->fConverter;
-}
-#if !UCONFIG_NO_FORMATTING
-U_CAPI const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *file)
-{
- return u_locbund_getNumberFormat(&file->str.fBundle, UNUM_DECIMAL);
-}
-#endif
-
-#endif
diff --git a/tools/icu/patches/55/source/io/ufile.c b/tools/icu/patches/55/source/io/ufile.c
deleted file mode 100644
index ab9f70a3d5..0000000000
--- a/tools/icu/patches/55/source/io/ufile.c
+++ /dev/null
@@ -1,360 +0,0 @@
-/*
-******************************************************************************
-*
-* Copyright (C) 1998-2015, International Business Machines
-* Corporation and others. All Rights Reserved.
-*
-******************************************************************************
-*
-* File ufile.c
-*
-* Modification History:
-*
-* Date Name Description
-* 11/19/98 stephen Creation.
-* 03/12/99 stephen Modified for new C API.
-* 06/16/99 stephen Changed T_LocaleBundle to u_locbund
-* 07/19/99 stephen Fixed to use ucnv's default codepage.
-******************************************************************************
-*/
-
-/*
- * fileno is not declared when building with GCC in strict mode.
- */
-#if defined(__GNUC__) && defined(__STRICT_ANSI__)
-#undef __STRICT_ANSI__
-#endif
-
-#include "locmap.h"
-#include "unicode/ustdio.h"
-
-#if !UCONFIG_NO_CONVERSION
-
-#include "ufile.h"
-#include "unicode/uloc.h"
-#include "unicode/ures.h"
-#include "unicode/ucnv.h"
-#include "unicode/ustring.h"
-#include "cstring.h"
-#include "cmemory.h"
-
-#if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
-/* Windows likes to rename Unix-like functions */
-#define fileno _fileno
-#endif
-
-static UFILE*
-finit_owner(FILE *f,
- const char *locale,
- const char *codepage,
- UBool takeOwnership
- )
-{
- UErrorCode status = U_ZERO_ERROR;
- UFILE *result;
- if(f == NULL) {
- return 0;
- }
- result = (UFILE*) uprv_malloc(sizeof(UFILE));
- if(result == NULL) {
- return 0;
- }
-
- uprv_memset(result, 0, sizeof(UFILE));
- result->fFileno = fileno(f);
-
-#if U_PLATFORM_USES_ONLY_WIN32_API && _MSC_VER < 1900
- /*
- * Below is a very old workaround (ICU ticket:231).
- *
- * Previously, 'FILE*' from inside and outside ICU's DLL
- * were different, because they pointed into local copies
- * of the io block. At least by VS 2015 the implementation
- * is something like:
- * stdio = _acrt_iob_func(0)
- * .. which is a function call, so should return the same pointer
- * regardless of call site.
- * As of _MSC_VER 1900 this patch is retired, at 16 years old.
- */
- if (0 <= result->fFileno && result->fFileno <= 2) {
- /* stdin, stdout and stderr need to be special cased for Windows 98 */
-#if _MSC_VER >= 1400
- result->fFile = &__iob_func()[_fileno(f)];
-#else
- result->fFile = &_iob[_fileno(f)];
-#endif
- }
- else
-#endif
- {
- result->fFile = f;
- }
-
- result->str.fBuffer = result->fUCBuffer;
- result->str.fPos = result->fUCBuffer;
- result->str.fLimit = result->fUCBuffer;
-
-#if !UCONFIG_NO_FORMATTING
- /* if locale is 0, use the default */
- if(u_locbund_init(&result->str.fBundle, locale) == 0) {
- /* DO NOT FCLOSE HERE! */
- uprv_free(result);
- return 0;
- }
-#endif
-
- /* If the codepage is not "" use the ucnv_open default behavior */
- if(codepage == NULL || *codepage != '\0') {
- result->fConverter = ucnv_open(codepage, &status);
- }
- /* else result->fConverter is already memset'd to NULL. */
-
- if(U_SUCCESS(status)) {
- result->fOwnFile = takeOwnership;
- }
- else {
-#if !UCONFIG_NO_FORMATTING
- u_locbund_close(&result->str.fBundle);
-#endif
- /* DO NOT fclose here!!!!!! */
- uprv_free(result);
- result = NULL;
- }
-
- return result;
-}
-
-U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_finit(FILE *f,
- const char *locale,
- const char *codepage)
-{
- return finit_owner(f, locale, codepage, FALSE);
-}
-
-U_CAPI UFILE* U_EXPORT2
-u_fadopt(FILE *f,
- const char *locale,
- const char *codepage)
-{
- return finit_owner(f, locale, codepage, TRUE);
-}
-
-U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fopen(const char *filename,
- const char *perm,
- const char *locale,
- const char *codepage)
-{
- UFILE *result;
- FILE *systemFile = fopen(filename, perm);
- if(systemFile == 0) {
- return 0;
- }
-
- result = finit_owner(systemFile, locale, codepage, TRUE);
-
- if (!result) {
- /* Something bad happened.
- Maybe the converter couldn't be opened. */
- fclose(systemFile);
- }
-
- return result; /* not a file leak */
-}
-
-U_CAPI UFILE* U_EXPORT2
-u_fopen_u(const UChar *filename,
- const char *perm,
- const char *locale,
- const char *codepage)
-{
- UFILE *result;
- char buffer[256];
-
- u_austrcpy(buffer, filename);
-
- result = u_fopen(buffer, perm, locale, codepage);
-#if U_PLATFORM_USES_ONLY_WIN32_API
- /* Try Windows API _wfopen if the above fails. */
- if (!result) {
- FILE *systemFile = _wfopen(filename, (UChar*)perm);
- if (systemFile) {
- result = finit_owner(systemFile, locale, codepage, TRUE);
- }
- if (!result) {
- /* Something bad happened.
- Maybe the converter couldn't be opened. */
- fclose(systemFile);
- }
- }
-#endif
- return result; /* not a file leak */
-}
-
-U_CAPI UFILE* U_EXPORT2
-u_fstropen(UChar *stringBuf,
- int32_t capacity,
- const char *locale)
-{
- UFILE *result;
-
- if (capacity < 0) {
- return NULL;
- }
-
- result = (UFILE*) uprv_malloc(sizeof(UFILE));
- /* Null pointer test */
- if (result == NULL) {
- return NULL; /* Just get out. */
- }
- uprv_memset(result, 0, sizeof(UFILE));
- result->str.fBuffer = stringBuf;
- result->str.fPos = stringBuf;
- result->str.fLimit = stringBuf+capacity;
-
-#if !UCONFIG_NO_FORMATTING
- /* if locale is 0, use the default */
- if(u_locbund_init(&result->str.fBundle, locale) == 0) {
- /* DO NOT FCLOSE HERE! */
- uprv_free(result);
- return 0;
- }
-#endif
-
- return result;
-}
-
-U_CAPI UBool U_EXPORT2
-u_feof(UFILE *f)
-{
- UBool endOfBuffer;
- if (f == NULL) {
- return TRUE;
- }
- endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit);
- if (f->fFile != NULL) {
- return endOfBuffer && feof(f->fFile);
- }
- return endOfBuffer;
-}
-
-U_CAPI void U_EXPORT2
-u_fflush(UFILE *file)
-{
- ufile_flush_translit(file);
- ufile_flush_io(file);
- if (file->fFile) {
- fflush(file->fFile);
- }
- else if (file->str.fPos < file->str.fLimit) {
- *(file->str.fPos++) = 0;
- }
- /* TODO: flush input */
-}
-
-U_CAPI void
-u_frewind(UFILE *file)
-{
- u_fflush(file);
- ucnv_reset(file->fConverter);
- if (file->fFile) {
- rewind(file->fFile);
- file->str.fLimit = file->fUCBuffer;
- file->str.fPos = file->fUCBuffer;
- }
- else {
- file->str.fPos = file->str.fBuffer;
- }
-}
-
-U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fclose(UFILE *file)
-{
- if (file) {
- u_fflush(file);
- ufile_close_translit(file);
-
- if(file->fOwnFile)
- fclose(file->fFile);
-
-#if !UCONFIG_NO_FORMATTING
- u_locbund_close(&file->str.fBundle);
-#endif
-
- ucnv_close(file->fConverter);
- uprv_free(file);
- }
-}
-
-U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetfile( UFILE *f)
-{
- return f->fFile;
-}
-
-#if !UCONFIG_NO_FORMATTING
-
-U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetlocale( UFILE *file)
-{
- return file->str.fBundle.fLocale;
-}
-
-U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fsetlocale(UFILE *file,
- const char *locale)
-{
- u_locbund_close(&file->str.fBundle);
-
- return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0;
-}
-
-#endif
-
-U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetcodepage(UFILE *file)
-{
- UErrorCode status = U_ZERO_ERROR;
- const char *codepage = NULL;
-
- if (file->fConverter) {
- codepage = ucnv_getName(file->fConverter, &status);
- if(U_FAILURE(status))
- return 0;
- }
- return codepage;
-}
-
-U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fsetcodepage( const char *codepage,
- UFILE *file)
-{
- UErrorCode status = U_ZERO_ERROR;
- int32_t retVal = -1;
-
- /* We use the normal default codepage for this system, and not the one for the locale. */
- if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) {
- ucnv_close(file->fConverter);
- file->fConverter = ucnv_open(codepage, &status);
- if(U_SUCCESS(status)) {
- retVal = 0;
- }
- }
- return retVal;
-}
-
-
-U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
-u_fgetConverter(UFILE *file)
-{
- return file->fConverter;
-}
-#if !UCONFIG_NO_FORMATTING
-U_CAPI const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *file)
-{
- return u_locbund_getNumberFormat(&file->str.fBundle, UNUM_DECIMAL);
-}
-#endif
-
-#endif
diff --git a/tools/icu/patches/58/source/i18n/digitlst.cpp b/tools/icu/patches/58/source/i18n/digitlst.cpp
deleted file mode 100644
index 9711a6cc22..0000000000
--- a/tools/icu/patches/58/source/i18n/digitlst.cpp
+++ /dev/null
@@ -1,1095 +0,0 @@
-// Copyright (C) 2016 and later: Unicode, Inc. and others.
-// License & terms of use: http://www.unicode.org/copyright.html
-/*
-**********************************************************************
-* Copyright (C) 1997-2015, International Business Machines
-* Corporation and others. All Rights Reserved.
-**********************************************************************
-*
-* File DIGITLST.CPP
-*
-* Modification History:
-*
-* Date Name Description
-* 03/21/97 clhuang Converted from java.
-* 03/21/97 clhuang Implemented with new APIs.
-* 03/27/97 helena Updated to pass the simple test after code review.
-* 03/31/97 aliu Moved isLONG_MIN to here, and fixed it.
-* 04/15/97 aliu Changed MAX_COUNT to DBL_DIG. Changed Digit to char.
-* Reworked representation by replacing fDecimalAt
-* with fExponent.
-* 04/16/97 aliu Rewrote set() and getDouble() to use sprintf/atof
-* to do digit conversion.
-* 09/09/97 aliu Modified for exponential notation support.
-* 08/02/98 stephen Added nearest/even rounding
-* Fixed bug in fitsIntoLong
-******************************************************************************
-*/
-
-#if defined(__CYGWIN__) && !defined(_GNU_SOURCE)
-#define _GNU_SOURCE
-#endif
-
-#include "digitlst.h"
-
-#if !UCONFIG_NO_FORMATTING
-
-#include "unicode/putil.h"
-#include "charstr.h"
-#include "cmemory.h"
-#include "cstring.h"
-#include "mutex.h"
-#include "putilimp.h"
-#include "uassert.h"
-#include "digitinterval.h"
-#include "ucln_in.h"
-#include "umutex.h"
-#include <stdlib.h>
-#include <limits.h>
-#include <string.h>
-#include <stdio.h>
-#include <limits>
-
-#if !defined(U_USE_STRTOD_L)
-# if U_PLATFORM_USES_ONLY_WIN32_API
-# define U_USE_STRTOD_L 1
-# elif defined(U_HAVE_STRTOD_L)
-# define U_USE_STRTOD_L U_HAVE_STRTOD_L
-# else
-# define U_USE_STRTOD_L 0
-# endif
-#endif
-
-// Patch for http://bugs.icu-project.org/trac/ticket/12822
-#if U_USE_STRTOD_L
-# if U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_CYGWIN
-# include <locale.h>
-# else
-# include <xlocale.h>
-# endif
-#endif
-
-// ***************************************************************************
-// class DigitList
-// A wrapper onto decNumber.
-// Used to be standalone.
-// ***************************************************************************
-
-/**
- * This is the zero digit. The base for the digits returned by getDigit()
- * Note that it is the platform invariant digit, and is not Unicode.
- */
-#define kZero '0'
-
-
-/* Only for 32 bit numbers. Ignore the negative sign. */
-//static const char LONG_MIN_REP[] = "2147483648";
-//static const char I64_MIN_REP[] = "9223372036854775808";
-
-
-U_NAMESPACE_BEGIN
-
-// -------------------------------------
-// default constructor
-
-DigitList::DigitList()
-{
- uprv_decContextDefault(&fContext, DEC_INIT_BASE);
- fContext.traps = 0;
- uprv_decContextSetRounding(&fContext, DEC_ROUND_HALF_EVEN);
- fContext.digits = fStorage.getCapacity();
-
- fDecNumber = fStorage.getAlias();
- uprv_decNumberZero(fDecNumber);
-
- internalSetDouble(0.0);
-}
-
-// -------------------------------------
-
-DigitList::~DigitList()
-{
-}
-
-// -------------------------------------
-// copy constructor
-
-DigitList::DigitList(const DigitList &other)
-{
- fDecNumber = fStorage.getAlias();
- *this = other;
-}
-
-
-// -------------------------------------
-// assignment operator
-
-DigitList&
-DigitList::operator=(const DigitList& other)
-{
- if (this != &other)
- {
- uprv_memcpy(&fContext, &other.fContext, sizeof(decContext));
-
- if (other.fStorage.getCapacity() > fStorage.getCapacity()) {
- fDecNumber = fStorage.resize(other.fStorage.getCapacity());
- }
- // Always reset the fContext.digits, even if fDecNumber was not reallocated,
- // because above we copied fContext from other.fContext.
- fContext.digits = fStorage.getCapacity();
- uprv_decNumberCopy(fDecNumber, other.fDecNumber);
-
- {
- // fDouble is lazily created and cached.
- // Avoid potential races with that happening with other.fDouble
- // while we are doing the assignment.
- Mutex mutex;
-
- if(other.fHave==kDouble) {
- fUnion.fDouble = other.fUnion.fDouble;
- }
- fHave = other.fHave;
- }
- }
- return *this;
-}
-
-// -------------------------------------
-// operator == (does not exactly match the old DigitList function)
-
-UBool
-DigitList::operator==(const DigitList& that) const
-{
- if (this == &that) {
- return TRUE;
- }
- decNumber n; // Has space for only a none digit value.
- decContext c;
- uprv_decContextDefault(&c, DEC_INIT_BASE);
- c.digits = 1;
- c.traps = 0;
-
- uprv_decNumberCompare(&n, this->fDecNumber, that.fDecNumber, &c);
- UBool result = decNumberIsZero(&n);
- return result;
-}
-
-// -------------------------------------
-// comparison function. Returns
-// Not Comparable : -2
-// < : -1
-// == : 0
-// > : +1
-int32_t DigitList::compare(const DigitList &other) {
- decNumber result;
- int32_t savedDigits = fContext.digits;
- fContext.digits = 1;
- uprv_decNumberCompare(&result, this->fDecNumber, other.fDecNumber, &fContext);
- fContext.digits = savedDigits;
- if (decNumberIsZero(&result)) {
- return 0;
- } else if (decNumberIsSpecial(&result)) {
- return -2;
- } else if (result.bits & DECNEG) {
- return -1;
- } else {
- return 1;
- }
-}
-
-
-// -------------------------------------
-// Reduce - remove trailing zero digits.
-void
-DigitList::reduce() {
- uprv_decNumberReduce(fDecNumber, fDecNumber, &fContext);
-}
-
-
-// -------------------------------------
-// trim - remove trailing fraction zero digits.
-void
-DigitList::trim() {
- uprv_decNumberTrim(fDecNumber);
-}
-
-// -------------------------------------
-// Resets the digit list; sets all the digits to zero.
-
-void
-DigitList::clear()
-{
- uprv_decNumberZero(fDecNumber);
- uprv_decContextSetRounding(&fContext, DEC_ROUND_HALF_EVEN);
- internalSetDouble(0.0);
-}
-
-
-/**
- * Formats a int64_t number into a base 10 string representation, and NULL terminates it.
- * @param number The number to format
- * @param outputStr The string to output to. Must be at least MAX_DIGITS+2 in length (21),
- * to hold the longest int64_t value.
- * @return the number of digits written, not including the sign.
- */
-static int32_t
-formatBase10(int64_t number, char *outputStr) {
- // The number is output backwards, starting with the LSD.
- // Fill the buffer from the far end. After the number is complete,
- // slide the string contents to the front.
-
- const int32_t MAX_IDX = MAX_DIGITS+2;
- int32_t destIdx = MAX_IDX;
- outputStr[--destIdx] = 0;
-
- int64_t n = number;
- if (number < 0) { // Negative numbers are slightly larger than a postive
- outputStr[--destIdx] = (char)(-(n % 10) + kZero);
- n /= -10;
- }
- do {
- outputStr[--destIdx] = (char)(n % 10 + kZero);
- n /= 10;
- } while (n > 0);
-
- if (number < 0) {
- outputStr[--destIdx] = '-';
- }
-
- // Slide the number to the start of the output str
- U_ASSERT(destIdx >= 0);
- int32_t length = MAX_IDX - destIdx;
- uprv_memmove(outputStr, outputStr+MAX_IDX-length, length);
-
- return length;
-}
-
-
-// -------------------------------------
-//
-// setRoundingMode()
-// For most modes, the meaning and names are the same between the decNumber library
-// (which DigitList follows) and the ICU Formatting Rounding Mode values.
-// The flag constants are different, however.
-//
-// Note that ICU's kRoundingUnnecessary is not implemented directly by DigitList.
-// This mode, inherited from Java, means that numbers that would not format exactly
-// will return an error when formatting is attempted.
-
-void
-DigitList::setRoundingMode(DecimalFormat::ERoundingMode m) {
- enum rounding r;
-
- switch (m) {
- case DecimalFormat::kRoundCeiling: r = DEC_ROUND_CEILING; break;
- case DecimalFormat::kRoundFloor: r = DEC_ROUND_FLOOR; break;
- case DecimalFormat::kRoundDown: r = DEC_ROUND_DOWN; break;
- case DecimalFormat::kRoundUp: r = DEC_ROUND_UP; break;
- case DecimalFormat::kRoundHalfEven: r = DEC_ROUND_HALF_EVEN; break;
- case DecimalFormat::kRoundHalfDown: r = DEC_ROUND_HALF_DOWN; break;
- case DecimalFormat::kRoundHalfUp: r = DEC_ROUND_HALF_UP; break;
- case DecimalFormat::kRoundUnnecessary: r = DEC_ROUND_HALF_EVEN; break;
- default:
- // TODO: how to report the problem?
- // Leave existing mode unchanged.
- r = uprv_decContextGetRounding(&fContext);
- }
- uprv_decContextSetRounding(&fContext, r);
-
-}
-
-
-// -------------------------------------
-
-void
-DigitList::setPositive(UBool s) {
- if (s) {
- fDecNumber->bits &= ~DECNEG;
- } else {
- fDecNumber->bits |= DECNEG;
- }
- internalClear();
-}
-// -------------------------------------
-
-void
-DigitList::setDecimalAt(int32_t d) {
- U_ASSERT((fDecNumber->bits & DECSPECIAL) == 0); // Not Infinity or NaN
- U_ASSERT(d-1>-999999999);
- U_ASSERT(d-1< 999999999);
- int32_t adjustedDigits = fDecNumber->digits;
- if (decNumberIsZero(fDecNumber)) {
- // Account for difference in how zero is represented between DigitList & decNumber.
- adjustedDigits = 0;
- }
- fDecNumber->exponent = d - adjustedDigits;
- internalClear();
-}
-
-int32_t
-DigitList::getDecimalAt() {
- U_ASSERT((fDecNumber->bits & DECSPECIAL) == 0); // Not Infinity or NaN
- if (decNumberIsZero(fDecNumber) || ((fDecNumber->bits & DECSPECIAL) != 0)) {
- return fDecNumber->exponent; // Exponent should be zero for these cases.
- }
- return fDecNumber->exponent + fDecNumber->digits;
-}
-
-void
-DigitList::setCount(int32_t c) {
- U_ASSERT(c <= fContext.digits);
- if (c == 0) {
- // For a value of zero, DigitList sets all fields to zero, while
- // decNumber keeps one digit (with that digit being a zero)
- c = 1;
- fDecNumber->lsu[0] = 0;
- }
- fDecNumber->digits = c;
- internalClear();
-}
-
-int32_t
-DigitList::getCount() const {
- if (decNumberIsZero(fDecNumber) && fDecNumber->exponent==0) {
- // The extra test for exponent==0 is needed because parsing sometimes appends
- // zero digits. It's bogus, decimalFormatter parsing needs to be cleaned up.
- return 0;
- } else {
- return fDecNumber->digits;
- }
-}
-
-void
-DigitList::setDigit(int32_t i, char v) {
- int32_t count = fDecNumber->digits;
- U_ASSERT(i<count);
- U_ASSERT(v>='0' && v<='9');
- v &= 0x0f;
- fDecNumber->lsu[count-i-1] = v;
- internalClear();
-}
-
-char
-DigitList::getDigit(int32_t i) {
- int32_t count = fDecNumber->digits;
- U_ASSERT(i<count);
- return fDecNumber->lsu[count-i-1] + '0';
-}
-
-// copied from DigitList::getDigit()
-uint8_t
-DigitList::getDigitValue(int32_t i) {
- int32_t count = fDecNumber->digits;
- U_ASSERT(i<count);
- return fDecNumber->lsu[count-i-1];
-}
-
-// -------------------------------------
-// Appends the digit to the digit list if it's not out of scope.
-// Ignores the digit, otherwise.
-//
-// This function is horribly inefficient to implement with decNumber because
-// the digits are stored least significant first, which requires moving all
-// existing digits down one to make space for the new one to be appended.
-//
-void
-DigitList::append(char digit)
-{
- U_ASSERT(digit>='0' && digit<='9');
- // Ignore digits which exceed the precision we can represent
- // And don't fix for larger precision. Fix callers instead.
- if (decNumberIsZero(fDecNumber)) {
- // Zero needs to be special cased because of the difference in the way
- // that the old DigitList and decNumber represent it.
- // digit cout was zero for digitList, is one for decNumber
- fDecNumber->lsu[0] = digit & 0x0f;
- fDecNumber->digits = 1;
- fDecNumber->exponent--; // To match the old digit list implementation.
- } else {
- int32_t nDigits = fDecNumber->digits;
- if (nDigits < fContext.digits) {
- int i;
- for (i=nDigits; i>0; i--) {
- fDecNumber->lsu[i] = fDecNumber->lsu[i-1];
- }
- fDecNumber->lsu[0] = digit & 0x0f;
- fDecNumber->digits++;
- // DigitList emulation - appending doesn't change the magnitude of existing
- // digits. With decNumber's decimal being after the
- // least signficant digit, we need to adjust the exponent.
- fDecNumber->exponent--;
- }
- }
- internalClear();
-}
-
-// -------------------------------------
-
-/**
- * Currently, getDouble() depends on strtod() to do its conversion.
- *
- * WARNING!!
- * This is an extremely costly function. ~1/2 of the conversion time
- * can be linked to this function.
- */
-double
-DigitList::getDouble() const
-{
- {
- Mutex mutex;
- if (fHave == kDouble) {
- return fUnion.fDouble;
- }
- }
-
- double tDouble = 0.0;
- if (isZero()) {
- tDouble = 0.0;
- if (decNumberIsNegative(fDecNumber)) {
- tDouble /= -1;
- }
- } else if (isInfinite()) {
- if (std::numeric_limits<double>::has_infinity) {
- tDouble = std::numeric_limits<double>::infinity();
- } else {
- tDouble = std::numeric_limits<double>::max();
- }
- if (!isPositive()) {
- tDouble = -tDouble; //this was incorrectly "-fDouble" originally.
- }
- } else {
- MaybeStackArray<char, MAX_DBL_DIGITS+18> s;
- // Note: 14 is a magic constant from the decNumber library documentation,
- // the max number of extra characters beyond the number of digits
- // needed to represent the number in string form. Add a few more
- // for the additional digits we retain.
-
- // Round down to appx. double precision, if the number is longer than that.
- // Copy the number first, so that we don't modify the original.
- if (getCount() > MAX_DBL_DIGITS + 3) {
- DigitList numToConvert(*this);
- numToConvert.reduce(); // Removes any trailing zeros, so that digit count is good.
- numToConvert.round(MAX_DBL_DIGITS+3);
- uprv_decNumberToString(numToConvert.fDecNumber, s.getAlias());
- // TODO: how many extra digits should be included for an accurate conversion?
- } else {
- uprv_decNumberToString(this->fDecNumber, s.getAlias());
- }
- U_ASSERT(uprv_strlen(&s[0]) < MAX_DBL_DIGITS+18);
-
- char *end = NULL;
- tDouble = decimalStrToDouble(s.getAlias(), &end);
- }
- {
- Mutex mutex;
- DigitList *nonConstThis = const_cast<DigitList *>(this);
- nonConstThis->internalSetDouble(tDouble);
- }
- return tDouble;
-}
-
-#if U_USE_STRTOD_L && U_PLATFORM_USES_ONLY_WIN32_API
-# define locale_t _locale_t
-# define freelocale _free_locale
-# define strtod_l _strtod_l
-#endif
-
-#if U_USE_STRTOD_L
-static locale_t gCLocale = (locale_t)0;
-#endif
-static icu::UInitOnce gCLocaleInitOnce = U_INITONCE_INITIALIZER;
-
-U_CDECL_BEGIN
-// Cleanup callback func
-static UBool U_CALLCONV digitList_cleanup(void)
-{
-#if U_USE_STRTOD_L
- if (gCLocale != (locale_t)0) {
- freelocale(gCLocale);
- }
-#endif
- return TRUE;
-}
-// C Locale initialization func
-static void U_CALLCONV initCLocale(void) {
- ucln_i18n_registerCleanup(UCLN_I18N_DIGITLIST, digitList_cleanup);
-#if U_USE_STRTOD_L
-# if U_PLATFORM_USES_ONLY_WIN32_API
- gCLocale = _create_locale(LC_ALL, "C");
-# else
- gCLocale = newlocale(LC_ALL_MASK, "C", (locale_t)0);
-# endif
-#endif
-}
-U_CDECL_END
-
-double
-DigitList::decimalStrToDouble(char *decstr, char **end) {
- umtx_initOnce(gCLocaleInitOnce, &initCLocale);
-#if U_USE_STRTOD_L
- return strtod_l(decstr, end, gCLocale);
-#else
- char *decimalPt = strchr(decstr, '.');
- if (decimalPt) {
- // We need to know the decimal separator character that will be used with strtod().
- // Depends on the C runtime global locale.
- // Most commonly is '.'
- char rep[MAX_DIGITS];
- sprintf(rep, "%+1.1f", 1.0);
- *decimalPt = rep[2];
- }
- return uprv_strtod(decstr, end);
-#endif
-}
-
-// -------------------------------------
-
-/**
- * convert this number to an int32_t. Round if there is a fractional part.
- * Return zero if the number cannot be represented.
- */
-int32_t DigitList::getLong() /*const*/
-{
- int32_t result = 0;
- if (getUpperExponent() > 10) {
- // Overflow, absolute value too big.
- return result;
- }
- if (fDecNumber->exponent != 0) {
- // Force to an integer, with zero exponent, rounding if necessary.
- // (decNumberToInt32 will only work if the exponent is exactly zero.)
- DigitList copy(*this);
- DigitList zero;
- uprv_decNumberQuantize(copy.fDecNumber, copy.fDecNumber, zero.fDecNumber, &fContext);
- result = uprv_decNumberToInt32(copy.fDecNumber, &fContext);
- } else {
- result = uprv_decNumberToInt32(fDecNumber, &fContext);
- }
- return result;
-}
-
-
-/**
- * convert this number to an int64_t. Truncate if there is a fractional part.
- * Return zero if the number cannot be represented.
- */
-int64_t DigitList::getInt64() /*const*/ {
- // TODO: fast conversion if fHave == fDouble
-
- // Truncate if non-integer.
- // Return 0 if out of range.
- // Range of in64_t is -9223372036854775808 to 9223372036854775807 (19 digits)
- //
- if (getUpperExponent() > 19) {
- // Overflow, absolute value too big.
- return 0;
- }
-
- // The number of integer digits may differ from the number of digits stored
- // in the decimal number.
- // for 12.345 numIntDigits = 2, number->digits = 5
- // for 12E4 numIntDigits = 6, number->digits = 2
- // The conversion ignores the fraction digits in the first case,
- // and fakes up extra zero digits in the second.
- // TODO: It would be faster to store a table of powers of ten to multiply by
- // instead of looping over zero digits, multiplying each time.
-
- int32_t numIntDigits = getUpperExponent();
- uint64_t value = 0;
- for (int32_t i = 0; i < numIntDigits; i++) {
- // Loop is iterating over digits starting with the most significant.
- // Numbers are stored with the least significant digit at index zero.
- int32_t digitIndex = fDecNumber->digits - i - 1;
- int32_t v = (digitIndex >= 0) ? fDecNumber->lsu[digitIndex] : 0;
- value = value * (uint64_t)10 + (uint64_t)v;
- }
-
- if (decNumberIsNegative(fDecNumber)) {
- value = ~value;
- value += 1;
- }
- int64_t svalue = (int64_t)value;
-
- // Check overflow. It's convenient that the MSD is 9 only on overflow, the amount of
- // overflow can't wrap too far. The test will also fail -0, but
- // that does no harm; the right answer is 0.
- if (numIntDigits == 19) {
- if (( decNumberIsNegative(fDecNumber) && svalue>0) ||
- (!decNumberIsNegative(fDecNumber) && svalue<0)) {
- svalue = 0;
- }
- }
-
- return svalue;
-}
-
-
-/**
- * Return a string form of this number.
- * Format is as defined by the decNumber library, for interchange of
- * decimal numbers.
- */
-void DigitList::getDecimal(CharString &str, UErrorCode &status) {
- if (U_FAILURE(status)) {
- return;
- }
-
- // A decimal number in string form can, worst case, be 14 characters longer
- // than the number of digits. So says the decNumber library doc.
- int32_t maxLength = fDecNumber->digits + 14;
- int32_t capacity = 0;
- char *buffer = str.clear().getAppendBuffer(maxLength, 0, capacity, status);
- if (U_FAILURE(status)) {
- return; // Memory allocation error on growing the string.
- }
- U_ASSERT(capacity >= maxLength);
- uprv_decNumberToString(this->fDecNumber, buffer);
- U_ASSERT((int32_t)uprv_strlen(buffer) <= maxLength);
- str.append(buffer, -1, status);
-}
-
-/**
- * Return true if this is an integer value that can be held
- * by an int32_t type.
- */
-UBool
-DigitList::fitsIntoLong(UBool ignoreNegativeZero) /*const*/
-{
- if (decNumberIsSpecial(this->fDecNumber)) {
- // NaN or Infinity. Does not fit in int32.
- return FALSE;
- }
- uprv_decNumberTrim(this->fDecNumber);
- if (fDecNumber->exponent < 0) {
- // Number contains fraction digits.
- return FALSE;
- }
- if (decNumberIsZero(this->fDecNumber) && !ignoreNegativeZero &&
- (fDecNumber->bits & DECNEG) != 0) {
- // Negative Zero, not ingored. Cannot represent as a long.
- return FALSE;
- }
- if (getUpperExponent() < 10) {
- // The number is 9 or fewer digits.
- // The max and min int32 are 10 digts, so this number fits.
- // This is the common case.
- return TRUE;
- }
-
- // TODO: Should cache these constants; construction is relatively costly.
- // But not of huge consequence; they're only needed for 10 digit ints.
- UErrorCode status = U_ZERO_ERROR;
- DigitList min32; min32.set("-2147483648", status);
- if (this->compare(min32) < 0) {
- return FALSE;
- }
- DigitList max32; max32.set("2147483647", status);
- if (this->compare(max32) > 0) {
- return FALSE;
- }
- if (U_FAILURE(status)) {
- return FALSE;
- }
- return true;
-}
-
-
-
-/**
- * Return true if the number represented by this object can fit into
- * a long.
- */
-UBool
-DigitList::fitsIntoInt64(UBool ignoreNegativeZero) /*const*/
-{
- if (decNumberIsSpecial(this->fDecNumber)) {
- // NaN or Infinity. Does not fit in int32.
- return FALSE;
- }
- uprv_decNumberTrim(this->fDecNumber);
- if (fDecNumber->exponent < 0) {
- // Number contains fraction digits.
- return FALSE;
- }
- if (decNumberIsZero(this->fDecNumber) && !ignoreNegativeZero &&
- (fDecNumber->bits & DECNEG) != 0) {
- // Negative Zero, not ingored. Cannot represent as a long.
- return FALSE;
- }
- if (getUpperExponent() < 19) {
- // The number is 18 or fewer digits.
- // The max and min int64 are 19 digts, so this number fits.
- // This is the common case.
- return TRUE;
- }
-
- // TODO: Should cache these constants; construction is relatively costly.
- // But not of huge consequence; they're only needed for 19 digit ints.
- UErrorCode status = U_ZERO_ERROR;
- DigitList min64; min64.set("-9223372036854775808", status);
- if (this->compare(min64) < 0) {
- return FALSE;
- }
- DigitList max64; max64.set("9223372036854775807", status);
- if (this->compare(max64) > 0) {
- return FALSE;
- }
- if (U_FAILURE(status)) {
- return FALSE;
- }
- return true;
-}
-
-
-// -------------------------------------
-
-void
-DigitList::set(int32_t source)
-{
- set((int64_t)source);
- internalSetDouble(source);
-}
-
-// -------------------------------------
-/**
- * Set an int64, via decnumber
- */
-void
-DigitList::set(int64_t source)
-{
- char str[MAX_DIGITS+2]; // Leave room for sign and trailing nul.
- formatBase10(source, str);
- U_ASSERT(uprv_strlen(str) < sizeof(str));
-
- uprv_decNumberFromString(fDecNumber, str, &fContext);
- internalSetDouble(static_cast<double>(source));
-}
-
-// -------------------------------------
-/**
- * Set the DigitList from a decimal number string.
- *
- * The incoming string _must_ be nul terminated, even though it is arriving
- * as a StringPiece because that is what the decNumber library wants.
- * We can get away with this for an internal function; it would not
- * be acceptable for a public API.
- */
-void
-DigitList::set(StringPiece source, UErrorCode &status, uint32_t /*fastpathBits*/) {
- if (U_FAILURE(status)) {
- return;
- }
-
-#if 0
- if(fastpathBits==(kFastpathOk|kNoDecimal)) {
- int32_t size = source.size();
- const char *data = source.data();
- int64_t r = 0;
- int64_t m = 1;
- // fast parse
- while(size>0) {
- char ch = data[--size];
- if(ch=='+') {
- break;
- } else if(ch=='-') {
- r = -r;
- break;
- } else {
- int64_t d = ch-'0';
- //printf("CH[%d]=%c, %d, *=%d\n", size,ch, (int)d, (int)m);
- r+=(d)*m;
- m *= 10;
- }
- }
- //printf("R=%d\n", r);
- set(r);
- } else
-#endif
- {
- // Figure out a max number of digits to use during the conversion, and
- // resize the number up if necessary.
- int32_t numDigits = source.length();
- if (numDigits > fContext.digits) {
- // fContext.digits == fStorage.getCapacity()
- decNumber *t = fStorage.resize(numDigits, fStorage.getCapacity());
- if (t == NULL) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- fDecNumber = t;
- fContext.digits = numDigits;
- }
-
- fContext.status = 0;
- uprv_decNumberFromString(fDecNumber, source.data(), &fContext);
- if ((fContext.status & DEC_Conversion_syntax) != 0) {
- status = U_DECIMAL_NUMBER_SYNTAX_ERROR;
- }
- }
- internalClear();
-}
-
-/**
- * Set the digit list to a representation of the given double value.
- * This method supports both fixed-point and exponential notation.
- * @param source Value to be converted.
- */
-void
-DigitList::set(double source)
-{
- // for now, simple implementation; later, do proper IEEE stuff
- char rep[MAX_DIGITS + 8]; // Extra space for '+', '.', e+NNN, and '\0' (actually +8 is enough)
-
- // Generate a representation of the form /[+-][0-9].[0-9]+e[+-][0-9]+/
- // Can also generate /[+-]nan/ or /[+-]inf/
- // TODO: Use something other than sprintf() here, since it's behavior is somewhat platform specific.
- // That is why infinity is special cased here.
- if (uprv_isInfinite(source)) {
- if (uprv_isNegativeInfinity(source)) {
- uprv_strcpy(rep,"-inf"); // Handle negative infinity
- } else {
- uprv_strcpy(rep,"inf");
- }
- } else {
- sprintf(rep, "%+1.*e", MAX_DBL_DIGITS - 1, source);
- }
- U_ASSERT(uprv_strlen(rep) < sizeof(rep));
-
- // uprv_decNumberFromString() will parse the string expecting '.' as a
- // decimal separator, however sprintf() can use ',' in certain locales.
- // Overwrite a ',' with '.' here before proceeding.
- char *decimalSeparator = strchr(rep, ',');
- if (decimalSeparator != NULL) {
- *decimalSeparator = '.';
- }
-
- // Create a decNumber from the string.
- uprv_decNumberFromString(fDecNumber, rep, &fContext);
- uprv_decNumberTrim(fDecNumber);
- internalSetDouble(source);
-}
-
-// -------------------------------------
-
-/*
- * Multiply
- * The number will be expanded if need be to retain full precision.
- * In practice, for formatting, multiply is by 10, 100 or 1000, so more digits
- * will not be required for this use.
- */
-void
-DigitList::mult(const DigitList &other, UErrorCode &status) {
- if (U_FAILURE(status)) {
- return;
- }
- fContext.status = 0;
- int32_t requiredDigits = this->digits() + other.digits();
- if (requiredDigits > fContext.digits) {
- reduce(); // Remove any trailing zeros
- int32_t requiredDigits = this->digits() + other.digits();
- ensureCapacity(requiredDigits, status);
- }
- uprv_decNumberMultiply(fDecNumber, fDecNumber, other.fDecNumber, &fContext);
- internalClear();
-}
-
-// -------------------------------------
-
-/*
- * Divide
- * The number will _not_ be expanded for inexact results.
- * TODO: probably should expand some, for rounding increments that
- * could add a few digits, e.g. .25, but not expand arbitrarily.
- */
-void
-DigitList::div(const DigitList &other, UErrorCode &status) {
- if (U_FAILURE(status)) {
- return;
- }
- uprv_decNumberDivide(fDecNumber, fDecNumber, other.fDecNumber, &fContext);
- internalClear();
-}
-
-// -------------------------------------
-
-/*
- * ensureCapacity. Grow the digit storage for the number if it's less than the requested
- * amount. Never reduce it. Available size is kept in fContext.digits.
- */
-void
-DigitList::ensureCapacity(int32_t requestedCapacity, UErrorCode &status) {
- if (U_FAILURE(status)) {
- return;
- }
- if (requestedCapacity <= 0) {
- status = U_ILLEGAL_ARGUMENT_ERROR;
- return;
- }
- if (requestedCapacity > DEC_MAX_DIGITS) {
- // Don't report an error for requesting too much.
- // Arithemetic Results will be rounded to what can be supported.
- // At 999,999,999 max digits, exceeding the limit is not too likely!
- requestedCapacity = DEC_MAX_DIGITS;
- }
- if (requestedCapacity > fContext.digits) {
- decNumber *newBuffer = fStorage.resize(requestedCapacity, fStorage.getCapacity());
- if (newBuffer == NULL) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- fContext.digits = requestedCapacity;
- fDecNumber = newBuffer;
- }
-}
-
-// -------------------------------------
-
-/**
- * Round the representation to the given number of digits.
- * @param maximumDigits The maximum number of digits to be shown.
- * Upon return, count will be less than or equal to maximumDigits.
- */
-void
-DigitList::round(int32_t maximumDigits)
-{
- reduce();
- if (maximumDigits >= fDecNumber->digits) {
- return;
- }
- int32_t savedDigits = fContext.digits;
- fContext.digits = maximumDigits;
- uprv_decNumberPlus(fDecNumber, fDecNumber, &fContext);
- fContext.digits = savedDigits;
- uprv_decNumberTrim(fDecNumber);
- reduce();
- internalClear();
-}
-
-
-void
-DigitList::roundFixedPoint(int32_t maximumFractionDigits) {
- reduce(); // Remove trailing zeros.
- if (fDecNumber->exponent >= -maximumFractionDigits) {
- return;
- }
- decNumber scale; // Dummy decimal number, but with the desired number of
- uprv_decNumberZero(&scale); // fraction digits.
- scale.exponent = -maximumFractionDigits;
- scale.lsu[0] = 1;
-
- uprv_decNumberQuantize(fDecNumber, fDecNumber, &scale, &fContext);
- reduce();
- internalClear();
-}
-
-// -------------------------------------
-
-void
-DigitList::toIntegralValue() {
- uprv_decNumberToIntegralValue(fDecNumber, fDecNumber, &fContext);
-}
-
-
-// -------------------------------------
-UBool
-DigitList::isZero() const
-{
- return decNumberIsZero(fDecNumber);
-}
-
-// -------------------------------------
-int32_t
-DigitList::getUpperExponent() const {
- return fDecNumber->digits + fDecNumber->exponent;
-}
-
-DigitInterval &
-DigitList::getSmallestInterval(DigitInterval &result) const {
- result.setLeastSignificantInclusive(fDecNumber->exponent);
- result.setMostSignificantExclusive(getUpperExponent());
- return result;
-}
-
-uint8_t
-DigitList::getDigitByExponent(int32_t exponent) const {
- int32_t idx = exponent - fDecNumber->exponent;
- if (idx < 0 || idx >= fDecNumber->digits) {
- return 0;
- }
- return fDecNumber->lsu[idx];
-}
-
-void
-DigitList::appendDigitsTo(CharString &str, UErrorCode &status) const {
- str.append((const char *) fDecNumber->lsu, fDecNumber->digits, status);
-}
-
-void
-DigitList::roundAtExponent(int32_t exponent, int32_t maxSigDigits) {
- reduce();
- if (maxSigDigits < fDecNumber->digits) {
- int32_t minExponent = getUpperExponent() - maxSigDigits;
- if (exponent < minExponent) {
- exponent = minExponent;
- }
- }
- if (exponent <= fDecNumber->exponent) {
- return;
- }
- int32_t digits = getUpperExponent() - exponent;
- if (digits > 0) {
- round(digits);
- } else {
- roundFixedPoint(-exponent);
- }
-}
-
-void
-DigitList::quantize(const DigitList &quantity, UErrorCode &status) {
- if (U_FAILURE(status)) {
- return;
- }
- div(quantity, status);
- roundAtExponent(0);
- mult(quantity, status);
- reduce();
-}
-
-int32_t
-DigitList::getScientificExponent(
- int32_t minIntDigitCount, int32_t exponentMultiplier) const {
- // The exponent for zero is always zero.
- if (isZero()) {
- return 0;
- }
- int32_t intDigitCount = getUpperExponent();
- int32_t exponent;
- if (intDigitCount >= minIntDigitCount) {
- int32_t maxAdjustment = intDigitCount - minIntDigitCount;
- exponent = (maxAdjustment / exponentMultiplier) * exponentMultiplier;
- } else {
- int32_t minAdjustment = minIntDigitCount - intDigitCount;
- exponent = ((minAdjustment + exponentMultiplier - 1) / exponentMultiplier) * -exponentMultiplier;
- }
- return exponent;
-}
-
-int32_t
-DigitList::toScientific(
- int32_t minIntDigitCount, int32_t exponentMultiplier) {
- int32_t exponent = getScientificExponent(
- minIntDigitCount, exponentMultiplier);
- shiftDecimalRight(-exponent);
- return exponent;
-}
-
-void
-DigitList::shiftDecimalRight(int32_t n) {
- fDecNumber->exponent += n;
- internalClear();
-}
-
-U_NAMESPACE_END
-#endif // #if !UCONFIG_NO_FORMATTING
-
-//eof
diff --git a/tools/icu/patches/62/source/i18n/decimfmt.cpp b/tools/icu/patches/62/source/i18n/decimfmt.cpp
deleted file mode 100644
index 8ae773b75c..0000000000
--- a/tools/icu/patches/62/source/i18n/decimfmt.cpp
+++ /dev/null
@@ -1,1384 +0,0 @@
-// © 2018 and later: Unicode, Inc. and others.
-// License & terms of use: http://www.unicode.org/copyright.html
-
-#include "unicode/utypes.h"
-
-#if !UCONFIG_NO_FORMATTING
-
-// Allow implicit conversion from char16_t* to UnicodeString for this file:
-// Helpful in toString methods and elsewhere.
-#define UNISTR_FROM_STRING_EXPLICIT
-
-#include <cmath>
-#include <cstdlib>
-#include <stdlib.h>
-#include "unicode/errorcode.h"
-#include "unicode/decimfmt.h"
-#include "number_decimalquantity.h"
-#include "number_types.h"
-#include "numparse_impl.h"
-#include "number_mapper.h"
-#include "number_patternstring.h"
-#include "putilimp.h"
-#include "number_utils.h"
-#include "number_utypes.h"
-
-using namespace icu;
-using namespace icu::number;
-using namespace icu::number::impl;
-using namespace icu::numparse;
-using namespace icu::numparse::impl;
-using ERoundingMode = icu::DecimalFormat::ERoundingMode;
-using EPadPosition = icu::DecimalFormat::EPadPosition;
-
-// MSVC warns C4805 when comparing bool with UBool
-// TODO: Move this macro into a better place?
-#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
-#define UBOOL_TO_BOOL(b) static_cast<bool>(b)
-#else
-#define UBOOL_TO_BOOL(b) b
-#endif
-
-
-UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormat)
-
-
-DecimalFormat::DecimalFormat(UErrorCode& status)
- : DecimalFormat(nullptr, status) {
- // Use the default locale and decimal pattern.
- const char* localeName = Locale::getDefault().getName();
- LocalPointer<NumberingSystem> ns(NumberingSystem::createInstance(status));
- UnicodeString patternString = utils::getPatternForStyle(
- localeName,
- ns->getName(),
- CLDR_PATTERN_STYLE_DECIMAL,
- status);
- setPropertiesFromPattern(patternString, IGNORE_ROUNDING_IF_CURRENCY, status);
- touch(status);
-}
-
-DecimalFormat::DecimalFormat(const UnicodeString& pattern, UErrorCode& status)
- : DecimalFormat(nullptr, status) {
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
- touch(status);
-}
-
-DecimalFormat::DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
- UErrorCode& status)
- : DecimalFormat(symbolsToAdopt, status) {
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
- touch(status);
-}
-
-DecimalFormat::DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
- UNumberFormatStyle style, UErrorCode& status)
- : DecimalFormat(symbolsToAdopt, status) {
- // If choice is a currency type, ignore the rounding information.
- if (style == UNumberFormatStyle::UNUM_CURRENCY || style == UNumberFormatStyle::UNUM_CURRENCY_ISO ||
- style == UNumberFormatStyle::UNUM_CURRENCY_ACCOUNTING ||
- style == UNumberFormatStyle::UNUM_CASH_CURRENCY ||
- style == UNumberFormatStyle::UNUM_CURRENCY_STANDARD ||
- style == UNumberFormatStyle::UNUM_CURRENCY_PLURAL) {
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_ALWAYS, status);
- } else {
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
- }
- // Note: in Java, CurrencyPluralInfo is set in NumberFormat.java, but in C++, it is not set there,
- // so we have to set it here.
- if (style == UNumberFormatStyle::UNUM_CURRENCY_PLURAL) {
- LocalPointer<CurrencyPluralInfo> cpi(
- new CurrencyPluralInfo(fields->symbols->getLocale(), status),
- status);
- if (U_FAILURE(status)) { return; }
- fields->properties->currencyPluralInfo.fPtr.adoptInstead(cpi.orphan());
- }
- touch(status);
-}
-
-DecimalFormat::DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status) {
- LocalPointer<const DecimalFormatSymbols> adoptedSymbols(symbolsToAdopt);
- fields = new DecimalFormatFields();
- if (U_FAILURE(status)) {
- return;
- }
- if (fields == nullptr) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- fields->properties.adoptInsteadAndCheckErrorCode(new DecimalFormatProperties(), status);
- fields->exportedProperties.adoptInsteadAndCheckErrorCode(new DecimalFormatProperties(), status);
- if (adoptedSymbols.isNull()) {
- fields->symbols.adoptInsteadAndCheckErrorCode(new DecimalFormatSymbols(status), status);
- } else {
- fields->symbols.adoptInsteadAndCheckErrorCode(adoptedSymbols.orphan(), status);
- }
-}
-
-#if UCONFIG_HAVE_PARSEALLINPUT
-
-void DecimalFormat::setParseAllInput(UNumberFormatAttributeValue value) {
- if (value == fields->properties->parseAllInput) { return; }
- fields->properties->parseAllInput = value;
-}
-
-#endif
-
-DecimalFormat&
-DecimalFormat::setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status) {
- if (U_FAILURE(status)) { return *this; }
-
- switch (attr) {
- case UNUM_LENIENT_PARSE:
- setLenient(newValue != 0);
- break;
-
- case UNUM_PARSE_INT_ONLY:
- setParseIntegerOnly(newValue != 0);
- break;
-
- case UNUM_GROUPING_USED:
- setGroupingUsed(newValue != 0);
- break;
-
- case UNUM_DECIMAL_ALWAYS_SHOWN:
- setDecimalSeparatorAlwaysShown(newValue != 0);
- break;
-
- case UNUM_MAX_INTEGER_DIGITS:
- setMaximumIntegerDigits(newValue);
- break;
-
- case UNUM_MIN_INTEGER_DIGITS:
- setMinimumIntegerDigits(newValue);
- break;
-
- case UNUM_INTEGER_DIGITS:
- setMinimumIntegerDigits(newValue);
- setMaximumIntegerDigits(newValue);
- break;
-
- case UNUM_MAX_FRACTION_DIGITS:
- setMaximumFractionDigits(newValue);
- break;
-
- case UNUM_MIN_FRACTION_DIGITS:
- setMinimumFractionDigits(newValue);
- break;
-
- case UNUM_FRACTION_DIGITS:
- setMinimumFractionDigits(newValue);
- setMaximumFractionDigits(newValue);
- break;
-
- case UNUM_SIGNIFICANT_DIGITS_USED:
- setSignificantDigitsUsed(newValue != 0);
- break;
-
- case UNUM_MAX_SIGNIFICANT_DIGITS:
- setMaximumSignificantDigits(newValue);
- break;
-
- case UNUM_MIN_SIGNIFICANT_DIGITS:
- setMinimumSignificantDigits(newValue);
- break;
-
- case UNUM_MULTIPLIER:
- setMultiplier(newValue);
- break;
-
- case UNUM_SCALE:
- setMultiplierScale(newValue);
- break;
-
- case UNUM_GROUPING_SIZE:
- setGroupingSize(newValue);
- break;
-
- case UNUM_ROUNDING_MODE:
- setRoundingMode((DecimalFormat::ERoundingMode) newValue);
- break;
-
- case UNUM_FORMAT_WIDTH:
- setFormatWidth(newValue);
- break;
-
- case UNUM_PADDING_POSITION:
- /** The position at which padding will take place. */
- setPadPosition((DecimalFormat::EPadPosition) newValue);
- break;
-
- case UNUM_SECONDARY_GROUPING_SIZE:
- setSecondaryGroupingSize(newValue);
- break;
-
-#if UCONFIG_HAVE_PARSEALLINPUT
- case UNUM_PARSE_ALL_INPUT:
- setParseAllInput((UNumberFormatAttributeValue) newValue);
- break;
-#endif
-
- case UNUM_PARSE_NO_EXPONENT:
- setParseNoExponent((UBool) newValue);
- break;
-
- case UNUM_PARSE_DECIMAL_MARK_REQUIRED:
- setDecimalPatternMatchRequired((UBool) newValue);
- break;
-
- case UNUM_CURRENCY_USAGE:
- setCurrencyUsage((UCurrencyUsage) newValue, &status);
- break;
-
- case UNUM_MINIMUM_GROUPING_DIGITS:
- setMinimumGroupingDigits(newValue);
- break;
-
- case UNUM_PARSE_CASE_SENSITIVE:
- setParseCaseSensitive(static_cast<UBool>(newValue));
- break;
-
- case UNUM_SIGN_ALWAYS_SHOWN:
- setSignAlwaysShown(static_cast<UBool>(newValue));
- break;
-
- case UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS:
- setFormatFailIfMoreThanMaxDigits(static_cast<UBool>(newValue));
- break;
-
- default:
- status = U_UNSUPPORTED_ERROR;
- break;
- }
- return *this;
-}
-
-int32_t DecimalFormat::getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const {
- if (U_FAILURE(status)) { return -1; }
- switch (attr) {
- case UNUM_LENIENT_PARSE:
- return isLenient();
-
- case UNUM_PARSE_INT_ONLY:
- return isParseIntegerOnly();
-
- case UNUM_GROUPING_USED:
- return isGroupingUsed();
-
- case UNUM_DECIMAL_ALWAYS_SHOWN:
- return isDecimalSeparatorAlwaysShown();
-
- case UNUM_MAX_INTEGER_DIGITS:
- return getMaximumIntegerDigits();
-
- case UNUM_MIN_INTEGER_DIGITS:
- return getMinimumIntegerDigits();
-
- case UNUM_INTEGER_DIGITS:
- // TBD: what should this return?
- return getMinimumIntegerDigits();
-
- case UNUM_MAX_FRACTION_DIGITS:
- return getMaximumFractionDigits();
-
- case UNUM_MIN_FRACTION_DIGITS:
- return getMinimumFractionDigits();
-
- case UNUM_FRACTION_DIGITS:
- // TBD: what should this return?
- return getMinimumFractionDigits();
-
- case UNUM_SIGNIFICANT_DIGITS_USED:
- return areSignificantDigitsUsed();
-
- case UNUM_MAX_SIGNIFICANT_DIGITS:
- return getMaximumSignificantDigits();
-
- case UNUM_MIN_SIGNIFICANT_DIGITS:
- return getMinimumSignificantDigits();
-
- case UNUM_MULTIPLIER:
- return getMultiplier();
-
- case UNUM_SCALE:
- return getMultiplierScale();
-
- case UNUM_GROUPING_SIZE:
- return getGroupingSize();
-
- case UNUM_ROUNDING_MODE:
- return getRoundingMode();
-
- case UNUM_FORMAT_WIDTH:
- return getFormatWidth();
-
- case UNUM_PADDING_POSITION:
- return getPadPosition();
-
- case UNUM_SECONDARY_GROUPING_SIZE:
- return getSecondaryGroupingSize();
-
- case UNUM_PARSE_NO_EXPONENT:
- return isParseNoExponent();
-
- case UNUM_PARSE_DECIMAL_MARK_REQUIRED:
- return isDecimalPatternMatchRequired();
-
- case UNUM_CURRENCY_USAGE:
- return getCurrencyUsage();
-
- case UNUM_MINIMUM_GROUPING_DIGITS:
- return getMinimumGroupingDigits();
-
- case UNUM_PARSE_CASE_SENSITIVE:
- return isParseCaseSensitive();
-
- case UNUM_SIGN_ALWAYS_SHOWN:
- return isSignAlwaysShown();
-
- case UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS:
- return isFormatFailIfMoreThanMaxDigits();
-
- default:
- status = U_UNSUPPORTED_ERROR;
- break;
- }
-
- return -1; /* undefined */
-}
-
-void DecimalFormat::setGroupingUsed(UBool enabled) {
- if (UBOOL_TO_BOOL(enabled) == fields->properties->groupingUsed) { return; }
- NumberFormat::setGroupingUsed(enabled); // to set field for compatibility
- fields->properties->groupingUsed = enabled;
- touchNoError();
-}
-
-void DecimalFormat::setParseIntegerOnly(UBool value) {
- if (UBOOL_TO_BOOL(value) == fields->properties->parseIntegerOnly) { return; }
- NumberFormat::setParseIntegerOnly(value); // to set field for compatibility
- fields->properties->parseIntegerOnly = value;
- touchNoError();
-}
-
-void DecimalFormat::setLenient(UBool enable) {
- ParseMode mode = enable ? PARSE_MODE_LENIENT : PARSE_MODE_STRICT;
- if (!fields->properties->parseMode.isNull() && mode == fields->properties->parseMode.getNoError()) { return; }
- NumberFormat::setLenient(enable); // to set field for compatibility
- fields->properties->parseMode = mode;
- touchNoError();
-}
-
-DecimalFormat::DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
- UParseError&, UErrorCode& status)
- : DecimalFormat(symbolsToAdopt, status) {
- // TODO: What is parseError for?
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
- touch(status);
-}
-
-DecimalFormat::DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols,
- UErrorCode& status)
- : DecimalFormat(new DecimalFormatSymbols(symbols), status) {
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
- touch(status);
-}
-
-DecimalFormat::DecimalFormat(const DecimalFormat& source) : NumberFormat(source) {
- // Note: it is not safe to copy fields->formatter or fWarehouse directly because fields->formatter might have
- // dangling pointers to fields inside fWarehouse. The safe thing is to re-construct fields->formatter from
- // the property bag, despite being somewhat slower.
- fields = new DecimalFormatFields();
- if (fields == nullptr) {
- return;
- }
- fields->properties.adoptInstead(new DecimalFormatProperties(*source.fields->properties));
- fields->symbols.adoptInstead(new DecimalFormatSymbols(*source.fields->symbols));
- fields->exportedProperties.adoptInstead(new DecimalFormatProperties());
- if (fields->properties == nullptr || fields->symbols == nullptr || fields->exportedProperties == nullptr) {
- return;
- }
- touchNoError();
-}
-
-DecimalFormat& DecimalFormat::operator=(const DecimalFormat& rhs) {
- *fields->properties = *rhs.fields->properties;
- fields->exportedProperties->clear();
- fields->symbols.adoptInstead(new DecimalFormatSymbols(*rhs.fields->symbols));
- touchNoError();
- return *this;
-}
-
-DecimalFormat::~DecimalFormat() {
- delete fields->atomicParser.exchange(nullptr);
- delete fields->atomicCurrencyParser.exchange(nullptr);
- delete fields;
-}
-
-Format* DecimalFormat::clone() const {
- return new DecimalFormat(*this);
-}
-
-UBool DecimalFormat::operator==(const Format& other) const {
- auto* otherDF = dynamic_cast<const DecimalFormat*>(&other);
- if (otherDF == nullptr) {
- return false;
- }
- return *fields->properties == *otherDF->fields->properties && *fields->symbols == *otherDF->fields->symbols;
-}
-
-UnicodeString& DecimalFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos) const {
- if (pos.getField() == FieldPosition::DONT_CARE && fastFormatDouble(number, appendTo)) {
- return appendTo;
- }
- UErrorCode localStatus = U_ZERO_ERROR;
- FormattedNumber output = fields->formatter->formatDouble(number, localStatus);
- fieldPositionHelper(output, pos, appendTo.length(), localStatus);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString& DecimalFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos,
- UErrorCode& status) const {
- if (pos.getField() == FieldPosition::DONT_CARE && fastFormatDouble(number, appendTo)) {
- return appendTo;
- }
- FormattedNumber output = fields->formatter->formatDouble(number, status);
- fieldPositionHelper(output, pos, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString&
-DecimalFormat::format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
- UErrorCode& status) const {
- if (posIter == nullptr && fastFormatDouble(number, appendTo)) {
- return appendTo;
- }
- FormattedNumber output = fields->formatter->formatDouble(number, status);
- fieldPositionIteratorHelper(output, posIter, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString& DecimalFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const {
- return format(static_cast<int64_t> (number), appendTo, pos);
-}
-
-UnicodeString& DecimalFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
- UErrorCode& status) const {
- return format(static_cast<int64_t> (number), appendTo, pos, status);
-}
-
-UnicodeString&
-DecimalFormat::format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
- UErrorCode& status) const {
- return format(static_cast<int64_t> (number), appendTo, posIter, status);
-}
-
-UnicodeString& DecimalFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const {
- if (pos.getField() == FieldPosition::DONT_CARE && fastFormatInt64(number, appendTo)) {
- return appendTo;
- }
- UErrorCode localStatus = U_ZERO_ERROR;
- FormattedNumber output = fields->formatter->formatInt(number, localStatus);
- fieldPositionHelper(output, pos, appendTo.length(), localStatus);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString& DecimalFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
- UErrorCode& status) const {
- if (pos.getField() == FieldPosition::DONT_CARE && fastFormatInt64(number, appendTo)) {
- return appendTo;
- }
- FormattedNumber output = fields->formatter->formatInt(number, status);
- fieldPositionHelper(output, pos, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString&
-DecimalFormat::format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
- UErrorCode& status) const {
- if (posIter == nullptr && fastFormatInt64(number, appendTo)) {
- return appendTo;
- }
- FormattedNumber output = fields->formatter->formatInt(number, status);
- fieldPositionIteratorHelper(output, posIter, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString&
-DecimalFormat::format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
- UErrorCode& status) const {
- FormattedNumber output = fields->formatter->formatDecimal(number, status);
- fieldPositionIteratorHelper(output, posIter, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString& DecimalFormat::format(const DecimalQuantity& number, UnicodeString& appendTo,
- FieldPositionIterator* posIter, UErrorCode& status) const {
- FormattedNumber output = fields->formatter->formatDecimalQuantity(number, status);
- fieldPositionIteratorHelper(output, posIter, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-UnicodeString&
-DecimalFormat::format(const DecimalQuantity& number, UnicodeString& appendTo, FieldPosition& pos,
- UErrorCode& status) const {
- FormattedNumber output = fields->formatter->formatDecimalQuantity(number, status);
- fieldPositionHelper(output, pos, appendTo.length(), status);
- auto appendable = UnicodeStringAppendable(appendTo);
- output.appendTo(appendable);
- return appendTo;
-}
-
-void DecimalFormat::parse(const UnicodeString& text, Formattable& output,
- ParsePosition& parsePosition) const {
- if (parsePosition.getIndex() < 0 || parsePosition.getIndex() >= text.length()) {
- return;
- }
-
- ErrorCode status;
- ParsedNumber result;
- // Note: if this is a currency instance, currencies will be matched despite the fact that we are not in the
- // parseCurrency method (backwards compatibility)
- int32_t startIndex = parsePosition.getIndex();
- const NumberParserImpl* parser = getParser(status);
- if (U_FAILURE(status)) { return; }
- parser->parse(text, startIndex, true, result, status);
- // TODO: Do we need to check for fImpl->properties->parseAllInput (UCONFIG_HAVE_PARSEALLINPUT) here?
- if (result.success()) {
- parsePosition.setIndex(result.charEnd);
- result.populateFormattable(output, parser->getParseFlags());
- } else {
- parsePosition.setErrorIndex(startIndex + result.charEnd);
- }
-}
-
-CurrencyAmount* DecimalFormat::parseCurrency(const UnicodeString& text, ParsePosition& parsePosition) const {
- if (parsePosition.getIndex() < 0 || parsePosition.getIndex() >= text.length()) {
- return nullptr;
- }
-
- ErrorCode status;
- ParsedNumber result;
- // Note: if this is a currency instance, currencies will be matched despite the fact that we are not in the
- // parseCurrency method (backwards compatibility)
- int32_t startIndex = parsePosition.getIndex();
- const NumberParserImpl* parser = getCurrencyParser(status);
- if (U_FAILURE(status)) { return nullptr; }
- parser->parse(text, startIndex, true, result, status);
- // TODO: Do we need to check for fImpl->properties->parseAllInput (UCONFIG_HAVE_PARSEALLINPUT) here?
- if (result.success()) {
- parsePosition.setIndex(result.charEnd);
- Formattable formattable;
- result.populateFormattable(formattable, parser->getParseFlags());
- return new CurrencyAmount(formattable, result.currencyCode, status);
- } else {
- parsePosition.setErrorIndex(startIndex + result.charEnd);
- return nullptr;
- }
-}
-
-const DecimalFormatSymbols* DecimalFormat::getDecimalFormatSymbols(void) const {
- return fields->symbols.getAlias();
-}
-
-void DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt) {
- if (symbolsToAdopt == nullptr) {
- return; // do not allow caller to set fields->symbols to NULL
- }
- fields->symbols.adoptInstead(symbolsToAdopt);
- touchNoError();
-}
-
-void DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols) {
- fields->symbols.adoptInstead(new DecimalFormatSymbols(symbols));
- touchNoError();
-}
-
-const CurrencyPluralInfo* DecimalFormat::getCurrencyPluralInfo(void) const {
- return fields->properties->currencyPluralInfo.fPtr.getAlias();
-}
-
-void DecimalFormat::adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt) {
- fields->properties->currencyPluralInfo.fPtr.adoptInstead(toAdopt);
- touchNoError();
-}
-
-void DecimalFormat::setCurrencyPluralInfo(const CurrencyPluralInfo& info) {
- if (fields->properties->currencyPluralInfo.fPtr.isNull()) {
- fields->properties->currencyPluralInfo.fPtr.adoptInstead(info.clone());
- } else {
- *fields->properties->currencyPluralInfo.fPtr = info; // copy-assignment operator
- }
- touchNoError();
-}
-
-UnicodeString& DecimalFormat::getPositivePrefix(UnicodeString& result) const {
- ErrorCode localStatus;
- fields->formatter->getAffixImpl(true, false, result, localStatus);
- return result;
-}
-
-void DecimalFormat::setPositivePrefix(const UnicodeString& newValue) {
- if (newValue == fields->properties->positivePrefix) { return; }
- fields->properties->positivePrefix = newValue;
- touchNoError();
-}
-
-UnicodeString& DecimalFormat::getNegativePrefix(UnicodeString& result) const {
- ErrorCode localStatus;
- fields->formatter->getAffixImpl(true, true, result, localStatus);
- return result;
-}
-
-void DecimalFormat::setNegativePrefix(const UnicodeString& newValue) {
- if (newValue == fields->properties->negativePrefix) { return; }
- fields->properties->negativePrefix = newValue;
- touchNoError();
-}
-
-UnicodeString& DecimalFormat::getPositiveSuffix(UnicodeString& result) const {
- ErrorCode localStatus;
- fields->formatter->getAffixImpl(false, false, result, localStatus);
- return result;
-}
-
-void DecimalFormat::setPositiveSuffix(const UnicodeString& newValue) {
- if (newValue == fields->properties->positiveSuffix) { return; }
- fields->properties->positiveSuffix = newValue;
- touchNoError();
-}
-
-UnicodeString& DecimalFormat::getNegativeSuffix(UnicodeString& result) const {
- ErrorCode localStatus;
- fields->formatter->getAffixImpl(false, true, result, localStatus);
- return result;
-}
-
-void DecimalFormat::setNegativeSuffix(const UnicodeString& newValue) {
- if (newValue == fields->properties->negativeSuffix) { return; }
- fields->properties->negativeSuffix = newValue;
- touchNoError();
-}
-
-UBool DecimalFormat::isSignAlwaysShown() const {
- return fields->properties->signAlwaysShown;
-}
-
-void DecimalFormat::setSignAlwaysShown(UBool value) {
- if (UBOOL_TO_BOOL(value) == fields->properties->signAlwaysShown) { return; }
- fields->properties->signAlwaysShown = value;
- touchNoError();
-}
-
-int32_t DecimalFormat::getMultiplier(void) const {
- if (fields->properties->multiplier != 1) {
- return fields->properties->multiplier;
- } else if (fields->properties->magnitudeMultiplier != 0) {
- return static_cast<int32_t>(uprv_pow10(fields->properties->magnitudeMultiplier));
- } else {
- return 1;
- }
-}
-
-void DecimalFormat::setMultiplier(int32_t multiplier) {
- if (multiplier == 0) {
- multiplier = 1; // one being the benign default value for a multiplier.
- }
-
- // Try to convert to a magnitude multiplier first
- int delta = 0;
- int value = multiplier;
- while (value != 1) {
- delta++;
- int temp = value / 10;
- if (temp * 10 != value) {
- delta = -1;
- break;
- }
- value = temp;
- }
- if (delta != -1) {
- fields->properties->magnitudeMultiplier = delta;
- fields->properties->multiplier = 1;
- } else {
- fields->properties->magnitudeMultiplier = 0;
- fields->properties->multiplier = multiplier;
- }
- touchNoError();
-}
-
-int32_t DecimalFormat::getMultiplierScale() const {
- return fields->properties->multiplierScale;
-}
-
-void DecimalFormat::setMultiplierScale(int32_t newValue) {
- if (newValue == fields->properties->multiplierScale) { return; }
- fields->properties->multiplierScale = newValue;
- touchNoError();
-}
-
-double DecimalFormat::getRoundingIncrement(void) const {
- return fields->exportedProperties->roundingIncrement;
-}
-
-void DecimalFormat::setRoundingIncrement(double newValue) {
- if (newValue == fields->properties->roundingIncrement) { return; }
- fields->properties->roundingIncrement = newValue;
- touchNoError();
-}
-
-ERoundingMode DecimalFormat::getRoundingMode(void) const {
- // UNumberFormatRoundingMode and ERoundingMode have the same values.
- return static_cast<ERoundingMode>(fields->exportedProperties->roundingMode.getNoError());
-}
-
-void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
- auto uRoundingMode = static_cast<UNumberFormatRoundingMode>(roundingMode);
- if (!fields->properties->roundingMode.isNull() && uRoundingMode == fields->properties->roundingMode.getNoError()) {
- return;
- }
- NumberFormat::setMaximumIntegerDigits(roundingMode); // to set field for compatibility
- fields->properties->roundingMode = uRoundingMode;
- touchNoError();
-}
-
-int32_t DecimalFormat::getFormatWidth(void) const {
- return fields->properties->formatWidth;
-}
-
-void DecimalFormat::setFormatWidth(int32_t width) {
- if (width == fields->properties->formatWidth) { return; }
- fields->properties->formatWidth = width;
- touchNoError();
-}
-
-UnicodeString DecimalFormat::getPadCharacterString() const {
- if (fields->properties->padString.isBogus()) {
- // Readonly-alias the static string kFallbackPaddingString
- return {TRUE, kFallbackPaddingString, -1};
- } else {
- return fields->properties->padString;
- }
-}
-
-void DecimalFormat::setPadCharacter(const UnicodeString& padChar) {
- if (padChar == fields->properties->padString) { return; }
- if (padChar.length() > 0) {
- fields->properties->padString = UnicodeString(padChar.char32At(0));
- } else {
- fields->properties->padString.setToBogus();
- }
- touchNoError();
-}
-
-EPadPosition DecimalFormat::getPadPosition(void) const {
- if (fields->properties->padPosition.isNull()) {
- return EPadPosition::kPadBeforePrefix;
- } else {
- // UNumberFormatPadPosition and EPadPosition have the same values.
- return static_cast<EPadPosition>(fields->properties->padPosition.getNoError());
- }
-}
-
-void DecimalFormat::setPadPosition(EPadPosition padPos) {
- auto uPadPos = static_cast<UNumberFormatPadPosition>(padPos);
- if (!fields->properties->padPosition.isNull() && uPadPos == fields->properties->padPosition.getNoError()) {
- return;
- }
- fields->properties->padPosition = uPadPos;
- touchNoError();
-}
-
-UBool DecimalFormat::isScientificNotation(void) const {
- return fields->properties->minimumExponentDigits != -1;
-}
-
-void DecimalFormat::setScientificNotation(UBool useScientific) {
- int32_t minExp = useScientific ? 1 : -1;
- if (fields->properties->minimumExponentDigits == minExp) { return; }
- if (useScientific) {
- fields->properties->minimumExponentDigits = 1;
- } else {
- fields->properties->minimumExponentDigits = -1;
- }
- touchNoError();
-}
-
-int8_t DecimalFormat::getMinimumExponentDigits(void) const {
- return static_cast<int8_t>(fields->properties->minimumExponentDigits);
-}
-
-void DecimalFormat::setMinimumExponentDigits(int8_t minExpDig) {
- if (minExpDig == fields->properties->minimumExponentDigits) { return; }
- fields->properties->minimumExponentDigits = minExpDig;
- touchNoError();
-}
-
-UBool DecimalFormat::isExponentSignAlwaysShown(void) const {
- return fields->properties->exponentSignAlwaysShown;
-}
-
-void DecimalFormat::setExponentSignAlwaysShown(UBool expSignAlways) {
- if (UBOOL_TO_BOOL(expSignAlways) == fields->properties->exponentSignAlwaysShown) { return; }
- fields->properties->exponentSignAlwaysShown = expSignAlways;
- touchNoError();
-}
-
-int32_t DecimalFormat::getGroupingSize(void) const {
- if (fields->properties->groupingSize < 0) {
- return 0;
- }
- return fields->properties->groupingSize;
-}
-
-void DecimalFormat::setGroupingSize(int32_t newValue) {
- if (newValue == fields->properties->groupingSize) { return; }
- fields->properties->groupingSize = newValue;
- touchNoError();
-}
-
-int32_t DecimalFormat::getSecondaryGroupingSize(void) const {
- int grouping2 = fields->properties->secondaryGroupingSize;
- if (grouping2 < 0) {
- return 0;
- }
- return grouping2;
-}
-
-void DecimalFormat::setSecondaryGroupingSize(int32_t newValue) {
- if (newValue == fields->properties->secondaryGroupingSize) { return; }
- fields->properties->secondaryGroupingSize = newValue;
- touchNoError();
-}
-
-int32_t DecimalFormat::getMinimumGroupingDigits() const {
- return fields->properties->minimumGroupingDigits;
-}
-
-void DecimalFormat::setMinimumGroupingDigits(int32_t newValue) {
- if (newValue == fields->properties->minimumGroupingDigits) { return; }
- fields->properties->minimumGroupingDigits = newValue;
- touchNoError();
-}
-
-UBool DecimalFormat::isDecimalSeparatorAlwaysShown(void) const {
- return fields->properties->decimalSeparatorAlwaysShown;
-}
-
-void DecimalFormat::setDecimalSeparatorAlwaysShown(UBool newValue) {
- if (UBOOL_TO_BOOL(newValue) == fields->properties->decimalSeparatorAlwaysShown) { return; }
- fields->properties->decimalSeparatorAlwaysShown = newValue;
- touchNoError();
-}
-
-UBool DecimalFormat::isDecimalPatternMatchRequired(void) const {
- return fields->properties->decimalPatternMatchRequired;
-}
-
-void DecimalFormat::setDecimalPatternMatchRequired(UBool newValue) {
- if (UBOOL_TO_BOOL(newValue) == fields->properties->decimalPatternMatchRequired) { return; }
- fields->properties->decimalPatternMatchRequired = newValue;
- touchNoError();
-}
-
-UBool DecimalFormat::isParseNoExponent() const {
- return fields->properties->parseNoExponent;
-}
-
-void DecimalFormat::setParseNoExponent(UBool value) {
- if (UBOOL_TO_BOOL(value) == fields->properties->parseNoExponent) { return; }
- fields->properties->parseNoExponent = value;
- touchNoError();
-}
-
-UBool DecimalFormat::isParseCaseSensitive() const {
- return fields->properties->parseCaseSensitive;
-}
-
-void DecimalFormat::setParseCaseSensitive(UBool value) {
- if (UBOOL_TO_BOOL(value) == fields->properties->parseCaseSensitive) { return; }
- fields->properties->parseCaseSensitive = value;
- touchNoError();
-}
-
-UBool DecimalFormat::isFormatFailIfMoreThanMaxDigits() const {
- return fields->properties->formatFailIfMoreThanMaxDigits;
-}
-
-void DecimalFormat::setFormatFailIfMoreThanMaxDigits(UBool value) {
- if (UBOOL_TO_BOOL(value) == fields->properties->formatFailIfMoreThanMaxDigits) { return; }
- fields->properties->formatFailIfMoreThanMaxDigits = value;
- touchNoError();
-}
-
-UnicodeString& DecimalFormat::toPattern(UnicodeString& result) const {
- // Pull some properties from exportedProperties and others from properties
- // to keep affix patterns intact. In particular, pull rounding properties
- // so that CurrencyUsage is reflected properly.
- // TODO: Consider putting this logic in number_patternstring.cpp instead.
- ErrorCode localStatus;
- DecimalFormatProperties tprops(*fields->properties);
- bool useCurrency = ((!tprops.currency.isNull()) || !tprops.currencyPluralInfo.fPtr.isNull() ||
- !tprops.currencyUsage.isNull() || AffixUtils::hasCurrencySymbols(
- tprops.positivePrefixPattern, localStatus) || AffixUtils::hasCurrencySymbols(
- tprops.positiveSuffixPattern, localStatus) || AffixUtils::hasCurrencySymbols(
- tprops.negativePrefixPattern, localStatus) || AffixUtils::hasCurrencySymbols(
- tprops.negativeSuffixPattern, localStatus));
- if (useCurrency) {
- tprops.minimumFractionDigits = fields->exportedProperties->minimumFractionDigits;
- tprops.maximumFractionDigits = fields->exportedProperties->maximumFractionDigits;
- tprops.roundingIncrement = fields->exportedProperties->roundingIncrement;
- }
- result = PatternStringUtils::propertiesToPatternString(tprops, localStatus);
- return result;
-}
-
-UnicodeString& DecimalFormat::toLocalizedPattern(UnicodeString& result) const {
- ErrorCode localStatus;
- result = toPattern(result);
- result = PatternStringUtils::convertLocalized(result, *fields->symbols, true, localStatus);
- return result;
-}
-
-void DecimalFormat::applyPattern(const UnicodeString& pattern, UParseError&, UErrorCode& status) {
- // TODO: What is parseError for?
- applyPattern(pattern, status);
-}
-
-void DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status) {
- setPropertiesFromPattern(pattern, IGNORE_ROUNDING_NEVER, status);
- touch(status);
-}
-
-void DecimalFormat::applyLocalizedPattern(const UnicodeString& localizedPattern, UParseError&,
- UErrorCode& status) {
- // TODO: What is parseError for?
- applyLocalizedPattern(localizedPattern, status);
-}
-
-void DecimalFormat::applyLocalizedPattern(const UnicodeString& localizedPattern, UErrorCode& status) {
- if (U_SUCCESS(status)) {
- UnicodeString pattern = PatternStringUtils::convertLocalized(
- localizedPattern, *fields->symbols, false, status);
- applyPattern(pattern, status);
- }
-}
-
-void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
- if (newValue == fields->properties->maximumIntegerDigits) { return; }
- // For backwards compatibility, conflicting min/max need to keep the most recent setting.
- int32_t min = fields->properties->minimumIntegerDigits;
- if (min >= 0 && min > newValue) {
- fields->properties->minimumIntegerDigits = newValue;
- }
- fields->properties->maximumIntegerDigits = newValue;
- touchNoError();
-}
-
-void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
- if (newValue == fields->properties->minimumIntegerDigits) { return; }
- // For backwards compatibility, conflicting min/max need to keep the most recent setting.
- int32_t max = fields->properties->maximumIntegerDigits;
- if (max >= 0 && max < newValue) {
- fields->properties->maximumIntegerDigits = newValue;
- }
- fields->properties->minimumIntegerDigits = newValue;
- touchNoError();
-}
-
-void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
- if (newValue == fields->properties->maximumFractionDigits) { return; }
- // For backwards compatibility, conflicting min/max need to keep the most recent setting.
- int32_t min = fields->properties->minimumFractionDigits;
- if (min >= 0 && min > newValue) {
- fields->properties->minimumFractionDigits = newValue;
- }
- fields->properties->maximumFractionDigits = newValue;
- touchNoError();
-}
-
-void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
- if (newValue == fields->properties->minimumFractionDigits) { return; }
- // For backwards compatibility, conflicting min/max need to keep the most recent setting.
- int32_t max = fields->properties->maximumFractionDigits;
- if (max >= 0 && max < newValue) {
- fields->properties->maximumFractionDigits = newValue;
- }
- fields->properties->minimumFractionDigits = newValue;
- touchNoError();
-}
-
-int32_t DecimalFormat::getMinimumSignificantDigits() const {
- return fields->exportedProperties->minimumSignificantDigits;
-}
-
-int32_t DecimalFormat::getMaximumSignificantDigits() const {
- return fields->exportedProperties->maximumSignificantDigits;
-}
-
-void DecimalFormat::setMinimumSignificantDigits(int32_t value) {
- if (value == fields->properties->minimumSignificantDigits) { return; }
- int32_t max = fields->properties->maximumSignificantDigits;
- if (max >= 0 && max < value) {
- fields->properties->maximumSignificantDigits = value;
- }
- fields->properties->minimumSignificantDigits = value;
- touchNoError();
-}
-
-void DecimalFormat::setMaximumSignificantDigits(int32_t value) {
- if (value == fields->properties->maximumSignificantDigits) { return; }
- int32_t min = fields->properties->minimumSignificantDigits;
- if (min >= 0 && min > value) {
- fields->properties->minimumSignificantDigits = value;
- }
- fields->properties->maximumSignificantDigits = value;
- touchNoError();
-}
-
-UBool DecimalFormat::areSignificantDigitsUsed() const {
- return fields->properties->minimumSignificantDigits != -1 || fields->properties->maximumSignificantDigits != -1;
-}
-
-void DecimalFormat::setSignificantDigitsUsed(UBool useSignificantDigits) {
- if (areSignificantDigitsUsed()) return;
- // These are the default values from the old implementation.
- int32_t minSig = useSignificantDigits ? 1 : -1;
- int32_t maxSig = useSignificantDigits ? 6 : -1;
- if (fields->properties->minimumSignificantDigits == minSig &&
- fields->properties->maximumSignificantDigits == maxSig) {
- return;
- }
- fields->properties->minimumSignificantDigits = minSig;
- fields->properties->maximumSignificantDigits = maxSig;
- touchNoError();
-}
-
-void DecimalFormat::setCurrency(const char16_t* theCurrency, UErrorCode& ec) {
- CurrencyUnit currencyUnit(theCurrency, ec);
- if (U_FAILURE(ec)) { return; }
- if (!fields->properties->currency.isNull() && fields->properties->currency.getNoError() == currencyUnit) {
- return;
- }
- NumberFormat::setCurrency(theCurrency, ec); // to set field for compatibility
- fields->properties->currency = currencyUnit;
- // TODO: Set values in fields->symbols, too?
- touchNoError();
-}
-
-void DecimalFormat::setCurrency(const char16_t* theCurrency) {
- ErrorCode localStatus;
- setCurrency(theCurrency, localStatus);
-}
-
-void DecimalFormat::setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec) {
- if (U_FAILURE(*ec)) {
- return;
- }
- if (!fields->properties->currencyUsage.isNull() && newUsage == fields->properties->currencyUsage.getNoError()) {
- return;
- }
- fields->properties->currencyUsage = newUsage;
- touch(*ec);
-}
-
-UCurrencyUsage DecimalFormat::getCurrencyUsage() const {
- // CurrencyUsage is not exported, so we have to get it from the input property bag.
- // TODO: Should we export CurrencyUsage instead?
- if (fields->properties->currencyUsage.isNull()) {
- return UCURR_USAGE_STANDARD;
- }
- return fields->properties->currencyUsage.getNoError();
-}
-
-void
-DecimalFormat::formatToDecimalQuantity(double number, DecimalQuantity& output, UErrorCode& status) const {
- fields->formatter->formatDouble(number, status).getDecimalQuantity(output, status);
-}
-
-void DecimalFormat::formatToDecimalQuantity(const Formattable& number, DecimalQuantity& output,
- UErrorCode& status) const {
- UFormattedNumberData obj;
- number.populateDecimalQuantity(obj.quantity, status);
- fields->formatter->formatImpl(&obj, status);
- output = std::move(obj.quantity);
-}
-
-const number::LocalizedNumberFormatter& DecimalFormat::toNumberFormatter() const {
- return *fields->formatter;
-}
-
-/** Rebuilds the formatter object from the property bag. */
-void DecimalFormat::touch(UErrorCode& status) {
- if (fields->exportedProperties == nullptr) {
- // fields->exportedProperties is null only when the formatter is not ready yet.
- // The only time when this happens is during legacy deserialization.
- return;
- }
-
- // In C++, fields->symbols is the source of truth for the locale.
- Locale locale = fields->symbols->getLocale();
-
- // Note: The formatter is relatively cheap to create, and we need it to populate fields->exportedProperties,
- // so automatically compute it here. The parser is a bit more expensive and is not needed until the
- // parse method is called, so defer that until needed.
- // TODO: Only update the pieces that changed instead of re-computing the whole formatter?
- fields->formatter.adoptInstead(
- new LocalizedNumberFormatter(
- NumberPropertyMapper::create(
- *fields->properties, *fields->symbols, fields->warehouse, *fields->exportedProperties, status).locale(
- locale)));
-
- // Do this after fields->exportedProperties are set up
- setupFastFormat();
-
- // Delete the parsers if they were made previously
- delete fields->atomicParser.exchange(nullptr);
- delete fields->atomicCurrencyParser.exchange(nullptr);
-
- // In order for the getters to work, we need to populate some fields in NumberFormat.
- NumberFormat::setCurrency(fields->exportedProperties->currency.get(status).getISOCurrency(), status);
- NumberFormat::setMaximumIntegerDigits(fields->exportedProperties->maximumIntegerDigits);
- NumberFormat::setMinimumIntegerDigits(fields->exportedProperties->minimumIntegerDigits);
- NumberFormat::setMaximumFractionDigits(fields->exportedProperties->maximumFractionDigits);
- NumberFormat::setMinimumFractionDigits(fields->exportedProperties->minimumFractionDigits);
- // fImpl->properties, not fields->exportedProperties, since this information comes from the pattern:
- NumberFormat::setGroupingUsed(fields->properties->groupingUsed);
-}
-
-void DecimalFormat::touchNoError() {
- UErrorCode localStatus = U_ZERO_ERROR;
- touch(localStatus);
-}
-
-void DecimalFormat::setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
- UErrorCode& status) {
- if (U_SUCCESS(status)) {
- // Cast workaround to get around putting the enum in the public header file
- auto actualIgnoreRounding = static_cast<IgnoreRounding>(ignoreRounding);
- PatternParser::parseToExistingProperties(pattern, *fields->properties, actualIgnoreRounding, status);
- }
-}
-
-const numparse::impl::NumberParserImpl* DecimalFormat::getParser(UErrorCode& status) const {
- if (U_FAILURE(status)) { return nullptr; }
-
- // First try to get the pre-computed parser
- auto* ptr = fields->atomicParser.load();
- if (ptr != nullptr) {
- return ptr;
- }
-
- // Try computing the parser on our own
- auto* temp = NumberParserImpl::createParserFromProperties(*fields->properties, *fields->symbols, false, status);
- if (temp == nullptr) {
- status = U_MEMORY_ALLOCATION_ERROR;
- // although we may still dereference, call sites should be guarded
- }
-
- // Note: ptr starts as nullptr; during compare_exchange, it is set to what is actually stored in the
- // atomic if another thread beat us to computing the parser object.
- auto* nonConstThis = const_cast<DecimalFormat*>(this);
- if (!nonConstThis->fields->atomicParser.compare_exchange_strong(ptr, temp)) {
- // Another thread beat us to computing the parser
- delete temp;
- return ptr;
- } else {
- // Our copy of the parser got stored in the atomic
- return temp;
- }
-}
-
-const numparse::impl::NumberParserImpl* DecimalFormat::getCurrencyParser(UErrorCode& status) const {
- if (U_FAILURE(status)) { return nullptr; }
-
- // First try to get the pre-computed parser
- auto* ptr = fields->atomicCurrencyParser.load();
- if (ptr != nullptr) {
- return ptr;
- }
-
- // Try computing the parser on our own
- auto* temp = NumberParserImpl::createParserFromProperties(*fields->properties, *fields->symbols, true, status);
- if (temp == nullptr) {
- status = U_MEMORY_ALLOCATION_ERROR;
- // although we may still dereference, call sites should be guarded
- }
-
- // Note: ptr starts as nullptr; during compare_exchange, it is set to what is actually stored in the
- // atomic if another thread beat us to computing the parser object.
- auto* nonConstThis = const_cast<DecimalFormat*>(this);
- if (!nonConstThis->fields->atomicCurrencyParser.compare_exchange_strong(ptr, temp)) {
- // Another thread beat us to computing the parser
- delete temp;
- return ptr;
- } else {
- // Our copy of the parser got stored in the atomic
- return temp;
- }
-}
-
-void
-DecimalFormat::fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
- int32_t offset, UErrorCode& status) {
- // always return first occurrence:
- fieldPosition.setBeginIndex(0);
- fieldPosition.setEndIndex(0);
- bool found = formatted.nextFieldPosition(fieldPosition, status);
- if (found && offset != 0) {
- FieldPositionOnlyHandler fpoh(fieldPosition);
- fpoh.shiftLast(offset);
- }
-}
-
-void
-DecimalFormat::fieldPositionIteratorHelper(const number::FormattedNumber& formatted, FieldPositionIterator* fpi,
- int32_t offset, UErrorCode& status) {
- if (fpi != nullptr) {
- FieldPositionIteratorHandler fpih(fpi, status);
- fpih.setShift(offset);
- formatted.getAllFieldPositionsImpl(fpih, status);
- }
-}
-
-// To debug fast-format, change void(x) to printf(x)
-#define trace(x) void(x)
-
-void DecimalFormat::setupFastFormat() {
- // Check the majority of properties:
- if (!fields->properties->equalsDefaultExceptFastFormat()) {
- trace("no fast format: equality\n");
- fields->canUseFastFormat = false;
- return;
- }
-
- // Now check the remaining properties.
- // Nontrivial affixes:
- UBool trivialPP = fields->properties->positivePrefixPattern.isEmpty();
- UBool trivialPS = fields->properties->positiveSuffixPattern.isEmpty();
- UBool trivialNP = fields->properties->negativePrefixPattern.isBogus() || (
- fields->properties->negativePrefixPattern.length() == 1 &&
- fields->properties->negativePrefixPattern.charAt(0) == u'-');
- UBool trivialNS = fields->properties->negativeSuffixPattern.isEmpty();
- if (!trivialPP || !trivialPS || !trivialNP || !trivialNS) {
- trace("no fast format: affixes\n");
- fields->canUseFastFormat = false;
- return;
- }
-
- // Grouping (secondary grouping is forbidden in equalsDefaultExceptFastFormat):
- bool groupingUsed = fields->properties->groupingUsed;
- int32_t groupingSize = fields->properties->groupingSize;
- bool unusualGroupingSize = groupingSize > 0 && groupingSize != 3;
- const UnicodeString& groupingString = fields->symbols->getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
- if (groupingUsed && (unusualGroupingSize || groupingString.length() != 1)) {
- trace("no fast format: grouping\n");
- fields->canUseFastFormat = false;
- return;
- }
-
- // Integer length:
- int32_t minInt = fields->exportedProperties->minimumIntegerDigits;
- int32_t maxInt = fields->exportedProperties->maximumIntegerDigits;
- // Fastpath supports up to only 10 digits (length of INT32_MIN)
- if (minInt > 10) {
- trace("no fast format: integer\n");
- fields->canUseFastFormat = false;
- return;
- }
-
- // Fraction length (no fraction part allowed in fast path):
- int32_t minFrac = fields->exportedProperties->minimumFractionDigits;
- if (minFrac > 0) {
- trace("no fast format: fraction\n");
- fields->canUseFastFormat = false;
- return;
- }
-
- // Other symbols:
- const UnicodeString& minusSignString = fields->symbols->getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
- UChar32 codePointZero = fields->symbols->getCodePointZero();
- if (minusSignString.length() != 1 || U16_LENGTH(codePointZero) != 1) {
- trace("no fast format: symbols\n");
- fields->canUseFastFormat = false;
- return;
- }
-
- // Good to go!
- trace("can use fast format!\n");
- fields->canUseFastFormat = true;
- fields->fastData.cpZero = static_cast<char16_t>(codePointZero);
- fields->fastData.cpGroupingSeparator = groupingUsed && groupingSize == 3 ? groupingString.charAt(0) : 0;
- fields->fastData.cpMinusSign = minusSignString.charAt(0);
- fields->fastData.minInt = (minInt < 0 || minInt > 127) ? 0 : static_cast<int8_t>(minInt);
- fields->fastData.maxInt = (maxInt < 0 || maxInt > 127) ? 127 : static_cast<int8_t>(maxInt);
-}
-
-bool DecimalFormat::fastFormatDouble(double input, UnicodeString& output) const {
- if (!fields->canUseFastFormat) {
- return false;
- }
- if (std::isnan(input)
- || std::trunc(input) != input
- || input <= INT32_MIN
- || input > INT32_MAX) {
- return false;
- }
- doFastFormatInt32(static_cast<int32_t>(input), std::signbit(input), output);
- return true;
-}
-
-bool DecimalFormat::fastFormatInt64(int64_t input, UnicodeString& output) const {
- if (!fields->canUseFastFormat) {
- return false;
- }
- if (input <= INT32_MIN || input > INT32_MAX) {
- return false;
- }
- doFastFormatInt32(static_cast<int32_t>(input), input < 0, output);
- return true;
-}
-
-void DecimalFormat::doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const {
- U_ASSERT(fields->canUseFastFormat);
- if (isNegative) {
- output.append(fields->fastData.cpMinusSign);
- U_ASSERT(input != INT32_MIN); // handled by callers
- input = -input;
- }
- // Cap at int32_t to make the buffer small and operations fast.
- // Longest string: "2,147,483,648" (13 chars in length)
- static constexpr int32_t localCapacity = 13;
- char16_t localBuffer[localCapacity];
- char16_t* ptr = localBuffer + localCapacity;
- int8_t group = 0;
- for (int8_t i = 0; i < fields->fastData.maxInt && (input != 0 || i < fields->fastData.minInt); i++) {
- if (group++ == 3 && fields->fastData.cpGroupingSeparator != 0) {
- *(--ptr) = fields->fastData.cpGroupingSeparator;
- group = 1;
- }
- std::div_t res = std::div(input, 10);
- *(--ptr) = static_cast<char16_t>(fields->fastData.cpZero + res.rem);
- input = res.quot;
- }
- int32_t len = localCapacity - static_cast<int32_t>(ptr - localBuffer);
- output.append(ptr, len);
-}
-
-
-#endif /* #if !UCONFIG_NO_FORMATTING */
diff --git a/tools/icu/patches/63/source/i18n/dtptngen.cpp b/tools/icu/patches/63/source/i18n/dtptngen.cpp
deleted file mode 100644
index 4ed724b6a4..0000000000
--- a/tools/icu/patches/63/source/i18n/dtptngen.cpp
+++ /dev/null
@@ -1,2752 +0,0 @@
-// © 2016 and later: Unicode, Inc. and others.
-// License & terms of use: http://www.unicode.org/copyright.html
-/*
-*******************************************************************************
-* Copyright (C) 2007-2016, International Business Machines Corporation and
-* others. All Rights Reserved.
-*******************************************************************************
-*
-* File DTPTNGEN.CPP
-*
-*******************************************************************************
-*/
-
-#include "unicode/utypes.h"
-#if !UCONFIG_NO_FORMATTING
-
-#include "unicode/datefmt.h"
-#include "unicode/decimfmt.h"
-#include "unicode/dtfmtsym.h"
-#include "unicode/dtptngen.h"
-#include "unicode/localpointer.h"
-#include "unicode/simpleformatter.h"
-#include "unicode/smpdtfmt.h"
-#include "unicode/udat.h"
-#include "unicode/udatpg.h"
-#include "unicode/uniset.h"
-#include "unicode/uloc.h"
-#include "unicode/ures.h"
-#include "unicode/ustring.h"
-#include "unicode/rep.h"
-#include "cpputils.h"
-#include "mutex.h"
-#include "umutex.h"
-#include "cmemory.h"
-#include "cstring.h"
-#include "locbased.h"
-#include "hash.h"
-#include "uhash.h"
-#include "uresimp.h"
-#include "dtptngen_impl.h"
-#include "ucln_in.h"
-#include "charstr.h"
-#include "uassert.h"
-
-#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
-/**
- * If we are on EBCDIC, use an iterator which will
- * traverse the bundles in ASCII order.
- */
-#define U_USE_ASCII_BUNDLE_ITERATOR
-#define U_SORT_ASCII_BUNDLE_ITERATOR
-#endif
-
-#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
-
-#include "unicode/ustring.h"
-#include "uarrsort.h"
-
-struct UResAEntry {
- UChar *key;
- UResourceBundle *item;
-};
-
-struct UResourceBundleAIterator {
- UResourceBundle *bund;
- UResAEntry *entries;
- int32_t num;
- int32_t cursor;
-};
-
-/* Must be C linkage to pass function pointer to the sort function */
-
-U_CDECL_BEGIN
-
-static int32_t U_CALLCONV
-ures_a_codepointSort(const void *context, const void *left, const void *right) {
- //CompareContext *cmp=(CompareContext *)context;
- return u_strcmp(((const UResAEntry *)left)->key,
- ((const UResAEntry *)right)->key);
-}
-
-U_CDECL_END
-
-static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) {
- if(U_FAILURE(*status)) {
- return;
- }
- aiter->bund = bund;
- aiter->num = ures_getSize(aiter->bund);
- aiter->cursor = 0;
-#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
- aiter->entries = nullptr;
-#else
- aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num);
- for(int i=0;i<aiter->num;i++) {
- aiter->entries[i].item = ures_getByIndex(aiter->bund, i, nullptr, status);
- const char *akey = ures_getKey(aiter->entries[i].item);
- int32_t len = uprv_strlen(akey)+1;
- aiter->entries[i].key = (UChar*)uprv_malloc(len*sizeof(UChar));
- u_charsToUChars(akey, aiter->entries[i].key, len);
- }
- uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, nullptr, TRUE, status);
-#endif
-}
-
-static void ures_a_close(UResourceBundleAIterator *aiter) {
-#if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
- for(int i=0;i<aiter->num;i++) {
- uprv_free(aiter->entries[i].key);
- ures_close(aiter->entries[i].item);
- }
-#endif
-}
-
-static const UChar *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) {
-#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
- return ures_getNextString(aiter->bund, len, key, err);
-#else
- if(U_FAILURE(*err)) return nullptr;
- UResourceBundle *item = aiter->entries[aiter->cursor].item;
- const UChar* ret = ures_getString(item, len, err);
- *key = ures_getKey(item);
- aiter->cursor++;
- return ret;
-#endif
-}
-
-
-#endif
-
-
-U_NAMESPACE_BEGIN
-
-// *****************************************************************************
-// class DateTimePatternGenerator
-// *****************************************************************************
-static const UChar Canonical_Items[] = {
- // GyQMwWEDFdaHmsSv
- CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, CAP_E,
- CAP_D, CAP_F, LOW_D, LOW_A, // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
- CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0
-};
-
-static const dtTypeElem dtTypes[] = {
- // patternChar, field, type, minLen, weight
- {CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,},
- {CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0},
- {CAP_G, UDATPG_ERA_FIELD, DT_NARROW, 5, 0},
-
- {LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20},
- {CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20},
- {LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20},
- {LOW_R, UDATPG_YEAR_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20},
- {CAP_U, UDATPG_YEAR_FIELD, DT_SHORT, 1, 3},
- {CAP_U, UDATPG_YEAR_FIELD, DT_LONG, 4, 0},
- {CAP_U, UDATPG_YEAR_FIELD, DT_NARROW, 5, 0},
-
- {CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2},
- {CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0},
- {CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0},
- {CAP_Q, UDATPG_QUARTER_FIELD, DT_NARROW, 5, 0},
- {LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
- {LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT - DT_DELTA, 3, 0},
- {LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {LOW_Q, UDATPG_QUARTER_FIELD, DT_NARROW - DT_DELTA, 5, 0},
-
- {CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2},
- {CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0},
- {CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0},
- {CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0},
- {CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
- {CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0},
- {CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0},
- {LOW_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 1},
-
- {LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2},
-
- {CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC, 1, 0},
-
- {CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3},
- {CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0},
- {CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0},
- {CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORTER, 6, 0},
- {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2},
- {LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0},
- {LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
- {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0},
- {LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORTER - 2*DT_DELTA, 6, 0},
- {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical
- {LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0},
- {LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0},
- {LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORTER - DT_DELTA, 6, 0},
-
- {LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2},
- {LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 20}, // really internal use, so we don't care
-
- {CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC, 1, 3},
-
- {CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC, 1, 0},
-
- {LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 3},
- {LOW_A, UDATPG_DAYPERIOD_FIELD, DT_LONG, 4, 0},
- {LOW_A, UDATPG_DAYPERIOD_FIELD, DT_NARROW, 5, 0},
- {LOW_B, UDATPG_DAYPERIOD_FIELD, DT_SHORT - DT_DELTA, 1, 3},
- {LOW_B, UDATPG_DAYPERIOD_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {LOW_B, UDATPG_DAYPERIOD_FIELD, DT_NARROW - DT_DELTA, 5, 0},
- // b needs to be closer to a than to B, so we make this 3*DT_DELTA
- {CAP_B, UDATPG_DAYPERIOD_FIELD, DT_SHORT - 3*DT_DELTA, 1, 3},
- {CAP_B, UDATPG_DAYPERIOD_FIELD, DT_LONG - 3*DT_DELTA, 4, 0},
- {CAP_B, UDATPG_DAYPERIOD_FIELD, DT_NARROW - 3*DT_DELTA, 5, 0},
-
- {CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour
- {LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2}, // 24 hour
- {LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour
- {CAP_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // 12 hour
- // The C code has had versions of the following 3, keep & update. Should not need these, but...
- // Without these, certain tests using e.g. staticGetSkeleton fail because j/J in patterns
- // get skipped instead of mapped to the right hour chars, for example in
- // DateFormatTest::TestPatternFromSkeleton
- // IntlTestDateTimePatternGeneratorAPI:: testStaticGetSkeleton
- // DateIntervalFormatTest::testTicket11985
- // Need to investigate better handling of jJC replacement e.g. in staticGetSkeleton.
- {CAP_J, UDATPG_HOUR_FIELD, DT_NUMERIC + 5*DT_DELTA, 1, 2}, // 12/24 hour no AM/PM
- {LOW_J, UDATPG_HOUR_FIELD, DT_NUMERIC + 6*DT_DELTA, 1, 6}, // 12/24 hour
- {CAP_C, UDATPG_HOUR_FIELD, DT_NUMERIC + 7*DT_DELTA, 1, 6}, // 12/24 hour with preferred dayPeriods for 12
-
- {LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2},
-
- {LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2},
- {CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000},
-
- {CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC, 1, 1000},
-
- {LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0},
- {LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
- {LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3},
- {LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0},
- {CAP_Z, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 3},
- {CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 5, 0},
- {CAP_O, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
- {CAP_O, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
- {CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 2, 0},
- {CAP_V, UDATPG_ZONE_FIELD, DT_LONG-1 - DT_DELTA, 3, 0},
- {CAP_V, UDATPG_ZONE_FIELD, DT_LONG-2 - DT_DELTA, 4, 0},
- {CAP_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
- {CAP_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
- {CAP_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
- {LOW_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
- {LOW_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
- {LOW_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
-
- {0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
- };
-
-static const char* const CLDR_FIELD_APPEND[] = {
- "Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week",
- "*", "*", "Day", "*", // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
- "Hour", "Minute", "Second", "*", "Timezone"
-};
-
-static const char* const CLDR_FIELD_NAME[UDATPG_FIELD_COUNT] = {
- "era", "year", "quarter", "month", "week", "weekOfMonth", "weekday",
- "dayOfYear", "weekdayOfMonth", "day", "dayperiod", // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
- "hour", "minute", "second", "*", "zone"
-};
-
-static const char* const CLDR_FIELD_WIDTH[] = { // [UDATPG_WIDTH_COUNT]
- "", "-short", "-narrow"
-};
-
-// TODO(ticket:13619): remove when definition uncommented in dtptngen.h.
-static const int32_t UDATPG_WIDTH_COUNT = UDATPG_NARROW + 1;
-static constexpr UDateTimePGDisplayWidth UDATPG_WIDTH_APPENDITEM = UDATPG_WIDE;
-static constexpr int32_t UDATPG_FIELD_KEY_MAX = 24; // max length of CLDR field tag (type + width)
-
-// For appendItems
-static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
- 0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
-
-//static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
-
-static const char DT_DateTimePatternsTag[]="DateTimePatterns";
-static const char DT_DateTimeCalendarTag[]="calendar";
-static const char DT_DateTimeGregorianTag[]="gregorian";
-static const char DT_DateTimeAppendItemsTag[]="appendItems";
-static const char DT_DateTimeFieldsTag[]="fields";
-static const char DT_DateTimeAvailableFormatsTag[]="availableFormats";
-//static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
-
-UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
-UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
-UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration)
-
-DateTimePatternGenerator* U_EXPORT2
-DateTimePatternGenerator::createInstance(UErrorCode& status) {
- return createInstance(Locale::getDefault(), status);
-}
-
-DateTimePatternGenerator* U_EXPORT2
-DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) {
- if (U_FAILURE(status)) {
- return nullptr;
- }
- LocalPointer<DateTimePatternGenerator> result(
- new DateTimePatternGenerator(locale, status), status);
- return U_SUCCESS(status) ? result.orphan() : nullptr;
-}
-
-DateTimePatternGenerator* U_EXPORT2
-DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) {
- if (U_FAILURE(status)) {
- return nullptr;
- }
- LocalPointer<DateTimePatternGenerator> result(
- new DateTimePatternGenerator(status), status);
- return U_SUCCESS(status) ? result.orphan() : nullptr;
-}
-
-DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
- skipMatcher(nullptr),
- fAvailableFormatKeyHash(nullptr),
- internalErrorCode(U_ZERO_ERROR)
-{
- fp = new FormatParser();
- dtMatcher = new DateTimeMatcher();
- distanceInfo = new DistanceInfo();
- patternMap = new PatternMap();
- if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
- internalErrorCode = status = U_MEMORY_ALLOCATION_ERROR;
- }
-}
-
-DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status) :
- skipMatcher(nullptr),
- fAvailableFormatKeyHash(nullptr),
- internalErrorCode(U_ZERO_ERROR)
-{
- fp = new FormatParser();
- dtMatcher = new DateTimeMatcher();
- distanceInfo = new DistanceInfo();
- patternMap = new PatternMap();
- if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
- internalErrorCode = status = U_MEMORY_ALLOCATION_ERROR;
- }
- else {
- initData(locale, status);
- }
-}
-
-DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) :
- UObject(),
- skipMatcher(nullptr),
- fAvailableFormatKeyHash(nullptr),
- internalErrorCode(U_ZERO_ERROR)
-{
- fp = new FormatParser();
- dtMatcher = new DateTimeMatcher();
- distanceInfo = new DistanceInfo();
- patternMap = new PatternMap();
- if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
- internalErrorCode = U_MEMORY_ALLOCATION_ERROR;
- }
- *this=other;
-}
-
-DateTimePatternGenerator&
-DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
- // reflexive case
- if (&other == this) {
- return *this;
- }
- internalErrorCode = other.internalErrorCode;
- pLocale = other.pLocale;
- fDefaultHourFormatChar = other.fDefaultHourFormatChar;
- *fp = *(other.fp);
- dtMatcher->copyFrom(other.dtMatcher->skeleton);
- *distanceInfo = *(other.distanceInfo);
- dateTimeFormat = other.dateTimeFormat;
- decimal = other.decimal;
- // NUL-terminate for the C API.
- dateTimeFormat.getTerminatedBuffer();
- decimal.getTerminatedBuffer();
- delete skipMatcher;
- if ( other.skipMatcher == nullptr ) {
- skipMatcher = nullptr;
- }
- else {
- skipMatcher = new DateTimeMatcher(*other.skipMatcher);
- if (skipMatcher == nullptr)
- {
- internalErrorCode = U_MEMORY_ALLOCATION_ERROR;
- return *this;
- }
- }
- for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) {
- appendItemFormats[i] = other.appendItemFormats[i];
- appendItemFormats[i].getTerminatedBuffer(); // NUL-terminate for the C API.
- for (int32_t j=0; j< UDATPG_WIDTH_COUNT; ++j ) {
- fieldDisplayNames[i][j] = other.fieldDisplayNames[i][j];
- fieldDisplayNames[i][j].getTerminatedBuffer(); // NUL-terminate for the C API.
- }
- }
- patternMap->copyFrom(*other.patternMap, internalErrorCode);
- copyHashtable(other.fAvailableFormatKeyHash, internalErrorCode);
- return *this;
-}
-
-
-UBool
-DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const {
- if (this == &other) {
- return TRUE;
- }
- if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
- (dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) {
- for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
- if (appendItemFormats[i] != other.appendItemFormats[i]) {
- return FALSE;
- }
- for (int32_t j=0; j< UDATPG_WIDTH_COUNT; ++j ) {
- if (fieldDisplayNames[i][j] != other.fieldDisplayNames[i][j]) {
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- else {
- return FALSE;
- }
-}
-
-UBool
-DateTimePatternGenerator::operator!=(const DateTimePatternGenerator& other) const {
- return !operator==(other);
-}
-
-DateTimePatternGenerator::~DateTimePatternGenerator() {
- if (fAvailableFormatKeyHash!=nullptr) {
- delete fAvailableFormatKeyHash;
- }
-
- if (fp != nullptr) delete fp;
- if (dtMatcher != nullptr) delete dtMatcher;
- if (distanceInfo != nullptr) delete distanceInfo;
- if (patternMap != nullptr) delete patternMap;
- if (skipMatcher != nullptr) delete skipMatcher;
-}
-
-namespace {
-
-UInitOnce initOnce = U_INITONCE_INITIALIZER;
-UHashtable *localeToAllowedHourFormatsMap = nullptr;
-
-// Value deleter for hashmap.
-U_CFUNC void U_CALLCONV deleteAllowedHourFormats(void *ptr) {
- uprv_free(ptr);
-}
-
-// Close hashmap at cleanup.
-U_CFUNC UBool U_CALLCONV allowedHourFormatsCleanup() {
- uhash_close(localeToAllowedHourFormatsMap);
- return TRUE;
-}
-
-enum AllowedHourFormat{
- ALLOWED_HOUR_FORMAT_UNKNOWN = -1,
- ALLOWED_HOUR_FORMAT_h,
- ALLOWED_HOUR_FORMAT_H,
- ALLOWED_HOUR_FORMAT_hb,
- ALLOWED_HOUR_FORMAT_Hb,
- ALLOWED_HOUR_FORMAT_hB,
- ALLOWED_HOUR_FORMAT_HB
-};
-
-} // namespace
-
-void
-DateTimePatternGenerator::initData(const Locale& locale, UErrorCode &status) {
- //const char *baseLangName = locale.getBaseName(); // unused
-
- skipMatcher = nullptr;
- fAvailableFormatKeyHash=nullptr;
- addCanonicalItems(status);
- addICUPatterns(locale, status);
- addCLDRData(locale, status);
- setDateTimeFromCalendar(locale, status);
- setDecimalSymbols(locale, status);
- umtx_initOnce(initOnce, loadAllowedHourFormatsData, status);
- getAllowedHourFormats(locale, status);
- // If any of the above methods failed then the object is in an invalid state.
- internalErrorCode = status;
-} // DateTimePatternGenerator::initData
-
-namespace {
-
-struct AllowedHourFormatsSink : public ResourceSink {
- // Initialize sub-sinks.
- AllowedHourFormatsSink() {}
- virtual ~AllowedHourFormatsSink();
-
- virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
- UErrorCode &errorCode) {
- ResourceTable timeData = value.getTable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- for (int32_t i = 0; timeData.getKeyAndValue(i, key, value); ++i) {
- const char *regionOrLocale = key;
- ResourceTable formatList = value.getTable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- for (int32_t j = 0; formatList.getKeyAndValue(j, key, value); ++j) {
- if (uprv_strcmp(key, "allowed") == 0) { // Ignore "preferred" list.
- LocalMemory<int32_t> list;
- int32_t length;
- if (value.getType() == URES_STRING) {
- if (list.allocateInsteadAndReset(2) == nullptr) {
- errorCode = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- list[0] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
- length = 1;
- }
- else {
- ResourceArray allowedFormats = value.getArray(errorCode);
- length = allowedFormats.getSize();
- if (list.allocateInsteadAndReset(length + 1) == nullptr) {
- errorCode = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- for (int32_t k = 0; k < length; ++k) {
- allowedFormats.getValue(k, value);
- list[k] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
- }
- }
- list[length] = ALLOWED_HOUR_FORMAT_UNKNOWN;
- uhash_put(localeToAllowedHourFormatsMap,
- const_cast<char *>(regionOrLocale), list.orphan(), &errorCode);
- if (U_FAILURE(errorCode)) { return; }
- }
- }
- }
- }
-
- AllowedHourFormat getHourFormatFromUnicodeString(const UnicodeString &s) {
- if (s.length() == 1) {
- if (s[0] == LOW_H) { return ALLOWED_HOUR_FORMAT_h; }
- if (s[0] == CAP_H) { return ALLOWED_HOUR_FORMAT_H; }
- } else if (s.length() == 2) {
- if (s[0] == LOW_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_hb; }
- if (s[0] == CAP_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_Hb; }
- if (s[0] == LOW_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_hB; }
- if (s[0] == CAP_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_HB; }
- }
-
- return ALLOWED_HOUR_FORMAT_UNKNOWN;
- }
-};
-
-} // namespace
-
-AllowedHourFormatsSink::~AllowedHourFormatsSink() {}
-
-U_CFUNC void U_CALLCONV DateTimePatternGenerator::loadAllowedHourFormatsData(UErrorCode &status) {
- if (U_FAILURE(status)) { return; }
- localeToAllowedHourFormatsMap = uhash_open(
- uhash_hashChars, uhash_compareChars, nullptr, &status);
- if (U_FAILURE(status)) { return; }
-
- uhash_setValueDeleter(localeToAllowedHourFormatsMap, deleteAllowedHourFormats);
- ucln_i18n_registerCleanup(UCLN_I18N_ALLOWED_HOUR_FORMATS, allowedHourFormatsCleanup);
-
- LocalUResourceBundlePointer rb(ures_openDirect(nullptr, "supplementalData", &status));
- if (U_FAILURE(status)) { return; }
-
- AllowedHourFormatsSink sink;
- // TODO: Currently in the enumeration each table allocates a new array.
- // Try to reduce the number of memory allocations. Consider storing a
- // UVector32 with the concatenation of all of the sub-arrays, put the start index
- // into the hashmap, store 6 single-value sub-arrays right at the beginning of the
- // vector (at index enum*2) for easy data sharing, copy sub-arrays into runtime
- // object. Remember to clean up the vector, too.
- ures_getAllItemsWithFallback(rb.getAlias(), "timeData", sink, status);
-}
-
-void DateTimePatternGenerator::getAllowedHourFormats(const Locale &locale, UErrorCode &status) {
- if (U_FAILURE(status)) { return; }
- const char *localeID = locale.getName();
- char maxLocaleID[ULOC_FULLNAME_CAPACITY];
- int32_t length = uloc_addLikelySubtags(localeID, maxLocaleID, ULOC_FULLNAME_CAPACITY, &status);
- if (U_FAILURE(status)) {
- return;
- } else if (length == ULOC_FULLNAME_CAPACITY) { // no room for NUL
- status = U_BUFFER_OVERFLOW_ERROR;
- return;
- }
- Locale maxLocale = Locale(maxLocaleID);
-
- const char *country = maxLocale.getCountry();
- if (*country == '\0') { country = "001"; }
- const char *language = maxLocale.getLanguage();
-
- CharString langCountry;
- langCountry.append(language, static_cast<int32_t>(uprv_strlen(language)), status);
- langCountry.append('_', status);
- langCountry.append(country, static_cast<int32_t>(uprv_strlen(country)), status);
-
- int32_t *allowedFormats;
- allowedFormats = (int32_t *)uhash_get(localeToAllowedHourFormatsMap, langCountry.data());
- if (allowedFormats == nullptr) {
- allowedFormats = (int32_t *)uhash_get(localeToAllowedHourFormatsMap, const_cast<char *>(country));
- }
-
- if (allowedFormats != nullptr) { // Lookup is successful
- for (int32_t i = 0; i < UPRV_LENGTHOF(fAllowedHourFormats); ++i) {
- fAllowedHourFormats[i] = allowedFormats[i];
- if (allowedFormats[i] == ALLOWED_HOUR_FORMAT_UNKNOWN) {
- break;
- }
- }
- } else { // Lookup failed, twice
- fAllowedHourFormats[0] = ALLOWED_HOUR_FORMAT_H;
- fAllowedHourFormats[1] = ALLOWED_HOUR_FORMAT_UNKNOWN;
- }
-}
-
-UnicodeString
-DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode&
-/*status*/) {
- FormatParser fp2;
- DateTimeMatcher matcher;
- PtnSkeleton localSkeleton;
- matcher.set(pattern, &fp2, localSkeleton);
- return localSkeleton.getSkeleton();
-}
-
-UnicodeString
-DateTimePatternGenerator::staticGetSkeleton(
- const UnicodeString& pattern, UErrorCode& /*status*/) {
- FormatParser fp;
- DateTimeMatcher matcher;
- PtnSkeleton localSkeleton;
- matcher.set(pattern, &fp, localSkeleton);
- return localSkeleton.getSkeleton();
-}
-
-UnicodeString
-DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) {
- FormatParser fp2;
- DateTimeMatcher matcher;
- PtnSkeleton localSkeleton;
- matcher.set(pattern, &fp2, localSkeleton);
- return localSkeleton.getBaseSkeleton();
-}
-
-UnicodeString
-DateTimePatternGenerator::staticGetBaseSkeleton(
- const UnicodeString& pattern, UErrorCode& /*status*/) {
- FormatParser fp;
- DateTimeMatcher matcher;
- PtnSkeleton localSkeleton;
- matcher.set(pattern, &fp, localSkeleton);
- return localSkeleton.getBaseSkeleton();
-}
-
-void
-DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
- UnicodeString dfPattern;
- UnicodeString conflictingString;
- DateFormat* df;
-
- // Load with ICU patterns
- for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) {
- DateFormat::EStyle style = (DateFormat::EStyle)i;
- df = DateFormat::createDateInstance(style, locale);
- SimpleDateFormat* sdf;
- if (df != nullptr && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != nullptr) {
- sdf->toPattern(dfPattern);
- addPattern(dfPattern, FALSE, conflictingString, status);
- }
- // TODO Maybe we should return an error when the date format isn't simple.
- delete df;
- if (U_FAILURE(status)) { return; }
-
- df = DateFormat::createTimeInstance(style, locale);
- if (df != nullptr && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != nullptr) {
- sdf->toPattern(dfPattern);
- addPattern(dfPattern, FALSE, conflictingString, status);
-
- // TODO: C++ and Java are inconsistent (see #12568).
- // C++ uses MEDIUM, but Java uses SHORT.
- if ( i==DateFormat::kShort && !dfPattern.isEmpty() ) {
- consumeShortTimePattern(dfPattern, status);
- }
- }
- // TODO Maybe we should return an error when the date format isn't simple.
- delete df;
- if (U_FAILURE(status)) { return; }
- }
-}
-
-void
-DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status) {
- UnicodeString conflictingString;
-
- fp->set(hackPattern);
- UnicodeString mmss;
- UBool gotMm=FALSE;
- for (int32_t i=0; i<fp->itemNumber; ++i) {
- UnicodeString field = fp->items[i];
- if ( fp->isQuoteLiteral(field) ) {
- if ( gotMm ) {
- UnicodeString quoteLiteral;
- fp->getQuoteLiteral(quoteLiteral, &i);
- mmss += quoteLiteral;
- }
- }
- else {
- if (fp->isPatternSeparator(field) && gotMm) {
- mmss+=field;
- }
- else {
- UChar ch=field.charAt(0);
- if (ch==LOW_M) {
- gotMm=TRUE;
- mmss+=field;
- }
- else {
- if (ch==LOW_S) {
- if (!gotMm) {
- break;
- }
- mmss+= field;
- addPattern(mmss, FALSE, conflictingString, status);
- break;
- }
- else {
- if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) {
- break;
- }
- }
- }
- }
- }
- }
-}
-
-#define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
-
-static const UChar hourFormatChars[] = { CAP_H, LOW_H, CAP_K, LOW_K, 0 }; // HhKk, the hour format characters
-
-void
-DateTimePatternGenerator::getCalendarTypeToUse(const Locale& locale, CharString& destination, UErrorCode& err) {
- destination.clear().append(DT_DateTimeGregorianTag, -1, err); // initial default
- if ( U_SUCCESS(err) ) {
- UErrorCode localStatus = U_ZERO_ERROR;
- char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY];
- // obtain a locale that always has the calendar key value that should be used
- ures_getFunctionalEquivalent(
- localeWithCalendarKey,
- ULOC_LOCALE_IDENTIFIER_CAPACITY,
- nullptr,
- "calendar",
- "calendar",
- locale.getName(),
- nullptr,
- FALSE,
- &localStatus);
- localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination
- // now get the calendar key value from that locale
- char calendarType[ULOC_KEYWORDS_CAPACITY];
- int32_t calendarTypeLen = uloc_getKeywordValue(
- localeWithCalendarKey,
- "calendar",
- calendarType,
- ULOC_KEYWORDS_CAPACITY,
- &localStatus);
- // If the input locale was invalid, don't fail with missing resource error, instead
- // continue with default of Gregorian.
- if (U_FAILURE(localStatus) && localStatus != U_MISSING_RESOURCE_ERROR) {
- err = localStatus;
- return;
- }
- if (calendarTypeLen < ULOC_KEYWORDS_CAPACITY) {
- destination.clear().append(calendarType, -1, err);
- if (U_FAILURE(err)) { return; }
- }
- }
-}
-
-void
-DateTimePatternGenerator::consumeShortTimePattern(const UnicodeString& shortTimePattern,
- UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
- // set fDefaultHourFormatChar to the hour format character from this pattern
- int32_t tfIdx, tfLen = shortTimePattern.length();
- UBool ignoreChars = FALSE;
- for (tfIdx = 0; tfIdx < tfLen; tfIdx++) {
- UChar tfChar = shortTimePattern.charAt(tfIdx);
- if ( tfChar == SINGLE_QUOTE ) {
- ignoreChars = !ignoreChars; // toggle (handle quoted literals & '' for single quote)
- } else if ( !ignoreChars && u_strchr(hourFormatChars, tfChar) != nullptr ) {
- fDefaultHourFormatChar = tfChar;
- break;
- }
- }
-
- // HACK for hh:ss
- hackTimes(shortTimePattern, status);
-}
-
-struct DateTimePatternGenerator::AppendItemFormatsSink : public ResourceSink {
-
- // Destination for data, modified via setters.
- DateTimePatternGenerator& dtpg;
-
- AppendItemFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
- virtual ~AppendItemFormatsSink();
-
- virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
- UErrorCode &errorCode) {
- ResourceTable itemsTable = value.getTable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- for (int32_t i = 0; itemsTable.getKeyAndValue(i, key, value); ++i) {
- UDateTimePatternField field = dtpg.getAppendFormatNumber(key);
- if (field == UDATPG_FIELD_COUNT) { continue; }
- const UnicodeString& valueStr = value.getUnicodeString(errorCode);
- if (dtpg.getAppendItemFormat(field).isEmpty() && !valueStr.isEmpty()) {
- dtpg.setAppendItemFormat(field, valueStr);
- }
- }
- }
-
- void fillInMissing() {
- UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, UPRV_LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias.
- for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
- UDateTimePatternField field = (UDateTimePatternField)i;
- if (dtpg.getAppendItemFormat(field).isEmpty()) {
- dtpg.setAppendItemFormat(field, defaultItemFormat);
- }
- }
- }
-};
-
-struct DateTimePatternGenerator::AppendItemNamesSink : public ResourceSink {
-
- // Destination for data, modified via setters.
- DateTimePatternGenerator& dtpg;
-
- AppendItemNamesSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
- virtual ~AppendItemNamesSink();
-
- virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
- UErrorCode &errorCode) {
- ResourceTable itemsTable = value.getTable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- for (int32_t i = 0; itemsTable.getKeyAndValue(i, key, value); ++i) {
- UDateTimePGDisplayWidth width;
- UDateTimePatternField field = dtpg.getFieldAndWidthIndices(key, &width);
- if (field == UDATPG_FIELD_COUNT) { continue; }
- ResourceTable detailsTable = value.getTable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- for (int32_t j = 0; detailsTable.getKeyAndValue(j, key, value); ++j) {
- if (uprv_strcmp(key, "dn") != 0) { continue; }
- const UnicodeString& valueStr = value.getUnicodeString(errorCode);
- if (dtpg.getFieldDisplayName(field,width).isEmpty() && !valueStr.isEmpty()) {
- dtpg.setFieldDisplayName(field,width,valueStr);
- }
- break;
- }
- }
- }
-
- void fillInMissing() {
- for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
- UnicodeString& valueStr = dtpg.getMutableFieldDisplayName((UDateTimePatternField)i, UDATPG_WIDE);
- if (valueStr.isEmpty()) {
- valueStr = CAP_F;
- U_ASSERT(i < 20);
- if (i < 10) {
- // F0, F1, ..., F9
- valueStr += (UChar)(i+0x30);
- } else {
- // F10, F11, ...
- valueStr += (UChar)0x31;
- valueStr += (UChar)(i-10 + 0x30);
- }
- // NUL-terminate for the C API.
- valueStr.getTerminatedBuffer();
- }
- for (int32_t j = 1; j < UDATPG_WIDTH_COUNT; j++) {
- UnicodeString& valueStr2 = dtpg.getMutableFieldDisplayName((UDateTimePatternField)i, (UDateTimePGDisplayWidth)j);
- if (valueStr2.isEmpty()) {
- valueStr2 = dtpg.getFieldDisplayName((UDateTimePatternField)i, (UDateTimePGDisplayWidth)(j-1));
- }
- }
- }
- }
-};
-
-struct DateTimePatternGenerator::AvailableFormatsSink : public ResourceSink {
-
- // Destination for data, modified via setters.
- DateTimePatternGenerator& dtpg;
-
- // Temporary variable, required for calling addPatternWithSkeleton.
- UnicodeString conflictingPattern;
-
- AvailableFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
- virtual ~AvailableFormatsSink();
-
- virtual void put(const char *key, ResourceValue &value, UBool isRoot,
- UErrorCode &errorCode) {
- ResourceTable itemsTable = value.getTable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- for (int32_t i = 0; itemsTable.getKeyAndValue(i, key, value); ++i) {
- const UnicodeString formatKey(key, -1, US_INV);
- if (!dtpg.isAvailableFormatSet(formatKey) ) {
- dtpg.setAvailableFormat(formatKey, errorCode);
- // Add pattern with its associated skeleton. Override any duplicate
- // derived from std patterns, but not a previous availableFormats entry:
- const UnicodeString& formatValue = value.getUnicodeString(errorCode);
- conflictingPattern.remove();
- dtpg.addPatternWithSkeleton(formatValue, &formatKey, !isRoot, conflictingPattern, errorCode);
- }
- }
- }
-};
-
-// Virtual destructors must be defined out of line.
-DateTimePatternGenerator::AppendItemFormatsSink::~AppendItemFormatsSink() {}
-DateTimePatternGenerator::AppendItemNamesSink::~AppendItemNamesSink() {}
-DateTimePatternGenerator::AvailableFormatsSink::~AvailableFormatsSink() {}
-
-void
-DateTimePatternGenerator::addCLDRData(const Locale& locale, UErrorCode& errorCode) {
- if (U_FAILURE(errorCode)) { return; }
- UnicodeString rbPattern, value, field;
- CharString path;
-
- LocalUResourceBundlePointer rb(ures_open(nullptr, locale.getName(), &errorCode));
- if (U_FAILURE(errorCode)) { return; }
-
- CharString calendarTypeToUse; // to be filled in with the type to use, if all goes well
- getCalendarTypeToUse(locale, calendarTypeToUse, errorCode);
- if (U_FAILURE(errorCode)) { return; }
-
- // Local err to ignore resource not found exceptions
- UErrorCode err = U_ZERO_ERROR;
-
- // Load append item formats.
- AppendItemFormatsSink appendItemFormatsSink(*this);
- path.clear()
- .append(DT_DateTimeCalendarTag, errorCode)
- .append('/', errorCode)
- .append(calendarTypeToUse, errorCode)
- .append('/', errorCode)
- .append(DT_DateTimeAppendItemsTag, errorCode); // i.e., calendar/xxx/appendItems
- if (U_FAILURE(errorCode)) { return; }
- ures_getAllItemsWithFallback(rb.getAlias(), path.data(), appendItemFormatsSink, err);
- appendItemFormatsSink.fillInMissing();
-
- // Load CLDR item names.
- err = U_ZERO_ERROR;
- AppendItemNamesSink appendItemNamesSink(*this);
- ures_getAllItemsWithFallback(rb.getAlias(), DT_DateTimeFieldsTag, appendItemNamesSink, err);
- appendItemNamesSink.fillInMissing();
-
- // Load the available formats from CLDR.
- err = U_ZERO_ERROR;
- initHashtable(errorCode);
- if (U_FAILURE(errorCode)) { return; }
- AvailableFormatsSink availableFormatsSink(*this);
- path.clear()
- .append(DT_DateTimeCalendarTag, errorCode)
- .append('/', errorCode)
- .append(calendarTypeToUse, errorCode)
- .append('/', errorCode)
- .append(DT_DateTimeAvailableFormatsTag, errorCode); // i.e., calendar/xxx/availableFormats
- if (U_FAILURE(errorCode)) { return; }
- ures_getAllItemsWithFallback(rb.getAlias(), path.data(), availableFormatsSink, err);
-}
-
-void
-DateTimePatternGenerator::initHashtable(UErrorCode& err) {
- if (U_FAILURE(err)) { return; }
- if (fAvailableFormatKeyHash!=nullptr) {
- return;
- }
- LocalPointer<Hashtable> hash(new Hashtable(FALSE, err), err);
- if (U_SUCCESS(err)) {
- fAvailableFormatKeyHash = hash.orphan();
- }
-}
-
-void
-DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value) {
- appendItemFormats[field] = value;
- // NUL-terminate for the C API.
- appendItemFormats[field].getTerminatedBuffer();
-}
-
-const UnicodeString&
-DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field) const {
- return appendItemFormats[field];
-}
-
-void
-DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field, const UnicodeString& value) {
- setFieldDisplayName(field, UDATPG_WIDTH_APPENDITEM, value);
-}
-
-const UnicodeString&
-DateTimePatternGenerator::getAppendItemName(UDateTimePatternField field) const {
- return fieldDisplayNames[field][UDATPG_WIDTH_APPENDITEM];
-}
-
-void
-DateTimePatternGenerator::setFieldDisplayName(UDateTimePatternField field, UDateTimePGDisplayWidth width, const UnicodeString& value) {
- fieldDisplayNames[field][width] = value;
- // NUL-terminate for the C API.
- fieldDisplayNames[field][width].getTerminatedBuffer();
-}
-
-UnicodeString
-DateTimePatternGenerator::getFieldDisplayName(UDateTimePatternField field, UDateTimePGDisplayWidth width) const {
- return fieldDisplayNames[field][width];
-}
-
-UnicodeString&
-DateTimePatternGenerator::getMutableFieldDisplayName(UDateTimePatternField field, UDateTimePGDisplayWidth width) {
- return fieldDisplayNames[field][width];
-}
-
-void
-DateTimePatternGenerator::getAppendName(UDateTimePatternField field, UnicodeString& value) {
- value = SINGLE_QUOTE;
- value += fieldDisplayNames[field][UDATPG_WIDTH_APPENDITEM];
- value += SINGLE_QUOTE;
-}
-
-UnicodeString
-DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErrorCode& status) {
- return getBestPattern(patternForm, UDATPG_MATCH_NO_OPTIONS, status);
-}
-
-UnicodeString
-DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UDateTimePatternMatchOptions options, UErrorCode& status) {
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return UnicodeString();
- }
- const UnicodeString *bestPattern = nullptr;
- UnicodeString dtFormat;
- UnicodeString resultPattern;
- int32_t flags = kDTPGNoFlags;
-
- int32_t dateMask=(1<<UDATPG_DAYPERIOD_FIELD) - 1;
- int32_t timeMask=(1<<UDATPG_FIELD_COUNT) - 1 - dateMask;
-
- // Replace hour metacharacters 'j', 'C' and 'J', set flags as necessary
- UnicodeString patternFormMapped = mapSkeletonMetacharacters(patternForm, &flags, status);
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
-
- resultPattern.remove();
- dtMatcher->set(patternFormMapped, fp);
- const PtnSkeleton* specifiedSkeleton = nullptr;
- bestPattern=getBestRaw(*dtMatcher, -1, distanceInfo, status, &specifiedSkeleton);
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
-
- if ( distanceInfo->missingFieldMask==0 && distanceInfo->extraFieldMask==0 ) {
- resultPattern = adjustFieldTypes(*bestPattern, specifiedSkeleton, flags, options);
-
- return resultPattern;
- }
- int32_t neededFields = dtMatcher->getFieldMask();
- UnicodeString datePattern=getBestAppending(neededFields & dateMask, flags, status, options);
- UnicodeString timePattern=getBestAppending(neededFields & timeMask, flags, status, options);
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
- if (datePattern.length()==0) {
- if (timePattern.length()==0) {
- resultPattern.remove();
- }
- else {
- return timePattern;
- }
- }
- if (timePattern.length()==0) {
- return datePattern;
- }
- resultPattern.remove();
- status = U_ZERO_ERROR;
- dtFormat=getDateTimeFormat();
- SimpleFormatter(dtFormat, 2, 2, status).format(timePattern, datePattern, resultPattern, status);
- return resultPattern;
-}
-
-/*
- * Map a skeleton that may have metacharacters jJC to one without, by replacing
- * the metacharacters with locale-appropriate fields of h/H/k/K and of a/b/B
- * (depends on fDefaultHourFormatChar and fAllowedHourFormats being set, which in
- * turn depends on initData having been run). This method also updates the flags
- * as necessary. Returns the updated skeleton.
- */
-UnicodeString
-DateTimePatternGenerator::mapSkeletonMetacharacters(const UnicodeString& patternForm, int32_t* flags, UErrorCode& status) {
- UnicodeString patternFormMapped;
- patternFormMapped.remove();
- UBool inQuoted = FALSE;
- int32_t patPos, patLen = patternForm.length();
- for (patPos = 0; patPos < patLen; patPos++) {
- UChar patChr = patternForm.charAt(patPos);
- if (patChr == SINGLE_QUOTE) {
- inQuoted = !inQuoted;
- } else if (!inQuoted) {
- // Handle special mappings for 'j' and 'C' in which fields lengths
- // 1,3,5 => hour field length 1
- // 2,4,6 => hour field length 2
- // 1,2 => abbreviated dayPeriod (field length 1..3)
- // 3,4 => long dayPeriod (field length 4)
- // 5,6 => narrow dayPeriod (field length 5)
- if (patChr == LOW_J || patChr == CAP_C) {
- int32_t extraLen = 0; // 1 less than total field length
- while (patPos+1 < patLen && patternForm.charAt(patPos+1)==patChr) {
- extraLen++;
- patPos++;
- }
- int32_t hourLen = 1 + (extraLen & 1);
- int32_t dayPeriodLen = (extraLen < 2)? 1: 3 + (extraLen >> 1);
- UChar hourChar = LOW_H;
- UChar dayPeriodChar = LOW_A;
- if (patChr == LOW_J) {
- hourChar = fDefaultHourFormatChar;
- } else {
- AllowedHourFormat preferred;
- if (fAllowedHourFormats[0] != ALLOWED_HOUR_FORMAT_UNKNOWN) {
- preferred = (AllowedHourFormat)fAllowedHourFormats[0];
- } else {
- status = U_INVALID_FORMAT_ERROR;
- return UnicodeString();
- }
- if (preferred == ALLOWED_HOUR_FORMAT_H || preferred == ALLOWED_HOUR_FORMAT_HB || preferred == ALLOWED_HOUR_FORMAT_Hb) {
- hourChar = CAP_H;
- }
- // in #13183 just add b/B to skeleton, no longer need to set special flags
- if (preferred == ALLOWED_HOUR_FORMAT_HB || preferred == ALLOWED_HOUR_FORMAT_hB) {
- dayPeriodChar = CAP_B;
- } else if (preferred == ALLOWED_HOUR_FORMAT_Hb || preferred == ALLOWED_HOUR_FORMAT_hb) {
- dayPeriodChar = LOW_B;
- }
- }
- if (hourChar==CAP_H || hourChar==LOW_K) {
- dayPeriodLen = 0;
- }
- while (dayPeriodLen-- > 0) {
- patternFormMapped.append(dayPeriodChar);
- }
- while (hourLen-- > 0) {
- patternFormMapped.append(hourChar);
- }
- } else if (patChr == CAP_J) {
- // Get pattern for skeleton with H, then replace H or k
- // with fDefaultHourFormatChar (if different)
- patternFormMapped.append(CAP_H);
- *flags |= kDTPGSkeletonUsesCapJ;
- } else {
- patternFormMapped.append(patChr);
- }
- }
- }
- return patternFormMapped;
-}
-
-UnicodeString
-DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
- const UnicodeString& skeleton,
- UErrorCode& status) {
- return replaceFieldTypes(pattern, skeleton, UDATPG_MATCH_NO_OPTIONS, status);
-}
-
-UnicodeString
-DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
- const UnicodeString& skeleton,
- UDateTimePatternMatchOptions options,
- UErrorCode& status) {
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return UnicodeString();
- }
- dtMatcher->set(skeleton, fp);
- UnicodeString result = adjustFieldTypes(pattern, nullptr, kDTPGNoFlags, options);
- return result;
-}
-
-void
-DateTimePatternGenerator::setDecimal(const UnicodeString& newDecimal) {
- this->decimal = newDecimal;
- // NUL-terminate for the C API.
- this->decimal.getTerminatedBuffer();
-}
-
-const UnicodeString&
-DateTimePatternGenerator::getDecimal() const {
- return decimal;
-}
-
-void
-DateTimePatternGenerator::addCanonicalItems(UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
- UnicodeString conflictingPattern;
-
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; i++) {
- if (Canonical_Items[i] > 0) {
- addPattern(UnicodeString(Canonical_Items[i]), FALSE, conflictingPattern, status);
- }
- if (U_FAILURE(status)) { return; }
- }
-}
-
-void
-DateTimePatternGenerator::setDateTimeFormat(const UnicodeString& dtFormat) {
- dateTimeFormat = dtFormat;
- // NUL-terminate for the C API.
- dateTimeFormat.getTerminatedBuffer();
-}
-
-const UnicodeString&
-DateTimePatternGenerator::getDateTimeFormat() const {
- return dateTimeFormat;
-}
-
-void
-DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale, UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
-
- const UChar *resStr;
- int32_t resStrLen = 0;
-
- LocalPointer<Calendar> fCalendar(Calendar::createInstance(locale, status), status);
- if (U_FAILURE(status)) { return; }
-
- LocalUResourceBundlePointer calData(ures_open(nullptr, locale.getBaseName(), &status));
- if (U_FAILURE(status)) { return; }
- ures_getByKey(calData.getAlias(), DT_DateTimeCalendarTag, calData.getAlias(), &status);
- if (U_FAILURE(status)) { return; }
-
- LocalUResourceBundlePointer dateTimePatterns;
- if (fCalendar->getType() != nullptr && *fCalendar->getType() != '\0'
- && uprv_strcmp(fCalendar->getType(), DT_DateTimeGregorianTag) != 0) {
- dateTimePatterns.adoptInstead(ures_getByKeyWithFallback(calData.getAlias(), fCalendar->getType(),
- nullptr, &status));
- ures_getByKeyWithFallback(dateTimePatterns.getAlias(), DT_DateTimePatternsTag,
- dateTimePatterns.getAlias(), &status);
- }
-
- if (dateTimePatterns.isNull() || status == U_MISSING_RESOURCE_ERROR) {
- status = U_ZERO_ERROR;
- dateTimePatterns.adoptInstead(ures_getByKeyWithFallback(calData.getAlias(), DT_DateTimeGregorianTag,
- dateTimePatterns.orphan(), &status));
- ures_getByKeyWithFallback(dateTimePatterns.getAlias(), DT_DateTimePatternsTag,
- dateTimePatterns.getAlias(), &status);
- }
- if (U_FAILURE(status)) { return; }
-
- if (ures_getSize(dateTimePatterns.getAlias()) <= DateFormat::kDateTime)
- {
- status = U_INVALID_FORMAT_ERROR;
- return;
- }
- resStr = ures_getStringByIndex(dateTimePatterns.getAlias(), (int32_t)DateFormat::kDateTime, &resStrLen, &status);
- setDateTimeFormat(UnicodeString(TRUE, resStr, resStrLen));
-}
-
-void
-DateTimePatternGenerator::setDecimalSymbols(const Locale& locale, UErrorCode& status) {
- DecimalFormatSymbols dfs = DecimalFormatSymbols(locale, status);
- if(U_SUCCESS(status)) {
- decimal = dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
- // NUL-terminate for the C API.
- decimal.getTerminatedBuffer();
- }
-}
-
-UDateTimePatternConflict
-DateTimePatternGenerator::addPattern(
- const UnicodeString& pattern,
- UBool override,
- UnicodeString &conflictingPattern,
- UErrorCode& status)
-{
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return UDATPG_NO_CONFLICT;
- }
-
- return addPatternWithSkeleton(pattern, nullptr, override, conflictingPattern, status);
-}
-
-// For DateTimePatternGenerator::addPatternWithSkeleton -
-// If skeletonToUse is specified, then an availableFormats entry is being added. In this case:
-// 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern.
-// 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified
-// (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override
-// parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual
-// specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was
-// derived (i.e. entries derived from the standard date/time patters for the specified locale).
-// 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a
-// specified skeleton (which sets a new field in the PtnElem in the PatternMap).
-UDateTimePatternConflict
-DateTimePatternGenerator::addPatternWithSkeleton(
- const UnicodeString& pattern,
- const UnicodeString* skeletonToUse,
- UBool override,
- UnicodeString& conflictingPattern,
- UErrorCode& status)
-{
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return UDATPG_NO_CONFLICT;
- }
-
- UnicodeString basePattern;
- PtnSkeleton skeleton;
- UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT;
-
- DateTimeMatcher matcher;
- if ( skeletonToUse == nullptr ) {
- matcher.set(pattern, fp, skeleton);
- matcher.getBasePattern(basePattern);
- } else {
- matcher.set(*skeletonToUse, fp, skeleton); // no longer trims skeleton fields to max len 3, per #7930
- matcher.getBasePattern(basePattern); // or perhaps instead: basePattern = *skeletonToUse;
- }
- // We only care about base conflicts - and replacing the pattern associated with a base - if:
- // 1. the conflicting previous base pattern did *not* have an explicit skeleton; in that case the previous
- // base + pattern combination was derived from either (a) a canonical item, (b) a standard format, or
- // (c) a pattern specified programmatically with a previous call to addPattern (which would only happen
- // if we are getting here from a subsequent call to addPattern).
- // 2. a skeleton is specified for the current pattern, but override=false; in that case we are checking
- // availableFormats items from root, which should not override any previous entry with the same base.
- UBool entryHadSpecifiedSkeleton;
- const UnicodeString *duplicatePattern = patternMap->getPatternFromBasePattern(basePattern, entryHadSpecifiedSkeleton);
- if (duplicatePattern != nullptr && (!entryHadSpecifiedSkeleton || (skeletonToUse != nullptr && !override))) {
- conflictingStatus = UDATPG_BASE_CONFLICT;
- conflictingPattern = *duplicatePattern;
- if (!override) {
- return conflictingStatus;
- }
- }
- // The only time we get here with override=true and skeletonToUse!=null is when adding availableFormats
- // items from CLDR data. In that case, we don't want an item from a parent locale to replace an item with
- // same skeleton from the specified locale, so skip the current item if skeletonWasSpecified is true for
- // the previously-specified conflicting item.
- const PtnSkeleton* entrySpecifiedSkeleton = nullptr;
- duplicatePattern = patternMap->getPatternFromSkeleton(skeleton, &entrySpecifiedSkeleton);
- if (duplicatePattern != nullptr ) {
- conflictingStatus = UDATPG_CONFLICT;
- conflictingPattern = *duplicatePattern;
- if (!override || (skeletonToUse != nullptr && entrySpecifiedSkeleton != nullptr)) {
- return conflictingStatus;
- }
- }
- patternMap->add(basePattern, skeleton, pattern, skeletonToUse != nullptr, status);
- if(U_FAILURE(status)) {
- return conflictingStatus;
- }
-
- return UDATPG_NO_CONFLICT;
-}
-
-
-UDateTimePatternField
-DateTimePatternGenerator::getAppendFormatNumber(const char* field) const {
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
- if (uprv_strcmp(CLDR_FIELD_APPEND[i], field)==0) {
- return (UDateTimePatternField)i;
- }
- }
- return UDATPG_FIELD_COUNT;
-}
-
-UDateTimePatternField
-DateTimePatternGenerator::getFieldAndWidthIndices(const char* key, UDateTimePGDisplayWidth* widthP) const {
- char cldrFieldKey[UDATPG_FIELD_KEY_MAX + 1];
- uprv_strncpy(cldrFieldKey, key, UDATPG_FIELD_KEY_MAX);
- cldrFieldKey[UDATPG_FIELD_KEY_MAX]=0; // ensure termination
- *widthP = UDATPG_WIDE;
- char* hyphenPtr = uprv_strchr(cldrFieldKey, '-');
- if (hyphenPtr) {
- for (int32_t i=UDATPG_WIDTH_COUNT-1; i>0; --i) {
- if (uprv_strcmp(CLDR_FIELD_WIDTH[i], hyphenPtr)==0) {
- *widthP=(UDateTimePGDisplayWidth)i;
- break;
- }
- }
- *hyphenPtr = 0; // now delete width portion of key
- }
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
- if (uprv_strcmp(CLDR_FIELD_NAME[i],cldrFieldKey)==0) {
- return (UDateTimePatternField)i;
- }
- }
- return UDATPG_FIELD_COUNT;
-}
-
-const UnicodeString*
-DateTimePatternGenerator::getBestRaw(DateTimeMatcher& source,
- int32_t includeMask,
- DistanceInfo* missingFields,
- UErrorCode &status,
- const PtnSkeleton** specifiedSkeletonPtr) {
- int32_t bestDistance = 0x7fffffff;
- DistanceInfo tempInfo;
- const UnicodeString *bestPattern=nullptr;
- const PtnSkeleton* specifiedSkeleton=nullptr;
-
- PatternMapIterator it(status);
- if (U_FAILURE(status)) { return nullptr; }
-
- for (it.set(*patternMap); it.hasNext(); ) {
- DateTimeMatcher trial = it.next();
- if (trial.equals(skipMatcher)) {
- continue;
- }
- int32_t distance=source.getDistance(trial, includeMask, tempInfo);
- if (distance<bestDistance) {
- bestDistance=distance;
- bestPattern=patternMap->getPatternFromSkeleton(*trial.getSkeletonPtr(), &specifiedSkeleton);
- missingFields->setTo(tempInfo);
- if (distance==0) {
- break;
- }
- }
- }
-
- // If the best raw match had a specified skeleton and that skeleton was requested by the caller,
- // then return it too. This generally happens when the caller needs to pass that skeleton
- // through to adjustFieldTypes so the latter can do a better job.
- if (bestPattern && specifiedSkeletonPtr) {
- *specifiedSkeletonPtr = specifiedSkeleton;
- }
- return bestPattern;
-}
-
-UnicodeString
-DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern,
- const PtnSkeleton* specifiedSkeleton,
- int32_t flags,
- UDateTimePatternMatchOptions options) {
- UnicodeString newPattern;
- fp->set(pattern);
- for (int32_t i=0; i < fp->itemNumber; i++) {
- UnicodeString field = fp->items[i];
- if ( fp->isQuoteLiteral(field) ) {
-
- UnicodeString quoteLiteral;
- fp->getQuoteLiteral(quoteLiteral, &i);
- newPattern += quoteLiteral;
- }
- else {
- if (fp->isPatternSeparator(field)) {
- newPattern+=field;
- continue;
- }
- int32_t canonicalIndex = fp->getCanonicalIndex(field);
- if (canonicalIndex < 0) {
- newPattern+=field;
- continue; // don't adjust
- }
- const dtTypeElem *row = &dtTypes[canonicalIndex];
- int32_t typeValue = row->field;
-
- // handle day periods - with #13183, no longer need special handling here, integrated with normal types
-
- if ((flags & kDTPGFixFractionalSeconds) != 0 && typeValue == UDATPG_SECOND_FIELD) {
- field += decimal;
- dtMatcher->skeleton.original.appendFieldTo(UDATPG_FRACTIONAL_SECOND_FIELD, field);
- } else if (dtMatcher->skeleton.type[typeValue]!=0) {
- // Here:
- // - "reqField" is the field from the originally requested skeleton, with length
- // "reqFieldLen".
- // - "field" is the field from the found pattern.
- //
- // The adjusted field should consist of characters from the originally requested
- // skeleton, except in the case of UDATPG_HOUR_FIELD or UDATPG_MONTH_FIELD or
- // UDATPG_WEEKDAY_FIELD or UDATPG_YEAR_FIELD, in which case it should consist
- // of characters from the found pattern.
- //
- // The length of the adjusted field (adjFieldLen) should match that in the originally
- // requested skeleton, except that in the following cases the length of the adjusted field
- // should match that in the found pattern (i.e. the length of this pattern field should
- // not be adjusted):
- // 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is
- // not set (ticket #7180). Note, we may want to implement a similar change for other
- // numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for
- // field length, but options bits can be used to override this.
- // 2. There is a specified skeleton for the found pattern and one of the following is true:
- // a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen.
- // b) The pattern field is numeric and the skeleton field is not, or vice versa.
-
- UChar reqFieldChar = dtMatcher->skeleton.original.getFieldChar(typeValue);
- int32_t reqFieldLen = dtMatcher->skeleton.original.getFieldLength(typeValue);
- if (reqFieldChar == CAP_E && reqFieldLen < 3)
- reqFieldLen = 3; // 1-3 for E are equivalent to 3 for c,e
- int32_t adjFieldLen = reqFieldLen;
- if ( (typeValue==UDATPG_HOUR_FIELD && (options & UDATPG_MATCH_HOUR_FIELD_LENGTH)==0) ||
- (typeValue==UDATPG_MINUTE_FIELD && (options & UDATPG_MATCH_MINUTE_FIELD_LENGTH)==0) ||
- (typeValue==UDATPG_SECOND_FIELD && (options & UDATPG_MATCH_SECOND_FIELD_LENGTH)==0) ) {
- adjFieldLen = field.length();
- } else if (specifiedSkeleton) {
- int32_t skelFieldLen = specifiedSkeleton->original.getFieldLength(typeValue);
- UBool patFieldIsNumeric = (row->type > 0);
- UBool skelFieldIsNumeric = (specifiedSkeleton->type[typeValue] > 0);
- if (skelFieldLen == reqFieldLen || (patFieldIsNumeric && !skelFieldIsNumeric) || (skelFieldIsNumeric && !patFieldIsNumeric)) {
- // don't adjust the field length in the found pattern
- adjFieldLen = field.length();
- }
- }
- UChar c = (typeValue!= UDATPG_HOUR_FIELD
- && typeValue!= UDATPG_MONTH_FIELD
- && typeValue!= UDATPG_WEEKDAY_FIELD
- && (typeValue!= UDATPG_YEAR_FIELD || reqFieldChar==CAP_Y))
- ? reqFieldChar
- : field.charAt(0);
- if (typeValue == UDATPG_HOUR_FIELD && (flags & kDTPGSkeletonUsesCapJ) != 0) {
- c = fDefaultHourFormatChar;
- }
- field.remove();
- for (int32_t j=adjFieldLen; j>0; --j) {
- field += c;
- }
- }
- newPattern+=field;
- }
- }
- return newPattern;
-}
-
-UnicodeString
-DateTimePatternGenerator::getBestAppending(int32_t missingFields, int32_t flags, UErrorCode &status, UDateTimePatternMatchOptions options) {
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
- UnicodeString resultPattern, tempPattern;
- const UnicodeString* tempPatternPtr;
- int32_t lastMissingFieldMask=0;
- if (missingFields!=0) {
- resultPattern=UnicodeString();
- const PtnSkeleton* specifiedSkeleton=nullptr;
- tempPatternPtr = getBestRaw(*dtMatcher, missingFields, distanceInfo, status, &specifiedSkeleton);
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
- tempPattern = *tempPatternPtr;
- resultPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options);
- if ( distanceInfo->missingFieldMask==0 ) {
- return resultPattern;
- }
- while (distanceInfo->missingFieldMask!=0) { // precondition: EVERY single field must work!
- if ( lastMissingFieldMask == distanceInfo->missingFieldMask ) {
- break; // cannot find the proper missing field
- }
- if (((distanceInfo->missingFieldMask & UDATPG_SECOND_AND_FRACTIONAL_MASK)==UDATPG_FRACTIONAL_MASK) &&
- ((missingFields & UDATPG_SECOND_AND_FRACTIONAL_MASK) == UDATPG_SECOND_AND_FRACTIONAL_MASK)) {
- resultPattern = adjustFieldTypes(resultPattern, specifiedSkeleton, flags | kDTPGFixFractionalSeconds, options);
- distanceInfo->missingFieldMask &= ~UDATPG_FRACTIONAL_MASK;
- continue;
- }
- int32_t startingMask = distanceInfo->missingFieldMask;
- tempPatternPtr = getBestRaw(*dtMatcher, distanceInfo->missingFieldMask, distanceInfo, status, &specifiedSkeleton);
- if (U_FAILURE(status)) {
- return UnicodeString();
- }
- tempPattern = *tempPatternPtr;
- tempPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options);
- int32_t foundMask=startingMask& ~distanceInfo->missingFieldMask;
- int32_t topField=getTopBitNumber(foundMask);
-
- if (appendItemFormats[topField].length() != 0) {
- UnicodeString appendName;
- getAppendName((UDateTimePatternField)topField, appendName);
- const UnicodeString *values[3] = {
- &resultPattern,
- &tempPattern,
- &appendName
- };
- SimpleFormatter(appendItemFormats[topField], 2, 3, status).
- formatAndReplace(values, 3, resultPattern, nullptr, 0, status);
- }
- lastMissingFieldMask = distanceInfo->missingFieldMask;
- }
- }
- return resultPattern;
-}
-
-int32_t
-DateTimePatternGenerator::getTopBitNumber(int32_t foundMask) const {
- if ( foundMask==0 ) {
- return 0;
- }
- int32_t i=0;
- while (foundMask!=0) {
- foundMask >>=1;
- ++i;
- }
- if (i-1 >UDATPG_ZONE_FIELD) {
- return UDATPG_ZONE_FIELD;
- }
- else
- return i-1;
-}
-
-void
-DateTimePatternGenerator::setAvailableFormat(const UnicodeString &key, UErrorCode& err)
-{
- fAvailableFormatKeyHash->puti(key, 1, err);
-}
-
-UBool
-DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString &key) const {
- return (UBool)(fAvailableFormatKeyHash->geti(key) == 1);
-}
-
-void
-DateTimePatternGenerator::copyHashtable(Hashtable *other, UErrorCode &status) {
- if (other == nullptr || U_FAILURE(status)) {
- return;
- }
- if (fAvailableFormatKeyHash != nullptr) {
- delete fAvailableFormatKeyHash;
- fAvailableFormatKeyHash = nullptr;
- }
- initHashtable(status);
- if(U_FAILURE(status)){
- return;
- }
- int32_t pos = UHASH_FIRST;
- const UHashElement* elem = nullptr;
- // walk through the hash table and create a deep clone
- while((elem = other->nextElement(pos))!= nullptr){
- const UHashTok otherKeyTok = elem->key;
- UnicodeString* otherKey = (UnicodeString*)otherKeyTok.pointer;
- fAvailableFormatKeyHash->puti(*otherKey, 1, status);
- if(U_FAILURE(status)){
- return;
- }
- }
-}
-
-StringEnumeration*
-DateTimePatternGenerator::getSkeletons(UErrorCode& status) const {
- if (U_FAILURE(status)) {
- return nullptr;
- }
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return nullptr;
- }
- LocalPointer<StringEnumeration> skeletonEnumerator(
- new DTSkeletonEnumeration(*patternMap, DT_SKELETON, status), status);
-
- return U_SUCCESS(status) ? skeletonEnumerator.orphan() : nullptr;
-}
-
-const UnicodeString&
-DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const {
- PtnElem *curElem;
-
- if (skeleton.length() ==0) {
- return emptyString;
- }
- curElem = patternMap->getHeader(skeleton.charAt(0));
- while ( curElem != nullptr ) {
- if ( curElem->skeleton->getSkeleton()==skeleton ) {
- return curElem->pattern;
- }
- curElem = curElem->next.getAlias();
- }
- return emptyString;
-}
-
-StringEnumeration*
-DateTimePatternGenerator::getBaseSkeletons(UErrorCode& status) const {
- if (U_FAILURE(status)) {
- return nullptr;
- }
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return nullptr;
- }
- LocalPointer<StringEnumeration> baseSkeletonEnumerator(
- new DTSkeletonEnumeration(*patternMap, DT_BASESKELETON, status), status);
-
- return U_SUCCESS(status) ? baseSkeletonEnumerator.orphan() : nullptr;
-}
-
-StringEnumeration*
-DateTimePatternGenerator::getRedundants(UErrorCode& status) {
- if (U_FAILURE(status)) { return nullptr; }
- if (U_FAILURE(internalErrorCode)) {
- status = internalErrorCode;
- return nullptr;
- }
- LocalPointer<StringEnumeration> output(new DTRedundantEnumeration(), status);
- if (U_FAILURE(status)) { return nullptr; }
- const UnicodeString *pattern;
- PatternMapIterator it(status);
- if (U_FAILURE(status)) { return nullptr; }
-
- for (it.set(*patternMap); it.hasNext(); ) {
- DateTimeMatcher current = it.next();
- pattern = patternMap->getPatternFromSkeleton(*(it.getSkeleton()));
- if ( isCanonicalItem(*pattern) ) {
- continue;
- }
- if ( skipMatcher == nullptr ) {
- skipMatcher = new DateTimeMatcher(current);
- if (skipMatcher == nullptr) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return nullptr;
- }
- }
- else {
- *skipMatcher = current;
- }
- UnicodeString trial = getBestPattern(current.getPattern(), status);
- if (U_FAILURE(status)) { return nullptr; }
- if (trial == *pattern) {
- ((DTRedundantEnumeration *)output.getAlias())->add(*pattern, status);
- if (U_FAILURE(status)) { return nullptr; }
- }
- if (current.equals(skipMatcher)) {
- continue;
- }
- }
- return output.orphan();
-}
-
-UBool
-DateTimePatternGenerator::isCanonicalItem(const UnicodeString& item) const {
- if ( item.length() != 1 ) {
- return FALSE;
- }
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
- if (item.charAt(0)==Canonical_Items[i]) {
- return TRUE;
- }
- }
- return FALSE;
-}
-
-
-DateTimePatternGenerator*
-DateTimePatternGenerator::clone() const {
- return new DateTimePatternGenerator(*this);
-}
-
-PatternMap::PatternMap() {
- for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
- boot[i] = nullptr;
- }
- isDupAllowed = TRUE;
-}
-
-void
-PatternMap::copyFrom(const PatternMap& other, UErrorCode& status) {
- if (U_FAILURE(status)) {
- return;
- }
- this->isDupAllowed = other.isDupAllowed;
- for (int32_t bootIndex = 0; bootIndex < MAX_PATTERN_ENTRIES; ++bootIndex) {
- PtnElem *curElem, *otherElem, *prevElem=nullptr;
- otherElem = other.boot[bootIndex];
- while (otherElem != nullptr) {
- LocalPointer<PtnElem> newElem(new PtnElem(otherElem->basePattern, otherElem->pattern), status);
- if (U_FAILURE(status)) {
- return; // out of memory
- }
- newElem->skeleton.adoptInsteadAndCheckErrorCode(new PtnSkeleton(*(otherElem->skeleton)), status);
- if (U_FAILURE(status)) {
- return; // out of memory
- }
- newElem->skeletonWasSpecified = otherElem->skeletonWasSpecified;
-
- // Release ownership from the LocalPointer of the PtnElem object.
- // The PtnElem will now be owned by either the boot (for the first entry in the linked-list)
- // or owned by the previous PtnElem object in the linked-list.
- curElem = newElem.orphan();
-
- if (this->boot[bootIndex] == nullptr) {
- this->boot[bootIndex] = curElem;
- } else {
- if (prevElem != nullptr) {
- prevElem->next.adoptInstead(curElem);
- } else {
- U_ASSERT(false);
- }
- }
- prevElem = curElem;
- otherElem = otherElem->next.getAlias();
- }
-
- }
-}
-
-PtnElem*
-PatternMap::getHeader(UChar baseChar) const {
- PtnElem* curElem;
-
- if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
- curElem = boot[baseChar-CAP_A];
- }
- else {
- if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
- curElem = boot[26+baseChar-LOW_A];
- }
- else {
- return nullptr;
- }
- }
- return curElem;
-}
-
-PatternMap::~PatternMap() {
- for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
- if (boot[i] != nullptr ) {
- delete boot[i];
- boot[i] = nullptr;
- }
- }
-} // PatternMap destructor
-
-void
-PatternMap::add(const UnicodeString& basePattern,
- const PtnSkeleton& skeleton,
- const UnicodeString& value,// mapped pattern value
- UBool skeletonWasSpecified,
- UErrorCode &status) {
- UChar baseChar = basePattern.charAt(0);
- PtnElem *curElem, *baseElem;
- status = U_ZERO_ERROR;
-
- // the baseChar must be A-Z or a-z
- if ((baseChar >= CAP_A) && (baseChar <= CAP_Z)) {
- baseElem = boot[baseChar-CAP_A];
- }
- else {
- if ((baseChar >=LOW_A) && (baseChar <= LOW_Z)) {
- baseElem = boot[26+baseChar-LOW_A];
- }
- else {
- status = U_ILLEGAL_CHARACTER;
- return;
- }
- }
-
- if (baseElem == nullptr) {
- LocalPointer<PtnElem> newElem(new PtnElem(basePattern, value), status);
- if (U_FAILURE(status)) {
- return; // out of memory
- }
- newElem->skeleton.adoptInsteadAndCheckErrorCode(new PtnSkeleton(skeleton), status);
- if (U_FAILURE(status)) {
- return; // out of memory
- }
- newElem->skeletonWasSpecified = skeletonWasSpecified;
- if (baseChar >= LOW_A) {
- boot[26 + (baseChar - LOW_A)] = newElem.orphan(); // the boot array now owns the PtnElem.
- }
- else {
- boot[baseChar - CAP_A] = newElem.orphan(); // the boot array now owns the PtnElem.
- }
- }
- if ( baseElem != nullptr ) {
- curElem = getDuplicateElem(basePattern, skeleton, baseElem);
-
- if (curElem == nullptr) {
- // add new element to the list.
- curElem = baseElem;
- while( curElem -> next != nullptr )
- {
- curElem = curElem->next.getAlias();
- }
-
- LocalPointer<PtnElem> newElem(new PtnElem(basePattern, value), status);
- if (U_FAILURE(status)) {
- return; // out of memory
- }
- newElem->skeleton.adoptInsteadAndCheckErrorCode(new PtnSkeleton(skeleton), status);
- if (U_FAILURE(status)) {
- return; // out of memory
- }
- newElem->skeletonWasSpecified = skeletonWasSpecified;
- curElem->next.adoptInstead(newElem.orphan());
- curElem = curElem->next.getAlias();
- }
- else {
- // Pattern exists in the list already.
- if ( !isDupAllowed ) {
- return;
- }
- // Overwrite the value.
- curElem->pattern = value;
- // It was a bug that we were not doing the following previously,
- // though that bug hid other problems by making things partly work.
- curElem->skeletonWasSpecified = skeletonWasSpecified;
- }
- }
-} // PatternMap::add
-
-// Find the pattern from the given basePattern string.
-const UnicodeString *
-PatternMap::getPatternFromBasePattern(const UnicodeString& basePattern, UBool& skeletonWasSpecified) const { // key to search for
- PtnElem *curElem;
-
- if ((curElem=getHeader(basePattern.charAt(0)))==nullptr) {
- return nullptr; // no match
- }
-
- do {
- if ( basePattern.compare(curElem->basePattern)==0 ) {
- skeletonWasSpecified = curElem->skeletonWasSpecified;
- return &(curElem->pattern);
- }
- curElem = curElem->next.getAlias();
- } while (curElem != nullptr);
-
- return nullptr;
-} // PatternMap::getFromBasePattern
-
-
-// Find the pattern from the given skeleton.
-// At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL),
-// the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw)
-// and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the
-// optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL),
-// for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily.
-const UnicodeString *
-PatternMap::getPatternFromSkeleton(const PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr) const { // key to search for
- PtnElem *curElem;
-
- if (specifiedSkeletonPtr) {
- *specifiedSkeletonPtr = nullptr;
- }
-
- // find boot entry
- UChar baseChar = skeleton.getFirstChar();
- if ((curElem=getHeader(baseChar))==nullptr) {
- return nullptr; // no match
- }
-
- do {
- UBool equal;
- if (specifiedSkeletonPtr != nullptr) { // called from DateTimePatternGenerator::getBestRaw or addPattern, use original
- equal = curElem->skeleton->original == skeleton.original;
- } else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal
- equal = curElem->skeleton->baseOriginal == skeleton.baseOriginal;
- }
- if (equal) {
- if (specifiedSkeletonPtr && curElem->skeletonWasSpecified) {
- *specifiedSkeletonPtr = curElem->skeleton.getAlias();
- }
- return &(curElem->pattern);
- }
- curElem = curElem->next.getAlias();
- } while (curElem != nullptr);
-
- return nullptr;
-}
-
-UBool
-PatternMap::equals(const PatternMap& other) const {
- if ( this==&other ) {
- return TRUE;
- }
- for (int32_t bootIndex = 0; bootIndex < MAX_PATTERN_ENTRIES; ++bootIndex) {
- if (boot[bootIndex] == other.boot[bootIndex]) {
- continue;
- }
- if ((boot[bootIndex] == nullptr) || (other.boot[bootIndex] == nullptr)) {
- return FALSE;
- }
- PtnElem *otherElem = other.boot[bootIndex];
- PtnElem *myElem = boot[bootIndex];
- while ((otherElem != nullptr) || (myElem != nullptr)) {
- if ( myElem == otherElem ) {
- break;
- }
- if ((otherElem == nullptr) || (myElem == nullptr)) {
- return FALSE;
- }
- if ( (myElem->basePattern != otherElem->basePattern) ||
- (myElem->pattern != otherElem->pattern) ) {
- return FALSE;
- }
- if ((myElem->skeleton.getAlias() != otherElem->skeleton.getAlias()) &&
- !myElem->skeleton->equals(*(otherElem->skeleton))) {
- return FALSE;
- }
- myElem = myElem->next.getAlias();
- otherElem = otherElem->next.getAlias();
- }
- }
- return TRUE;
-}
-
-// find any key existing in the mapping table already.
-// return TRUE if there is an existing key, otherwise return FALSE.
-PtnElem*
-PatternMap::getDuplicateElem(
- const UnicodeString &basePattern,
- const PtnSkeleton &skeleton,
- PtnElem *baseElem) {
- PtnElem *curElem;
-
- if ( baseElem == nullptr ) {
- return nullptr;
- }
- else {
- curElem = baseElem;
- }
- do {
- if ( basePattern.compare(curElem->basePattern)==0 ) {
- UBool isEqual = TRUE;
- for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
- if (curElem->skeleton->type[i] != skeleton.type[i] ) {
- isEqual = FALSE;
- break;
- }
- }
- if (isEqual) {
- return curElem;
- }
- }
- curElem = curElem->next.getAlias();
- } while( curElem != nullptr );
-
- // end of the list
- return nullptr;
-
-} // PatternMap::getDuplicateElem
-
-DateTimeMatcher::DateTimeMatcher(void) {
-}
-
-DateTimeMatcher::~DateTimeMatcher() {}
-
-DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher& other) {
- copyFrom(other.skeleton);
-}
-
-
-void
-DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp) {
- PtnSkeleton localSkeleton;
- return set(pattern, fp, localSkeleton);
-}
-
-void
-DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeletonResult) {
- int32_t i;
- for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
- skeletonResult.type[i] = NONE;
- }
- skeletonResult.original.clear();
- skeletonResult.baseOriginal.clear();
- skeletonResult.addedDefaultDayPeriod = FALSE;
-
- fp->set(pattern);
- for (i=0; i < fp->itemNumber; i++) {
- const UnicodeString& value = fp->items[i];
- // don't skip 'a' anymore, dayPeriod handled specially below
-
- if ( fp->isQuoteLiteral(value) ) {
- UnicodeString quoteLiteral;
- fp->getQuoteLiteral(quoteLiteral, &i);
- continue;
- }
- int32_t canonicalIndex = fp->getCanonicalIndex(value);
- if (canonicalIndex < 0) {
- continue;
- }
- const dtTypeElem *row = &dtTypes[canonicalIndex];
- int32_t field = row->field;
- skeletonResult.original.populate(field, value);
- UChar repeatChar = row->patternChar;
- int32_t repeatCount = row->minLen;
- skeletonResult.baseOriginal.populate(field, repeatChar, repeatCount);
- int16_t subField = row->type;
- if (row->type > 0) {
- U_ASSERT(value.length() < INT16_MAX);
- subField += static_cast<int16_t>(value.length());
- }
- skeletonResult.type[field] = subField;
- }
- // #13183, handle special behavior for day period characters (a, b, B)
- if (!skeletonResult.original.isFieldEmpty(UDATPG_HOUR_FIELD)) {
- if (skeletonResult.original.getFieldChar(UDATPG_HOUR_FIELD)==LOW_H || skeletonResult.original.getFieldChar(UDATPG_HOUR_FIELD)==CAP_K) {
- // We have a skeleton with 12-hour-cycle format
- if (skeletonResult.original.isFieldEmpty(UDATPG_DAYPERIOD_FIELD)) {
- // But we do not have a day period in the skeleton; add the default DAYPERIOD (currently "a")
- for (i = 0; dtTypes[i].patternChar != 0; i++) {
- if ( dtTypes[i].field == UDATPG_DAYPERIOD_FIELD ) {
- // first entry for UDATPG_DAYPERIOD_FIELD
- skeletonResult.original.populate(UDATPG_DAYPERIOD_FIELD, dtTypes[i].patternChar, dtTypes[i].minLen);
- skeletonResult.baseOriginal.populate(UDATPG_DAYPERIOD_FIELD, dtTypes[i].patternChar, dtTypes[i].minLen);
- skeletonResult.type[UDATPG_DAYPERIOD_FIELD] = dtTypes[i].type;
- skeletonResult.addedDefaultDayPeriod = TRUE;
- break;
- }
- }
- }
- } else {
- // Skeleton has 24-hour-cycle hour format and has dayPeriod, delete dayPeriod (i.e. ignore it)
- skeletonResult.original.clearField(UDATPG_DAYPERIOD_FIELD);
- skeletonResult.baseOriginal.clearField(UDATPG_DAYPERIOD_FIELD);
- skeletonResult.type[UDATPG_DAYPERIOD_FIELD] = NONE;
- }
- }
- copyFrom(skeletonResult);
-}
-
-void
-DateTimeMatcher::getBasePattern(UnicodeString &result ) {
- result.remove(); // Reset the result first.
- skeleton.baseOriginal.appendTo(result);
-}
-
-UnicodeString
-DateTimeMatcher::getPattern() {
- UnicodeString result;
- return skeleton.original.appendTo(result);
-}
-
-int32_t
-DateTimeMatcher::getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo) const {
- int32_t result = 0;
- distanceInfo.clear();
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
- int32_t myType = (includeMask&(1<<i))==0 ? 0 : skeleton.type[i];
- int32_t otherType = other.skeleton.type[i];
- if (myType==otherType) {
- continue;
- }
- if (myType==0) {// and other is not
- result += EXTRA_FIELD;
- distanceInfo.addExtra(i);
- }
- else {
- if (otherType==0) {
- result += MISSING_FIELD;
- distanceInfo.addMissing(i);
- }
- else {
- result += abs(myType - otherType);
- }
- }
-
- }
- return result;
-}
-
-void
-DateTimeMatcher::copyFrom(const PtnSkeleton& newSkeleton) {
- skeleton.copyFrom(newSkeleton);
-}
-
-void
-DateTimeMatcher::copyFrom() {
- // same as clear
- skeleton.clear();
-}
-
-UBool
-DateTimeMatcher::equals(const DateTimeMatcher* other) const {
- if (other==nullptr) { return FALSE; }
- return skeleton.original == other->skeleton.original;
-}
-
-int32_t
-DateTimeMatcher::getFieldMask() const {
- int32_t result = 0;
-
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
- if (skeleton.type[i]!=0) {
- result |= (1<<i);
- }
- }
- return result;
-}
-
-PtnSkeleton*
-DateTimeMatcher::getSkeletonPtr() {
- return &skeleton;
-}
-
-FormatParser::FormatParser () {
- status = START;
- itemNumber = 0;
-}
-
-
-FormatParser::~FormatParser () {
-}
-
-
-// Find the next token with the starting position and length
-// Note: the startPos may
-FormatParser::TokenStatus
-FormatParser::setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len) {
- int32_t curLoc = startPos;
- if ( curLoc >= pattern.length()) {
- return DONE;
- }
- // check the current char is between A-Z or a-z
- do {
- UChar c=pattern.charAt(curLoc);
- if ( (c>=CAP_A && c<=CAP_Z) || (c>=LOW_A && c<=LOW_Z) ) {
- curLoc++;
- }
- else {
- startPos = curLoc;
- *len=1;
- return ADD_TOKEN;
- }
-
- if ( pattern.charAt(curLoc)!= pattern.charAt(startPos) ) {
- break; // not the same token
- }
- } while(curLoc <= pattern.length());
- *len = curLoc-startPos;
- return ADD_TOKEN;
-}
-
-void
-FormatParser::set(const UnicodeString& pattern) {
- int32_t startPos = 0;
- TokenStatus result = START;
- int32_t len = 0;
- itemNumber = 0;
-
- do {
- result = setTokens( pattern, startPos, &len );
- if ( result == ADD_TOKEN )
- {
- items[itemNumber++] = UnicodeString(pattern, startPos, len );
- startPos += len;
- }
- else {
- break;
- }
- } while (result==ADD_TOKEN && itemNumber < MAX_DT_TOKEN);
-}
-
-int32_t
-FormatParser::getCanonicalIndex(const UnicodeString& s, UBool strict) {
- int32_t len = s.length();
- if (len == 0) {
- return -1;
- }
- UChar ch = s.charAt(0);
-
- // Verify that all are the same character.
- for (int32_t l = 1; l < len; l++) {
- if (ch != s.charAt(l)) {
- return -1;
- }
- }
- int32_t i = 0;
- int32_t bestRow = -1;
- while (dtTypes[i].patternChar != 0x0000) {
- if ( dtTypes[i].patternChar != ch ) {
- ++i;
- continue;
- }
- bestRow = i;
- if (dtTypes[i].patternChar != dtTypes[i+1].patternChar) {
- return i;
- }
- if (dtTypes[i+1].minLen <= len) {
- ++i;
- continue;
- }
- return i;
- }
- return strict ? -1 : bestRow;
-}
-
-UBool
-FormatParser::isQuoteLiteral(const UnicodeString& s) {
- return (UBool)(s.charAt(0) == SINGLE_QUOTE);
-}
-
-// This function assumes the current itemIndex points to the quote literal.
-// Please call isQuoteLiteral prior to this function.
-void
-FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
- int32_t i = *itemIndex;
-
- quote.remove();
- if (items[i].charAt(0)==SINGLE_QUOTE) {
- quote += items[i];
- ++i;
- }
- while ( i < itemNumber ) {
- if ( items[i].charAt(0)==SINGLE_QUOTE ) {
- if ( (i+1<itemNumber) && (items[i+1].charAt(0)==SINGLE_QUOTE)) {
- // two single quotes e.g. 'o''clock'
- quote += items[i++];
- quote += items[i++];
- continue;
- }
- else {
- quote += items[i];
- break;
- }
- }
- else {
- quote += items[i];
- }
- ++i;
- }
- *itemIndex=i;
-}
-
-UBool
-FormatParser::isPatternSeparator(const UnicodeString& field) const {
- for (int32_t i=0; i<field.length(); ++i ) {
- UChar c= field.charAt(i);
- if ( (c==SINGLE_QUOTE) || (c==BACKSLASH) || (c==SPACE) || (c==COLON) ||
- (c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ||(items[i].charAt(0)==DOT) ) {
- continue;
- }
- else {
- return FALSE;
- }
- }
- return TRUE;
-}
-
-DistanceInfo::~DistanceInfo() {}
-
-void
-DistanceInfo::setTo(const DistanceInfo& other) {
- missingFieldMask = other.missingFieldMask;
- extraFieldMask= other.extraFieldMask;
-}
-
-PatternMapIterator::PatternMapIterator(UErrorCode& status) :
- bootIndex(0), nodePtr(nullptr), matcher(nullptr), patternMap(nullptr)
-{
- if (U_FAILURE(status)) { return; }
- matcher.adoptInsteadAndCheckErrorCode(new DateTimeMatcher(), status);
-}
-
-PatternMapIterator::~PatternMapIterator() {
-}
-
-void
-PatternMapIterator::set(PatternMap& newPatternMap) {
- this->patternMap=&newPatternMap;
-}
-
-PtnSkeleton*
-PatternMapIterator::getSkeleton() const {
- if ( nodePtr == nullptr ) {
- return nullptr;
- }
- else {
- return nodePtr->skeleton.getAlias();
- }
-}
-
-UBool
-PatternMapIterator::hasNext() const {
- int32_t headIndex = bootIndex;
- PtnElem *curPtr = nodePtr;
-
- if (patternMap==nullptr) {
- return FALSE;
- }
- while ( headIndex < MAX_PATTERN_ENTRIES ) {
- if ( curPtr != nullptr ) {
- if ( curPtr->next != nullptr ) {
- return TRUE;
- }
- else {
- headIndex++;
- curPtr=nullptr;
- continue;
- }
- }
- else {
- if ( patternMap->boot[headIndex] != nullptr ) {
- return TRUE;
- }
- else {
- headIndex++;
- continue;
- }
- }
- }
- return FALSE;
-}
-
-DateTimeMatcher&
-PatternMapIterator::next() {
- while ( bootIndex < MAX_PATTERN_ENTRIES ) {
- if ( nodePtr != nullptr ) {
- if ( nodePtr->next != nullptr ) {
- nodePtr = nodePtr->next.getAlias();
- break;
- }
- else {
- bootIndex++;
- nodePtr=nullptr;
- continue;
- }
- }
- else {
- if ( patternMap->boot[bootIndex] != nullptr ) {
- nodePtr = patternMap->boot[bootIndex];
- break;
- }
- else {
- bootIndex++;
- continue;
- }
- }
- }
- if (nodePtr!=nullptr) {
- matcher->copyFrom(*nodePtr->skeleton);
- }
- else {
- matcher->copyFrom();
- }
- return *matcher;
-}
-
-
-SkeletonFields::SkeletonFields() {
- // Set initial values to zero
- clear();
-}
-
-void SkeletonFields::clear() {
- uprv_memset(chars, 0, sizeof(chars));
- uprv_memset(lengths, 0, sizeof(lengths));
-}
-
-void SkeletonFields::copyFrom(const SkeletonFields& other) {
- uprv_memcpy(chars, other.chars, sizeof(chars));
- uprv_memcpy(lengths, other.lengths, sizeof(lengths));
-}
-
-void SkeletonFields::clearField(int32_t field) {
- chars[field] = 0;
- lengths[field] = 0;
-}
-
-UChar SkeletonFields::getFieldChar(int32_t field) const {
- return chars[field];
-}
-
-int32_t SkeletonFields::getFieldLength(int32_t field) const {
- return lengths[field];
-}
-
-void SkeletonFields::populate(int32_t field, const UnicodeString& value) {
- populate(field, value.charAt(0), value.length());
-}
-
-void SkeletonFields::populate(int32_t field, UChar ch, int32_t length) {
- chars[field] = (int8_t) ch;
- lengths[field] = (int8_t) length;
-}
-
-UBool SkeletonFields::isFieldEmpty(int32_t field) const {
- return lengths[field] == 0;
-}
-
-UnicodeString& SkeletonFields::appendTo(UnicodeString& string) const {
- for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
- appendFieldTo(i, string);
- }
- return string;
-}
-
-UnicodeString& SkeletonFields::appendFieldTo(int32_t field, UnicodeString& string) const {
- UChar ch(chars[field]);
- int32_t length = (int32_t) lengths[field];
-
- for (int32_t i=0; i<length; i++) {
- string += ch;
- }
- return string;
-}
-
-UChar SkeletonFields::getFirstChar() const {
- for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
- if (lengths[i] != 0) {
- return chars[i];
- }
- }
- return '\0';
-}
-
-
-PtnSkeleton::PtnSkeleton() {
-}
-
-PtnSkeleton::PtnSkeleton(const PtnSkeleton& other) {
- copyFrom(other);
-}
-
-void PtnSkeleton::copyFrom(const PtnSkeleton& other) {
- uprv_memcpy(type, other.type, sizeof(type));
- original.copyFrom(other.original);
- baseOriginal.copyFrom(other.baseOriginal);
-}
-
-void PtnSkeleton::clear() {
- uprv_memset(type, 0, sizeof(type));
- original.clear();
- baseOriginal.clear();
-}
-
-UBool
-PtnSkeleton::equals(const PtnSkeleton& other) const {
- return (original == other.original)
- && (baseOriginal == other.baseOriginal)
- && (uprv_memcmp(type, other.type, sizeof(type)) == 0);
-}
-
-UnicodeString
-PtnSkeleton::getSkeleton() const {
- UnicodeString result;
- result = original.appendTo(result);
- int32_t pos;
- if (addedDefaultDayPeriod && (pos = result.indexOf(LOW_A)) >= 0) {
- // for backward compatibility: if DateTimeMatcher.set added a single 'a' that
- // was not in the provided skeleton, remove it here before returning skeleton.
- result.remove(pos, 1);
- }
- return result;
-}
-
-UnicodeString
-PtnSkeleton::getBaseSkeleton() const {
- UnicodeString result;
- result = baseOriginal.appendTo(result);
- int32_t pos;
- if (addedDefaultDayPeriod && (pos = result.indexOf(LOW_A)) >= 0) {
- // for backward compatibility: if DateTimeMatcher.set added a single 'a' that
- // was not in the provided skeleton, remove it here before returning skeleton.
- result.remove(pos, 1);
- }
- return result;
-}
-
-UChar
-PtnSkeleton::getFirstChar() const {
- return baseOriginal.getFirstChar();
-}
-
-PtnSkeleton::~PtnSkeleton() {
-}
-
-PtnElem::PtnElem(const UnicodeString &basePat, const UnicodeString &pat) :
- basePattern(basePat), skeleton(nullptr), pattern(pat), next(nullptr)
-{
-}
-
-PtnElem::~PtnElem() {
-}
-
-DTSkeletonEnumeration::DTSkeletonEnumeration(PatternMap& patternMap, dtStrEnum type, UErrorCode& status) : fSkeletons(nullptr) {
- PtnElem *curElem;
- PtnSkeleton *curSkeleton;
- UnicodeString s;
- int32_t bootIndex;
-
- pos=0;
- fSkeletons.adoptInsteadAndCheckErrorCode(new UVector(status), status);
- if (U_FAILURE(status)) {
- return;
- }
-
- for (bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
- curElem = patternMap.boot[bootIndex];
- while (curElem!=nullptr) {
- switch(type) {
- case DT_BASESKELETON:
- s=curElem->basePattern;
- break;
- case DT_PATTERN:
- s=curElem->pattern;
- break;
- case DT_SKELETON:
- curSkeleton=curElem->skeleton.getAlias();
- s=curSkeleton->getSkeleton();
- break;
- }
- if ( !isCanonicalItem(s) ) {
- LocalPointer<UnicodeString> newElem(new UnicodeString(s), status);
- if (U_FAILURE(status)) {
- return;
- }
- fSkeletons->addElement(newElem.getAlias(), status);
- if (U_FAILURE(status)) {
- fSkeletons.adoptInstead(nullptr);
- return;
- }
- newElem.orphan(); // fSkeletons vector now owns the UnicodeString.
- }
- curElem = curElem->next.getAlias();
- }
- }
- if ((bootIndex==MAX_PATTERN_ENTRIES) && (curElem!=nullptr) ) {
- status = U_BUFFER_OVERFLOW_ERROR;
- }
-}
-
-const UnicodeString*
-DTSkeletonEnumeration::snext(UErrorCode& status) {
- if (U_SUCCESS(status) && fSkeletons.isValid() && pos < fSkeletons->size()) {
- return (const UnicodeString*)fSkeletons->elementAt(pos++);
- }
- return nullptr;
-}
-
-void
-DTSkeletonEnumeration::reset(UErrorCode& /*status*/) {
- pos=0;
-}
-
-int32_t
-DTSkeletonEnumeration::count(UErrorCode& /*status*/) const {
- return (fSkeletons.isNull()) ? 0 : fSkeletons->size();
-}
-
-UBool
-DTSkeletonEnumeration::isCanonicalItem(const UnicodeString& item) {
- if ( item.length() != 1 ) {
- return FALSE;
- }
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
- if (item.charAt(0)==Canonical_Items[i]) {
- return TRUE;
- }
- }
- return FALSE;
-}
-
-DTSkeletonEnumeration::~DTSkeletonEnumeration() {
- UnicodeString *s;
- if (fSkeletons.isValid()) {
- for (int32_t i = 0; i < fSkeletons->size(); ++i) {
- if ((s = (UnicodeString *)fSkeletons->elementAt(i)) != nullptr) {
- delete s;
- }
- }
- }
-}
-
-DTRedundantEnumeration::DTRedundantEnumeration() : pos(0), fPatterns(nullptr) {
-}
-
-void
-DTRedundantEnumeration::add(const UnicodeString& pattern, UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
- if (fPatterns.isNull()) {
- fPatterns.adoptInsteadAndCheckErrorCode(new UVector(status), status);
- if (U_FAILURE(status)) {
- return;
- }
- }
- LocalPointer<UnicodeString> newElem(new UnicodeString(pattern), status);
- if (U_FAILURE(status)) {
- return;
- }
- fPatterns->addElement(newElem.getAlias(), status);
- if (U_FAILURE(status)) {
- fPatterns.adoptInstead(nullptr);
- return;
- }
- newElem.orphan(); // fPatterns now owns the string.
-}
-
-const UnicodeString*
-DTRedundantEnumeration::snext(UErrorCode& status) {
- if (U_SUCCESS(status) && fPatterns.isValid() && pos < fPatterns->size()) {
- return (const UnicodeString*)fPatterns->elementAt(pos++);
- }
- return nullptr;
-}
-
-void
-DTRedundantEnumeration::reset(UErrorCode& /*status*/) {
- pos=0;
-}
-
-int32_t
-DTRedundantEnumeration::count(UErrorCode& /*status*/) const {
- return (fPatterns.isNull()) ? 0 : fPatterns->size();
-}
-
-UBool
-DTRedundantEnumeration::isCanonicalItem(const UnicodeString& item) const {
- if ( item.length() != 1 ) {
- return FALSE;
- }
- for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
- if (item.charAt(0)==Canonical_Items[i]) {
- return TRUE;
- }
- }
- return FALSE;
-}
-
-DTRedundantEnumeration::~DTRedundantEnumeration() {
- UnicodeString *s;
- if (fPatterns.isValid()) {
- for (int32_t i = 0; i < fPatterns->size(); ++i) {
- if ((s = (UnicodeString *)fPatterns->elementAt(i)) != nullptr) {
- delete s;
- }
- }
- }
-}
-
-U_NAMESPACE_END
-
-
-#endif /* #if !UCONFIG_NO_FORMATTING */
-
-//eof
diff --git a/tools/icu/patches/63/source/tools/toolutil/pkg_genc.cpp b/tools/icu/patches/63/source/tools/toolutil/pkg_genc.cpp
deleted file mode 100644
index f23995bb21..0000000000
--- a/tools/icu/patches/63/source/tools/toolutil/pkg_genc.cpp
+++ /dev/null
@@ -1,1221 +0,0 @@
-// © 2016 and later: Unicode, Inc. and others.
-// License & terms of use: http://www.unicode.org/copyright.html
-/******************************************************************************
- * Copyright (C) 2009-2016, International Business Machines
- * Corporation and others. All Rights Reserved.
- *******************************************************************************
- */
-#include "unicode/utypes.h"
-
-#if U_PLATFORM_HAS_WIN32_API
-# define VC_EXTRALEAN
-# define WIN32_LEAN_AND_MEAN
-# define NOUSER
-# define NOSERVICE
-# define NOIME
-# define NOMCX
-#include <windows.h>
-#include <time.h>
-# ifdef __GNUC__
-# define WINDOWS_WITH_GNUC
-# endif
-#endif
-
-#if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H
-# define U_ELF
-#endif
-
-#ifdef U_ELF
-# include <elf.h>
-# if defined(ELFCLASS64)
-# define U_ELF64
-# endif
- /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */
-# ifndef EM_X86_64
-# define EM_X86_64 62
-# endif
-# define ICU_ENTRY_OFFSET 0
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "unicode/putil.h"
-#include "cmemory.h"
-#include "cstring.h"
-#include "filestrm.h"
-#include "toolutil.h"
-#include "unicode/uclean.h"
-#include "uoptions.h"
-#include "pkg_genc.h"
-#include "filetools.h"
-
-#define MAX_COLUMN ((uint32_t)(0xFFFFFFFFU))
-
-#define HEX_0X 0 /* 0x1234 */
-#define HEX_0H 1 /* 01234h */
-
-/* prototypes --------------------------------------------------------------- */
-static void
-getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix, const char *optFilename);
-
-static uint32_t
-write8(FileStream *out, uint8_t byte, uint32_t column);
-
-static uint32_t
-write32(FileStream *out, uint32_t byte, uint32_t column);
-
-#if U_PLATFORM == U_PF_OS400
-static uint32_t
-write8str(FileStream *out, uint8_t byte, uint32_t column);
-#endif
-/* -------------------------------------------------------------------------- */
-
-/*
-Creating Template Files for New Platforms
-
-Let the cc compiler help you get started.
-Compile this program
- const unsigned int x[5] = {1, 2, 0xdeadbeef, 0xffffffff, 16};
-with the -S option to produce assembly output.
-
-For example, this will generate array.s:
-gcc -S array.c
-
-This will produce a .s file that may look like this:
-
- .file "array.c"
- .version "01.01"
-gcc2_compiled.:
- .globl x
- .section .rodata
- .align 4
- .type x,@object
- .size x,20
-x:
- .long 1
- .long 2
- .long -559038737
- .long -1
- .long 16
- .ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.1 2.96-85)"
-
-which gives a starting point that will compile, and can be transformed
-to become the template, generally with some consulting of as docs and
-some experimentation.
-
-If you want ICU to automatically use this assembly, you should
-specify "GENCCODE_ASSEMBLY=-a name" in the specific config/mh-* file,
-where the name is the compiler or platform that you used in this
-assemblyHeader data structure.
-*/
-static const struct AssemblyType {
- const char *name;
- const char *header;
- const char *beginLine;
- const char *footer;
- int8_t hexType; /* HEX_0X or HEX_0h */
-} assemblyHeader[] = {
- /* For gcc assemblers, the meaning of .align changes depending on the */
- /* hardware, so we use .balign 16 which always means 16 bytes. */
- /* https://sourceware.org/binutils/docs/as/Pseudo-Ops.html */
- {"gcc",
- ".globl %s\n"
- "\t.section .note.GNU-stack,\"\",%%progbits\n"
- "\t.section .rodata\n"
- "\t.balign 16\n"
- "#ifdef U_HIDE_DATA_SYMBOL\n"
- "\t.hidden %s\n"
- "#endif\n"
- "\t.type %s,%%object\n"
- "%s:\n\n",
-
- ".long ",".size %s, .-%s\n",HEX_0X
- },
- {"gcc-darwin",
- /*"\t.section __TEXT,__text,regular,pure_instructions\n"
- "\t.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"*/
- ".globl _%s\n"
- "#ifdef U_HIDE_DATA_SYMBOL\n"
- "\t.private_extern _%s\n"
- "#endif\n"
- "\t.data\n"
- "\t.const\n"
- "\t.balign 16\n"
- "_%s:\n\n",
-
- ".long ","",HEX_0X
- },
- {"gcc-cygwin",
- ".globl _%s\n"
- "\t.section .rodata\n"
- "\t.balign 16\n"
- "_%s:\n\n",
-
- ".long ","",HEX_0X
- },
- {"gcc-mingw64",
- ".globl %s\n"
- "\t.section .rodata\n"
- "\t.balign 16\n"
- "%s:\n\n",
-
- ".long ","",HEX_0X
- },
-/* 16 bytes alignment. */
-/* http://docs.oracle.com/cd/E19641-01/802-1947/802-1947.pdf */
- {"sun",
- "\t.section \".rodata\"\n"
- "\t.align 16\n"
- ".globl %s\n"
- "%s:\n",
-
- ".word ","",HEX_0X
- },
-/* 16 bytes alignment for sun-x86. */
-/* http://docs.oracle.com/cd/E19963-01/html/821-1608/eoiyg.html */
- {"sun-x86",
- "Drodata.rodata:\n"
- "\t.type Drodata.rodata,@object\n"
- "\t.size Drodata.rodata,0\n"
- "\t.globl %s\n"
- "\t.align 16\n"
- "%s:\n",
-
- ".4byte ","",HEX_0X
- },
-/* 1<<4 bit alignment for aix. */
-/* http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.aixassem%2Fdoc%2Falangref%2Fidalangref_csect_pseudoop.htm */
- {"xlc",
- ".globl %s{RO}\n"
- "\t.toc\n"
- "%s:\n"
- "\t.csect %s{RO}, 4\n",
-
- ".long ","",HEX_0X
- },
- {"aCC-ia64",
- "\t.file \"%s.s\"\n"
- "\t.type %s,@object\n"
- "\t.global %s\n"
- "\t.secalias .abe$0.rodata, \".rodata\"\n"
- "\t.section .abe$0.rodata = \"a\", \"progbits\"\n"
- "\t.align 16\n"
- "%s::\t",
-
- "data4 ","",HEX_0X
- },
- {"aCC-parisc",
- "\t.SPACE $TEXT$\n"
- "\t.SUBSPA $LIT$\n"
- "%s\n"
- "\t.EXPORT %s\n"
- "\t.ALIGN 16\n",
-
- ".WORD ","",HEX_0X
- },
-/* align 16 bytes */
-/* http://msdn.microsoft.com/en-us/library/dwa9fwef.aspx */
- { "masm",
- "\tTITLE %s\n"
- "; generated by genccode\n"
- ".386\n"
- ".model flat\n"
- "\tPUBLIC _%s\n"
- "ICUDATA_%s\tSEGMENT READONLY PARA PUBLIC FLAT 'DATA'\n"
- "\tALIGN 16\n"
- "_%s\tLABEL DWORD\n",
- "\tDWORD ","\nICUDATA_%s\tENDS\n\tEND\n",HEX_0H
- }
-};
-
-static int32_t assemblyHeaderIndex = -1;
-static int32_t hexType = HEX_0X;
-
-U_CAPI UBool U_EXPORT2
-checkAssemblyHeaderName(const char* optAssembly) {
- int32_t idx;
- assemblyHeaderIndex = -1;
- for (idx = 0; idx < UPRV_LENGTHOF(assemblyHeader); idx++) {
- if (uprv_strcmp(optAssembly, assemblyHeader[idx].name) == 0) {
- assemblyHeaderIndex = idx;
- hexType = assemblyHeader[idx].hexType; /* set the hex type */
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-
-U_CAPI void U_EXPORT2
-printAssemblyHeadersToStdErr(void) {
- int32_t idx;
- fprintf(stderr, "%s", assemblyHeader[0].name);
- for (idx = 1; idx < UPRV_LENGTHOF(assemblyHeader); idx++) {
- fprintf(stderr, ", %s", assemblyHeader[idx].name);
- }
- fprintf(stderr,
- ")\n");
-}
-
-U_CAPI void U_EXPORT2
-writeAssemblyCode(const char *filename, const char *destdir, const char *optEntryPoint, const char *optFilename, char *outFilePath) {
- uint32_t column = MAX_COLUMN;
- char entry[64];
- uint32_t buffer[1024];
- char *bufferStr = (char *)buffer;
- FileStream *in, *out;
- size_t i, length;
-
- in=T_FileStream_open(filename, "rb");
- if(in==NULL) {
- fprintf(stderr, "genccode: unable to open input file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- getOutFilename(filename, destdir, bufferStr, entry, ".S", optFilename);
- out=T_FileStream_open(bufferStr, "w");
- if(out==NULL) {
- fprintf(stderr, "genccode: unable to open output file %s\n", bufferStr);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- if (outFilePath != NULL) {
- uprv_strcpy(outFilePath, bufferStr);
- }
-
-#if defined (WINDOWS_WITH_GNUC) && U_PLATFORM != U_PF_CYGWIN
- /* Need to fix the file separator character when using MinGW. */
- swapFileSepChar(outFilePath, U_FILE_SEP_CHAR, '/');
-#endif
-
- if(optEntryPoint != NULL) {
- uprv_strcpy(entry, optEntryPoint);
- uprv_strcat(entry, "_dat");
- }
-
- /* turn dashes or dots in the entry name into underscores */
- length=uprv_strlen(entry);
- for(i=0; i<length; ++i) {
- if(entry[i]=='-' || entry[i]=='.') {
- entry[i]='_';
- }
- }
-
- sprintf(bufferStr, assemblyHeader[assemblyHeaderIndex].header,
- entry, entry, entry, entry,
- entry, entry, entry, entry);
- T_FileStream_writeLine(out, bufferStr);
- T_FileStream_writeLine(out, assemblyHeader[assemblyHeaderIndex].beginLine);
-
- for(;;) {
- length=T_FileStream_read(in, buffer, sizeof(buffer));
- if(length==0) {
- break;
- }
- if (length != sizeof(buffer)) {
- /* pad with extra 0's when at the end of the file */
- for(i=0; i < (length % sizeof(uint32_t)); ++i) {
- buffer[length+i] = 0;
- }
- }
- for(i=0; i<(length/sizeof(buffer[0])); i++) {
- column = write32(out, buffer[i], column);
- }
- }
-
- T_FileStream_writeLine(out, "\n");
-
- sprintf(bufferStr, assemblyHeader[assemblyHeaderIndex].footer,
- entry, entry, entry, entry,
- entry, entry, entry, entry);
- T_FileStream_writeLine(out, bufferStr);
-
- if(T_FileStream_error(in)) {
- fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- if(T_FileStream_error(out)) {
- fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- T_FileStream_close(out);
- T_FileStream_close(in);
-}
-
-U_CAPI void U_EXPORT2
-writeCCode(const char *filename, const char *destdir, const char *optName, const char *optFilename, char *outFilePath) {
- uint32_t column = MAX_COLUMN;
- char buffer[4096], entry[64];
- FileStream *in, *out;
- size_t i, length;
-
- in=T_FileStream_open(filename, "rb");
- if(in==NULL) {
- fprintf(stderr, "genccode: unable to open input file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- if(optName != NULL) { /* prepend 'icudt28_' */
- strcpy(entry, optName);
- strcat(entry, "_");
- } else {
- entry[0] = 0;
- }
-
- getOutFilename(filename, destdir, buffer, entry+uprv_strlen(entry), ".c", optFilename);
- if (outFilePath != NULL) {
- uprv_strcpy(outFilePath, buffer);
- }
- out=T_FileStream_open(buffer, "w");
- if(out==NULL) {
- fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- /* turn dashes or dots in the entry name into underscores */
- length=uprv_strlen(entry);
- for(i=0; i<length; ++i) {
- if(entry[i]=='-' || entry[i]=='.') {
- entry[i]='_';
- }
- }
-
-#if U_PLATFORM == U_PF_OS400
- /*
- TODO: Fix this once the compiler implements this feature. Keep in sync with udatamem.c
-
- This is here because this platform can't currently put
- const data into the read-only pages of an object or
- shared library (service program). Only strings are allowed in read-only
- pages, so we use char * strings to store the data.
-
- In order to prevent the beginning of the data from ever matching the
- magic numbers we must still use the initial double.
- [grhoten 4/24/2003]
- */
- sprintf(buffer,
- "#ifndef IN_GENERATED_CCODE\n"
- "#define IN_GENERATED_CCODE\n"
- "#define U_DISABLE_RENAMING 1\n"
- "#include \"unicode/umachine.h\"\n"
- "#endif\n"
- "U_CDECL_BEGIN\n"
- "const struct {\n"
- " double bogus;\n"
- " const char *bytes; \n"
- "} %s={ 0.0, \n",
- entry);
- T_FileStream_writeLine(out, buffer);
-
- for(;;) {
- length=T_FileStream_read(in, buffer, sizeof(buffer));
- if(length==0) {
- break;
- }
- for(i=0; i<length; ++i) {
- column = write8str(out, (uint8_t)buffer[i], column);
- }
- }
-
- T_FileStream_writeLine(out, "\"\n};\nU_CDECL_END\n");
-#else
- /* Function renaming shouldn't be done in data */
- sprintf(buffer,
- "#ifndef IN_GENERATED_CCODE\n"
- "#define IN_GENERATED_CCODE\n"
- "#define U_DISABLE_RENAMING 1\n"
- "#include \"unicode/umachine.h\"\n"
- "#endif\n"
- "U_CDECL_BEGIN\n"
- "const struct {\n"
- " double bogus;\n"
- " uint8_t bytes[%ld]; \n"
- "} %s={ 0.0, {\n",
- (long)T_FileStream_size(in), entry);
- T_FileStream_writeLine(out, buffer);
-
- for(;;) {
- length=T_FileStream_read(in, buffer, sizeof(buffer));
- if(length==0) {
- break;
- }
- for(i=0; i<length; ++i) {
- column = write8(out, (uint8_t)buffer[i], column);
- }
- }
-
- T_FileStream_writeLine(out, "\n}\n};\nU_CDECL_END\n");
-#endif
-
- if(T_FileStream_error(in)) {
- fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- if(T_FileStream_error(out)) {
- fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- T_FileStream_close(out);
- T_FileStream_close(in);
-}
-
-static uint32_t
-write32(FileStream *out, uint32_t bitField, uint32_t column) {
- int32_t i;
- char bitFieldStr[64]; /* This is more bits than needed for a 32-bit number */
- char *s = bitFieldStr;
- uint8_t *ptrIdx = (uint8_t *)&bitField;
- static const char hexToStr[16] = {
- '0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F'
- };
-
- /* write the value, possibly with comma and newline */
- if(column==MAX_COLUMN) {
- /* first byte */
- column=1;
- } else if(column<32) {
- *(s++)=',';
- ++column;
- } else {
- *(s++)='\n';
- uprv_strcpy(s, assemblyHeader[assemblyHeaderIndex].beginLine);
- s+=uprv_strlen(s);
- column=1;
- }
-
- if (bitField < 10) {
- /* It's a small number. Don't waste the space for 0x */
- *(s++)=hexToStr[bitField];
- }
- else {
- int seenNonZero = 0; /* This is used to remove leading zeros */
-
- if(hexType==HEX_0X) {
- *(s++)='0';
- *(s++)='x';
- } else if(hexType==HEX_0H) {
- *(s++)='0';
- }
-
- /* This creates a 32-bit field */
-#if U_IS_BIG_ENDIAN
- for (i = 0; i < sizeof(uint32_t); i++)
-#else
- for (i = sizeof(uint32_t)-1; i >= 0 ; i--)
-#endif
- {
- uint8_t value = ptrIdx[i];
- if (value || seenNonZero) {
- *(s++)=hexToStr[value>>4];
- *(s++)=hexToStr[value&0xF];
- seenNonZero = 1;
- }
- }
- if(hexType==HEX_0H) {
- *(s++)='h';
- }
- }
-
- *(s++)=0;
- T_FileStream_writeLine(out, bitFieldStr);
- return column;
-}
-
-static uint32_t
-write8(FileStream *out, uint8_t byte, uint32_t column) {
- char s[4];
- int i=0;
-
- /* convert the byte value to a string */
- if(byte>=100) {
- s[i++]=(char)('0'+byte/100);
- byte%=100;
- }
- if(i>0 || byte>=10) {
- s[i++]=(char)('0'+byte/10);
- byte%=10;
- }
- s[i++]=(char)('0'+byte);
- s[i]=0;
-
- /* write the value, possibly with comma and newline */
- if(column==MAX_COLUMN) {
- /* first byte */
- column=1;
- } else if(column<16) {
- T_FileStream_writeLine(out, ",");
- ++column;
- } else {
- T_FileStream_writeLine(out, ",\n");
- column=1;
- }
- T_FileStream_writeLine(out, s);
- return column;
-}
-
-#if U_PLATFORM == U_PF_OS400
-static uint32_t
-write8str(FileStream *out, uint8_t byte, uint32_t column) {
- char s[8];
-
- if (byte > 7)
- sprintf(s, "\\x%X", byte);
- else
- sprintf(s, "\\%X", byte);
-
- /* write the value, possibly with comma and newline */
- if(column==MAX_COLUMN) {
- /* first byte */
- column=1;
- T_FileStream_writeLine(out, "\"");
- } else if(column<24) {
- ++column;
- } else {
- T_FileStream_writeLine(out, "\"\n\"");
- column=1;
- }
- T_FileStream_writeLine(out, s);
- return column;
-}
-#endif
-
-static void
-getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix, const char *optFilename) {
- const char *basename=findBasename(inFilename), *suffix=uprv_strrchr(basename, '.');
-
- /* copy path */
- if(destdir!=NULL && *destdir!=0) {
- do {
- *outFilename++=*destdir++;
- } while(*destdir!=0);
- if(*(outFilename-1)!=U_FILE_SEP_CHAR) {
- *outFilename++=U_FILE_SEP_CHAR;
- }
- inFilename=basename;
- } else {
- while(inFilename<basename) {
- *outFilename++=*inFilename++;
- }
- }
-
- if(suffix==NULL) {
- /* the filename does not have a suffix */
- uprv_strcpy(entryName, inFilename);
- if(optFilename != NULL) {
- uprv_strcpy(outFilename, optFilename);
- } else {
- uprv_strcpy(outFilename, inFilename);
- }
- uprv_strcat(outFilename, newSuffix);
- } else {
- char *saveOutFilename = outFilename;
- /* copy basename */
- while(inFilename<suffix) {
- if(*inFilename=='-') {
- /* iSeries cannot have '-' in the .o objects. */
- *outFilename++=*entryName++='_';
- inFilename++;
- }
- else {
- *outFilename++=*entryName++=*inFilename++;
- }
- }
-
- /* replace '.' by '_' */
- *outFilename++=*entryName++='_';
- ++inFilename;
-
- /* copy suffix */
- while(*inFilename!=0) {
- *outFilename++=*entryName++=*inFilename++;
- }
-
- *entryName=0;
-
- if(optFilename != NULL) {
- uprv_strcpy(saveOutFilename, optFilename);
- uprv_strcat(saveOutFilename, newSuffix);
- } else {
- /* add ".c" */
- uprv_strcpy(outFilename, newSuffix);
- }
- }
-}
-
-#ifdef CAN_GENERATE_OBJECTS
-static void
-getArchitecture(uint16_t *pCPU, uint16_t *pBits, UBool *pIsBigEndian, const char *optMatchArch) {
- union {
- char bytes[2048];
-#ifdef U_ELF
- Elf32_Ehdr header32;
- /* Elf32_Ehdr and ELF64_Ehdr are identical for the necessary fields. */
-#elif U_PLATFORM_HAS_WIN32_API
- IMAGE_FILE_HEADER header;
-#endif
- } buffer;
-
- const char *filename;
- FileStream *in;
- int32_t length;
-
-#ifdef U_ELF
-
-#elif U_PLATFORM_HAS_WIN32_API
- const IMAGE_FILE_HEADER *pHeader;
-#else
-# error "Unknown platform for CAN_GENERATE_OBJECTS."
-#endif
-
- if(optMatchArch != NULL) {
- filename=optMatchArch;
- } else {
- /* set defaults */
-#ifdef U_ELF
- /* set EM_386 because elf.h does not provide better defaults */
- *pCPU=EM_386;
- *pBits=32;
- *pIsBigEndian=(UBool)(U_IS_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB);
-#elif U_PLATFORM_HAS_WIN32_API
- // Windows always runs in little-endian mode.
- *pIsBigEndian = FALSE;
-
- // Note: The various _M_<arch> macros are predefined by the MSVC compiler based
- // on the target compilation architecture.
- // https://docs.microsoft.com/cpp/preprocessor/predefined-macros
-
- // link.exe will link an IMAGE_FILE_MACHINE_UNKNOWN data-only .obj file
- // no matter what architecture it is targeting (though other values are
- // required to match). Unfortunately, the variable name decoration/mangling
- // is slightly different on x86, which means we can't use the UNKNOWN type
- // for all architectures though.
-# if defined(_M_IX86)
- *pCPU = IMAGE_FILE_MACHINE_I386;
-# else
- *pCPU = IMAGE_FILE_MACHINE_UNKNOWN;
-# endif
-# if defined(_M_IA64) || defined(_M_AMD64) || defined (_M_ARM64)
- *pBits = 64; // Doesn't seem to be used for anything interesting though?
-# elif defined(_M_IX86) || defined(_M_ARM)
- *pBits = 32;
-# else
-# error "Unknown platform for CAN_GENERATE_OBJECTS."
-# endif
-#else
-# error "Unknown platform for CAN_GENERATE_OBJECTS."
-#endif
- return;
- }
-
- in=T_FileStream_open(filename, "rb");
- if(in==NULL) {
- fprintf(stderr, "genccode: unable to open match-arch file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
- length=T_FileStream_read(in, buffer.bytes, sizeof(buffer.bytes));
-
-#ifdef U_ELF
- if(length<(int32_t)sizeof(Elf32_Ehdr)) {
- fprintf(stderr, "genccode: match-arch file %s is too short\n", filename);
- exit(U_UNSUPPORTED_ERROR);
- }
- if(
- buffer.header32.e_ident[0]!=ELFMAG0 ||
- buffer.header32.e_ident[1]!=ELFMAG1 ||
- buffer.header32.e_ident[2]!=ELFMAG2 ||
- buffer.header32.e_ident[3]!=ELFMAG3 ||
- buffer.header32.e_ident[EI_CLASS]<ELFCLASS32 || buffer.header32.e_ident[EI_CLASS]>ELFCLASS64
- ) {
- fprintf(stderr, "genccode: match-arch file %s is not an ELF object file, or not supported\n", filename);
- exit(U_UNSUPPORTED_ERROR);
- }
-
- *pBits= buffer.header32.e_ident[EI_CLASS]==ELFCLASS32 ? 32 : 64; /* only 32 or 64: see check above */
-#ifdef U_ELF64
- if(*pBits!=32 && *pBits!=64) {
- fprintf(stderr, "genccode: currently only supports 32-bit and 64-bit ELF format\n");
- exit(U_UNSUPPORTED_ERROR);
- }
-#else
- if(*pBits!=32) {
- fprintf(stderr, "genccode: built with elf.h missing 64-bit definitions\n");
- exit(U_UNSUPPORTED_ERROR);
- }
-#endif
-
- *pIsBigEndian=(UBool)(buffer.header32.e_ident[EI_DATA]==ELFDATA2MSB);
- if(*pIsBigEndian!=U_IS_BIG_ENDIAN) {
- fprintf(stderr, "genccode: currently only same-endianness ELF formats are supported\n");
- exit(U_UNSUPPORTED_ERROR);
- }
- /* TODO: Support byte swapping */
-
- *pCPU=buffer.header32.e_machine;
-#elif U_PLATFORM_HAS_WIN32_API
- if(length<sizeof(IMAGE_FILE_HEADER)) {
- fprintf(stderr, "genccode: match-arch file %s is too short\n", filename);
- exit(U_UNSUPPORTED_ERROR);
- }
- /* TODO: Use buffer.header. Keep aliasing legal. */
- pHeader=(const IMAGE_FILE_HEADER *)buffer.bytes;
- *pCPU=pHeader->Machine;
- /*
- * The number of bits is implicit with the Machine value.
- * *pBits is ignored in the calling code, so this need not be precise.
- */
- *pBits= *pCPU==IMAGE_FILE_MACHINE_I386 ? 32 : 64;
- /* Windows always runs on little-endian CPUs. */
- *pIsBigEndian=FALSE;
-#else
-# error "Unknown platform for CAN_GENERATE_OBJECTS."
-#endif
-
- T_FileStream_close(in);
-}
-
-U_CAPI void U_EXPORT2
-writeObjectCode(const char *filename, const char *destdir, const char *optEntryPoint, const char *optMatchArch, const char *optFilename, char *outFilePath) {
- /* common variables */
- char buffer[4096], entry[96]={ 0 };
- FileStream *in, *out;
- const char *newSuffix;
- int32_t i, entryLength, length, size, entryOffset=0, entryLengthOffset=0;
-
- uint16_t cpu, bits;
- UBool makeBigEndian;
-
- /* platform-specific variables and initialization code */
-#ifdef U_ELF
- /* 32-bit Elf file header */
- static Elf32_Ehdr header32={
- {
- /* e_ident[] */
- ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
- ELFCLASS32,
- U_IS_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB,
- EV_CURRENT /* EI_VERSION */
- },
- ET_REL,
- EM_386,
- EV_CURRENT, /* e_version */
- 0, /* e_entry */
- 0, /* e_phoff */
- (Elf32_Off)sizeof(Elf32_Ehdr), /* e_shoff */
- 0, /* e_flags */
- (Elf32_Half)sizeof(Elf32_Ehdr), /* eh_size */
- 0, /* e_phentsize */
- 0, /* e_phnum */
- (Elf32_Half)sizeof(Elf32_Shdr), /* e_shentsize */
- 5, /* e_shnum */
- 2 /* e_shstrndx */
- };
-
- /* 32-bit Elf section header table */
- static Elf32_Shdr sectionHeaders32[5]={
- { /* SHN_UNDEF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- },
- { /* .symtab */
- 1, /* sh_name */
- SHT_SYMTAB,
- 0, /* sh_flags */
- 0, /* sh_addr */
- (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)), /* sh_offset */
- (Elf32_Word)(2*sizeof(Elf32_Sym)), /* sh_size */
- 3, /* sh_link=sect hdr index of .strtab */
- 1, /* sh_info=One greater than the symbol table index of the last
- * local symbol (with STB_LOCAL). */
- 4, /* sh_addralign */
- (Elf32_Word)(sizeof(Elf32_Sym)) /* sh_entsize */
- },
- { /* .shstrtab */
- 9, /* sh_name */
- SHT_STRTAB,
- 0, /* sh_flags */
- 0, /* sh_addr */
- (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)+2*sizeof(Elf32_Sym)), /* sh_offset */
- 40, /* sh_size */
- 0, /* sh_link */
- 0, /* sh_info */
- 1, /* sh_addralign */
- 0 /* sh_entsize */
- },
- { /* .strtab */
- 19, /* sh_name */
- SHT_STRTAB,
- 0, /* sh_flags */
- 0, /* sh_addr */
- (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)+2*sizeof(Elf32_Sym)+40), /* sh_offset */
- (Elf32_Word)sizeof(entry), /* sh_size */
- 0, /* sh_link */
- 0, /* sh_info */
- 1, /* sh_addralign */
- 0 /* sh_entsize */
- },
- { /* .rodata */
- 27, /* sh_name */
- SHT_PROGBITS,
- SHF_ALLOC, /* sh_flags */
- 0, /* sh_addr */
- (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)+2*sizeof(Elf32_Sym)+40+sizeof(entry)), /* sh_offset */
- 0, /* sh_size */
- 0, /* sh_link */
- 0, /* sh_info */
- 16, /* sh_addralign */
- 0 /* sh_entsize */
- }
- };
-
- /* symbol table */
- static Elf32_Sym symbols32[2]={
- { /* STN_UNDEF */
- 0, 0, 0, 0, 0, 0
- },
- { /* data entry point */
- 1, /* st_name */
- 0, /* st_value */
- 0, /* st_size */
- ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT),
- 0, /* st_other */
- 4 /* st_shndx=index of related section table entry */
- }
- };
-
- /* section header string table, with decimal string offsets */
- static const char sectionStrings[40]=
- /* 0 */ "\0"
- /* 1 */ ".symtab\0"
- /* 9 */ ".shstrtab\0"
- /* 19 */ ".strtab\0"
- /* 27 */ ".rodata\0"
- /* 35 */ "\0\0\0\0"; /* contains terminating NUL */
- /* 40: padded to multiple of 8 bytes */
-
- /*
- * Use entry[] for the string table which will contain only the
- * entry point name.
- * entry[0] must be 0 (NUL)
- * The entry point name can be up to 38 characters long (sizeof(entry)-2).
- */
-
- /* 16-align .rodata in the .o file, just in case */
- static const char padding[16]={ 0 };
- int32_t paddingSize;
-
-#ifdef U_ELF64
- /* 64-bit Elf file header */
- static Elf64_Ehdr header64={
- {
- /* e_ident[] */
- ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
- ELFCLASS64,
- U_IS_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB,
- EV_CURRENT /* EI_VERSION */
- },
- ET_REL,
- EM_X86_64,
- EV_CURRENT, /* e_version */
- 0, /* e_entry */
- 0, /* e_phoff */
- (Elf64_Off)sizeof(Elf64_Ehdr), /* e_shoff */
- 0, /* e_flags */
- (Elf64_Half)sizeof(Elf64_Ehdr), /* eh_size */
- 0, /* e_phentsize */
- 0, /* e_phnum */
- (Elf64_Half)sizeof(Elf64_Shdr), /* e_shentsize */
- 5, /* e_shnum */
- 2 /* e_shstrndx */
- };
-
- /* 64-bit Elf section header table */
- static Elf64_Shdr sectionHeaders64[5]={
- { /* SHN_UNDEF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- },
- { /* .symtab */
- 1, /* sh_name */
- SHT_SYMTAB,
- 0, /* sh_flags */
- 0, /* sh_addr */
- (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)), /* sh_offset */
- (Elf64_Xword)(2*sizeof(Elf64_Sym)), /* sh_size */
- 3, /* sh_link=sect hdr index of .strtab */
- 1, /* sh_info=One greater than the symbol table index of the last
- * local symbol (with STB_LOCAL). */
- 4, /* sh_addralign */
- (Elf64_Xword)(sizeof(Elf64_Sym)) /* sh_entsize */
- },
- { /* .shstrtab */
- 9, /* sh_name */
- SHT_STRTAB,
- 0, /* sh_flags */
- 0, /* sh_addr */
- (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)+2*sizeof(Elf64_Sym)), /* sh_offset */
- 40, /* sh_size */
- 0, /* sh_link */
- 0, /* sh_info */
- 1, /* sh_addralign */
- 0 /* sh_entsize */
- },
- { /* .strtab */
- 19, /* sh_name */
- SHT_STRTAB,
- 0, /* sh_flags */
- 0, /* sh_addr */
- (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)+2*sizeof(Elf64_Sym)+40), /* sh_offset */
- (Elf64_Xword)sizeof(entry), /* sh_size */
- 0, /* sh_link */
- 0, /* sh_info */
- 1, /* sh_addralign */
- 0 /* sh_entsize */
- },
- { /* .rodata */
- 27, /* sh_name */
- SHT_PROGBITS,
- SHF_ALLOC, /* sh_flags */
- 0, /* sh_addr */
- (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)+2*sizeof(Elf64_Sym)+40+sizeof(entry)), /* sh_offset */
- 0, /* sh_size */
- 0, /* sh_link */
- 0, /* sh_info */
- 16, /* sh_addralign */
- 0 /* sh_entsize */
- }
- };
-
- /*
- * 64-bit symbol table
- * careful: different order of items compared with Elf32_sym!
- */
- static Elf64_Sym symbols64[2]={
- { /* STN_UNDEF */
- 0, 0, 0, 0, 0, 0
- },
- { /* data entry point */
- 1, /* st_name */
- ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT),
- 0, /* st_other */
- 4, /* st_shndx=index of related section table entry */
- 0, /* st_value */
- 0 /* st_size */
- }
- };
-
-#endif /* U_ELF64 */
-
- /* entry[] have a leading NUL */
- entryOffset=1;
-
- /* in the common code, count entryLength from after the NUL */
- entryLengthOffset=1;
-
- newSuffix=".o";
-
-#elif U_PLATFORM_HAS_WIN32_API
- struct {
- IMAGE_FILE_HEADER fileHeader;
- IMAGE_SECTION_HEADER sections[2];
- char linkerOptions[100];
- } objHeader;
- IMAGE_SYMBOL symbols[1];
- struct {
- DWORD sizeofLongNames;
- char longNames[100];
- } symbolNames;
-
- /*
- * entry sometimes have a leading '_'
- * overwritten if entryOffset==0 depending on the target platform
- * see check for cpu below
- */
- entry[0]='_';
-
- newSuffix=".obj";
-#else
-# error "Unknown platform for CAN_GENERATE_OBJECTS."
-#endif
-
- /* deal with options, files and the entry point name */
- getArchitecture(&cpu, &bits, &makeBigEndian, optMatchArch);
- if (optMatchArch)
- {
- printf("genccode: --match-arch cpu=%hu bits=%hu big-endian=%d\n", cpu, bits, makeBigEndian);
- }
- else
- {
- printf("genccode: using architecture cpu=%hu bits=%hu big-endian=%d\n", cpu, bits, makeBigEndian);
- }
-#if U_PLATFORM_HAS_WIN32_API
- if(cpu==IMAGE_FILE_MACHINE_I386) {
- entryOffset=1;
- }
-#endif
-
- in=T_FileStream_open(filename, "rb");
- if(in==NULL) {
- fprintf(stderr, "genccode: unable to open input file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
- size=T_FileStream_size(in);
-
- getOutFilename(filename, destdir, buffer, entry+entryOffset, newSuffix, optFilename);
- if (outFilePath != NULL) {
- uprv_strcpy(outFilePath, buffer);
- }
-
- if(optEntryPoint != NULL) {
- uprv_strcpy(entry+entryOffset, optEntryPoint);
- uprv_strcat(entry+entryOffset, "_dat");
- }
- /* turn dashes in the entry name into underscores */
- entryLength=(int32_t)uprv_strlen(entry+entryLengthOffset);
- for(i=0; i<entryLength; ++i) {
- if(entry[entryLengthOffset+i]=='-') {
- entry[entryLengthOffset+i]='_';
- }
- }
-
- /* open the output file */
- out=T_FileStream_open(buffer, "wb");
- if(out==NULL) {
- fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
- exit(U_FILE_ACCESS_ERROR);
- }
-
-#ifdef U_ELF
- if(bits==32) {
- header32.e_ident[EI_DATA]= makeBigEndian ? ELFDATA2MSB : ELFDATA2LSB;
- header32.e_machine=cpu;
-
- /* 16-align .rodata in the .o file, just in case */
- paddingSize=sectionHeaders32[4].sh_offset & 0xf;
- if(paddingSize!=0) {
- paddingSize=0x10-paddingSize;
- sectionHeaders32[4].sh_offset+=paddingSize;
- }
-
- sectionHeaders32[4].sh_size=(Elf32_Word)size;
-
- symbols32[1].st_size=(Elf32_Word)size;
-
- /* write .o headers */
- T_FileStream_write(out, &header32, (int32_t)sizeof(header32));
- T_FileStream_write(out, sectionHeaders32, (int32_t)sizeof(sectionHeaders32));
- T_FileStream_write(out, symbols32, (int32_t)sizeof(symbols32));
- } else /* bits==64 */ {
-#ifdef U_ELF64
- header64.e_ident[EI_DATA]= makeBigEndian ? ELFDATA2MSB : ELFDATA2LSB;
- header64.e_machine=cpu;
-
- /* 16-align .rodata in the .o file, just in case */
- paddingSize=sectionHeaders64[4].sh_offset & 0xf;
- if(paddingSize!=0) {
- paddingSize=0x10-paddingSize;
- sectionHeaders64[4].sh_offset+=paddingSize;
- }
-
- sectionHeaders64[4].sh_size=(Elf64_Xword)size;
-
- symbols64[1].st_size=(Elf64_Xword)size;
-
- /* write .o headers */
- T_FileStream_write(out, &header64, (int32_t)sizeof(header64));
- T_FileStream_write(out, sectionHeaders64, (int32_t)sizeof(sectionHeaders64));
- T_FileStream_write(out, symbols64, (int32_t)sizeof(symbols64));
-#endif
- }
-
- T_FileStream_write(out, sectionStrings, (int32_t)sizeof(sectionStrings));
- T_FileStream_write(out, entry, (int32_t)sizeof(entry));
- if(paddingSize!=0) {
- T_FileStream_write(out, padding, paddingSize);
- }
-#elif U_PLATFORM_HAS_WIN32_API
- /* populate the .obj headers */
- uprv_memset(&objHeader, 0, sizeof(objHeader));
- uprv_memset(&symbols, 0, sizeof(symbols));
- uprv_memset(&symbolNames, 0, sizeof(symbolNames));
-
- /* write the linker export directive */
- uprv_strcpy(objHeader.linkerOptions, "-export:");
- length=8;
- uprv_strcpy(objHeader.linkerOptions+length, entry);
- length+=entryLength;
- uprv_strcpy(objHeader.linkerOptions+length, ",data ");
- length+=6;
-
- /* set the file header */
- objHeader.fileHeader.Machine=cpu;
- objHeader.fileHeader.NumberOfSections=2;
- objHeader.fileHeader.TimeDateStamp=(DWORD)time(NULL);
- objHeader.fileHeader.PointerToSymbolTable=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER+length+size; /* start of symbol table */
- objHeader.fileHeader.NumberOfSymbols=1;
-
- /* set the section for the linker options */
- uprv_strncpy((char *)objHeader.sections[0].Name, ".drectve", 8);
- objHeader.sections[0].SizeOfRawData=length;
- objHeader.sections[0].PointerToRawData=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER;
- objHeader.sections[0].Characteristics=IMAGE_SCN_LNK_INFO|IMAGE_SCN_LNK_REMOVE|IMAGE_SCN_ALIGN_1BYTES;
-
- /* set the data section */
- uprv_strncpy((char *)objHeader.sections[1].Name, ".rdata", 6);
- objHeader.sections[1].SizeOfRawData=size;
- objHeader.sections[1].PointerToRawData=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER+length;
- objHeader.sections[1].Characteristics=IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_ALIGN_16BYTES|IMAGE_SCN_MEM_READ;
-
- /* set the symbol table */
- if(entryLength<=8) {
- uprv_strncpy((char *)symbols[0].N.ShortName, entry, entryLength);
- symbolNames.sizeofLongNames=4;
- } else {
- symbols[0].N.Name.Short=0;
- symbols[0].N.Name.Long=4;
- symbolNames.sizeofLongNames=4+entryLength+1;
- uprv_strcpy(symbolNames.longNames, entry);
- }
- symbols[0].SectionNumber=2;
- symbols[0].StorageClass=IMAGE_SYM_CLASS_EXTERNAL;
-
- /* write the file header and the linker options section */
- T_FileStream_write(out, &objHeader, objHeader.sections[1].PointerToRawData);
-#else
-# error "Unknown platform for CAN_GENERATE_OBJECTS."
-#endif
-
- /* copy the data file into section 2 */
- for(;;) {
- length=T_FileStream_read(in, buffer, sizeof(buffer));
- if(length==0) {
- break;
- }
- T_FileStream_write(out, buffer, (int32_t)length);
- }
-
-#if U_PLATFORM_HAS_WIN32_API
- /* write the symbol table */
- T_FileStream_write(out, symbols, IMAGE_SIZEOF_SYMBOL);
- T_FileStream_write(out, &symbolNames, symbolNames.sizeofLongNames);
-#endif
-
- if(T_FileStream_error(in)) {
- fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- if(T_FileStream_error(out)) {
- fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- T_FileStream_close(out);
- T_FileStream_close(in);
-}
-#endif