From 5d24ad278dcde9b9f9cf5fc795c79e7503c79201 Mon Sep 17 00:00:00 2001 From: Zhang Sheng Date: Mon, 31 Mar 2025 15:23:53 +0800 Subject: [PATCH] refactor: optimize extension mapping in docparser - Refactored the extension mapping logic by encapsulating it in dedicated functions for better organization and maintainability. - Introduced static functions to create the mapping of file extensions to their respective creation functions and similar extensions. - Updated the file conversion logic to utilize the new mapping functions, enhancing clarity and reducing redundancy. Log: This change improves the structure and readability of the document parsing code. --- src/docparser.cpp | 85 ++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/src/docparser.cpp b/src/docparser.cpp index 91274cd..60e18c5 100644 --- a/src/docparser.cpp +++ b/src/docparser.cpp @@ -97,54 +97,62 @@ static std::unique_ptr createOfd(const std::string &file return std::make_unique(filename); } -// 缓存后缀到创建函数的映射 -static const std::unordered_map extensionMap = { - { "docx", createDocx }, - { "pptx", createPptx }, - { "ppsx", createPptx }, - { "doc", createDoc }, - { "dot", createDoc }, - { "wps", createDoc }, - { "rtf", createRtf }, - { "odg", createOdf }, - { "odt", createOdf }, - { "ods", createOdf }, - { "odp", createOdf }, - { "xls", createExcel }, - { "xlsx", createExcel }, - { "xlsb", createXlsb }, - { "ppt", createPpt }, - { "pps", createPpt }, - { "dps", createPpt }, - { "pot", createPpt }, - { "pdf", createPdf }, - { "ofd", createOfd } -}; - -// 缓存相似后缀的映射关系 -static const std::unordered_map similarExtensionMap = { - { "doc", "docx" }, - { "docx", "doc" }, - { "xls", "xlsx" }, - { "xlsx", "xls" }, - { "ppt", "pptx" }, - { "pptx", "ppt" } -}; +// Caching the mapping of extensions to creation functions +static const std::unordered_map createExtensionMap() +{ + return { + { "docx", createDocx }, + { "pptx", createPptx }, + { "ppsx", createPptx }, + { "doc", createDoc }, + { "dot", createDoc }, + { "wps", createDoc }, + { "rtf", createRtf }, + { "odg", createOdf }, + { "odt", createOdf }, + { "ods", createOdf }, + { "odp", createOdf }, + { "xls", createExcel }, + { "xlsx", createExcel }, + { "xlsb", createXlsb }, + { "ppt", createPpt }, + { "pps", createPpt }, + { "dps", createPpt }, + { "pot", createPpt }, + { "pdf", createPdf }, + { "ofd", createOfd } + }; +} + +static const std::unordered_map createSimilarExtensionMap() +{ + return { + { "doc", "docx" }, + { "docx", "doc" }, + { "xls", "xlsx" }, + { "xlsx", "xls" }, + { "ppt", "pptx" }, + { "pptx", "ppt" } + }; +} static std::string doConvertFile(const std::string &filename, std::string suffix) { - // 转换后缀为小写 + static const std::unordered_map extensionMap = createExtensionMap(); + static const std::unordered_map similarExtensionMap = createSimilarExtensionMap(); + + // Convert suffix to lowercase std::transform(suffix.begin(), suffix.end(), suffix.begin(), [](unsigned char c) { return std::tolower(c); }); std::unique_ptr document; try { - // 先检查是否是文本文件 + // First check if it is a text file if (isTextSuffix(suffix)) { document = createTxt(filename, suffix); } else { - // 查找对应的创建函数 + // Find the corresponding creation function auto it = extensionMap.find(suffix); if (it != extensionMap.end()) { document = it->second(filename, suffix); @@ -154,7 +162,7 @@ static std::string doConvertFile(const std::string &filename, std::string suffix } document->convert(); - // 使用移动语义避免复制 + // Use move semantics to avoid copying return std::move(document->m_text); } catch (const std::logic_error &error) { std::cout << error.what() << std::endl; @@ -182,6 +190,9 @@ std::string DocParser::convertFile(const std::string &filename) return content; // 尝试相似后缀 + static const std::unordered_map extensionMap = createExtensionMap(); + static const std::unordered_map similarExtensionMap = createSimilarExtensionMap(); + auto it = similarExtensionMap.find(suffix); if (it != similarExtensionMap.end()) { return doConvertFile(filename, it->second);