From 109a830ed2dec57a2fbba50b1e560e73dc1f691c Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Mon, 27 Sep 2021 11:46:06 -0700 Subject: [PATCH] ICU-21545 fix Unicode properties Bazel build --- .../tools/icuexportdata/icuexportdata.cpp | 17 +++++++++++++++- icu4c/source/tools/toolutil/BUILD | 6 +++++- icu4c/source/tools/toolutil/writesrc.cpp | 14 +++++++++---- icu4c/source/tools/toolutil/writesrc.h | 20 ++++++++++++++++--- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/icu4c/source/tools/icuexportdata/icuexportdata.cpp b/icu4c/source/tools/icuexportdata/icuexportdata.cpp index 737f891c515..ef933676115 100644 --- a/icu4c/source/tools/icuexportdata/icuexportdata.cpp +++ b/icu4c/source/tools/icuexportdata/icuexportdata.cpp @@ -34,6 +34,20 @@ void handleError(ErrorCode& status, const char* context) { } } +class PropertyValueNameGetter : public ValueNameGetter { +public: + PropertyValueNameGetter(UProperty prop) : property(prop) {} + ~PropertyValueNameGetter() override; + const char *getName(uint32_t value) override { + return u_getPropertyValueName(property, value, U_SHORT_PROPERTY_NAME); + } + +private: + UProperty property; +}; + +PropertyValueNameGetter::~PropertyValueNameGetter() {} + void dumpBinaryProperty(UProperty uproperty, FILE* f) { IcuToolErrorCode status("icuexportdata: dumpBinaryProperty"); const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME); @@ -57,7 +71,8 @@ void dumpEnumeratedProperty(UProperty uproperty, FILE* f) { fputs("[[enum_property]]\n", f); fprintf(f, "long_name = \"%s\"\n", fullPropName); if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName); - usrc_writeUCPMap(f, umap, uproperty, UPRV_TARGET_SYNTAX_TOML); + PropertyValueNameGetter valueNameGetter(uproperty); + usrc_writeUCPMap(f, umap, &valueNameGetter, UPRV_TARGET_SYNTAX_TOML); fputs("\n", f); U_ASSERT(u_getIntPropertyMinValue(uproperty) >= 0); diff --git a/icu4c/source/tools/toolutil/BUILD b/icu4c/source/tools/toolutil/BUILD index 734e9c7b59f..276c857f124 100644 --- a/icu4c/source/tools/toolutil/BUILD +++ b/icu4c/source/tools/toolutil/BUILD @@ -54,7 +54,11 @@ cc_library( local_defines = [ "U_TOOLUTIL_IMPLEMENTATION", ], - deps = ["//icu4c/source/common:platform"], + deps = [ + "//icu4c/source/common:bytestream", + "//icu4c/source/common:platform", + "//icu4c/source/common:uniset_core", + ], ) cc_library( diff --git a/icu4c/source/tools/toolutil/writesrc.cpp b/icu4c/source/tools/toolutil/writesrc.cpp index 9db3118e13c..143254a7f34 100644 --- a/icu4c/source/tools/toolutil/writesrc.cpp +++ b/icu4c/source/tools/toolutil/writesrc.cpp @@ -32,6 +32,12 @@ #include "writesrc.h" #include "util.h" +U_NAMESPACE_BEGIN + +ValueNameGetter::~ValueNameGetter() {} + +U_NAMESPACE_END + U_NAMESPACE_USE static FILE * @@ -401,7 +407,7 @@ U_CAPI void U_EXPORT2 usrc_writeUCPMap( FILE *f, const UCPMap *pMap, - UProperty uproperty, + icu::ValueNameGetter *valueNameGetter, UTargetSyntax syntax) { // ccode is not yet supported U_ASSERT(syntax == UPRV_TARGET_SYNTAX_TOML); @@ -413,9 +419,9 @@ usrc_writeUCPMap( fprintf(f, "# Code points `a` through `b` have value `v`, corresponding to `name`.\n"); fprintf(f, "ranges = [\n"); while ((end = ucpmap_getRange(pMap, start, UCPMAP_RANGE_NORMAL, 0, nullptr, nullptr, &value)) >= 0) { - if (uproperty != UCHAR_INVALID_CODE) { - const char* short_name = u_getPropertyValueName(uproperty, value, U_SHORT_PROPERTY_NAME); - fprintf(f, " {a=0x%x, b=0x%x, v=%u, name=\"%s\"},\n", start, end, value, short_name); + if (valueNameGetter != nullptr) { + const char *name = valueNameGetter->getName(value); + fprintf(f, " {a=0x%x, b=0x%x, v=%u, name=\"%s\"},\n", start, end, value, name); } else { fprintf(f, " {a=0x%x, b=0x%x, v=%u},\n", start, end, value); } diff --git a/icu4c/source/tools/toolutil/writesrc.h b/icu4c/source/tools/toolutil/writesrc.h index 25377af25c0..784a9b9c7a7 100644 --- a/icu4c/source/tools/toolutil/writesrc.h +++ b/icu4c/source/tools/toolutil/writesrc.h @@ -143,19 +143,33 @@ usrc_writeUnicodeSet( const USet *pSet, UTargetSyntax syntax); +#ifdef __cplusplus + +U_NAMESPACE_BEGIN + +class U_TOOLUTIL_API ValueNameGetter { +public: + virtual ~ValueNameGetter(); + virtual const char *getName(uint32_t value) = 0; +}; + +U_NAMESPACE_END + /** * Writes the UCPMap ranges list. * - * The "uproperty" argument is optional; ignored if UCHAR_INVALID_CODE. If present, it will be used - * to look up the property value name strings. + * The "valueNameGetter" argument is optional; ignored if nullptr. + * If present, it will be used to look up value name strings. */ U_CAPI void U_EXPORT2 usrc_writeUCPMap( FILE *f, const UCPMap *pMap, - UProperty uproperty, + icu::ValueNameGetter *valueNameGetter, UTargetSyntax syntax); +#endif // __cplusplus + /** * Writes the contents of an array of mostly invariant characters. * Characters 0..0x1f are printed as numbers, -- 2.40.0