From 843673fb4a8ea95214f99a9b847e618c2c8f5ae0 Mon Sep 17 00:00:00 2001 From: Craig Cornelius Date: Mon, 3 Jun 2024 14:37:14 -0700 Subject: [PATCH] Clang tidy - initial application (#235) * Add plural rules to CPP * Clang-tidy applied to *.cpp * More style fixes --- executors/cpp/coll.cpp | 30 ++++------- executors/cpp/datetime_fmt.cpp | 12 ++--- executors/cpp/langnames.cpp | 9 +--- executors/cpp/likely_subtags.cpp | 62 +++++++++++----------- executors/cpp/list_fmt.cpp | 19 +++---- executors/cpp/number_fmt.cpp | 90 +++++++++++++------------------- executors/cpp/plural_rules.cpp | 22 ++++---- 7 files changed, 97 insertions(+), 147 deletions(-) diff --git a/executors/cpp/coll.cpp b/executors/cpp/coll.cpp index 37718d1a..f9f59e54 100644 --- a/executors/cpp/coll.cpp +++ b/executors/cpp/coll.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -132,8 +133,7 @@ const string TestCollator(json_object *json_in) { if (rules_string != "") { char uni_rules_out[1000] = ""; - int32_t rule_chars_out = - uni_rules.extract(uni_rules_out, 1000, nullptr, status); + uni_rules.extract(uni_rules_out, 1000, nullptr, status); // ignore length // Make sure normalization is consistent rb_coll = new RuleBasedCollator(uni_rules, UCOL_ON, status); @@ -165,10 +165,9 @@ const string TestCollator(json_object *json_in) { delete rb_coll; } else { // Not a rule-based collator. - if (locale_string == "") { + if (strlen(locale_string) <= 0) { uni_coll = Collator::createInstance(status); } else { - cout << "# Locale set to " << locale_string << endl; uni_coll = Collator::createInstance(Locale(locale_string), status); } @@ -219,20 +218,10 @@ const string TestCollator(json_object *json_in) { } // Just to check the result. - UColAttributeValue alternate_value = - uni_coll->getAttribute(UCOL_ALTERNATE_HANDLING, status); + uni_coll->getAttribute(UCOL_ALTERNATE_HANDLING, status); // ignore return - // Try two differen APIs - uni_result_utf8 = uni_coll->compareUTF8(string1, string2, status); - // This one seems to work better. + // Perform the string comparison uni_result = uni_coll->compare(us1, us2, status); - - if (uni_result != uni_result_utf8) { - cout << "# UNI_COLL COMPARE Unicode String " << uni_result << " "; - cout << "# UNI_COLL COMPARE UTF8 String " << uni_result_utf8 << endl; - cout << "# ******* results different in " << label_string << endl; - } - if (U_FAILURE(status)) { json_object_object_add( return_json, @@ -243,8 +232,7 @@ const string TestCollator(json_object *json_in) { error_message << endl; } if (uni_coll) { - UColAttributeValue alternate_value = - uni_coll->getAttribute(UCOL_ALTERNATE_HANDLING, status); + uni_coll->getAttribute(UCOL_ALTERNATE_HANDLING, status); // ignore result } delete uni_coll; } @@ -259,7 +247,7 @@ const string TestCollator(json_object *json_in) { // Check unescaped versions. char char_out1[1000] = ""; char char_out2[1000] = ""; - int32_t chars_out = us1.extract(char_out1, 1000, nullptr, status); + us1.extract(char_out1, 1000, nullptr, status); // ignore result if (U_FAILURE(status)) { test_result = error_message; json_object_object_add( @@ -270,7 +258,7 @@ const string TestCollator(json_object *json_in) { test_result << endl; } - int32_t chars_out2 = us2.extract(char_out2, 1000, nullptr, status); + us2.extract(char_out2, 1000, nullptr, status); // ignore result if (U_FAILURE(status)) { test_result = error_message; // TODO: report the error in creating the instance @@ -289,7 +277,7 @@ const string TestCollator(json_object *json_in) { json_object_object_add( return_json, "s2", json_object_new_string(string2.c_str())); - // What was the actual returned value? + // Record the actual returned value json_object_object_add( return_json, "compare", json_object_new_int64(uni_result)); } else { diff --git a/executors/cpp/datetime_fmt.cpp b/executors/cpp/datetime_fmt.cpp index 2fc5a4cc..6179d242 100644 --- a/executors/cpp/datetime_fmt.cpp +++ b/executors/cpp/datetime_fmt.cpp @@ -1,9 +1,5 @@ /******************************************************************** - * Comments and license as needed - ************************************ - -/****** - * testing datetime format + * testing icu4c datetime format */ #include @@ -251,11 +247,9 @@ const string TestDatetimeFmt(json_object *json_in) { df->format(test_date_time, formatted_result); // Get the resulting value as a string - string test_result; - int32_t chars_out; // Extracted characters from Unicode string char test_result_string[1000] = ""; - chars_out = formatted_result.extract( - test_result_string, 1000, nullptr, status); + formatted_result.extract( + test_result_string, 1000, nullptr, status); // ignore return value if (U_FAILURE(status)) { json_object_object_add( diff --git a/executors/cpp/langnames.cpp b/executors/cpp/langnames.cpp index 80fa1bd0..38cdff6c 100644 --- a/executors/cpp/langnames.cpp +++ b/executors/cpp/langnames.cpp @@ -1,9 +1,5 @@ /******************************************************************** - * Comments and license as needed - ************************************ - -/****** - * testing language display names + * testing icu4c for language display names */ #include @@ -60,8 +56,7 @@ const string TestLangNames (json_object *json_in) { char test_result_string[1000] = ""; - int32_t chars_out = testLang.extract( - test_result_string, 1000, nullptr, status); + testLang.extract(test_result_string, 1000, nullptr, status); // ignore return if (U_FAILURE(status)) { json_object_object_add( return_json, diff --git a/executors/cpp/likely_subtags.cpp b/executors/cpp/likely_subtags.cpp index bfbe59f7..85db2fac 100644 --- a/executors/cpp/likely_subtags.cpp +++ b/executors/cpp/likely_subtags.cpp @@ -39,11 +39,7 @@ const string TestLikelySubtags(json_object *json_in) { Locale displayLocale(locale_string.c_str()); - const char* test_result; - const string error_message_max = "error in maximize"; - const string error_message_min = "error in minimize"; - const string protected_msg = "This ICU4C API is protected"; - const char* empty_result = ""; + string test_result = ""; json_object *return_json = json_object_new_object(); json_object_object_add(return_json, "label", label_obj); @@ -52,17 +48,13 @@ const string TestLikelySubtags(json_object *json_in) { string name_string; StringByteSink byteSink(&name_string); - // If needed. - json_object *error_msg = json_object_new_object(); - - test_result = empty_result; if (option_string == "maximize") { // This makes the maximized form Locale maximized(displayLocale); maximized.addLikelySubtags(status); if (U_FAILURE(status)) { - test_result = error_message_max.c_str(); + test_result = "error in maximize"; } else { maximized.toLanguageTag(byteSink, status); @@ -72,17 +64,18 @@ const string TestLikelySubtags(json_object *json_in) { "error", json_object_new_string("toLanguageTag")); } - test_result = name_string.c_str(); + test_result = name_string; } } else if (option_string == "minimize" || option_string == "minimizeFavorRegion") { // Minimize displayLocale.minimizeSubtags(status); if (U_FAILURE(status)) { - test_result = error_message_min.c_str(); + const string error_message_min = "error in minimize"; + test_result = error_message_min; } else { displayLocale.toLanguageTag(byteSink, status); - test_result = name_string.c_str(); + test_result = name_string; if (U_FAILURE(status)) { json_object_object_add( @@ -94,18 +87,23 @@ const string TestLikelySubtags(json_object *json_in) { } else if (option_string == "minimizeFavorScript") { // Minimize with script preferred. bool favorScript = true; - json_object_object_add(return_json, - "error", - json_object_new_string("unsupported option")); - json_object_object_add(return_json, - "error_type", - json_object_new_string("unsupported")); - json_object_object_add(return_json, - "unsupported", - json_object_new_string(option_string.c_str())); - json_object_object_add(return_json, - "error_detail", - json_object_new_string(protected_msg.c_str())); + json_object_object_add( + return_json, + "error", + json_object_new_string("unsupported option")); + json_object_object_add( + return_json, + "error_type", + json_object_new_string("unsupported")); + json_object_object_add( + return_json, + "unsupported", + json_object_new_string(option_string.c_str())); + json_object_object_add( + return_json, + "error_detail", + json_object_new_string("This ICU4C API is protected")); + // This is a protected API in ICU4C. // displayLocale.minimizeSubtags(favorScript, status); } else { @@ -121,14 +119,16 @@ const string TestLikelySubtags(json_object *json_in) { } if (U_FAILURE(status)) { - json_object_object_add(return_json, - "error", - json_object_new_string(test_result)); + json_object_object_add( + return_json, + "error", + json_object_new_string(test_result.c_str())); } else { // The output of the likely subtag operation. - json_object_object_add(return_json, - "result", - json_object_new_string(test_result)); + json_object_object_add( + return_json, + "result", + json_object_new_string(test_result.c_str())); } return json_object_to_json_string(return_json); diff --git a/executors/cpp/list_fmt.cpp b/executors/cpp/list_fmt.cpp index a1fbe09c..ece56fbe 100644 --- a/executors/cpp/list_fmt.cpp +++ b/executors/cpp/list_fmt.cpp @@ -1,9 +1,5 @@ /******************************************************************** - * Comments and license as needed - ************************************ - -/****** - * testing list formatting + * testing list formatting in icu4c */ #include @@ -51,7 +47,6 @@ const string TestListFmt (json_object* json_in) { std::vector u_strings; int u_strings_size = 0; if (input_list_obj) { - struct array_list* input_list_array = json_object_get_array(input_list_obj); int input_length = json_object_array_length(input_list_obj); // Construct the list of Unicode Strings @@ -61,7 +56,7 @@ const string TestListFmt (json_object* json_in) { string item_string = json_object_get_string(item); u_strings.push_back(item_string.c_str()); } - u_strings_size = u_strings_size = u_strings.size(); + u_strings_size = u_strings.size(); } else { json_object_object_add( return_json, @@ -110,9 +105,9 @@ const string TestListFmt (json_object* json_in) { UnicodeString *u_array = &u_strings[0]; UnicodeString u_result_string; u_result_string = list_formatter->format(u_array, - u_strings_size, - u_result_string, - status); + u_strings_size, + u_result_string, + status); char test_result_string[1000] = ""; if (U_FAILURE(status)) { @@ -121,8 +116,8 @@ const string TestListFmt (json_object* json_in) { "error", json_object_new_string("calling list format")); } else { - int32_t chars_out = - u_result_string.extract(test_result_string, 1000, nullptr, status); + u_result_string.extract( + test_result_string, 1000, nullptr, status); // result ignored } if (U_FAILURE(status)) { diff --git a/executors/cpp/number_fmt.cpp b/executors/cpp/number_fmt.cpp index f7cc036c..6b1b4cb7 100644 --- a/executors/cpp/number_fmt.cpp +++ b/executors/cpp/number_fmt.cpp @@ -1,10 +1,7 @@ /******************************************************************** - * Comments and license as needed - ************************************ - -/****** - * testing number format + * testing icu4c number format */ + #include #include @@ -56,8 +53,6 @@ using icu::number::Notation; using icu::number::Precision; using icu::number::Scale; -const char error_message[] = "error"; - // Get the integer value of a settting int16_t get_integer_setting(string key_value_string) { int16_t return_val = -1; @@ -215,7 +210,6 @@ const string TestNumfmt(json_object *json_in) { // Other parts of the input. json_object *options_obj = json_object_object_get(json_in, "options"); json_object *skeleton_obj = json_object_object_get(json_in, "skeleton"); - json_object *pattern_obj = json_object_object_get(json_in, "pattern"); // The locale for numbers json_object *locale_label_obj = json_object_object_get(json_in, "locale"); @@ -226,15 +220,6 @@ const string TestNumfmt(json_object *json_in) { const Locale displayLocale(locale_string.c_str()); - // Try using the skeleton to create the formatter - json_object *skelecton_obj = json_object_object_get(json_in, "skeleton"); - UnicodeString unicode_skeleton_string; - string skeleton_string; - if (skeleton_obj) { - skeleton_string = json_object_get_string(skeleton_obj); - unicode_skeleton_string = skeleton_string.c_str(); - } - // Get options json_object *notation_obj; json_object *unit_obj; @@ -339,7 +324,7 @@ const string TestNumfmt(json_object *json_in) { string currencyDisplay_string = json_object_get_string(currencyDisplay_obj); unit_width_setting = UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW; - if (currencyDisplay_string == "narrowSymbol") { + if (currencyDisplay_string == "narrowSymbol") { unit_width_setting = UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW; } else if (currencyDisplay_string == "symbol") { unit_width_setting = UNumberUnitWidth::UNUM_UNIT_WIDTH_SHORT; @@ -441,12 +426,7 @@ const string TestNumfmt(json_object *json_in) { int32_t chars_out; // Results of extracting characters from Unicode string bool no_error = true; - char test_result_string[1000] = ""; - - string test_result; - - // Get the numeric value - double input_double = std::stod(input_string); + char test_result[1000] = ""; LocalizedNumberFormatter nf; if (notation_string == "scientific") { @@ -468,35 +448,38 @@ const string TestNumfmt(json_object *json_in) { } if (skeleton_obj) { - // If present, use the skeleton + // Use the skeleton provided + UnicodeString unicode_skeleton_string; + string skeleton_string = json_object_get_string(skeleton_obj); + unicode_skeleton_string = skeleton_string.c_str(); + nf = NumberFormatter::forSkeleton( unicode_skeleton_string, status).locale(displayLocale); } else { - // Use settings to initialize the formatter - nf = NumberFormatter::withLocale(displayLocale) - .notation(notation_setting) - .decimal(separator_setting) - .precision(precision_setting) - .integerWidth(integerWidth_setting) - .grouping(grouping_setting) - .adoptSymbols(numbering_system) - .roundingMode(rounding_setting) - .scale(scale_setting) - .sign(signDisplay_setting) - .unit(unit_setting) - .unitWidth(unit_width_setting); + // Use settings to initialize the formatter + nf = NumberFormatter::withLocale(displayLocale) + .notation(notation_setting) + .decimal(separator_setting) + .precision(precision_setting) + .integerWidth(integerWidth_setting) + .grouping(grouping_setting) + .adoptSymbols(numbering_system) + .roundingMode(rounding_setting) + .scale(scale_setting) + .sign(signDisplay_setting) + .unit(unit_setting) + .unitWidth(unit_width_setting); } if (U_FAILURE(status)) { - test_result = error_message; - const char* error_name = u_errorName(status); - json_object_object_add( - return_json, - "error", json_object_new_string("error in constructor")); - json_object_object_add( - return_json, - "error_detail", json_object_new_string(error_name)); - no_error = false; + const char* error_name = u_errorName(status); + json_object_object_add( + return_json, + "error", json_object_new_string("error in constructor")); + json_object_object_add( + return_json, + "error_detail", json_object_new_string(error_name)); + no_error = false; } if (no_error) { @@ -512,13 +495,10 @@ const string TestNumfmt(json_object *json_in) { json_object_object_add( return_json, "error_detail", json_object_new_string(error_name)); - no_error = false; } // Get the resulting value as a string - chars_out = number_result.extract( - test_result_string, 1000, nullptr, status); - test_result = test_result_string; + number_result.extract(test_result, 1000, nullptr, status); // ignore result if (U_FAILURE(status)) { // Report a failure @@ -528,20 +508,20 @@ const string TestNumfmt(json_object *json_in) { json_object_new_string("error in string extract")); json_object_object_add( return_json, "error_detail", json_object_new_string(error_name)); - no_error = false; } else { // It worked! json_object_object_add(return_json, "result", - json_object_new_string(test_result.c_str())); + json_object_new_string(test_result)); } } + // To see what was actually used. UnicodeString u_skeleton_out = nf.toSkeleton(status); - chars_out = u_skeleton_out.extract(test_result_string, 1000, nullptr, status); + u_skeleton_out.extract(test_result, 1000, nullptr, status); // result ignored json_object_object_add(return_json, "actual_skeleton", - json_object_new_string(test_result_string)); + json_object_new_string(test_result)); string return_string = json_object_to_json_string(return_json); return return_string; diff --git a/executors/cpp/plural_rules.cpp b/executors/cpp/plural_rules.cpp index dbd2f1d1..33cb5829 100644 --- a/executors/cpp/plural_rules.cpp +++ b/executors/cpp/plural_rules.cpp @@ -1,10 +1,6 @@ /******************************************************************** - * Comments and license as needed - ************************************ - -/****** - * testing plural rules - * Based on code of ICU4C testing: + * testing plural rules in icu4c + * * https://github.com/unicode-org/icu/blob/maint/maint-75/icu4c/source/test/intltest/plurfmts.cpp */ @@ -60,8 +56,9 @@ const string TestPluralRules (json_object* json_in) { string sample_string = json_object_get_string(sample_obj); if (sample_string.find('c') != std::string::npos) { - + // TODO: Handle compact numbers } + if (sample_string.find('.') != std::string::npos) { // Convert into an integer, decimal, or compact decimal input_double_sample = std::stod(sample_string); @@ -137,8 +134,8 @@ const string TestPluralRules (json_object* json_in) { json_object_new_string("calling plural rules select")); return json_object_to_json_string(return_json); } else { - int32_t chars_out = - u_result.extract(test_result_string, 1000, nullptr, status); + u_result.extract( + test_result_string, 1000, nullptr, status); // ignore result } if (U_FAILURE(status)) { @@ -149,9 +146,10 @@ const string TestPluralRules (json_object* json_in) { return json_object_to_json_string(return_json); } else { // It all seems to work! - json_object_object_add(return_json, - "result", - json_object_new_string(test_result_string)); + json_object_object_add( + return_json, + "result", + json_object_new_string(test_result_string)); } // The JSON output.