diff --git a/AGENTS.md b/AGENTS.md index b83c47f3c..7dadbb541 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -167,6 +167,9 @@ cmake --build cmake-build-relwithdebinfo --target translate # CLI: file to HTML for index-like values (`std::size_t`) or where an API forces them. - Use `std::array`, never a C array. - Mark locals, parameters and members `const` where they do not mutate. +- For `std::optional` and similar value-or-error types, test presence with + `.has_value()`. Access the value with `.value()`, or use `*` and `->` where + they read more naturally. - Prefer `std::ranges` algorithms and range-based loops over iterator pairs. - Define a header-declared free function with its qualified name inside the reopened namespace, never as a bare redeclaration. A signature that drifts diff --git a/apple/src/ODRDocument.mm b/apple/src/ODRDocument.mm index 9f31b8886..77287b8b0 100644 --- a/apple/src/ODRDocument.mm +++ b/apple/src/ODRDocument.mm @@ -118,7 +118,7 @@ - (NSString *)locale { return guarded_value( [&]() -> NSString * { const std::optional locale = _handle->locale(); - return locale ? odr::apple::to_nsstring(*locale) : nil; + return locale.has_value() ? odr::apple::to_nsstring(*locale) : nil; }, nil); } diff --git a/cli/src/meta.cpp b/cli/src/meta.cpp index 27978d690..f77d4e465 100644 --- a/cli/src/meta.cpp +++ b/cli/src/meta.cpp @@ -29,7 +29,7 @@ int main(const int argc, char **argv) { DecodedFile file = open(input); if (file.password_encrypted()) { - if (!password) { + if (!password.has_value()) { ODR_FATAL(logger, "document encrypted but no password given"); return 2; } diff --git a/cli/src/server.cpp b/cli/src/server.cpp index 5cd329f96..798a9adf6 100644 --- a/cli/src/server.cpp +++ b/cli/src/server.cpp @@ -31,7 +31,7 @@ int main(const int argc, char **argv) { DecodedFile decoded_file = open(input, {}, logger); if (decoded_file.password_encrypted()) { - if (!password) { + if (!password.has_value()) { ODR_FATAL(logger, "document encrypted but no password given"); return 2; } diff --git a/cli/src/translate.cpp b/cli/src/translate.cpp index 134bdc77e..1772929bd 100644 --- a/cli/src/translate.cpp +++ b/cli/src/translate.cpp @@ -30,7 +30,7 @@ int main(const int argc, char **argv) { DecodedFile decoded_file = open(input); if (decoded_file.password_encrypted()) { - if (!password) { + if (!password.has_value()) { ODR_FATAL(logger, "document encrypted but no password given"); return 2; } diff --git a/src/odr/document_element.cpp b/src/odr/document_element.cpp index 05a470aa6..a253bf8f0 100644 --- a/src/odr/document_element.cpp +++ b/src/odr/document_element.cpp @@ -433,12 +433,15 @@ namespace { void refuse_unwritable(const TableCellStyle &cell_style, const TextStyle &text_style) { const auto stated = [](const auto &sides) { - return sides.right || sides.top || sides.left || sides.bottom; + return sides.right.has_value() || sides.top.has_value() || + sides.left.has_value() || sides.bottom.has_value(); }; - if (cell_style.vertical_align || stated(cell_style.padding) || - stated(cell_style.border) || cell_style.text_rotation || - cell_style.wrap_text || text_style.font_name || text_style.font_shadow || - text_style.background_color || text_style.font_position) { + if (cell_style.vertical_align.has_value() || stated(cell_style.padding) || + stated(cell_style.border) || cell_style.text_rotation.has_value() || + cell_style.wrap_text.has_value() || text_style.font_name.has_value() || + text_style.font_shadow.has_value() || + text_style.background_color.has_value() || + text_style.font_position.has_value()) { throw UnsupportedOperation(); } } diff --git a/src/odr/file.cpp b/src/odr/file.cpp index 4f63245e2..9ea0eb59c 100644 --- a/src/odr/file.cpp +++ b/src/odr/file.cpp @@ -82,7 +82,8 @@ std::size_t File::size() const { return deref(m_impl).size(); } std::string File::name() const { return deref(m_impl).name(); } std::optional File::disk_path() const { - if (const std::optional path = deref(m_impl).disk_path()) { + if (const std::optional path = deref(m_impl).disk_path(); + path.has_value()) { return path->string(); } return {}; diff --git a/src/odr/internal/common/list_numbering.cpp b/src/odr/internal/common/list_numbering.cpp index 1a5f2c2fe..e0cafad66 100644 --- a/src/odr/internal/common/list_numbering.cpp +++ b/src/odr/internal/common/list_numbering.cpp @@ -87,7 +87,7 @@ std::string ListCounter::advance(const std::uint32_t level, if (number == std::numeric_limits::max()) { throw std::overflow_error("List counter overflow"); } - number = number ? *number + 1 : list_level.start; + number = number.has_value() ? *number + 1 : list_level.start; m_formats[level] = list_level.format; std::ranges::fill(m_numbers | std::views::drop(static_cast(level) + 1), diff --git a/src/odr/internal/common/sheet_cell_source.cpp b/src/odr/internal/common/sheet_cell_source.cpp index f3156fb9d..188f4d926 100644 --- a/src/odr/internal/common/sheet_cell_source.cpp +++ b/src/odr/internal/common/sheet_cell_source.cpp @@ -21,7 +21,8 @@ std::optional value_of(const CellValue &value, if (value.has_formula() && value.type() == ValueType::float_number && value.has_text()) { if (const std::optional error = - formula::error_of_text(value.text())) { + formula::error_of_text(value.text()); + error.has_value()) { return Value{*error}; } if (value.text().starts_with('#') || value.text().starts_with("Err:")) { @@ -55,7 +56,8 @@ std::optional value_of(const CellValue &value, case ValueType::error: if (const std::optional error = value.has_text() ? formula::error_of_text(value.text()) - : std::nullopt) { + : std::nullopt; + error.has_value()) { return Value{*error}; } return std::nullopt; @@ -129,7 +131,8 @@ TableDimensions SheetCellSource::extent(const std::uint32_t sheet) const { } // the stated cells, a formula without a result too, which shows no content TableDimensions extent; - if (const std::optional> &sheet_bands = bands(sheet)) { + if (const std::optional> &sheet_bands = bands(sheet); + sheet_bands.has_value()) { for (const RowBand &band : *sheet_bands) { extent.rows = std::max(extent.rows, band.end_row); for (const CellRun &run : band.cells) { diff --git a/src/odr/internal/common/sheet_recalculation.cpp b/src/odr/internal/common/sheet_recalculation.cpp index 6772a32f4..b4bd877be 100644 --- a/src/odr/internal/common/sheet_recalculation.cpp +++ b/src/odr/internal/common/sheet_recalculation.cpp @@ -148,7 +148,8 @@ class StaleCells final : public formula::CellSource { std::vector> pending{{position, 0}}; while (true) { const std::optional result = cell(pending.back().first); - if (const auto frontier = std::exchange(m_frontier, std::nullopt)) { + if (const auto frontier = std::exchange(m_frontier, std::nullopt); + frontier.has_value()) { const std::size_t previous = m_ancestors.size(); m_ancestors.insert(m_ancestors.end(), m_frontier_path.begin(), m_frontier_path.end()); diff --git a/src/odr/internal/csv/csv_document.cpp b/src/odr/internal/csv/csv_document.cpp index 4f5820bdc..f75b013ec 100644 --- a/src/odr/internal/csv/csv_document.cpp +++ b/src/odr/internal/csv/csv_document.cpp @@ -245,7 +245,8 @@ class ElementAdapter final : public AdapterBase { CellValue result = CellValue(sheet_cell_value_type(element_id)); if (result.type() == ValueType::float_number) { if (const std::optional number = util::number::parse( - m_document->cell(column_of(element_id), row_of(element_id)))) { + m_document->cell(column_of(element_id), row_of(element_id))); + number.has_value()) { result = result.with_number(*number); } } diff --git a/src/odr/internal/font/cff_builder.cpp b/src/odr/internal/font/cff_builder.cpp index 1bd5a7423..e14ce9246 100644 --- a/src/odr/internal/font/cff_builder.cpp +++ b/src/odr/internal/font/cff_builder.cpp @@ -51,7 +51,8 @@ void dict_number(std::string &out, const double value) { throw std::runtime_error("cff: non-finite DICT operand"); } if (const std::optional integer = - util::number::to_integer(value)) { + util::number::to_integer(value); + integer.has_value()) { dict_int(out, *integer); return; } diff --git a/src/odr/internal/font/cff_font.cpp b/src/odr/internal/font/cff_font.cpp index be22eb5c9..9d8cf261b 100644 --- a/src/odr/internal/font/cff_font.cpp +++ b/src/odr/internal/font/cff_font.cpp @@ -110,7 +110,7 @@ std::string_view checked_slice(const std::string_view bytes, std::uint32_t dict_offset(const double value) { const std::optional result = util::number::to_integer(value); - if (!result) { + if (!result.has_value()) { throw std::runtime_error("cff: invalid DICT offset or length"); } return *result; @@ -177,7 +177,8 @@ using Dict = std::map>; } // An unreadable real reads as zero, so it does not refuse the font. const std::optional value = util::number::parse(number); - operands.push_back(value && std::isfinite(*value) ? *value : 0.0); + operands.push_back(value.has_value() && std::isfinite(*value) ? *value + : 0.0); } else if (b0 >= 32 && b0 <= 246) { operands.push_back(static_cast(b0) - 139); ++p; diff --git a/src/odr/internal/font/type1_charstring.cpp b/src/odr/internal/font/type1_charstring.cpp index e6c0a4a55..0eeda40a7 100644 --- a/src/odr/internal/font/type1_charstring.cpp +++ b/src/odr/internal/font/type1_charstring.cpp @@ -83,7 +83,7 @@ void emit_num(std::string &out, const double v) { } const std::optional scaled = util::number::to_integer(std::round(v * 65536.0)); - if (!scaled) { + if (!scaled.has_value()) { throw std::runtime_error("type1: operand exceeds Type2 fixed-point range"); } const std::int32_t fixed = *scaled; @@ -97,7 +97,7 @@ void emit_num(std::string &out, const double v) { std::int32_t integer_operand(const double value) { const std::optional result = util::number::to_integer(value); - if (!result) { + if (!result.has_value()) { throw std::runtime_error("type1: invalid integer operand"); } return *result; diff --git a/src/odr/internal/font/type1_crypt.cpp b/src/odr/internal/font/type1_crypt.cpp index a5764251c..aba465954 100644 --- a/src/odr/internal/font/type1_crypt.cpp +++ b/src/odr/internal/font/type1_crypt.cpp @@ -23,7 +23,7 @@ constexpr std::uint16_t c2 = 22719; continue; } const std::optional value = util::string::hex_digit(ch); - if (!value) { + if (!value.has_value()) { break; } if (high < 0) { @@ -44,7 +44,7 @@ constexpr std::uint16_t c2 = 22719; if (util::string::is_ascii_whitespace(ch)) { continue; } - if (!util::string::hex_digit(ch)) { + if (!util::string::hex_digit(ch).has_value()) { return false; } if (++seen == 4) { diff --git a/src/odr/internal/font/type1_font.cpp b/src/odr/internal/font/type1_font.cpp index 22bef94f3..308c4cc44 100644 --- a/src/odr/internal/font/type1_font.cpp +++ b/src/odr/internal/font/type1_font.cpp @@ -46,7 +46,7 @@ namespace { [[nodiscard]] double parse_double(const std::string_view token) { // An unreadable number reads as zero, so it does not refuse the font. const std::optional value = util::number::parse(token); - return value && std::isfinite(*value) ? *value : 0.0; + return value.has_value() && std::isfinite(*value) ? *value : 0.0; } /// A `/FontBBox` number as an FWord. Clamping keeps an out-of-range number out @@ -91,7 +91,7 @@ read_rd_binary(const std::string_view s, std::size_t &p) { std::size_t q = p; const std::string_view length_token = read_token(s, q); const auto length = util::number::parse_integer(length_token); - if (!length || *length < 0) { + if (!length.has_value() || *length < 0) { return std::nullopt; } const std::string_view rd = read_token(s, q); // "RD" or "-|" @@ -209,7 +209,7 @@ void Type1Font::parse_clear(const std::string_view clear) { const std::size_t slash = after.find('/', q); if (const auto code = util::number::parse_integer(code_token); - code && slash != std::string_view::npos) { + code.has_value() && slash != std::string_view::npos) { std::size_t r = slash + 1; m_encoding[*code] = std::string(read_token(after, r)); } @@ -224,8 +224,9 @@ void Type1Font::parse_private(const std::string_view decrypted) { if (const std::size_t k = decrypted.find("/lenIV"); k != std::string_view::npos) { std::size_t p = k + 6; - if (const auto value = util::number::parse_integer( - read_token(decrypted, p))) { + if (const auto value = + util::number::parse_integer(read_token(decrypted, p)); + value.has_value()) { if (*value < -1) { throw std::runtime_error("type1: invalid /lenIV"); } @@ -254,7 +255,7 @@ void Type1Font::parse_private(const std::string_view decrypted) { util::number::parse_integer(read_token(decrypted, q)); // Every subr needs at least one byte of input, so an index at or past the // input size cannot name one — and must not size the vector. - if (!index || *index < 0 || + if (!index.has_value() || *index < 0 || static_cast(*index) >= decrypted.size()) { p += 4; continue; diff --git a/src/odr/internal/formula/formula_functions_logic.cpp b/src/odr/internal/formula/formula_functions_logic.cpp index 934f80f9e..7a9900184 100644 --- a/src/odr/internal/formula/formula_functions_logic.cpp +++ b/src/odr/internal/formula/formula_functions_logic.cpp @@ -101,7 +101,8 @@ Value combine(const Call &call, const bool start, const Combine &next) { errors.add(std::get(truth_of_text(*text))); } }); - if (const std::optional error = errors.first()) { + if (const std::optional error = errors.first(); + error.has_value()) { return Value{*error}; } } diff --git a/src/odr/internal/formula/formula_functions_lookup.cpp b/src/odr/internal/formula/formula_functions_lookup.cpp index 5a4f51ce4..1d4047d68 100644 --- a/src/odr/internal/formula/formula_functions_lookup.cpp +++ b/src/odr/internal/formula/formula_functions_lookup.cpp @@ -361,7 +361,7 @@ Value matched(const Call &call, const std::span conditions, errors.add(*error); } }); - if (const auto error = errors.first()) { + if (const auto error = errors.first(); error.has_value()) { throw ErrorResult{*error}; } if (average && numbers.empty()) { diff --git a/src/odr/internal/formula/formula_functions_math.cpp b/src/odr/internal/formula/formula_functions_math.cpp index 459f08da6..9a94bc544 100644 --- a/src/odr/internal/formula/formula_functions_math.cpp +++ b/src/odr/internal/formula/formula_functions_math.cpp @@ -268,7 +268,8 @@ std::optional no_numbers(const Call &call, const Collected &collected) { Value median(const Call &call) { Collected collected = collect(call); - if (const std::optional refused = no_numbers(call, collected)) { + if (const std::optional refused = no_numbers(call, collected); + refused.has_value()) { return *refused; } std::vector &numbers = collected.numbers; @@ -285,7 +286,8 @@ Value median(const Call &call) { template Value kth(const Call &call) { expect_arguments(call, 2, 2); Collected collected = collect(call, 1); - if (const std::optional refused = no_numbers(call, collected)) { + if (const std::optional refused = no_numbers(call, collected); + refused.has_value()) { return *refused; } double rank = number_argument(call, 1); @@ -435,7 +437,8 @@ Value sum_product(const Call &call) { products[i] *= factor; } } - if (const std::optional error = errors.first()) { + if (const std::optional error = errors.first(); + error.has_value()) { return Value{*error}; } return sum_of(call, products); diff --git a/src/odr/internal/formula/formula_functions_text.cpp b/src/odr/internal/formula/formula_functions_text.cpp index ebb328426..8d6f7fc3d 100644 --- a/src/odr/internal/formula/formula_functions_text.cpp +++ b/src/odr/internal/formula/formula_functions_text.cpp @@ -142,7 +142,8 @@ Value concat(const Call &call) { throw NoAnswer{}; } }); - if (const std::optional error = errors.first()) { + if (const std::optional error = errors.first(); + error.has_value()) { return Value{*error}; } } diff --git a/src/odr/internal/formula/formula_text.cpp b/src/odr/internal/formula/formula_text.cpp index 10f30543d..8b39294ae 100644 --- a/src/odr/internal/formula/formula_text.cpp +++ b/src/odr/internal/formula/formula_text.cpp @@ -68,7 +68,8 @@ std::optional mapped(const char16_t c, const bool to_upper) { {0x14A, 0x177, false}, {0x179, 0x17E, true}}) { if (const std::optional other = - alternating(c, first, last, parity, to_upper)) { + alternating(c, first, last, parity, to_upper); + other.has_value()) { return other; } } diff --git a/src/odr/internal/formula/formula_value.cpp b/src/odr/internal/formula/formula_value.cpp index e58478f88..f7060714c 100644 --- a/src/odr/internal/formula/formula_value.cpp +++ b/src/odr/internal/formula/formula_value.cpp @@ -127,7 +127,8 @@ formula::number_of_text(const std::string_view text) { } const bool percent = trimmed.ends_with('%'); if (const std::optional number = plain_number( - percent ? trimmed.substr(0, trimmed.size() - 1) : trimmed)) { + percent ? trimmed.substr(0, trimmed.size() - 1) : trimmed); + number.has_value()) { // a point is the decimal sign of some locales only if (trimmed.find('.') != std::string_view::npos) { return std::nullopt; diff --git a/src/odr/internal/html/common.cpp b/src/odr/internal/html/common.cpp index 617c6e375..f1b01c55a 100644 --- a/src/odr/internal/html/common.cpp +++ b/src/odr/internal/html/common.cpp @@ -377,7 +377,7 @@ html::fill_path_variables(const std::string &path, const std::optional index) { std::string result = path; util::string::replace_all(result, "{index}", - index ? std::to_string(*index) : ""); + index.has_value() ? std::to_string(*index) : ""); return result; } diff --git a/src/odr/internal/html/document_element.cpp b/src/odr/internal/html/document_element.cpp index a1f5d3621..6b071ca8e 100644 --- a/src/odr/internal/html/document_element.cpp +++ b/src/odr/internal/html/document_element.cpp @@ -159,12 +159,12 @@ TableDimensions html::sheet_rendered_extent(const Sheet &sheet, end_column = content.columns; end_row = content.rows; } - if (config.spreadsheet_limit) { + if (config.spreadsheet_limit.has_value()) { end_column = std::min(end_column, config.spreadsheet_limit->columns); end_row = std::min(end_row, config.spreadsheet_limit->rows); } end_column = std::max(1u, end_column); - if (config.spreadsheet_cell_limit) { + if (config.spreadsheet_cell_limit.has_value()) { const std::uint64_t limit = std::max(1, *config.spreadsheet_cell_limit); end_column = @@ -1175,7 +1175,7 @@ void html::translate_image(const Element &element, const WritingState &state) { path, image.file(), false, false, true); resource_location = state.config().resource_locator(resource, state.config()); - if (resource_location) { + if (resource_location.has_value()) { // a location taken by anything but this same image embeds instead const odr::HtmlResource *existing = resource_at(state.resources(), *resource_location); diff --git a/src/odr/internal/html/document_style.cpp b/src/odr/internal/html/document_style.cpp index 7117d9aed..1821ddb77 100644 --- a/src/odr/internal/html/document_style.cpp +++ b/src/odr/internal/html/document_style.cpp @@ -201,9 +201,9 @@ std::string html::translate_text_style(const TextStyle &text_style) { } // one declaration: a second `text-decoration` replaces the first const bool underline = - text_style.font_underline && *text_style.font_underline; + text_style.font_underline.has_value() && *text_style.font_underline; const bool line_through = - text_style.font_line_through && *text_style.font_line_through; + text_style.font_line_through.has_value() && *text_style.font_line_through; if (underline || line_through) { result += "text-decoration:"; if (underline) { diff --git a/src/odr/internal/html/filesystem.cpp b/src/odr/internal/html/filesystem.cpp index f272adc9e..4bdeb5673 100644 --- a/src/odr/internal/html/filesystem.cpp +++ b/src/odr/internal/html/filesystem.cpp @@ -260,8 +260,9 @@ class HtmlServiceImpl final : public HtmlService { "a", HtmlElementOptions() .set_inline(true) .set_extra(std::string( - location ? link_target_attributes(uri_kind(*location)) - : std::string_view())) + location.has_value() + ? link_target_attributes(uri_kind(*location)) + : std::string_view())) .set_attributes(HtmlAttributesVector{ {"href", *href}, {"download", xml::escape_attribute(name)}, diff --git a/src/odr/internal/html/font_file.cpp b/src/odr/internal/html/font_file.cpp index 6a488351f..5eb791052 100644 --- a/src/odr/internal/html/font_file.cpp +++ b/src/odr/internal/html/font_file.cpp @@ -150,7 +150,7 @@ class HtmlServiceImpl final : public HtmlService { out.out() << "
&#x" << std::hex << static_cast(font::pua_code_point(gid)) << std::dec << ";" << gid; - if (const auto cp = font->code_point_for_glyph(gid)) { + if (const auto cp = font->code_point_for_glyph(gid); cp.has_value()) { out.out() << " U+" << std::hex << std::uppercase << static_cast(*cp) << std::nouppercase << std::dec; diff --git a/src/odr/internal/html/html_service.cpp b/src/odr/internal/html/html_service.cpp index 60d686277..98b6f225b 100644 --- a/src/odr/internal/html/html_service.cpp +++ b/src/odr/internal/html/html_service.cpp @@ -95,7 +95,7 @@ namespace odr::internal { bool html::resource_location_taken(const HtmlResources &resources, const std::string_view location) { return std::ranges::any_of(resources, [&](const auto &entry) { - return entry.second && + return entry.second.has_value() && util::string::equals_ignore_case(*entry.second, location); }); } diff --git a/src/odr/internal/html/html_writer.cpp b/src/odr/internal/html/html_writer.cpp index d02378c0e..ad31e21b8 100644 --- a/src/odr/internal/html/html_writer.cpp +++ b/src/odr/internal/html/html_writer.cpp @@ -65,13 +65,13 @@ void write_attributes(std::ostream &out, const HtmlAttributes &attributes) { void write_element_options(std::ostream &out, const HtmlElementOptions &options) { - const bool has_clazz = options.clazz && !is_empty(*options.clazz); - if (has_clazz || options.style_class) { + const bool has_clazz = options.clazz.has_value() && !is_empty(*options.clazz); + if (has_clazz || options.style_class.has_value()) { out << " class=\""; if (has_clazz) { write_writable(out, *options.clazz); } - if (options.style_class) { + if (options.style_class.has_value()) { if (has_clazz) { out << " "; } @@ -79,15 +79,15 @@ void write_element_options(std::ostream &out, } out << "\""; } - if (options.style && !is_empty(*options.style)) { + if (options.style.has_value() && !is_empty(*options.style)) { out << " style=\""; write_writable(out, *options.style); out << "\""; } - if (options.attributes) { + if (options.attributes.has_value()) { write_attributes(out, *options.attributes); } - if (options.extra && !is_empty(*options.extra)) { + if (options.extra.has_value() && !is_empty(*options.extra)) { out << " "; write_writable(out, *options.extra); } diff --git a/src/odr/internal/html/media_file.cpp b/src/odr/internal/html/media_file.cpp index 09108cf5f..27f91654f 100644 --- a/src/odr/internal/html/media_file.cpp +++ b/src/odr/internal/html/media_file.cpp @@ -97,8 +97,9 @@ class HtmlServiceImpl final : public HtmlService { m_media_file.file(), false, false, true); HtmlResourceLocation location = this->config().resource_locator(resource, this->config()); - if (location && (util::string::equals_ignore_case(*location, m_view_path) || - resource_location_taken(m_resources, *location))) { + if (location.has_value() && + (util::string::equals_ignore_case(*location, m_view_path) || + resource_location_taken(m_resources, *location))) { location.reset(); } m_resources.emplace_back(resource, std::move(location)); diff --git a/src/odr/internal/html/pdf_file.cpp b/src/odr/internal/html/pdf_file.cpp index 82f82d0de..4be86e3c9 100644 --- a/src/odr/internal/html/pdf_file.cpp +++ b/src/odr/internal/html/pdf_file.cpp @@ -252,15 +252,16 @@ std::vector collect_page_links(const pdf::Page &page, target_attributes = target.attributes; } } else if (kind == "GoTo" && a.has_value("D")) { - if (const auto index = resolver.resolve_dest_page(a.get("D"))) { + if (const auto index = resolver.resolve_dest_page(a.get("D")); + index.has_value()) { href = page_href(*index); } } } } if (href.empty() && dictionary.has_value("Dest")) { - if (const auto index = - resolver.resolve_dest_page(dictionary.get("Dest"))) { + if (const auto index = resolver.resolve_dest_page(dictionary.get("Dest")); + index.has_value()) { href = page_href(*index); } } @@ -1586,7 +1587,8 @@ class HtmlServiceImpl final : public HtmlService { const double vis_local = local_scale(font_size_pt); std::ostringstream key; key << font << '|' << font_size_pt << '|' << round2(asc * text.size); - if (font == 0 && text.font != nullptr && text.font->substitute) { + if (font == 0 && text.font != nullptr && + text.font->substitute.has_value()) { key << '|' << font_substitute_declaration(*text.font->substitute); } const std::string vis_flow_key = std::move(key).str(); @@ -1621,7 +1623,8 @@ class HtmlServiceImpl final : public HtmlService { if (font != 0) { run_classes += ' '; run_classes += font_class(font_class_used, font, invisible); - } else if (text.font != nullptr && text.font->substitute) { + } else if (text.font != nullptr && + text.font->substitute.has_value()) { // Non-embedded: real Unicode in the substitute family, whose // metric-overriding face pins the baseline to `asc`. add_class( @@ -1697,7 +1700,7 @@ class HtmlServiceImpl final : public HtmlService { const double unit_font_pt = is_matrix ? text.size * scale : font_pt; double unit_ox = is_matrix ? 0 : ox; double unit_baseline = is_matrix ? 0 : baseline; - if (sel_matrix_origin) { + if (sel_matrix_origin.has_value()) { unit_ox = (*sel_matrix_origin)[0]; unit_baseline = (*sel_matrix_origin)[1]; } @@ -1810,7 +1813,7 @@ class HtmlServiceImpl final : public HtmlService { if (const double owed = round2(sel_pending_space); owed > 0 && sel_prev_ends_space && !runs.empty()) { sel_tail_width_pt = round2(sel_tail_width_pt + owed); - if (sel_tail_gap_font_size_pt) { + if (sel_tail_gap_font_size_pt.has_value()) { runs.back().classes = gap_classes(*sel_tail_gap_font_size_pt, sel_tail_width_pt); } else { @@ -2301,7 +2304,7 @@ class HtmlServiceImpl final : public HtmlService { // a Times run never share one line block's `font_class`. const std::string substitute_declaration = (font == 0 && !invisible && text.font != nullptr && - text.font->substitute) + text.font->substitute.has_value()) ? substitute_faces.declaration(*text.font->substitute, asc) : std::string(); std::ostringstream fk; @@ -3027,7 +3030,7 @@ class HtmlServiceImpl final : public HtmlService { /// the `@font-face` descent can make the two sum to it. static double ascent_em(const pdf::Font *font) { double em = 0.8; - if (font != nullptr && font->descriptor_ascent) { + if (font != nullptr && font->descriptor_ascent.has_value()) { em = *font->descriptor_ascent; } else if (font != nullptr && font->embedded_font != nullptr) { const std::uint16_t units = font->embedded_font->units_per_em(); diff --git a/src/odr/internal/markdown/markdown_parser.cpp b/src/odr/internal/markdown/markdown_parser.cpp index ef629ca50..9bfb0ed5f 100644 --- a/src/odr/internal/markdown/markdown_parser.cpp +++ b/src/odr/internal/markdown/markdown_parser.cpp @@ -42,7 +42,7 @@ std::string resolve_entity(const std::string_view entity) { const std::string_view digits = name.substr(hexadecimal ? 2 : 1); const auto code_point = util::number::parse_integer( digits, {.base = hexadecimal ? 16 : 10}); - if (!code_point) { + if (!code_point.has_value()) { return std::string(entity); } // an unpaired surrogate or an out-of-range value is not a character diff --git a/src/odr/internal/number_format/number_format.cpp b/src/odr/internal/number_format/number_format.cpp index 62b2d0e9c..e1bf68052 100644 --- a/src/odr/internal/number_format/number_format.cpp +++ b/src/odr/internal/number_format/number_format.cpp @@ -148,7 +148,7 @@ void parse_bracket(const std::string_view content, Section §ion) { } const std::optional operand = util::number::parse(content.substr(length)); - if (!operand) { + if (!operand.has_value()) { throw std::invalid_argument("condition without a number"); } condition.operand = *operand; @@ -167,7 +167,8 @@ void parse_bracket(const std::string_view content, Section §ion) { ? std::string_view() : content.substr(dash + 1); if (const auto lcid = - util::number::parse_integer(id, {.base = 16})) { + util::number::parse_integer(id, {.base = 16}); + lcid.has_value()) { section.names = names_of(language_of(*lcid & 0xffff)); } return; @@ -681,7 +682,7 @@ std::string format_fraction(const std::vector &tokens, std::int64_t denominator = 1; if (fixed_denominator) { const auto parsed = util::number::parse_integer(fixed); - if (!parsed) { + if (!parsed.has_value()) { return format_general(value, symbols); } denominator = *parsed; @@ -1005,13 +1006,13 @@ std::string Format::format(const double value, const Epoch epoch, chosen = count - 1; for (std::size_t i = 0; i < count; ++i) { const std::optional &condition = m_sections[i].condition; - if (!condition || condition->matches(value)) { + if (!condition.has_value() || condition->matches(value)) { chosen = i; break; } } const std::optional &condition = m_sections[chosen].condition; - negative_section = condition && + negative_section = condition.has_value() && (condition->op == Condition::Operator::less || condition->op == Condition::Operator::less_equal) && condition->operand <= 0; diff --git a/src/odr/internal/odf/odf_chart.cpp b/src/odr/internal/odf/odf_chart.cpp index 55dd37a23..ccf7887f9 100644 --- a/src/odr/internal/odf/odf_chart.cpp +++ b/src/odr/internal/odf/odf_chart.cpp @@ -223,7 +223,7 @@ class ChartWriter { if (const pugi::xml_node header = table.child("table:table-header-rows").child("table:table-row")) { const auto cells = read_row(header); - if (!cells) { + if (!cells.has_value()) { return false; } labels = *cells | std::views::transform(read_text) | @@ -236,7 +236,7 @@ class ChartWriter { const auto cells = read_row(row); const std::uint32_t repeated = row.attribute("table:number-rows-repeated").as_uint(1); - if (!cells || repeated == 0 || + if (!cells.has_value() || repeated == 0 || repeated > max_cells - m_categories.size()) { return false; } diff --git a/src/odr/internal/odf/odf_document.cpp b/src/odr/internal/odf/odf_document.cpp index 738470108..12534ec8d 100644 --- a/src/odr/internal/odf/odf_document.cpp +++ b/src/odr/internal/odf/odf_document.cpp @@ -329,8 +329,8 @@ std::optional date_days(const std::string_view text) { util::number::parse_integer(text.substr(5, 2)); const auto day = util::number::parse_integer(text.substr(8, 2)); - if (!year || !month || !day || *month < 1 || *month > 12 || *day < 1 || - *day > 31) { + if (!year.has_value() || !month.has_value() || !day.has_value() || + *month < 1 || *month > 12 || *day < 1 || *day > 31) { return std::nullopt; } const double result = @@ -351,7 +351,7 @@ std::optional date_days(const std::string_view text) { seconds.remove_suffix(1); } const std::optional second = util::number::parse(seconds); - if (!hours || !minutes || !second) { + if (!hours.has_value() || !minutes.has_value() || !second.has_value()) { return std::nullopt; } return result + (*hours * 3600.0 + *minutes * 60.0 + *second) / 86400; @@ -381,7 +381,7 @@ std::optional duration_days(std::string_view text) { unit == std::string_view::npos ? std::nullopt : util::number::parse(text.substr(0, unit)); - if (!amount) { + if (!amount.has_value()) { return std::nullopt; } switch (text[unit]) { @@ -885,7 +885,8 @@ class ElementAdapter final : public AdapterBase { } std::string text; if (const std::optional shown = - shown_value(element_id, cell_id, position, typed)) { + shown_value(element_id, cell_id, position, typed); + shown.has_value()) { text = *shown; } else if (typed.type() == ValueType::boolean) { text = typed.number() != 0 ? "TRUE" : "FALSE"; @@ -1709,7 +1710,8 @@ class ElementAdapter final : public AdapterBase { ? date_days(node.attribute("office:date-value").value()) : result.type() == ValueType::time ? duration_days(node.attribute("office:time-value").value()) - : util::number::parse(node.attribute("office:value").value())) { + : util::number::parse(node.attribute("office:value").value()); + number.has_value()) { result = result.with_number(*number); } if (result.type() == ValueType::boolean) { @@ -2008,10 +2010,12 @@ class ElementAdapter final : public AdapterBase { frame_x(const ElementIdentifier element_id) const override { const pugi::xml_node node = get_node(element_id); if (const std::optional measure = - read_measure(node.attribute("svg:x"))) { + read_measure(node.attribute("svg:x")); + measure.has_value()) { return measure; } - if (const std::optional box = connector_box(node)) { + if (const std::optional box = connector_box(node); + box.has_value()) { return hundredth_millimetres(box->x); } return {}; @@ -2020,10 +2024,12 @@ class ElementAdapter final : public AdapterBase { frame_y(const ElementIdentifier element_id) const override { const pugi::xml_node node = get_node(element_id); if (const std::optional measure = - read_measure(node.attribute("svg:y"))) { + read_measure(node.attribute("svg:y")); + measure.has_value()) { return measure; } - if (const std::optional box = connector_box(node)) { + if (const std::optional box = connector_box(node); + box.has_value()) { return hundredth_millimetres(box->y); } return {}; @@ -2032,10 +2038,12 @@ class ElementAdapter final : public AdapterBase { frame_width(const ElementIdentifier element_id) const override { const pugi::xml_node node = get_node(element_id); if (const std::optional measure = - read_measure(node.attribute("svg:width"))) { + read_measure(node.attribute("svg:width")); + measure.has_value()) { return measure; } - if (const std::optional box = connector_box(node)) { + if (const std::optional box = connector_box(node); + box.has_value()) { return hundredth_millimetres(box->width); } return {}; @@ -2044,10 +2052,12 @@ class ElementAdapter final : public AdapterBase { frame_height(const ElementIdentifier element_id) const override { const pugi::xml_node node = get_node(element_id); if (const std::optional measure = - read_measure(node.attribute("svg:height"))) { + read_measure(node.attribute("svg:height")); + measure.has_value()) { return measure; } - if (const std::optional box = connector_box(node)) { + if (const std::optional box = connector_box(node); + box.has_value()) { return hundredth_millimetres(box->height); } return {}; @@ -2525,7 +2535,8 @@ class ElementAdapter final : public AdapterBase { [[nodiscard]] std::optional object_file(const ElementIdentifier element_id) const { - if (const std::optional svg = chart_svg(element_id)) { + if (const std::optional svg = chart_svg(element_id); + svg.has_value()) { return File(std::make_shared(*svg)); } const std::optional replacement = replacement_href(element_id); @@ -2565,7 +2576,8 @@ class ElementAdapter final : public AdapterBase { return it->second; } std::optional result; - if (const std::optional path = object_part(element_id)) { + if (const std::optional path = object_part(element_id); + path.has_value()) { try { const pugi::xml_document content = xml::parse(*m_document->as_filesystem()->open(*path)); @@ -2660,7 +2672,7 @@ class ElementAdapter final : public AdapterBase { format_code(data_style, [&styles](const std::string_view name) { return styles.data_style_node(name); }); - if (!code) { + if (!code.has_value()) { return std::nullopt; } try { @@ -2690,7 +2702,7 @@ class ElementAdapter final : public AdapterBase { format_code(data_style, [&styles](const std::string_view name) { return styles.data_style_node(name); }); - if (!code) { + if (!code.has_value()) { return std::nullopt; } try { @@ -2795,7 +2807,8 @@ formula::Settings Document::formula_settings() const { if (const std::optional days = date_days(settings.child("table:null-date") .attribute("table:date-value") - .value())) { + .value()); + days.has_value()) { result.null_date = static_cast(*days); } // [ODF 1.2] 4.3.2.1 `meta:generator`, the application that computed the diff --git a/src/odr/internal/odf/odf_enhanced_geometry.cpp b/src/odr/internal/odf/odf_enhanced_geometry.cpp index 1b6b08fb3..1571b9a83 100644 --- a/src/odr/internal/odf/odf_enhanced_geometry.cpp +++ b/src/odr/internal/odf/odf_enhanced_geometry.cpp @@ -93,7 +93,7 @@ class FormulaParser : private ValueCursor { ++m_depth; const std::optional value = primary(); --m_depth; - return value && negate ? std::optional(-*value) : value; + return value.has_value() && negate ? std::optional(-*value) : value; } [[nodiscard]] std::optional primary() { diff --git a/src/odr/internal/odf/odf_geometry.cpp b/src/odr/internal/odf/odf_geometry.cpp index db13fa968..71871edae 100644 --- a/src/odr/internal/odf/odf_geometry.cpp +++ b/src/odr/internal/odf/odf_geometry.cpp @@ -135,34 +135,35 @@ class TransformParser : private ValueCursor { const std::optional d = read_number(); const std::optional e = read_length(); const std::optional f = read_length(); - if (!a || !b || !c || !d || !e || !f) { + if (!a.has_value() || !b.has_value() || !c.has_value() || + !d.has_value() || !e.has_value() || !f.has_value()) { return false; } compose({*a, *b, *c, *d, *e, *f}); } else if (name == "translate") { const std::optional x = read_length(); - if (!x) { + if (!x.has_value()) { return false; } const std::optional y = peek_argument() ? read_length() : std::optional(0); - if (!y) { + if (!y.has_value()) { return false; } compose(util::math::Transform2D::translation(*x, *y)); } else if (name == "scale") { const std::optional x = read_number(); - if (!x) { + if (!x.has_value()) { return false; } const std::optional y = peek_argument() ? read_number() : x; - if (!y) { + if (!y.has_value()) { return false; } compose(util::math::Transform2D::scaling(*x, *y)); } else if (name == "rotate" || name == "skewX" || name == "skewY") { const std::optional angle = read_number(); - if (!angle) { + if (!angle.has_value()) { return false; } // Radians, counter-clockwise, so the sine changes sign against svg's @@ -532,7 +533,8 @@ std::optional read_enhanced_geometry(const pugi::xml_node node) { } EnhancedGeometryContext context; - if (const std::optional view_box = read_view_box(geometry)) { + if (const std::optional view_box = read_view_box(geometry); + view_box.has_value()) { context.left = view_box->x; context.top = view_box->y; context.right = view_box->x + view_box->width; @@ -667,7 +669,8 @@ std::optional odf::read_path(const pugi::xml_node node) { return {}; } // The path's own extent is only the fallback for a shape stating none. - if (const std::optional view_box = odf::read_view_box(node)) { + if (const std::optional view_box = odf::read_view_box(node); + view_box.has_value()) { data->x = view_box->x; data->y = view_box->y; data->width = view_box->width; diff --git a/src/odr/internal/odf/odf_manifest.cpp b/src/odr/internal/odf/odf_manifest.cpp index 4e45a0123..4a55c622d 100644 --- a/src/odr/internal/odf/odf_manifest.cpp +++ b/src/odr/internal/odf/odf_manifest.cpp @@ -144,7 +144,7 @@ Manifest parse_manifest(const pugi::xml_document &manifest) { } } - if (!smallest_file_size || entry.size < *smallest_file_size) { + if (!smallest_file_size.has_value() || entry.size < *smallest_file_size) { smallest_file_size = entry.size; result.smallest_file_path = path; } diff --git a/src/odr/internal/odf/odf_number_format.cpp b/src/odr/internal/odf/odf_number_format.cpp index 7cce5d7f1..c6b9ddba9 100644 --- a/src/odr/internal/odf/odf_number_format.cpp +++ b/src/odr/internal/odf/odf_number_format.cpp @@ -200,7 +200,7 @@ namespace odr::internal { std::optional odf::format_code(const pugi::xml_node data_style, const DataStyleLookup &lookup) { std::optional main = section_code(data_style); - if (!main) { + if (!main.has_value()) { return std::nullopt; } @@ -214,7 +214,7 @@ std::optional odf::format_code(const pugi::xml_node data_style, lookup(map.attribute("style:apply-style-name").value()); const std::optional code = mapped ? section_code(mapped) : std::nullopt; - if (!condition || !code) { + if (!condition.has_value() || !code.has_value()) { return std::nullopt; } maps.emplace_back(*condition, *code); diff --git a/src/odr/internal/odf/odf_sheet_references.cpp b/src/odr/internal/odf/odf_sheet_references.cpp index 6262a92b1..b83756073 100644 --- a/src/odr/internal/odf/odf_sheet_references.cpp +++ b/src/odr/internal/odf/odf_sheet_references.cpp @@ -194,13 +194,15 @@ void move_subtree(const pugi::xml_node node, std::string sheet, } else if (std::ranges::find(condition_attributes, name) != condition_attributes.end()) { if (const std::optional moved = - move_bracketed(attribute.value(), own, shift, edit)) { + move_bracketed(attribute.value(), own, shift, edit); + moved.has_value()) { attribute.set_value(moved->c_str()); } } else if (std::ranges::find(address_attributes, name) != address_attributes.end()) { if (const std::optional moved = formula::move_addresses( - attribute.value(), edit, sheet, formula::Syntax::opendocument)) { + attribute.value(), edit, sheet, formula::Syntax::opendocument); + moved.has_value()) { attribute.set_value(moved->empty() ? "#REF!" : moved->c_str()); } } @@ -254,7 +256,8 @@ odf::move_sheet_references(const pugi::xml_node spreadsheet, const std::uint32_t along = rows ? row : column; if (!edited) { result.emplace_back(ordinal, column, row); - } else if (const auto moved = edit.span(along, along)) { + } else if (const auto moved = edit.span(along, along); + moved.has_value()) { result.emplace_back(ordinal, rows ? column : moved->first, rows ? moved->first : row); } diff --git a/src/odr/internal/odf/odf_style.cpp b/src/odr/internal/odf/odf_style.cpp index fa2cb3262..be401e98c 100644 --- a/src/odr/internal/odf/odf_style.cpp +++ b/src/odr/internal/odf/odf_style.cpp @@ -264,7 +264,7 @@ std::optional read_background(const pugi::xml_attribute attribute, return {}; } const std::optional color = read_color(attribute); - return color ? color : inherited; + return color.has_value() ? color : inherited; } /// A stated `none` clears the @p inherited border. @@ -373,13 +373,15 @@ void Style::resolve_text_style_(const StyleRegistry *registry, registry->font_face_node(font_name.value()), result); } if (const std::optional font_size = - read_measure(text_properties.attribute("fo:font-size"))) { + read_measure(text_properties.attribute("fo:font-size")); + font_size.has_value()) { // A percentage is of the parent's size, resolved here rather than passed // to css - the parent style is not on the element in the render. if (font_size->unit().name() != "%") { result.font_size = font_size; } else { - if (const std::optional parent_font_size = result.font_size) { + if (const std::optional parent_font_size = result.font_size; + parent_font_size.has_value()) { result.font_size = Measure(parent_font_size->magnitude() * font_size->magnitude() * 1e-2, parent_font_size->unit()); @@ -387,11 +389,13 @@ void Style::resolve_text_style_(const StyleRegistry *registry, } } if (const std::optional font_weight = - read_font_weight(text_properties.attribute("fo:font-weight"))) { + read_font_weight(text_properties.attribute("fo:font-weight")); + font_weight.has_value()) { result.font_weight = font_weight; } if (const std::optional font_style = - read_font_style(text_properties.attribute("fo:font-style"))) { + read_font_style(text_properties.attribute("fo:font-style")); + font_style.has_value()) { result.font_style = font_style; } if (const pugi::xml_attribute font_underline = @@ -406,11 +410,13 @@ void Style::resolve_text_style_(const StyleRegistry *registry, std::strcmp("none", font_line_through.value()) != 0; } if (const std::optional font_shadow = - read_string(text_properties.attribute("fo:text-shadow"))) { + read_string(text_properties.attribute("fo:text-shadow")); + font_shadow.has_value()) { result.font_shadow = font_shadow; } if (const std::optional font_color = - read_color(text_properties.attribute("fo:color"))) { + read_color(text_properties.attribute("fo:color")); + font_color.has_value()) { result.font_color = font_color; } result.background_color = @@ -419,7 +425,8 @@ void Style::resolve_text_style_(const StyleRegistry *registry, if (const pugi::xml_attribute text_position = text_properties.attribute("style:text-position")) { if (const std::optional font_position = - read_font_position(text_position)) { + read_font_position(text_position); + font_position.has_value()) { result.font_position = font_position; } // optional second component: relative font size in percent @@ -448,53 +455,64 @@ void Style::resolve_paragraph_style_(const pugi::xml_node node, result.text_align.reset(); } if (const std::optional text_align = - read_text_align(paragraph_properties.attribute("fo:text-align"))) { + read_text_align(paragraph_properties.attribute("fo:text-align")); + text_align.has_value()) { result.text_align = text_align; } if (const std::optional direction = read_text_direction( - paragraph_properties.attribute("style:writing-mode"))) { + paragraph_properties.attribute("style:writing-mode")); + direction.has_value()) { result.direction = direction; } // the shorthand is the four sides, each against its own inherited value if (const std::optional margin = - read_measure(paragraph_properties.attribute("fo:margin"))) { + read_measure(paragraph_properties.attribute("fo:margin")); + margin.has_value()) { result.margin.right = resolve_margin(*margin, result.margin.right); result.margin.top = resolve_margin(*margin, result.margin.top); result.margin.left = resolve_margin(*margin, result.margin.left); result.margin.bottom = resolve_margin(*margin, result.margin.bottom); } if (const std::optional margin_right = - read_measure(paragraph_properties.attribute("fo:margin-right"))) { + read_measure(paragraph_properties.attribute("fo:margin-right")); + margin_right.has_value()) { result.margin.right = resolve_margin(*margin_right, result.margin.right); } if (const std::optional margin_top = - read_measure(paragraph_properties.attribute("fo:margin-top"))) { + read_measure(paragraph_properties.attribute("fo:margin-top")); + margin_top.has_value()) { result.margin.top = resolve_margin(*margin_top, result.margin.top); } if (const std::optional margin_left = - read_measure(paragraph_properties.attribute("fo:margin-left"))) { + read_measure(paragraph_properties.attribute("fo:margin-left")); + margin_left.has_value()) { result.margin.left = resolve_margin(*margin_left, result.margin.left); } if (const std::optional margin_bottom = - read_measure(paragraph_properties.attribute("fo:margin-bottom"))) { + read_measure(paragraph_properties.attribute("fo:margin-bottom")); + margin_bottom.has_value()) { result.margin.bottom = resolve_margin(*margin_bottom, result.margin.bottom); } if (const std::optional line_height = - read_measure(paragraph_properties.attribute("fo:line-height"))) { + read_measure(paragraph_properties.attribute("fo:line-height")); + line_height.has_value()) { // percent line height is relative to the text font size; the HTML // translation emits it as a unitless ratio result.line_height = line_height; } if (const std::optional text_indent = - read_measure(paragraph_properties.attribute("fo:text-indent"))) { + read_measure(paragraph_properties.attribute("fo:text-indent")); + text_indent.has_value()) { result.text_indent = text_indent; } if (const std::optional break_before = - read_break(paragraph_properties.attribute("fo:break-before"))) { + read_break(paragraph_properties.attribute("fo:break-before")); + break_before.has_value()) { result.break_before = break_before; } if (const std::optional break_after = - read_break(paragraph_properties.attribute("fo:break-after"))) { + read_break(paragraph_properties.attribute("fo:break-after")); + break_after.has_value()) { result.break_after = break_after; } } @@ -504,7 +522,8 @@ void Style::resolve_table_style_(const pugi::xml_node node, const pugi::xml_node table_properties = node.child("style:table-properties"); if (const std::optional width = - read_measure(table_properties.attribute("style:width"))) { + read_measure(table_properties.attribute("style:width")); + width.has_value()) { result.width = width; } } @@ -514,8 +533,9 @@ void Style::resolve_table_column_style_(const pugi::xml_node node, const pugi::xml_node table_column_properties = node.child("style:table-column-properties"); - if (const std::optional width = read_measure( - table_column_properties.attribute("style:column-width"))) { + if (const std::optional width = + read_measure(table_column_properties.attribute("style:column-width")); + width.has_value()) { result.width = width; } } @@ -526,7 +546,8 @@ void Style::resolve_table_row_style_(const pugi::xml_node node, node.child("style:table-row-properties"); if (const std::optional height = - read_measure(table_row_properties.attribute("style:row-height"))) { + read_measure(table_row_properties.attribute("style:row-height")); + height.has_value()) { result.height = height; } } @@ -537,7 +558,8 @@ void Style::resolve_table_cell_style_(const pugi::xml_node node, node.child("style:table-cell-properties"); if (const std::optional vertical_align = read_vertical_align( - table_cell_properties.attribute("style:vertical-align"))) { + table_cell_properties.attribute("style:vertical-align")); + vertical_align.has_value()) { result.vertical_align = vertical_align; } if (const pugi::xml_attribute wrap_option = @@ -548,23 +570,28 @@ void Style::resolve_table_cell_style_(const pugi::xml_node node, read_background(table_cell_properties.attribute("fo:background-color"), result.background_color); if (const std::optional padding = - read_measure(table_cell_properties.attribute("fo:padding"))) { + read_measure(table_cell_properties.attribute("fo:padding")); + padding.has_value()) { result.padding = DirectionalStyle(padding); } if (const std::optional padding_right = - read_measure(table_cell_properties.attribute("fo:padding-right"))) { + read_measure(table_cell_properties.attribute("fo:padding-right")); + padding_right.has_value()) { result.padding.right = padding_right; } if (const std::optional padding_top = - read_measure(table_cell_properties.attribute("fo:padding-top"))) { + read_measure(table_cell_properties.attribute("fo:padding-top")); + padding_top.has_value()) { result.padding.top = padding_top; } if (const std::optional padding_left = - read_measure(table_cell_properties.attribute("fo:padding-left"))) { + read_measure(table_cell_properties.attribute("fo:padding-left")); + padding_left.has_value()) { result.padding.left = padding_left; } if (const std::optional padding_bottom = - read_measure(table_cell_properties.attribute("fo:padding-bottom"))) { + read_measure(table_cell_properties.attribute("fo:padding-bottom")); + padding_bottom.has_value()) { result.padding.bottom = padding_bottom; } if (const pugi::xml_attribute border = @@ -588,11 +615,13 @@ void Style::resolve_graphic_style_(const pugi::xml_node node, node.child("style:graphic-properties"); if (const std::optional stroke_width = - read_measure(graphic_properties.attribute("svg:stroke-width"))) { + read_measure(graphic_properties.attribute("svg:stroke-width")); + stroke_width.has_value()) { result.stroke_width = stroke_width; } if (const std::optional stroke_color = - read_color(graphic_properties.attribute("svg:stroke-color"))) { + read_color(graphic_properties.attribute("svg:stroke-color")); + stroke_color.has_value()) { result.stroke_color = stroke_color; } // `draw:fill` and `draw:fill-color` cascade independently, so the fill state @@ -605,17 +634,20 @@ void Style::resolve_graphic_style_(const pugi::xml_node node, std::strcmp("solid", fill.value()) == 0 ? 255 : 0); } if (const std::optional fill_color = - read_color(graphic_properties.attribute("draw:fill-color"))) { + read_color(graphic_properties.attribute("draw:fill-color")); + fill_color.has_value()) { result.fill_color = Color(fill_color->red, fill_color->green, fill_color->blue, result.fill_color.has_value() ? result.fill_color->alpha : 0); } if (const std::optional vertical_align = read_vertical_align( - graphic_properties.attribute("draw:textarea-vertical-align"))) { + graphic_properties.attribute("draw:textarea-vertical-align")); + vertical_align.has_value()) { result.vertical_align = vertical_align; } if (const std::optional text_wrap = - read_text_wrap(graphic_properties.attribute("style:wrap"))) { + read_text_wrap(graphic_properties.attribute("style:wrap")); + text_wrap.has_value()) { result.text_wrap = text_wrap; } // assigned even when it reads as none, so a `from-*` overrides an inherited @@ -667,7 +699,7 @@ void StyleRegistry::generate_indices_(const pugi::xml_node node) { } else if (name == "style:master-page") { std::string master_page_name = e.attribute("style:name").value(); m_index_master_page[master_page_name] = e; - if (!m_first_master_page) { + if (!m_first_master_page.has_value()) { m_first_master_page = master_page_name; } } @@ -781,7 +813,7 @@ void StyleRegistry::generate_master_pages_(Document &document) { parse_tree(document.element_registry(), node); } - if (m_first_master_page) { + if (m_first_master_page.has_value()) { m_first_master_page_element = m_master_page_elements[*m_first_master_page]; } } @@ -895,14 +927,16 @@ std::string StyleRegistry::create_cell_style(pugi::xml_node automatic_styles, }; const std::string key = fmt::format( "{}|{}|{}|{}|{}|{}|{}|{}|{}", base, - cell.background_color + cell.background_color.has_value() ? fmt::format("{:08x}", cell.background_color->argb()) : "-", key_of(cell.horizontal_align), key_of(text.font_weight), key_of(text.font_style), key_of(text.font_underline), key_of(text.font_line_through), - text.font_color ? fmt::format("{:08x}", text.font_color->argb()) : "-", - text.font_size ? text.font_size->to_string() : "-"); + text.font_color.has_value() + ? fmt::format("{:08x}", text.font_color->argb()) + : "-", + text.font_size.has_value() ? text.font_size->to_string() : "-"); if (const auto it = m_created_cell_styles.find(key); it != std::end(m_created_cell_styles)) { return it->second; @@ -930,9 +964,12 @@ std::string StyleRegistry::create_cell_style(pugi::xml_node automatic_styles, } TextStyle text_properties = text; text_properties.background_color.reset(); - if (text_properties.font_size || text_properties.font_weight || - text_properties.font_style || text_properties.font_underline || - text_properties.font_line_through || text_properties.font_color) { + if (text_properties.font_size.has_value() || + text_properties.font_weight.has_value() || + text_properties.font_style.has_value() || + text_properties.font_underline.has_value() || + text_properties.font_line_through.has_value() || + text_properties.font_color.has_value()) { write_text_properties(properties_of(node, "style:text-properties"), text_properties); } diff --git a/src/odr/internal/odf/odf_value_cursor.hpp b/src/odr/internal/odf/odf_value_cursor.hpp index 8ba77c7d0..abd423e83 100644 --- a/src/odr/internal/odf/odf_value_cursor.hpp +++ b/src/odr/internal/odf/odf_value_cursor.hpp @@ -54,7 +54,7 @@ class ValueCursor : public TextCursor { } const std::optional value = util::number::parse(rest().substr(0, end)); - if (value) { + if (value.has_value()) { advance(end); } return value; diff --git a/src/odr/internal/oldms/presentation/ppt_parser.cpp b/src/odr/internal/oldms/presentation/ppt_parser.cpp index e3728b2ed..954eb75bc 100644 --- a/src/odr/internal/oldms/presentation/ppt_parser.cpp +++ b/src/odr/internal/oldms/presentation/ppt_parser.cpp @@ -99,7 +99,8 @@ find_child(std::istream &in, const RecordHeader &container, const std::uint16_t rec_type, const std::optional rec_instance = std::nullopt) { ChildCursor children(in, container); - while (const std::optional child = children.next()) { + for (std::optional child = children.next(); child.has_value(); + child = children.next()) { if (child->recType == rec_type && (!rec_instance.has_value() || child->recInstance == *rec_instance)) { return child; // stream is positioned at the child body @@ -112,7 +113,8 @@ find_child(std::istream &in, const RecordHeader &container, RecordHeader require_child(std::istream &in, const RecordHeader &container, const std::uint16_t rec_type) { if (const std::optional child = - find_child(in, container, rec_type)) { + find_child(in, container, rec_type); + child.has_value()) { return *child; } throw std::runtime_error("ppt: missing required record type " + @@ -346,7 +348,8 @@ void gather_text(std::istream &in, const RecordHeader &container, }; ChildCursor children(in, container); - while (const std::optional child = children.next()) { + for (std::optional child = children.next(); child.has_value(); + child = children.next()) { if (child->recType == RT_TextCharsAtom || child->recType == RT_TextBytesAtom) { flush({}); @@ -385,7 +388,8 @@ Shape read_shape(std::istream &in, const RecordHeader &header, StyleContext &context) { Shape shape; ChildCursor children(in, header); - while (const std::optional child = children.next()) { + for (std::optional child = children.next(); child.has_value(); + child = children.next()) { if (child->recType == RT_OfficeArtClientAnchor) { shape.anchor = read_client_anchor(in, child->recLen); children.consume(child->recLen); @@ -445,11 +449,13 @@ read_slide_shapes(std::istream &in, const RecordHeader &slide, bool seen_group = false; ChildCursor children(in, dg); - while (const std::optional child = children.next()) { + for (std::optional child = children.next(); child.has_value(); + child = children.next()) { if (child->recType == RT_OfficeArtSpgrContainer) { seen_group = true; ChildCursor group(in, *child); - while (const std::optional shape = group.next()) { + for (std::optional shape = group.next(); shape.has_value(); + shape = group.next()) { if (shape->recType != RT_OfficeArtSpContainer) { continue; // not a shape; the cursor skips it } @@ -534,7 +540,8 @@ SlideListText read_slide_list_text(std::istream &in, }; ChildCursor children(in, slide_list); - while (const std::optional child = children.next()) { + for (std::optional child = children.next(); child.has_value(); + child = children.next()) { if (child->recType == RT_SlidePersistAtom && child->recLen >= persist_ref_size) { flush({}); @@ -624,7 +631,8 @@ std::vector read_blip_store(std::istream &in, } ChildCursor entries(in, *store); - while (const std::optional child = entries.next()) { + for (std::optional child = entries.next(); child.has_value(); + child = entries.next()) { BlipSlot &slot = slots.emplace_back(); if (child->recType == RT_OfficeArtFBSE) { entries.consume(sizeof(OfficeArtFbseFixed)); @@ -677,7 +685,8 @@ std::vector read_font_collection(std::istream &in, } ChildCursor entries(in, *collection); - while (const std::optional child = entries.next()) { + for (std::optional child = entries.next(); child.has_value(); + child = entries.next()) { if (child->recType != RT_FontEntityAtom) { continue; } diff --git a/src/odr/internal/oldms/spreadsheet/xls_io.cpp b/src/odr/internal/oldms/spreadsheet/xls_io.cpp index e9624dd55..a0b2822e4 100644 --- a/src/odr/internal/oldms/spreadsheet/xls_io.cpp +++ b/src/odr/internal/oldms/spreadsheet/xls_io.cpp @@ -21,7 +21,7 @@ bool BiffReader::next_record() { } const std::optional header = util::byte_stream::try_read(*m_in); - if (!header) { + if (!header.has_value()) { if (m_in->bad() || !m_in->eof() || m_in->gcount() != 0) { throw std::runtime_error("xls: truncated or unreadable record header"); } diff --git a/src/odr/internal/ooxml/ooxml_util.cpp b/src/odr/internal/ooxml/ooxml_util.cpp index d99ade21e..6db65e52e 100644 --- a/src/odr/internal/ooxml/ooxml_util.cpp +++ b/src/odr/internal/ooxml/ooxml_util.cpp @@ -188,7 +188,8 @@ ooxml::read_color_attribute(const pugi::xml_attribute attribute) { std::optional ooxml::read_drawing_rgb_color(const pugi::xml_node parent) { if (const std::optional color = - read_color_attribute(parent.child("a:srgbClr").attribute("val"))) { + read_color_attribute(parent.child("a:srgbClr").attribute("val")); + color.has_value()) { return color; } return read_color_attribute(parent.child("a:sysClr").attribute("lastClr")); @@ -487,7 +488,8 @@ std::optional ooxml::read_border_node(const pugi::xml_node node) { // `auto` reads as no color, which css then takes from the text std::string result = size->to_string() + " solid"; if (const std::optional color = - read_color_attribute(node.attribute("w:color"))) { + read_color_attribute(node.attribute("w:color")); + color.has_value()) { result.append(" ").append(html::color(*color)); } return result; diff --git a/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp b/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp index 4bf17a575..a4e07c93b 100644 --- a/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp +++ b/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp @@ -60,11 +60,13 @@ Document::Document(std::shared_ptr files, if (const pugi::xml_node slide_size = m_document_xml.document_element().child("p:sldSz")) { if (const std::optional width = - read_emus_attribute(slide_size.attribute("cx"))) { + read_emus_attribute(slide_size.attribute("cx")); + width.has_value()) { m_slide_layout.width = width; } if (const std::optional height = - read_emus_attribute(slide_size.attribute("cy"))) { + read_emus_attribute(slide_size.attribute("cy")); + height.has_value()) { m_slide_layout.height = height; } } @@ -163,7 +165,8 @@ void Document::load_slide_styles_(const std::vector &slides) { std::optional background = layout_style.background; if (const std::optional> stated = - read_background_color(slide_node, layout_style.color_scheme)) { + read_background_color(slide_node, layout_style.color_scheme); + stated.has_value()) { background = *stated; } if (background.has_value()) { @@ -528,7 +531,8 @@ class ElementAdapter final : public AdapterBase { table_column_style(const ElementIdentifier element_id) const override { TableColumnStyle result; if (const std::optional width = - read_emus_attribute(get_node(element_id).attribute("w"))) { + read_emus_attribute(get_node(element_id).attribute("w")); + width.has_value()) { result.width = width; } return result; @@ -538,7 +542,8 @@ class ElementAdapter final : public AdapterBase { table_row_style(const ElementIdentifier element_id) const override { TableRowStyle result; if (const std::optional height = - read_emus_attribute(get_node(element_id).attribute("h"))) { + read_emus_attribute(get_node(element_id).attribute("h")); + height.has_value()) { result.height = height; } return result; @@ -608,12 +613,14 @@ class ElementAdapter final : public AdapterBase { result.fill_color = Color(0, 0, 0, 0); } else if (const std::optional fill_color = read_drawing_color(shape_properties.child("a:solidFill"), - get_color_scheme(element_id))) { + get_color_scheme(element_id)); + fill_color.has_value()) { result.fill_color = fill_color; } if (const std::optional vertical_align = read_drawing_vertical_align_attribute( - node.child("p:txBody").child("a:bodyPr").attribute("anchor"))) { + node.child("p:txBody").child("a:bodyPr").attribute("anchor")); + vertical_align.has_value()) { result.vertical_align = vertical_align; } return result; diff --git a/src/odr/internal/ooxml/presentation/ooxml_presentation_style.cpp b/src/odr/internal/ooxml/presentation/ooxml_presentation_style.cpp index e51582811..9a3b3cc81 100644 --- a/src/odr/internal/ooxml/presentation/ooxml_presentation_style.cpp +++ b/src/odr/internal/ooxml/presentation/ooxml_presentation_style.cpp @@ -48,7 +48,8 @@ namespace odr::internal::ooxml { presentation::ColorScheme::ColorScheme(const pugi::xml_node color_scheme, const pugi::xml_node color_map) { for (const pugi::xml_node slot : color_scheme.children()) { - if (const std::optional color = read_drawing_rgb_color(slot)) { + if (const std::optional color = read_drawing_rgb_color(slot); + color.has_value()) { // the slot names are `a:dk1`, `a:lt1`, `a:accent1`, … const std::string_view name = slot.name(); const std::size_t colon = name.find(':'); @@ -78,7 +79,8 @@ presentation::ColorScheme::resolve(const char *name) const { std::optional presentation::read_drawing_color(const pugi::xml_node parent, const ColorScheme *color_scheme) { - if (const std::optional color = read_drawing_rgb_color(parent)) { + if (const std::optional color = read_drawing_rgb_color(parent); + color.has_value()) { return color; } if (const pugi::xml_attribute scheme_color = @@ -138,8 +140,9 @@ presentation::LayoutStyle presentation::load_layout_style( .value_or(std::optional()); } - if (const std::optional> stated = read_background_color( - layout.document_element(), result.color_scheme)) { + if (const std::optional> stated = + read_background_color(layout.document_element(), result.color_scheme); + stated.has_value()) { result.background = *stated; } return result; @@ -155,35 +158,43 @@ void presentation::resolve_text_style(const pugi::xml_node node, result.font_name = font_name.value(); } if (const std::optional font_size = - read_hundredth_point_attribute(run_properties.attribute("sz"))) { + read_hundredth_point_attribute(run_properties.attribute("sz")); + font_size.has_value()) { result.font_size = font_size; } if (const std::optional font_weight = - read_font_weight_attribute(run_properties.attribute("b"))) { + read_font_weight_attribute(run_properties.attribute("b")); + font_weight.has_value()) { result.font_weight = font_weight; } if (const std::optional font_style = - read_font_style_attribute(run_properties.attribute("i"))) { + read_font_style_attribute(run_properties.attribute("i")); + font_style.has_value()) { result.font_style = font_style; } if (const std::optional font_underline = - read_line_attribute(run_properties.attribute("u"))) { + read_line_attribute(run_properties.attribute("u")); + font_underline.has_value()) { result.font_underline = font_underline; } if (const std::optional font_line_through = - read_line_attribute(run_properties.attribute("strike"))) { + read_line_attribute(run_properties.attribute("strike")); + font_line_through.has_value()) { result.font_line_through = font_line_through; } if (const std::optional font_shadow = - read_shadow_attribute(run_properties.attribute("shadow"))) { + read_shadow_attribute(run_properties.attribute("shadow")); + font_shadow.has_value()) { result.font_shadow = font_shadow; } - if (const std::optional font_color = read_drawing_color( - run_properties.child("a:solidFill"), color_scheme)) { + if (const std::optional font_color = + read_drawing_color(run_properties.child("a:solidFill"), color_scheme); + font_color.has_value()) { result.font_color = font_color; } - if (const std::optional background_color = read_drawing_color( - run_properties.child("a:highlight"), color_scheme)) { + if (const std::optional background_color = + read_drawing_color(run_properties.child("a:highlight"), color_scheme); + background_color.has_value()) { result.background_color = background_color; } // the sign carries the direction @@ -201,24 +212,29 @@ void presentation::resolve_paragraph_style(const pugi::xml_node node, if (const std::optional text_align = read_drawing_text_align_attribute( - paragraph_properties.attribute("algn"))) { + paragraph_properties.attribute("algn")); + text_align.has_value()) { result.text_align = text_align; } if (const std::optional direction = read_drawing_text_direction_attribute( - paragraph_properties.attribute("rtl"))) { + paragraph_properties.attribute("rtl")); + direction.has_value()) { result.direction = direction; } if (const std::optional margin_left = - read_emus_attribute(paragraph_properties.attribute("marL"))) { + read_emus_attribute(paragraph_properties.attribute("marL")); + margin_left.has_value()) { result.margin.left = margin_left; } if (const std::optional margin_right = - read_emus_attribute(paragraph_properties.attribute("marR"))) { + read_emus_attribute(paragraph_properties.attribute("marR")); + margin_right.has_value()) { result.margin.right = margin_right; } if (const std::optional line_height = - read_line_spacing(paragraph_properties.child("a:lnSpc"))) { + read_line_spacing(paragraph_properties.child("a:lnSpc")); + line_height.has_value()) { result.line_height = line_height; } // absolute only: a percent here is of the text size, which css would @@ -226,13 +242,15 @@ void presentation::resolve_paragraph_style(const pugi::xml_node node, if (const std::optional margin_top = read_hundredth_point_attribute(paragraph_properties.child("a:spcBef") .child("a:spcPts") - .attribute("val"))) { + .attribute("val")); + margin_top.has_value()) { result.margin.top = margin_top; } if (const std::optional margin_bottom = read_hundredth_point_attribute(paragraph_properties.child("a:spcAft") .child("a:spcPts") - .attribute("val"))) { + .attribute("val")); + margin_bottom.has_value()) { result.margin.bottom = margin_bottom; } } diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp index 3795b09ba..29083843a 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp @@ -78,7 +78,7 @@ Document::Document(std::shared_ptr files, parse_relationship_target(*m_files, workbook_path, "styles"); const AbsPath styles_path = styles_target.value_or(AbsPath("/xl/styles.xml")); pugi::xml_node styles_root; - if (styles_target || m_files->is_file(styles_path)) { + if (styles_target.has_value() || m_files->is_file(styles_path)) { styles_root = parse_xml_(styles_path).first.document_element(); m_written_parts.push_back(styles_path); } else { @@ -116,7 +116,7 @@ Document::Document(std::shared_ptr files, pugi::xml_node theme_root; if (const std::optional theme_path = parse_relationship_target(*m_files, workbook_path, "theme"); - theme_path && m_files->is_file(*theme_path)) { + theme_path.has_value() && m_files->is_file(*theme_path)) { theme_root = parse_xml_(*theme_path).first.document_element(); } m_style_registry = StyleRegistry(styles_root, theme_root); @@ -311,8 +311,10 @@ namespace { /// Whether a cell draws something on its own: a border or a visible fill. bool draws(const TableCellStyle &style) { const DirectionalStyle &border = style.border; - return (style.background_color && style.background_color->alpha != 0) || - border.top || border.right || border.bottom || border.left; + return (style.background_color.has_value() && + style.background_color->alpha != 0) || + border.top.has_value() || border.right.has_value() || + border.bottom.has_value() || border.left.has_value(); } using AdapterBase = internal::RegistryElementAdapter< @@ -908,7 +910,7 @@ class ElementAdapter final : public AdapterBase { sheet_id, edit); for (const auto &[position, cell] : sheet.cells) { - if (!move_position(position, edit)) { + if (!move_position(position, edit).has_value()) { m_registry->invalidate(cell.element_id); } } @@ -930,8 +932,9 @@ class ElementAdapter final : public AdapterBase { } for (pugi::xml_node cell = row_node.child("c"); cell;) { const pugi::xml_node next_cell = cell.next_sibling("c"); - if (const std::optional moved = move_position( - TablePosition(cell.attribute("r").value()), edit)) { + if (const std::optional moved = + move_position(TablePosition(cell.attribute("r").value()), edit); + moved.has_value()) { cell.attribute("r").set_value(moved->to_string().c_str()); } else { row_node.remove_child(cell); @@ -970,7 +973,8 @@ class ElementAdapter final : public AdapterBase { const pugi::xml_node next = merge.next_sibling("mergeCell"); if (const std::optional moved = move_addresses(merge.attribute("ref").value(), edit, sheet.name, - formula::Syntax::ooxml)) { + formula::Syntax::ooxml); + moved.has_value()) { if (moved->empty()) { merges.remove_child(merge); } else { @@ -991,7 +995,8 @@ class ElementAdapter final : public AdapterBase { sheet_node.child("dimension").attribute("ref"); ref) { if (const std::optional moved = move_addresses( - ref.value(), edit, sheet.name, formula::Syntax::ooxml)) { + ref.value(), edit, sheet.name, formula::Syntax::ooxml); + moved.has_value()) { ref.set_value(moved->empty() ? "A1" : moved->c_str()); } } @@ -1005,7 +1010,7 @@ class ElementAdapter final : public AdapterBase { if (rows_edited) { decltype(sheet.rows) rows; for (const auto &[index, entry] : sheet.rows) { - if (const auto moved = edit.span(index, index)) { + if (const auto moved = edit.span(index, index); moved.has_value()) { rows.emplace(moved->first, entry); } } @@ -1013,8 +1018,8 @@ class ElementAdapter final : public AdapterBase { } decltype(sheet.cells) cells; for (const auto &[position, cell] : sheet.cells) { - if (const std::optional at = - move_position(position, edit)) { + if (const std::optional at = move_position(position, edit); + at.has_value()) { m_registry->sheet_cell_element_at(cell.element_id).position = *at; cells.emplace(*at, cell); } @@ -1060,7 +1065,7 @@ class ElementAdapter final : public AdapterBase { std::uint64_t{edit.index} + edit.count, std::uint64_t{max} + edit.count); place(col, min, edit.index - 1); - } else if (const auto span = edit.span(min, max)) { + } else if (const auto span = edit.span(min, max); span.has_value()) { place(col, span->first, span->second); } else { cols.remove_child(col); @@ -1214,7 +1219,7 @@ class ElementAdapter final : public AdapterBase { const std::optional format = shown_format( sheet_element.cell_node(column, row), sheet_element.row_node(row), sheet_element.column_node(column)); - if (!format) { + if (!format.has_value()) { return {}; } return m_document->style_registry().cell_style(*format).table_cell_style; @@ -1275,7 +1280,8 @@ class ElementAdapter final : public AdapterBase { result.type() == ValueType::date || result.type() == ValueType::time || result.type() == ValueType::boolean) { if (const std::optional number = - util::number::parse(node.child("v").text().get())) { + util::number::parse(node.child("v").text().get()); + number.has_value()) { // a date states days since 1899-12-30, whatever the workbook counts; // a time is a duration, which no epoch moves result = result @@ -1363,7 +1369,8 @@ class ElementAdapter final : public AdapterBase { std::string_view(cell.attribute("t").value()) == "n" || std::string_view(cell.attribute("t").value()) == "b")) { if (const std::optional number = - util::number::parse(first.text().get())) { + util::number::parse(first.text().get()); + number.has_value()) { return shown_value(element_parent(element_id), cell, *number); } } @@ -1626,7 +1633,8 @@ class ElementAdapter final : public AdapterBase { .column_node( m_registry->sheet_cell_element_at(element_id).position.column); if (const std::optional format = - shown_format(node, node.parent(), column_node)) { + shown_format(node, node.parent(), column_node); + format.has_value()) { return m_document->style_registry().cell_style(*format); } return {}; diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_parser.cpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_parser.cpp index 969d036f0..f30247637 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_parser.cpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_parser.cpp @@ -103,7 +103,7 @@ void parse_sheet_cell_children(ElementRegistry ®istry, // Like LibreOffice, an unknown shared string leaves the cell empty. const std::optional ref = read_index(node.child("v").text().get()); - if (ref && *ref < context.shared_strings().size()) { + if (ref.has_value() && *ref < context.shared_strings().size()) { parse_any_element_children(registry, context, parent_id, context.shared_strings()[*ref]); } @@ -134,7 +134,7 @@ parse_sheet_element(ElementRegistry ®istry, const ParseContext &context, read_index(col_node.attribute("min").value()); const std::optional max = read_index(col_node.attribute("max").value()); - if (!min || !max || *min == 0 || *max < *min) { + if (!min.has_value() || !max.has_value() || *min == 0 || *max < *min) { continue; } sheet.register_column(*min - 1, *max - 1, col_node); @@ -149,13 +149,13 @@ parse_sheet_element(ElementRegistry ®istry, const ParseContext &context, const std::optional stated = row_attribute ? read_index(row_attribute.value()) : std::nullopt; const std::uint64_t row_number = - stated && *stated != 0 ? *stated : next_row; + stated.has_value() && *stated != 0 ? *stated : next_row; if (row_number > std::numeric_limits::max()) { throw std::runtime_error("invalid spreadsheet row"); } const std::uint32_t row = static_cast(row_number - 1); next_row = row_number + 1; - if (!stated || *stated != row_number) { + if (!stated.has_value() || *stated != row_number) { if (!row_attribute) { row_attribute = row_node.append_attribute("r"); } diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_references.cpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_references.cpp index 4762d80a6..19a541606 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_references.cpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_references.cpp @@ -47,7 +47,8 @@ void move_ref(pugi::xml_node formula, const std::string &sheet, const formula::SheetEdit &edit) { if (pugi::xml_attribute ref = formula.attribute("ref")) { if (const std::optional moved = - formula::move_addresses(ref.value(), edit, sheet, syntax)) { + formula::move_addresses(ref.value(), edit, sheet, syntax); + moved.has_value()) { ref.set_value(moved->c_str()); } } @@ -85,7 +86,8 @@ void move_anchored_formulas(const std::string &sqref, if (stays == anchor) { move_text(text, edit.sheet, edit); } else if (const std::optional expression = - formula::parse(text.text().get(), syntax)) { + formula::parse(text.text().get(), syntax); + expression.has_value()) { formula::Node moved = shifted(*expression, anchor, stays); formula::move_references(moved, edit, edit.sheet); text.text().set(formula::to_string(moved, syntax).c_str()); @@ -170,7 +172,8 @@ void move_extensions(const NamedWorksheet &worksheet, const bool edited, } move_anchored_formulas(sqref.text().get(), formulas, edit); if (const std::optional moved = formula::move_addresses( - sqref.text().get(), edit, edit.sheet, syntax)) { + sqref.text().get(), edit, edit.sheet, syntax); + moved.has_value()) { if (moved->empty()) { lost.push_back(rule); } else { @@ -194,7 +197,8 @@ void move_extensions(const NamedWorksheet &worksheet, const bool edited, continue; } if (const std::optional moved = formula::move_addresses( - sqref.text().get(), edit, edit.sheet, syntax)) { + sqref.text().get(), edit, edit.sheet, syntax); + moved.has_value()) { if (moved->empty()) { lost.push_back(sparkline); } else { @@ -308,8 +312,9 @@ void move_calc_chain(pugi::xml_node calc_chain, sheet_id = id.value(); } if (sheet_id == edited_sheet_id) { - if (const std::optional at = move_position( - TablePosition(entry.attribute("r").value()), edit)) { + if (const std::optional at = + move_position(TablePosition(entry.attribute("r").value()), edit); + at.has_value()) { entry.attribute("r").set_value(at->to_string().c_str()); } else { if (next && !next.attribute("i") && entry.attribute("i")) { @@ -344,8 +349,8 @@ constexpr std::array removable_ranges{{ std::string moved_cell(const std::string &address, const formula::SheetEdit &edit) { const TablePosition position(address); - if (const std::optional moved = - move_position(position, edit)) { + if (const std::optional moved = move_position(position, edit); + moved.has_value()) { return moved->to_string(); } return (edit.axis == formula::Axis::row @@ -371,7 +376,7 @@ void move_filter_columns(pugi::xml_node filter, const pugi::xml_node next = column.next_sibling("filterColumn"); const std::uint32_t at = range.from().column + column.attribute("colId").as_uint(); - if (const auto moved = edit.span(at, at)) { + if (const auto moved = edit.span(at, at); moved.has_value()) { column.attribute("colId").set_value(moved->first - columns->first); } else { filter.remove_child(column); @@ -387,7 +392,8 @@ void move_range(pugi::xml_node node, const formula::SheetEdit &edit) { return; } if (const std::optional moved = - formula::move_addresses(ref.value(), edit, edit.sheet, syntax)) { + formula::move_addresses(ref.value(), edit, edit.sheet, syntax); + moved.has_value()) { if (moved->empty()) { node.parent().remove_child(node); } else { @@ -433,7 +439,8 @@ void collect_ranges(const pugi::xml_node node, const formula::SheetEdit &edit, continue; } if (const std::optional moved = formula::move_addresses( - attribute.value(), edit, edit.sheet, syntax)) { + attribute.value(), edit, edit.sheet, syntax); + moved.has_value()) { if (moved->empty()) { lost.push_back(child); removed = true; @@ -449,7 +456,8 @@ void collect_ranges(const pugi::xml_node node, const formula::SheetEdit &edit, } if (pugi::xml_attribute sqref = child.attribute("sqref")) { if (const std::optional moved = formula::move_addresses( - sqref.value(), edit, edit.sheet, syntax)) { + sqref.value(), edit, edit.sheet, syntax); + moved.has_value()) { if (!moved->empty()) { sqref.set_value(moved->c_str()); } else if (active) { @@ -479,7 +487,8 @@ const char *corner_axis(const formula::SheetEdit &edit) { /// ones that stay where a delete takes it. void move_corner(pugi::xml_node corner, const formula::SheetEdit &edit) { pugi::xml_text at = corner.child(corner_axis(edit)).text(); - if (const auto span = edit.span(at.as_uint(), at.as_uint())) { + if (const auto span = edit.span(at.as_uint(), at.as_uint()); + span.has_value()) { at.set(span->first); } else { at.set(edit.index); @@ -607,7 +616,8 @@ void ooxml::spreadsheet::move_comments(const pugi::xml_node comments, const pugi::xml_node next = comment.next_sibling(name); pugi::xml_attribute ref = comment.attribute("ref"); if (const std::optional moved = - formula::move_addresses(ref.value(), edit, edit.sheet, syntax)) { + formula::move_addresses(ref.value(), edit, edit.sheet, syntax); + moved.has_value()) { if (moved->empty()) { list.remove_child(comment); } else { diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_style.cpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_style.cpp index 6f90e4491..8abb04a47 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_style.cpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_style.cpp @@ -270,7 +270,8 @@ StyleRegistry::read_color_(const pugi::xml_node node) const { std::strtoull(rgb.value(), nullptr, 16) & 0xffffff)); } } - if (const pugi::xml_attribute tint = node.attribute("tint"); result && tint) { + if (const pugi::xml_attribute tint = node.attribute("tint"); + result.has_value() && tint) { result = apply_tint(*result, tint.as_double()); } return result; @@ -331,7 +332,8 @@ void StyleRegistry::resolve_border_(const std::uint32_t i, } // TODO: thin only std::string declaration = "0.75pt solid "; - if (const std::optional color = read_color_(node.child("color"))) { + if (const std::optional color = read_color_(node.child("color")); + color.has_value()) { declaration.append(html::color(*color)); } return declaration; @@ -353,34 +355,35 @@ std::uint32_t StyleRegistry::create_cell_format( pugi::xml_node xf = scratch.append_copy( m_cell_formats_index.at(base < m_cell_formats_index.size() ? base : 0)); - if (text_style.font_weight || text_style.font_style || - text_style.font_underline || text_style.font_line_through || - text_style.font_color || text_style.font_size) { + if (text_style.font_weight.has_value() || text_style.font_style.has_value() || + text_style.font_underline.has_value() || + text_style.font_line_through.has_value() || + text_style.font_color.has_value() || text_style.font_size.has_value()) { const std::uint32_t font_id = xf.attribute("fontId").as_uint(); pugi::xml_node font = scratch.append_copy( m_fonts_index.at(font_id < m_fonts_index.size() ? font_id : 0)); - if (text_style.font_weight) { + if (text_style.font_weight.has_value()) { set_toggle(font, "b", *text_style.font_weight == FontWeight::bold); } - if (text_style.font_style) { + if (text_style.font_style.has_value()) { set_toggle(font, "i", *text_style.font_style == FontStyle::italic); } - if (text_style.font_line_through) { + if (text_style.font_line_through.has_value()) { set_toggle(font, "strike", *text_style.font_line_through); } - if (text_style.font_underline) { + if (text_style.font_underline.has_value()) { pugi::xml_node underline = ordered_child(font, "u", font_order); underline.remove_attribute("val"); if (!*text_style.font_underline) { underline.append_attribute("val").set_value("none"); } } - if (text_style.font_size) { + if (text_style.font_size.has_value()) { xml::set_attribute( ordered_child(font, "sz", font_order), "val", fmt::format("{:g}", points(*text_style.font_size)).c_str()); } - if (text_style.font_color) { + if (text_style.font_color.has_value()) { pugi::xml_node color = ordered_child(font, "color", font_order); color.remove_attributes(); color.append_attribute("rgb").set_value( @@ -392,7 +395,7 @@ std::uint32_t StyleRegistry::create_cell_format( xml::set_attribute(xf, "applyFont", "1"); } - if (cell_style.background_color) { + if (cell_style.background_color.has_value()) { pugi::xml_node fill = scratch.append_child("fill"); pugi::xml_node pattern = fill.append_child("patternFill"); if (cell_style.background_color->alpha == 0) { @@ -410,7 +413,7 @@ std::uint32_t StyleRegistry::create_cell_format( xml::set_attribute(xf, "applyFill", "1"); } - if (cell_style.wrap_text) { + if (cell_style.wrap_text.has_value()) { pugi::xml_node alignment = xf.child("alignment"); if (!alignment) { alignment = xf.prepend_child("alignment"); @@ -419,7 +422,7 @@ std::uint32_t StyleRegistry::create_cell_format( *cell_style.wrap_text ? "1" : "0"); xml::set_attribute(xf, "applyAlignment", "1"); } - if (cell_style.horizontal_align) { + if (cell_style.horizontal_align.has_value()) { pugi::xml_node alignment = xf.child("alignment"); if (!alignment) { alignment = xf.prepend_child("alignment"); @@ -436,7 +439,7 @@ std::uint32_t StyleRegistry::create_cell_format( xml::set_attribute(xf, "applyAlignment", "1"); } - if (number_format_id) { + if (number_format_id.has_value()) { xml::set_attribute(xf, "numFmtId", std::to_string(*number_format_id).c_str()); xml::set_attribute(xf, "applyNumberFormat", "1"); diff --git a/src/odr/internal/ooxml/text/ooxml_text_document.cpp b/src/odr/internal/ooxml/text/ooxml_text_document.cpp index 67e86b85f..6b4558dd0 100644 --- a/src/odr/internal/ooxml/text/ooxml_text_document.cpp +++ b/src/odr/internal/ooxml/text/ooxml_text_document.cpp @@ -610,7 +610,8 @@ class ElementAdapter final : public AdapterBase { const pugi::xml_node node = get_node(element_id); TableColumnStyle result; if (const std::optional width = - read_twips_attribute(node.attribute("w:w"))) { + read_twips_attribute(node.attribute("w:w")); + width.has_value()) { result.width = width; } return result; diff --git a/src/odr/internal/ooxml/text/ooxml_text_list.cpp b/src/odr/internal/ooxml/text/ooxml_text_list.cpp index d159c29cd..30ca2f4c7 100644 --- a/src/odr/internal/ooxml/text/ooxml_text_list.cpp +++ b/src/odr/internal/ooxml/text/ooxml_text_list.cpp @@ -52,7 +52,7 @@ std::uint32_t read_number( } const auto result = util::number::parse_integer( util::string::trim_view(attribute.value()), {.allow_plus = true}); - if (!result || *result > maximum) { + if (!result.has_value() || *result > maximum) { return fallback; } return *result; diff --git a/src/odr/internal/ooxml/text/ooxml_text_style.cpp b/src/odr/internal/ooxml/text/ooxml_text_style.cpp index a23a71580..44dbcd700 100644 --- a/src/odr/internal/ooxml/text/ooxml_text_style.cpp +++ b/src/odr/internal/ooxml/text/ooxml_text_style.cpp @@ -19,40 +19,49 @@ void resolve_text_style_(const pugi::xml_node node, TextStyle &result) { result.font_name = font_name.value(); } if (const std::optional font_size = read_half_point_attribute( - run_properties.child("w:sz").attribute("w:val"))) { + run_properties.child("w:sz").attribute("w:val")); + font_size.has_value()) { result.font_size = font_size; } if (const std::optional font_weight = - read_font_weight_attribute(run_properties.child("w:b"))) { + read_font_weight_attribute(run_properties.child("w:b")); + font_weight.has_value()) { result.font_weight = font_weight; } if (const std::optional font_style = - read_font_style_attribute(run_properties.child("w:i"))) { + read_font_style_attribute(run_properties.child("w:i")); + font_style.has_value()) { result.font_style = font_style; } if (const std::optional font_underline = - read_line_attribute(run_properties.child("w:u"))) { + read_line_attribute(run_properties.child("w:u")); + font_underline.has_value()) { result.font_underline = font_underline; } if (const std::optional font_line_through = - read_line_attribute(run_properties.child("w:strike"))) { + read_line_attribute(run_properties.child("w:strike")); + font_line_through.has_value()) { result.font_line_through = font_line_through; } if (const std::optional font_shadow = - read_shadow_attribute(run_properties.child("w:shadow"))) { + read_shadow_attribute(run_properties.child("w:shadow")); + font_shadow.has_value()) { result.font_shadow = font_shadow; } if (const std::optional font_color = read_color_attribute( - run_properties.child("w:color").attribute("w:val"))) { + run_properties.child("w:color").attribute("w:val")); + font_color.has_value()) { result.font_color = font_color; } // [ECMA-376] 17.3.2.32: a highlight paints over a shading, so the shading // is read only where no highlight names a colour if (const std::optional background_color = read_color_attribute( - run_properties.child("w:highlight").attribute("w:val"))) { + run_properties.child("w:highlight").attribute("w:val")); + background_color.has_value()) { result.background_color = background_color; } else if (const std::optional shading = read_color_attribute( - run_properties.child("w:shd").attribute("w:fill"))) { + run_properties.child("w:shd").attribute("w:fill")); + shading.has_value()) { result.background_color = shading; } } @@ -62,27 +71,33 @@ void resolve_paragraph_style_(const pugi::xml_node node, const pugi::xml_node paragraph_properties = node.child("w:pPr"); if (const std::optional text_align = read_text_align_attribute( - paragraph_properties.child("w:jc").attribute("w:val"))) { + paragraph_properties.child("w:jc").attribute("w:val")); + text_align.has_value()) { result.text_align = text_align; } if (const std::optional direction = - read_text_direction_attribute(paragraph_properties.child("w:bidi"))) { + read_text_direction_attribute(paragraph_properties.child("w:bidi")); + direction.has_value()) { result.direction = direction; } if (const std::optional margin_left = read_twips_attribute( - paragraph_properties.child("w:ind").attribute("w:left"))) { + paragraph_properties.child("w:ind").attribute("w:left")); + margin_left.has_value()) { result.margin.left = margin_left; } if (const std::optional margin_left = read_twips_attribute( - paragraph_properties.child("w:ind").attribute("w:start"))) { + paragraph_properties.child("w:ind").attribute("w:start")); + margin_left.has_value()) { result.margin.left = margin_left; } if (const std::optional margin_right = read_twips_attribute( - paragraph_properties.child("w:ind").attribute("w:right"))) { + paragraph_properties.child("w:ind").attribute("w:right")); + margin_right.has_value()) { result.margin.right = margin_right; } if (const std::optional margin_right = read_twips_attribute( - paragraph_properties.child("w:ind").attribute("w:end"))) { + paragraph_properties.child("w:ind").attribute("w:end")); + margin_right.has_value()) { result.margin.right = margin_right; } @@ -92,13 +107,15 @@ void resolve_paragraph_style_(const pugi::xml_node node, // value next to it if (!read_on_off_attribute(spacing.attribute("w:beforeAutospacing"))) { if (const std::optional margin_top = - read_twips_attribute(spacing.attribute("w:before"))) { + read_twips_attribute(spacing.attribute("w:before")); + margin_top.has_value()) { result.margin.top = margin_top; } } if (!read_on_off_attribute(spacing.attribute("w:afterAutospacing"))) { if (const std::optional margin_bottom = - read_twips_attribute(spacing.attribute("w:after"))) { + read_twips_attribute(spacing.attribute("w:after")); + margin_bottom.has_value()) { result.margin.bottom = margin_bottom; } } @@ -128,7 +145,8 @@ void resolve_table_style_(const pugi::xml_node node, TableStyle &result) { const pugi::xml_node table_properties = node.child("w:tblPr"); if (const std::optional width = - read_width_attribute(table_properties.child("w:tblW"))) { + read_width_attribute(table_properties.child("w:tblW")); + width.has_value()) { result.width = width; } @@ -136,11 +154,13 @@ void resolve_table_style_(const pugi::xml_node node, TableStyle &result) { const pugi::xml_node borders = table_properties.child("w:tblBorders"); result.border.override(read_borders_node(borders)); if (const std::optional inside_horizontal = - read_border_node(borders.child("w:insideH"))) { + read_border_node(borders.child("w:insideH")); + inside_horizontal.has_value()) { result.border_inside_horizontal = inside_horizontal; } if (const std::optional inside_vertical = - read_border_node(borders.child("w:insideV"))) { + read_border_node(borders.child("w:insideV")); + inside_vertical.has_value()) { result.border_inside_vertical = inside_vertical; } } @@ -154,7 +174,8 @@ void resolve_table_row_style_(const pugi::xml_node node, const pugi::xml_node height = table_row_properties.child("w:trHeight"); if (std::strcmp("auto", height.attribute("w:hRule").value()) != 0) { if (const std::optional height_value = - read_twips_attribute(height.attribute("w:val"))) { + read_twips_attribute(height.attribute("w:val")); + height_value.has_value()) { result.height = height_value; } } @@ -167,11 +188,13 @@ void resolve_table_cell_style_(const pugi::xml_node node, // TODO `w:tcW` is read and dropped. A cell width here fights the column // width the table already states, and the two disagree in real documents. if (const std::optional width = - read_width_attribute(table_cell_properties.child("w:tcW"))) { + read_width_attribute(table_cell_properties.child("w:tcW")); + width.has_value()) { } if (const std::optional vertical_align = read_vertical_align_attribute( - table_cell_properties.child("w:vAlign").attribute("w:val"))) { + table_cell_properties.child("w:vAlign").attribute("w:val")); + vertical_align.has_value()) { result.vertical_align = vertical_align; } result.border.override( diff --git a/src/odr/internal/pdf/pdf_cmap.cpp b/src/odr/internal/pdf/pdf_cmap.cpp index 75aa1c096..853f3cee4 100644 --- a/src/odr/internal/pdf/pdf_cmap.cpp +++ b/src/odr/internal/pdf/pdf_cmap.cpp @@ -121,7 +121,8 @@ std::string CMap::translate_string(const std::string &codes, const std::string code = codes.substr(pos, width); pos += width; - if (const std::optional unicode = unicode_for_code(code)) { + if (const std::optional unicode = unicode_for_code(code); + unicode.has_value()) { result += *unicode; continue; } @@ -130,7 +131,8 @@ std::string CMap::translate_string(const std::string &codes, // `<0020>` distinct. if (fixed_width == 1) { if (const std::optional unicode = - unicode_for_code(std::string(1, '\0') + code)) { + unicode_for_code(std::string(1, '\0') + code); + unicode.has_value()) { result += *unicode; continue; } diff --git a/src/odr/internal/pdf/pdf_document.cpp b/src/odr/internal/pdf/pdf_document.cpp index b5712a8df..6c46f54f1 100644 --- a/src/odr/internal/pdf/pdf_document.cpp +++ b/src/odr/internal/pdf/pdf_document.cpp @@ -41,7 +41,7 @@ double Font::advance_width(const std::uint32_t code) const { return cid_default_width / 1000.0; } const std::int64_t index = static_cast(code) - first_char; - if (type3) { + if (type3.has_value()) { // Type3 widths are in glyph space, mapped to text space by `/FontMatrix` // (ISO 32000-1 9.6.5) — not the fixed 1/1000 em of other fonts. The // horizontal advance is the x-component of `(w, 0)` through the matrix. @@ -55,9 +55,10 @@ double Font::advance_width(const std::uint32_t code) const { } // Non-embedded standard-14 fonts usually ship no `/Widths`: fall back to the // substitute's AFM metrics (ISO 32000-1 9.6.2.2) so placement stays correct. - if (substitute && substitute->metrics && code <= 0xFF) { + if (substitute.has_value() && substitute->metrics.has_value() && + code <= 0xFF) { const StandardFont metrics = *substitute->metrics; - if (encoding) { + if (encoding.has_value()) { // An explicit `/Encoding` (a `/Differences` override or a base encoding) // names the glyph; look that name up only. The built-in code table // assumes the font's own code->glyph mapping, which the encoding @@ -66,12 +67,14 @@ double Font::advance_width(const std::uint32_t code) const { if (const std::string_view name = encoding->glyph_name(static_cast(code)); !name.empty()) { - if (const std::optional width = afm_width(metrics, name)) { + if (const std::optional width = afm_width(metrics, name); + width.has_value()) { return *width / 1000.0; } } } else if (const std::optional width = - afm_code_width(metrics, static_cast(code))) { + afm_code_width(metrics, static_cast(code)); + width.has_value()) { // No `/Encoding`: the font's built-in encoding (the AFM's own codes) maps // code->glyph — the Symbol/ZapfDingbats case (text fonts default to // StandardEncoding). @@ -183,7 +186,8 @@ std::string Font::to_unicode(const std::string &codes) const { // tables. Both are authoritative, so a match — even a partial one — wins. if (!cid_encoding_name.empty()) { if (std::optional unicode = - translate_predefined_cmap(cid_encoding_name, codes)) { + translate_predefined_cmap(cid_encoding_name, codes); + unicode.has_value()) { return *unicode; } } @@ -199,7 +203,8 @@ std::string Font::to_unicode(const std::string &codes) const { std::string result; for (const std::uint32_t cid : this->codes(codes)) { if (const std::optional unicode = - cid_to_unicode(cid_registry, cid_ordering, cid)) { + cid_to_unicode(cid_registry, cid_ordering, cid); + unicode.has_value()) { util::string::append_c32(*unicode, result); } } diff --git a/src/odr/internal/pdf/pdf_document_parser.cpp b/src/odr/internal/pdf/pdf_document_parser.cpp index fede55a31..cec05e83e 100644 --- a/src/odr/internal/pdf/pdf_document_parser.cpp +++ b/src/odr/internal/pdf/pdf_document_parser.cpp @@ -195,7 +195,7 @@ struct PageAttributes { std::optional font_code(const Object &object, const std::uint32_t maximum) { const std::optional value = object.as_integer_opt(); - if (!value || *value < 0 || *value > maximum) { + if (!value.has_value() || *value < 0 || *value > maximum) { return std::nullopt; } return static_cast(*value); @@ -209,7 +209,8 @@ std::optional parse_encoding(DocumentParser &parser, const Object resolved = parser.resolve_object_copy(encoding_object); if (resolved.is_name()) { - if (const auto base = base_encoding_from_name(resolved.as_name())) { + if (const auto base = base_encoding_from_name(resolved.as_name()); + base.has_value()) { return Encoding(*base); } ODR_WARNING(parser.logger(), @@ -230,7 +231,8 @@ std::optional parse_encoding(DocumentParser &parser, if (dictionary.has_key("BaseEncoding")) { const Object &base_object = dictionary["BaseEncoding"]; if (base_object.is_name()) { - if (const auto named = base_encoding_from_name(base_object.as_name())) { + if (const auto named = base_encoding_from_name(base_object.as_name()); + named.has_value()) { base = *named; } else { // An explicitly named base we cannot represent: bail out rather than @@ -299,7 +301,7 @@ void parse_cid_widths(const Array &w, Font &font) { const Integer last = w[i + 1].as_integer(); const double width = w[i + 2].as_real(); // a reversed range is empty, and one past the CID range is cut there - if (first && last >= *first) { + if (first.has_value() && last >= *first) { const auto end = static_cast(std::min(last, max_cid)); for (std::uint32_t c = *first; c <= end; ++c) { @@ -442,11 +444,13 @@ void resolve_font_substitute(DocumentParser &parser, if (descriptor.is_dictionary()) { const Dictionary &d = descriptor.as_dictionary(); if (const auto v = - parser.resolve_object_copy(d.get("Flags")).as_integer_opt()) { + parser.resolve_object_copy(d.get("Flags")).as_integer_opt(); + v.has_value()) { flags = static_cast(*v); } - if (const auto v = parser.resolve_object_copy(d.get("FontWeight")) - .as_integer_opt()) { + if (const auto v = + parser.resolve_object_copy(d.get("FontWeight")).as_integer_opt(); + v.has_value()) { font_weight = static_cast(*v); } const Object angle = parser.resolve_object_copy(d.get("ItalicAngle")); @@ -460,7 +464,8 @@ void resolve_font_substitute(DocumentParser &parser, pdf::resolve_font_substitute(base_font, flags, font_weight, italic_angle); // A substitute with standard-14 metrics can supply the baseline shift the // HTML layer needs when the descriptor declared no `/Ascent`. - if (!font.descriptor_ascent && font.substitute->metrics) { + if (!font.descriptor_ascent.has_value() && + font.substitute->metrics.has_value()) { font.descriptor_ascent = afm_ascender(*font.substitute->metrics); } } @@ -918,7 +923,8 @@ void parse_image_data(DocumentParser &parser, const Dictionary &dictionary, parser.read_object_stream(object), filter, decode_parms, width, height, bits_per_component, color_space.get(), decode_array, alpha, color_key, smask_in_data, - jbig2_decode_options(parser, dictionary.get("DecodeParms")))) { + jbig2_decode_options(parser, dictionary.get("DecodeParms"))); + encoded.has_value()) { x_object.image_data = std::move(encoded->data); x_object.image_mime = std::move(encoded->mime); } @@ -1371,7 +1377,8 @@ void parse_annotation_appearance(State &state, const Dictionary &dictionary, annotation.appearance_transform = fit_appearance_to_rect(*appearance->bbox, appearance->matrix, corners); if (const std::optional alpha = - parser.resolve_object_copy(dictionary.get("CA")).as_real_opt()) { + parser.resolve_object_copy(dictionary.get("CA")).as_real_opt(); + alpha.has_value()) { annotation.appearance_alpha = std::clamp(*alpha, 0.0, 1.0); } } diff --git a/src/odr/internal/pdf/pdf_encryption.cpp b/src/odr/internal/pdf/pdf_encryption.cpp index c9d942434..c66b7f877 100644 --- a/src/odr/internal/pdf/pdf_encryption.cpp +++ b/src/odr/internal/pdf/pdf_encryption.cpp @@ -283,7 +283,7 @@ Authenticator::create(const Dictionary &encrypt, const std::string &file_id0) { filter_method(encrypt, "StmF", EncryptionMethod::aes_v3); const auto string_method = filter_method(encrypt, "StrF", EncryptionMethod::aes_v3); - if (!stream_method || !string_method) { + if (!stream_method.has_value() || !string_method.has_value()) { return std::nullopt; // unsupported crypt filter } d.m_stream_method = *stream_method; @@ -307,7 +307,7 @@ Authenticator::create(const Dictionary &encrypt, const std::string &file_id0) { filter_method(encrypt, "StmF", EncryptionMethod::none); const auto string_method = filter_method(encrypt, "StrF", EncryptionMethod::none); - if (!stream_method || !string_method) { + if (!stream_method.has_value() || !string_method.has_value()) { return std::nullopt; // unsupported crypt filter } d.m_stream_method = *stream_method; diff --git a/src/odr/internal/pdf/pdf_file_parser.cpp b/src/odr/internal/pdf/pdf_file_parser.cpp index 8fb380a82..5e6dacb27 100644 --- a/src/odr/internal/pdf/pdf_file_parser.cpp +++ b/src/odr/internal/pdf/pdf_file_parser.cpp @@ -498,8 +498,8 @@ std::pair FileParser::recover_xref() { const std::string line = p.read_line(); const auto [lead, content] = trim_line(line); - if (const std::optional ref = - match_object_start(content)) { + if (const std::optional ref = match_object_start(content); + ref.has_value()) { // the last definition of an id wins, whatever its generation const auto previous = xref.table.lower_bound(ObjectReference(ref->id, 0)); if (previous != xref.table.end() && previous->first.id == ref->id) { diff --git a/src/odr/internal/pdf/pdf_filter.cpp b/src/odr/internal/pdf/pdf_filter.cpp index 5eea260e0..1688606c3 100644 --- a/src/odr/internal/pdf/pdf_filter.cpp +++ b/src/odr/internal/pdf/pdf_filter.cpp @@ -304,14 +304,16 @@ pdf::DecodeResult pdf::decode(const Object &filter, const Object &decode_parms, // the other image codecs. if (name == "CCITTFaxDecode") { if (std::optional samples = - decode_ccitt(data, ccitt_parameters(parms))) { + decode_ccitt(data, ccitt_parameters(parms)); + samples.has_value()) { data = std::move(*samples); continue; } } if (name == "JBIG2Decode") { if (std::optional image = - decode_jbig2(data, options.jbig2_globals)) { + decode_jbig2(data, options.jbig2_globals); + image.has_value()) { data = std::move(image->samples); continue; } diff --git a/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp b/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp index 4273249c1..d9a1aab82 100644 --- a/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp +++ b/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp @@ -313,7 +313,8 @@ GraphicsOperatorParser::read_inline_image_data(const Dictionary &dictionary) { // contain `E I `, which the scan below would mistake for the // real `EI`, truncating the image and derailing the rest of the page. if (const std::optional length = - inline_image_raw_length(dictionary)) { + inline_image_raw_length(dictionary); + length.has_value()) { std::string data; for (std::size_t i = 0; i < *length; ++i) { const int_type c = m_parser.geti(); diff --git a/src/odr/internal/pdf/pdf_object_parser.cpp b/src/odr/internal/pdf/pdf_object_parser.cpp index 5ec6e8ddb..db1a7e2f5 100644 --- a/src/odr/internal/pdf/pdf_object_parser.cpp +++ b/src/odr/internal/pdf/pdf_object_parser.cpp @@ -22,7 +22,7 @@ namespace { template T parse_integer(const std::string_view token) { const auto value = util::number::parse_integer( token, {.allow_plus = std::is_signed_v}); - if (!value) { + if (!value.has_value()) { throw std::runtime_error("invalid or out-of-range PDF integer"); } return *value; @@ -101,7 +101,7 @@ void ObjectParser::ungetc() { std::uint8_t ObjectParser::hex_char_to_int(const char_type c) { const std::optional value = util::string::hex_digit(c); - if (!value) { + if (!value.has_value()) { throw std::runtime_error("invalid character in hex_char_to_int"); } return *value; @@ -292,7 +292,7 @@ std::variant ObjectParser::read_integer_or_real() { } const auto value = util::number::parse(token); - if (!value || !std::isfinite(*value)) { + if (!value.has_value() || !std::isfinite(*value)) { throw std::runtime_error("out-of-range PDF number"); } return *value; diff --git a/src/odr/internal/pdf/pdf_page_extractor.cpp b/src/odr/internal/pdf/pdf_page_extractor.cpp index 1ebdd4af3..003932285 100644 --- a/src/odr/internal/pdf/pdf_page_extractor.cpp +++ b/src/odr/internal/pdf/pdf_page_extractor.cpp @@ -875,7 +875,7 @@ void show_type3(std::vector &out, const Resources &resources, ++i; const std::string_view name = - font->encoding + font->encoding.has_value() ? font->encoding->glyph_name(static_cast(code)) : std::string_view{}; const auto it = name.empty() ? type3.char_procs.end() @@ -919,7 +919,7 @@ void run_content(const std::string &content, const Resources &resources, // Route a shown string through the Type3 char-proc renderer or the normal // text path, by the font kind. const auto show_run = [&](const std::string &codes, Font *font) { - if (font != nullptr && font->type3) { + if (font != nullptr && font->type3.has_value()) { show_type3(out, resources, state, logger, warned, active, marked, pen, codes, font, *font->type3); } else { diff --git a/src/odr/internal/rtf/rtf_tokenizer.cpp b/src/odr/internal/rtf/rtf_tokenizer.cpp index 88f14b017..7f160f80e 100644 --- a/src/odr/internal/rtf/rtf_tokenizer.cpp +++ b/src/odr/internal/rtf/rtf_tokenizer.cpp @@ -18,7 +18,7 @@ namespace { std::uint8_t hex_char_to_int(const char c) { const std::optional value = str::hex_digit(c); - if (!value) { + if (!value.has_value()) { throw std::runtime_error("invalid hex digit"); } return *value; diff --git a/src/odr/internal/svm/svm_to_svg.cpp b/src/odr/internal/svm/svm_to_svg.cpp index 11afe8a00..d01c5a4bb 100644 --- a/src/odr/internal/svm/svm_to_svg.cpp +++ b/src/odr/internal/svm/svm_to_svg.cpp @@ -1002,7 +1002,7 @@ void write_text(const IntPair &point, const std::string &text, const auto positions = dx_array.empty() ? std::nullopt : get_x_list_string(point, text, dx_array, context); - if (positions) { + if (positions.has_value()) { out.write_attribute("x", *positions); } else { if (!dx_array.empty()) { @@ -1223,7 +1223,7 @@ void translate_action(const ActionHeader &action_header, std::istream &in, case META_CLIPREGION_ACTION: { const auto [region, clip] = read_clip_region_action(in); state.clip.clear(); - if (clip && region) { + if (clip && region.has_value()) { intersect_clip(get_region_path_data(*region, context), state); } } break; @@ -1233,7 +1233,8 @@ void translate_action(const ActionHeader &action_header, std::istream &in, intersect_clip(get_path_data_string({&polygon, 1}, true, context), state); } break; case META_ISECTREGIONCLIPREGION_ACTION: { - if (const std::optional region = read_region(in)) { + if (const std::optional region = read_region(in); + region.has_value()) { intersect_clip(get_region_path_data(*region, context), state); } } break; diff --git a/src/odr/internal/util/odr_meta_util.cpp b/src/odr/internal/util/odr_meta_util.cpp index fdec6242f..36a1ec961 100644 --- a/src/odr/internal/util/odr_meta_util.cpp +++ b/src/odr/internal/util/odr_meta_util.cpp @@ -20,13 +20,13 @@ nlohmann::json meta_to_json(const FileMeta &meta) { if (meta.document_type != DocumentType::unknown) { result["documentType"] = document_type_to_string(meta.document_type); - if (meta.entry_count) { + if (meta.entry_count.has_value()) { result["entryCount"] = *meta.entry_count; } const auto put = [&](const char *key, const std::optional &value) { - if (value) { + if (value.has_value()) { result[key] = *value; } }; diff --git a/src/odr/quantity.cpp b/src/odr/quantity.cpp index 347e5dc23..c670f6a4a 100644 --- a/src/odr/quantity.cpp +++ b/src/odr/quantity.cpp @@ -37,7 +37,7 @@ double QuantityBase::parse_magnitude(std::string_view &text) { } const auto magnitude = internal::util::number::parse( start.substr(0, start.size() - cursor.rest().size())); - if (!magnitude) { + if (!magnitude.has_value()) { throw std::invalid_argument("invalid quantity magnitude"); } cursor.skip_whitespace(); diff --git a/test/src/html_test.cpp b/test/src/html_test.cpp index d68967fe6..6374f1125 100644 --- a/test/src/html_test.cpp +++ b/test/src/html_test.cpp @@ -172,7 +172,7 @@ TEST(html, document_images_yield_to_shipped_resources) { if (resource.type() == HtmlResourceType::image) { ++images; EXPECT_FALSE(location.has_value()); - } else if (resource.is_shipped() && location) { + } else if (resource.is_shipped() && location.has_value()) { std::ostringstream expected; resource.write_resource(expected); std::ostringstream served; diff --git a/test/src/internal/odf/odf_chart_test.cpp b/test/src/internal/odf/odf_chart_test.cpp index 5808905db..4a1065dc0 100644 --- a/test/src/internal/odf/odf_chart_test.cpp +++ b/test/src/internal/odf/odf_chart_test.cpp @@ -175,7 +175,7 @@ TEST(OdfChart, ragged_repeated_rows_keep_all_series_on_their_categories) { first.append_attribute("table:number-rows-repeated").set_value(2); const auto svg = render_chart(document.document_element()); - ASSERT_TRUE(svg); + ASSERT_TRUE(svg.has_value()); pugi::xml_document output; ASSERT_TRUE(output.load_string(svg->c_str())); const auto points = output.select_nodes("/svg/circle"); @@ -198,7 +198,7 @@ TEST(OdfChart, repeated_tables_are_bounded_before_expansion) { .append_attribute(rows ? "table:number-rows-repeated" : "table:number-columns-repeated") .set_value(count); - EXPECT_FALSE(render_chart(document.document_element())); + EXPECT_FALSE(render_chart(document.document_element()).has_value()); } } pugi::xml_document document; @@ -209,7 +209,7 @@ TEST(OdfChart, repeated_tables_are_bounded_before_expansion) { row.last_child() .append_attribute("table:number-columns-repeated") .set_value(1025); - EXPECT_FALSE(render_chart(document.document_element())); + EXPECT_FALSE(render_chart(document.document_element()).has_value()); } TEST(OdfChart, invalid_geometry_falls_back_before_integer_conversion) { @@ -220,7 +220,7 @@ TEST(OdfChart, invalid_geometry_falls_back_before_integer_conversion) { .node() .attribute("svg:width") .set_value(width); - EXPECT_FALSE(render_chart(document.document_element())); + EXPECT_FALSE(render_chart(document.document_element()).has_value()); } for (const char *value : {"1e308", "5e-324"}) { pugi::xml_document document; @@ -230,7 +230,7 @@ TEST(OdfChart, invalid_geometry_falls_back_before_integer_conversion) { cell.node().attribute("office:value").set_value(value); } const auto svg = render_chart(document.document_element()); - if (svg) { + if (svg.has_value()) { EXPECT_EQ(svg->find("nan"), std::string::npos); EXPECT_EQ(svg->find("inf"), std::string::npos); } @@ -246,7 +246,7 @@ TEST(OdfChart, pie_and_ring_totals_do_not_overflow) { cell.node().attribute("office:value").set_value("1e308"); } const auto svg = render_chart(document.document_element()); - ASSERT_TRUE(svg); + ASSERT_TRUE(svg.has_value()); EXPECT_EQ(count(*svg, "find("nan"), std::string::npos); EXPECT_EQ(svg->find("inf"), std::string::npos); @@ -261,7 +261,7 @@ TEST(OdfChart, a_constant_large_line_still_has_an_axis) { cell.node().attribute("office:value").set_value("1e300"); } const auto svg = render_chart(document.document_element()); - ASSERT_TRUE(svg); + ASSERT_TRUE(svg.has_value()); EXPECT_EQ(svg->find("nan"), std::string::npos); EXPECT_EQ(svg->find("inf"), std::string::npos); } diff --git a/test/src/internal/odf/odf_enhanced_geometry_test.cpp b/test/src/internal/odf/odf_enhanced_geometry_test.cpp index 8adc0fce9..c1e348e36 100644 --- a/test/src/internal/odf/odf_enhanced_geometry_test.cpp +++ b/test/src/internal/odf/odf_enhanced_geometry_test.cpp @@ -190,8 +190,9 @@ TEST(OdfFormula, nesting_is_bounded_and_results_must_be_finite) { square(), none()), 1); EXPECT_FALSE(evaluate(std::string(10000, '(') + "1" + std::string(10000, ')'), - square(), none())); - EXPECT_FALSE(evaluate("1e308*1e308", square(), none())); + square(), none()) + .has_value()); + EXPECT_FALSE(evaluate("1e308*1e308", square(), none()).has_value()); } TEST(OdfFormula, an_equation_continues_the_nesting_of_its_reference) { @@ -214,7 +215,8 @@ TEST(OdfEnhancedPath, "U 0 0 1 1 0 6000000 U 0 0 1 1 0 6000000", "M 1e999 0", "M 0 0 X 1e999 1"}) { SCOPED_TRACE(path); - EXPECT_FALSE(convert_enhanced_path(path, square(), none())); + EXPECT_FALSE(convert_enhanced_path(path, square(), none()).has_value()); } - EXPECT_TRUE(convert_enhanced_path("U 0 0 -1 1 0 90", square(), none())); + EXPECT_TRUE( + convert_enhanced_path("U 0 0 -1 1 0 90", square(), none()).has_value()); } diff --git a/test/src/internal/odf/odf_geometry_test.cpp b/test/src/internal/odf/odf_geometry_test.cpp index 3d6e5cf33..6f2b8a283 100644 --- a/test/src/internal/odf/odf_geometry_test.cpp +++ b/test/src/internal/odf/odf_geometry_test.cpp @@ -327,9 +327,11 @@ TEST(OdfShape, generated_geometry_has_bounded_expansion_and_equation_depth) { pugi::xml_document document; for (const std::string corners : {"65537", "2147483647", "4294967295", "18446744073709551615"}) { - EXPECT_FALSE(read_path( - parse_shape(document, ""))); + EXPECT_FALSE( + read_path( + parse_shape(document, "")) + .has_value()); } std::string xml = R"()"; @@ -339,7 +341,7 @@ TEST(OdfShape, generated_geometry_has_bounded_expansion_and_equation_depth) { } xml += R"()"; - EXPECT_FALSE(read_path(parse_shape(document, xml))); + EXPECT_FALSE(read_path(parse_shape(document, xml)).has_value()); } TEST(OdfTransform, decimal_tokens_ignore_the_numeric_locale) { @@ -349,10 +351,10 @@ TEST(OdfTransform, decimal_tokens_ignore_the_numeric_locale) { GTEST_SKIP() << "German locale unavailable"; } const auto transform = parse_transform("scale(1.5,2.25)"); - ASSERT_TRUE(transform); + ASSERT_TRUE(transform.has_value()); expect_linear(*transform, 1.5, 0, 0, 2.25); const auto path = parse_path_data("M.5 .25 L1.25 2.5"); - ASSERT_TRUE(path); + ASSERT_TRUE(path.has_value()); EXPECT_EQ(path->data, "M 0.5 0.25 L 1.25 2.5"); } @@ -364,7 +366,7 @@ TEST(OdfValueCursor, a_number_stops_at_the_next_token) { EXPECT_EQ(cursor.rest(), "E"); for (const std::string_view invalid : {".", "+", "-", "1e999"}) { ValueCursor bad(invalid); - EXPECT_FALSE(bad.read_number()); + EXPECT_FALSE(bad.read_number().has_value()); EXPECT_EQ(bad.rest(), invalid); } } diff --git a/test/src/internal/odf/odf_number_format_test.cpp b/test/src/internal/odf/odf_number_format_test.cpp index b1ee4f0c8..b1c257462 100644 --- a/test/src/internal/odf/odf_number_format_test.cpp +++ b/test/src/internal/odf/odf_number_format_test.cpp @@ -47,10 +47,12 @@ TEST(OdfNumberFormat, a_number_style_is_its_placeholders) { R"( k)"), "00.0##,\" k\""); for (const std::string factor : {"100", "inf", "1e999", "0"}) { - EXPECT_FALSE(code_of( - "")); + EXPECT_FALSE( + code_of( + "") + .has_value()); } } @@ -123,7 +125,7 @@ TEST(OdfNumberFormat, placeholder_counts_are_clamped) { "\"/>"; SCOPED_TRACE(xml); const auto code = code_of(xml); - ASSERT_TRUE(code); + ASSERT_TRUE(code.has_value()); EXPECT_LE(code->size(), 4096 + 16); } } diff --git a/test/src/internal/odf/odf_sheet_style_test.cpp b/test/src/internal/odf/odf_sheet_style_test.cpp index f576f564e..b48938937 100644 --- a/test/src/internal/odf/odf_sheet_style_test.cpp +++ b/test/src/internal/odf/odf_sheet_style_test.cpp @@ -77,7 +77,7 @@ std::optional fill_at(const Sheet &sheet, const std::uint32_t row) { const std::optional color = sheet.cell_style(column, row).background_color; - return color ? std::optional(color->rgb()) : std::nullopt; + return color.has_value() ? std::optional(color->rgb()) : std::nullopt; } std::size_t count(const std::string &text, const std::string &part) { diff --git a/test/src/internal/ooxml/ooxml_spreadsheet_style_test.cpp b/test/src/internal/ooxml/ooxml_spreadsheet_style_test.cpp index 6060e1cb6..b01e92a88 100644 --- a/test/src/internal/ooxml/ooxml_spreadsheet_style_test.cpp +++ b/test/src/internal/ooxml/ooxml_spreadsheet_style_test.cpp @@ -45,7 +45,7 @@ std::string fill_of(const std::string &fill) { const ResolvedStyle style = resolve("", "" + fill + "", R"()"); const std::optional color = style.table_cell_style.background_color; - return color ? internal::html::color(*color) : "none"; + return color.has_value() ? internal::html::color(*color) : "none"; } TextStyle font_of(const std::string &font) { diff --git a/test/src/internal/ooxml/ooxml_spreadsheet_style_write_test.cpp b/test/src/internal/ooxml/ooxml_spreadsheet_style_write_test.cpp index 40b03f3ba..1e20995ec 100644 --- a/test/src/internal/ooxml/ooxml_spreadsheet_style_write_test.cpp +++ b/test/src/internal/ooxml/ooxml_spreadsheet_style_write_test.cpp @@ -59,7 +59,7 @@ std::optional fill_at(const Sheet &sheet, const std::uint32_t row) { const std::optional color = sheet.cell_style(column, row).background_color; - return color ? std::optional(color->rgb()) : std::nullopt; + return color.has_value() ? std::optional(color->rgb()) : std::nullopt; } /// The style the one run of the cell shows. diff --git a/test/src/internal/pdf/pdf_font.cpp b/test/src/internal/pdf/pdf_font.cpp index c46356f4a..83703fa0e 100644 --- a/test/src/internal/pdf/pdf_font.cpp +++ b/test/src/internal/pdf/pdf_font.cpp @@ -245,12 +245,12 @@ TEST(PdfAfm, glyph_and_code_widths) { EXPECT_EQ(afm_width(StandardFont::helvetica, "space"), 278.0); EXPECT_EQ(afm_width(StandardFont::times_roman, "A"), 722.0); EXPECT_EQ(afm_width(StandardFont::courier, "A"), 600.0); - EXPECT_FALSE(afm_width(StandardFont::helvetica, "nonexistent")); + EXPECT_FALSE(afm_width(StandardFont::helvetica, "nonexistent").has_value()); // Built-in-encoding fallback: Symbol code 32 is space = 250; an empty slot // (code 0) has no width. EXPECT_EQ(afm_code_width(StandardFont::symbol, 32), 250.0); - EXPECT_FALSE(afm_code_width(StandardFont::symbol, 0)); + EXPECT_FALSE(afm_code_width(StandardFont::symbol, 0).has_value()); } TEST(PdfFont, advance_width_afm_fallback_via_encoding) { diff --git a/test/src/internal/util/number_util_test.cpp b/test/src/internal/util/number_util_test.cpp index 959360d6c..69d390331 100644 --- a/test/src/internal/util/number_util_test.cpp +++ b/test/src/internal/util/number_util_test.cpp @@ -15,17 +15,18 @@ TEST(ParseInteger, checks_the_destination_range_without_rounding) { std::numeric_limits::max()); EXPECT_EQ(parse_integer("-9223372036854775808"), std::numeric_limits::min()); - EXPECT_FALSE(parse_integer("9223372036854775808")); - EXPECT_FALSE(parse_integer("-9223372036854775809")); + EXPECT_FALSE(parse_integer("9223372036854775808").has_value()); + EXPECT_FALSE(parse_integer("-9223372036854775809").has_value()); EXPECT_EQ(parse_integer("18446744073709551615"), std::numeric_limits::max()); - EXPECT_FALSE(parse_integer("18446744073709551616")); + EXPECT_FALSE( + parse_integer("18446744073709551616").has_value()); EXPECT_EQ(parse_integer("127"), 127); EXPECT_EQ(parse_integer("-128"), -128); - EXPECT_FALSE(parse_integer("128")); - EXPECT_FALSE(parse_integer("-129")); + EXPECT_FALSE(parse_integer("128").has_value()); + EXPECT_FALSE(parse_integer("-129").has_value()); EXPECT_EQ(parse_integer("255"), 255); - EXPECT_FALSE(parse_integer("256")); + EXPECT_FALSE(parse_integer("256").has_value()); } TEST(ParseInteger, accepts_plus_only_when_requested) { @@ -33,29 +34,34 @@ TEST(ParseInteger, accepts_plus_only_when_requested) { EXPECT_EQ(parse_integer("-42", {.allow_plus = true}), -42); EXPECT_EQ(parse_integer("+42", {.allow_plus = true}), 42); EXPECT_EQ(parse_integer("+42", {.allow_plus = true}), 42U); - EXPECT_FALSE(parse_integer("+42")); - EXPECT_FALSE(parse_integer("+42")); + EXPECT_FALSE(parse_integer("+42").has_value()); + EXPECT_FALSE(parse_integer("+42").has_value()); for (const auto token : std::array{"+", "-", "++1", "+-1", "-+1", "--1"}) { SCOPED_TRACE(token); - EXPECT_FALSE(parse_integer(token, {.allow_plus = true})); - EXPECT_FALSE(parse_integer(token, {.allow_plus = true})); + EXPECT_FALSE( + parse_integer(token, {.allow_plus = true}).has_value()); + EXPECT_FALSE( + parse_integer(token, {.allow_plus = true}).has_value()); } for (const auto token : std::array{"-1", "-0"}) { SCOPED_TRACE(token); - EXPECT_FALSE(parse_integer(token)); - EXPECT_FALSE(parse_integer(token, {.allow_plus = true})); + EXPECT_FALSE(parse_integer(token).has_value()); + EXPECT_FALSE( + parse_integer(token, {.allow_plus = true}).has_value()); } } TEST(ParseInteger, requires_a_whole_token_without_whitespace) { EXPECT_EQ(parse_integer("00042"), 42); EXPECT_EQ(parse_integer("0"), 0U); - EXPECT_FALSE(parse_integer(std::string_view{})); - EXPECT_FALSE(parse_integer(std::string_view("1\0x", 3))); + EXPECT_FALSE(parse_integer(std::string_view{}).has_value()); + EXPECT_FALSE( + parse_integer(std::string_view("1\0x", 3)).has_value()); for (const auto token : std::array{"", " 1", "1 ", "1\n", "1x", "1.0", "1e2", "0x10", "+ 1"}) { SCOPED_TRACE(token); - EXPECT_FALSE(parse_integer(token, {.allow_plus = true})); + EXPECT_FALSE( + parse_integer(token, {.allow_plus = true}).has_value()); } } @@ -67,11 +73,12 @@ TEST(ParseInteger, reads_digits_in_the_requested_base) { 255U); EXPECT_EQ(parse_integer("101", {.base = 2}), 5U); EXPECT_EQ(parse_integer("z", {.base = 36}), 35U); - EXPECT_FALSE(parse_integer("0xff", {.base = 16})); - EXPECT_FALSE(parse_integer("100000000", {.base = 16})); - EXPECT_FALSE(parse_integer("2", {.base = 2})); + EXPECT_FALSE(parse_integer("0xff", {.base = 16}).has_value()); + EXPECT_FALSE( + parse_integer("100000000", {.base = 16}).has_value()); + EXPECT_FALSE(parse_integer("2", {.base = 2}).has_value()); for (const std::int32_t base : std::array{-1, 0, 1, 37}) { - EXPECT_FALSE(parse_integer("1", {.base = base})); + EXPECT_FALSE(parse_integer("1", {.base = base}).has_value()); } } diff --git a/wasm/src/wasm_document.cpp b/wasm/src/wasm_document.cpp index 6d509433a..1e2682841 100644 --- a/wasm/src/wasm_document.cpp +++ b/wasm/src/wasm_document.cpp @@ -35,7 +35,8 @@ emscripten::val locale(const Handle handle) { return ok(emscripten::val::null()); } const std::optional result = document_of(s).locale(); - return ok(result ? emscripten::val(*result) : emscripten::val::null()); + return ok(result.has_value() ? emscripten::val(*result) + : emscripten::val::null()); }); }