Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ The release run heads these entries with the version and opens a fresh
parse refuses with the new code `formulaInvalid` (1012).
- Localized function input accepts lowercase and mixed-case non-ASCII letters,
including German umlauts and Russian names.
- A locale tag with a script (`pt-Latn-BR`), with `_` (`de_CH`) or in another
case (`DE-ch`) now selects the function names, the decimal sign and the
grouping, the month names and the boolean words of its language and region.
Before, the script read as the region, and the other two missed the tables.
- An xlsx formula with a cached string result writes valid formula-result XML.
Rejecting an invalid formula no longer creates an empty cell or grows the sheet.
- `Sheet::set_cell` and the `setCell` op write a formula: `{"type":
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ set(ODR_SOURCE_FILES
"src/odr/internal/util/byte_string.cpp"
"src/odr/internal/util/file_util.cpp"
"src/odr/internal/util/hash_util.cpp"
"src/odr/internal/util/locale_util.cpp"
"src/odr/internal/util/lowercase_table.cpp"
"src/odr/internal/util/number_util.cpp"
"src/odr/internal/util/odr_meta_util.cpp"
"src/odr/internal/util/stream_util.cpp"
Expand Down
182 changes: 0 additions & 182 deletions src/odr/internal/formula/formula_function_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,186 +1540,4 @@ std::span<const formula::FunctionName> formula::function_names() {
return names;
}

char32_t formula::lower_function_letter(const char32_t letter,
const std::string_view language) {
if (language == "tr" && letter == U'I') {
return U'ı';
}
if (language == "tr" && letter == U'İ') {
return U'i';
}
switch (letter) {
case U'A':
return U'a';
case U'B':
return U'b';
case U'C':
return U'c';
case U'D':
return U'd';
case U'E':
return U'e';
case U'F':
return U'f';
case U'G':
return U'g';
case U'H':
return U'h';
case U'I':
return U'i';
case U'J':
return U'j';
case U'K':
return U'k';
case U'L':
return U'l';
case U'M':
return U'm';
case U'N':
return U'n';
case U'O':
return U'o';
case U'P':
return U'p';
case U'Q':
return U'q';
case U'R':
return U'r';
case U'S':
return U's';
case U'T':
return U't';
case U'U':
return U'u';
case U'V':
return U'v';
case U'W':
return U'w';
case U'X':
return U'x';
case U'Y':
return U'y';
case U'Z':
return U'z';
case U'Á':
return U'á';
case U'Ã':
return U'ã';
case U'Ä':
return U'ä';
case U'Å':
return U'å';
case U'Æ':
return U'æ';
case U'Ç':
return U'ç';
case U'É':
return U'é';
case U'Ê':
return U'ê';
case U'Í':
return U'í';
case U'Ñ':
return U'ñ';
case U'Ó':
return U'ó';
case U'Ö':
return U'ö';
case U'Ø':
return U'ø';
case U'Ù':
return U'ù';
case U'Ú':
return U'ú';
case U'Ü':
return U'ü';
case U'Ą':
return U'ą';
case U'Ć':
return U'ć';
case U'Ę':
return U'ę';
case U'Ğ':
return U'ğ';
case U'Ł':
return U'ł';
case U'Ń':
return U'ń';
case U'Ő':
return U'ő';
case U'Ś':
return U'ś';
case U'Ş':
return U'ş';
case U'Ű':
return U'ű';
case U'Ź':
return U'ź';
case U'Ż':
return U'ż';
case U'Ё':
return U'ё';
case U'А':
return U'а';
case U'Б':
return U'б';
case U'В':
return U'в';
case U'Г':
return U'г';
case U'Д':
return U'д';
case U'Е':
return U'е';
case U'Ж':
return U'ж';
case U'З':
return U'з';
case U'И':
return U'и';
case U'Й':
return U'й';
case U'К':
return U'к';
case U'Л':
return U'л';
case U'М':
return U'м';
case U'Н':
return U'н';
case U'О':
return U'о';
case U'П':
return U'п';
case U'Р':
return U'р';
case U'С':
return U'с';
case U'Т':
return U'т';
case U'У':
return U'у';
case U'Ф':
return U'ф';
case U'Х':
return U'х';
case U'Ц':
return U'ц';
case U'Ч':
return U'ч';
case U'Ш':
return U'ш';
case U'Щ':
return U'щ';
case U'Ы':
return U'ы';
case U'Ь':
return U'ь';
case U'Я':
return U'я';
default:
return letter;
}
}

} // namespace odr::internal
5 changes: 0 additions & 5 deletions src/odr/internal/formula/formula_function_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ struct FunctionName final {
/// evaluator implements. `tools/formula/generate_function_names.py` writes it.
[[nodiscard]] std::span<const FunctionName> function_names();

/// @p letter in lowercase, for the letters of the generated names in
/// @p language, as LibreOffice names it.
[[nodiscard]] char32_t lower_function_letter(char32_t letter,
std::string_view language);

/// @p formula with each function name spelled as LibreOffice shows it in the
/// language of the BCP 47 tag @p locale. A name the language keeps stays.
[[nodiscard]] std::string localized_functions(std::string_view formula,
Expand Down
27 changes: 7 additions & 20 deletions src/odr/internal/formula/formula_localize.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
#include <odr/internal/formula/formula_function_names.hpp>

#include <odr/internal/util/locale_util.hpp>
#include <odr/internal/util/string_util.hpp>

#include <optional>
#include <utility>

namespace odr::internal::formula {

namespace {

/// The language of @p locale as LibreOffice names its translations.
std::string language_of(const std::string_view locale) {
if (util::string::equals_ignore_case(locale, "pt-BR")) {
return "pt-BR";
}
std::string language =
util::string::to_lower(locale.substr(0, locale.find('-')));
// `no` is Norwegian, which is written as Bokmål
return language == "no" ? "nb" : language;
}

/// Whether two names in @p language are the same, ignoring case.
bool same_name(std::string_view a, std::string_view b,
const std::string_view language) {
while (!a.empty() && !b.empty()) {
if (lower_function_letter(util::string::next_utf8(a), language) !=
lower_function_letter(util::string::next_utf8(b), language)) {
return false;
}
}
return a.empty() && b.empty();
util::locale::LanguageTag tag = util::locale::LanguageTag::parse(locale);
return tag.language == "pt" && tag.region == "BR" ? "pt-BR"
: std::move(tag.language);
}

/// What a function name is spelled out of: a byte of a letter past ASCII
Expand Down Expand Up @@ -113,7 +99,8 @@ std::string formula::english_functions(const std::string_view formula,
}
for (const FunctionName &entry : function_names()) {
if (entry.language == language &&
same_name(entry.localized, name, language)) {
util::locale::equals_ignore_case(entry.localized, name,
language)) {
return entry.english;
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/odr/internal/number_format/number_format.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <odr/internal/number_format/number_format.hpp>

#include <odr/internal/util/locale_util.hpp>
#include <odr/internal/util/number_util.hpp>
#include <odr/internal/util/string_util.hpp>

Expand Down Expand Up @@ -83,10 +84,8 @@ void append_literal(std::vector<Token> &tokens, const std::string &text) {

/// The names of @p language, or null where the table has none.
const CalendarNames *names_of(const std::string_view language) {
// `no` is Norwegian, which is written as Bokmål
const std::string_view key = language == "no" ? "nb" : language;
const auto found =
std::ranges::find(calendar_names, key, &CalendarNames::language);
std::ranges::find(calendar_names, language, &CalendarNames::language);
return found != std::end(calendar_names) ? &*found : nullptr;
}

Expand Down Expand Up @@ -1172,17 +1171,17 @@ double number_format::serial_from_days(const double days, const Epoch epoch) {

number_format::Symbols
number_format::symbols_of(const std::string_view locale) {
const std::string_view language = locale.substr(0, locale.find('-'));
const std::string_view region = locale.find('-') == std::string_view::npos
? std::string_view()
: locale.substr(locale.rfind('-') + 1);
const util::locale::LanguageTag tag =
util::locale::LanguageTag::parse(locale);
const std::string_view language = tag.language;
const std::string_view region = tag.region;
// CLDR, for the languages a spreadsheet is most often written in
constexpr std::array<std::string_view, 16> point_group{
"de", "es", "it", "nl", "pt", "id", "tr", "da",
"el", "ro", "sl", "hr", "sr", "bs", "vi", "ca"};
constexpr std::array<std::string_view, 18> space_group{
constexpr std::array<std::string_view, 17> space_group{
"fr", "ru", "pl", "cs", "sk", "sv", "fi", "nb", "nn",
"no", "uk", "hu", "bg", "lt", "lv", "et", "be", "kk"};
"uk", "hu", "bg", "lt", "lv", "et", "be", "kk"};
Symbols result;
if ((language == "de" || language == "it") &&
(region == "CH" || region == "LI")) {
Expand Down Expand Up @@ -1214,9 +1213,7 @@ number_format::boolean_words(const std::string_view locale,
(!shown_false.empty() &&
util::string::equals_ignore_case(entry.false_word, shown_false));
};
const std::string_view language = locale.substr(0, locale.find('-'));
// `no` is Norwegian, which is written as Bokmål
const std::string_view key = language == "no" ? "nb" : language;
const std::string key = util::locale::LanguageTag::parse(locale).language;
const BooleanEntry *entry = &boolean_entries.front();
// `FALSO` is Spanish, Italian and Portuguese, so it names no pair
if (std::ranges::count_if(boolean_entries, pairs_shown) == 1) {
Expand Down
72 changes: 72 additions & 0 deletions src/odr/internal/util/locale_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <odr/internal/util/locale_util.hpp>

#include <odr/internal/util/lowercase_table.hpp>
#include <odr/internal/util/string_util.hpp>

#include <algorithm>
#include <span>

namespace odr::internal {

util::locale::LanguageTag
util::locale::LanguageTag::parse(const std::string_view tag) {
LanguageTag result;
std::string_view rest = tag;
for (bool first = true; !rest.empty(); first = false) {
const std::size_t end = rest.find_first_of("-_");
const std::string_view subtag = rest.substr(0, end);
rest = end == std::string_view::npos ? std::string_view()
: rest.substr(end + 1);
const bool letters =
!subtag.empty() &&
std::ranges::all_of(subtag, util::string::is_ascii_letter);
const bool digits =
!subtag.empty() &&
std::ranges::all_of(subtag, util::string::is_ascii_digit);
if (first) {
result.language = util::string::to_lower(subtag);
// `no` is Norwegian, which is written as Bokmål
if (result.language == "no") {
result.language = "nb";
}
} else if (subtag.size() == 1) {
break; // an extension or a private use follows
} else if (subtag.size() == 4 && letters && result.script.empty() &&
result.region.empty()) {
result.script = util::string::to_lower(subtag);
result.script.front() = util::string::to_upper(result.script.front());
} else if (((subtag.size() == 2 && letters) ||
(subtag.size() == 3 && digits)) &&
result.region.empty()) {
for (const char c : subtag) {
result.region += util::string::to_upper(c);
}
}
}
return result;
}

char32_t util::locale::to_lower(const char32_t letter,
const std::string_view language) {
if (letter == U'I' && (language == "tr" || language == "az")) {
return U'ı';
}
const std::span<const LowercasePair> pairs = lowercase_pairs();
const auto found =
std::ranges::lower_bound(pairs, letter, {}, &LowercasePair::letter);
return found != pairs.end() && found->letter == letter ? found->lower
: letter;
}

bool util::locale::equals_ignore_case(std::string_view a, std::string_view b,
const std::string_view language) {
while (!a.empty() && !b.empty()) {
if (to_lower(util::string::next_utf8(a), language) !=
to_lower(util::string::next_utf8(b), language)) {
return false;
}
}
return a.empty() && b.empty();
}

} // namespace odr::internal
Loading
Loading