From 416704fa47822d6532bde0d0ee54c8e4988030aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 25 Jun 2021 18:57:53 +0200 Subject: [PATCH] Any{Image,Scene,Shader}Converter: modernize the string code a bit. Only those use StringViews in the plugin API so far, importers not yet. --- .../AnyImageConverter/AnyImageConverter.cpp | 51 +++++++------- .../AnySceneConverter/AnySceneConverter.cpp | 17 +++-- .../AnyShaderConverter/AnyConverter.cpp | 70 +++++++++---------- 3 files changed, 72 insertions(+), 66 deletions(-) diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp index 5ea4570f4..862086131 100644 --- a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp +++ b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp @@ -26,11 +26,11 @@ #include "AnyImageConverter.h" #include -#include /* for Directory */ +#include /* for PluginManager */ #include #include #include -#include +#include /* for PluginMetadata::name() */ #include #include "Magnum/Trade/ImageData.h" @@ -38,6 +38,8 @@ namespace Magnum { namespace Trade { +using namespace Containers::Literals; + AnyImageConverter::AnyImageConverter(PluginManager::Manager& manager): AbstractImageConverter{manager} {} AnyImageConverter::AnyImageConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractImageConverter{manager, plugin} {} @@ -51,30 +53,31 @@ ImageConverterFeatures AnyImageConverter::doFeatures() const { bool AnyImageConverter::doConvertToFile(const ImageView2D& image, const Containers::StringView filename) { CORRADE_INTERNAL_ASSERT(manager()); - /** @todo lowercase only the extension, once Directory::split() is done */ - const std::string normalized = Utility::String::lowercase(filename); + /** @todo once Directory is std::string-free, use splitExtension(), but + only if we don't detect more than one extension yet */ + const Containers::String normalized = Utility::String::lowercase(filename); /* Detect the plugin from extension */ - std::string plugin; - if(Utility::String::endsWith(normalized, ".bmp")) - plugin = "BmpImageConverter"; - else if(Utility::String::endsWith(normalized, ".basis")) - plugin = "BasisImageConverter"; - else if(Utility::String::endsWith(normalized, ".exr")) - plugin = "OpenExrImageConverter"; - else if(Utility::String::endsWith(normalized, ".hdr")) - plugin = "HdrImageConverter"; - else if(Utility::String::endsWith(normalized, ".jpg") || - Utility::String::endsWith(normalized, ".jpeg") || - Utility::String::endsWith(normalized, ".jpe")) - plugin = "JpegImageConverter"; - else if(Utility::String::endsWith(normalized, ".png")) - plugin = "PngImageConverter"; - else if(Utility::String::endsWith(normalized, ".tga") || - Utility::String::endsWith(normalized, ".vda") || - Utility::String::endsWith(normalized, ".icb") || - Utility::String::endsWith(normalized, ".vst")) - plugin = "TgaImageConverter"; + Containers::StringView plugin; + if(normalized.hasSuffix(".bmp"_s)) + plugin = "BmpImageConverter"_s; + else if(normalized.hasSuffix(".basis"_s)) + plugin = "BasisImageConverter"_s; + else if(normalized.hasSuffix(".exr"_s)) + plugin = "OpenExrImageConverter"_s; + else if(normalized.hasSuffix(".hdr"_s)) + plugin = "HdrImageConverter"_s; + else if(normalized.hasSuffix(".jpg"_s) || + normalized.hasSuffix(".jpeg"_s) || + normalized.hasSuffix(".jpe"_s)) + plugin = "JpegImageConverter"_s; + else if(normalized.hasSuffix(".png"_s)) + plugin = "PngImageConverter"_s; + else if(normalized.hasSuffix(".tga"_s) || + normalized.hasSuffix(".vda"_s) || + normalized.hasSuffix(".icb"_s) || + normalized.hasSuffix( ".vst"_s)) + plugin = "TgaImageConverter"_s; else { Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename; return false; diff --git a/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp b/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp index a96587dfc..2c1c450a4 100644 --- a/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp +++ b/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp @@ -26,11 +26,11 @@ #include "AnySceneConverter.h" #include -#include +#include /* for PluginManager */ #include #include #include -#include +#include /* for PluginMetadata::name() */ #include #include "Magnum/Trade/ImageData.h" @@ -38,6 +38,8 @@ namespace Magnum { namespace Trade { +using namespace Containers::Literals; + AnySceneConverter::AnySceneConverter(PluginManager::Manager& manager): AbstractSceneConverter{manager} {} AnySceneConverter::AnySceneConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractSceneConverter{manager, plugin} {} @@ -51,13 +53,14 @@ SceneConverterFeatures AnySceneConverter::doFeatures() const { bool AnySceneConverter::doConvertToFile(const MeshData& mesh, const Containers::StringView filename) { CORRADE_INTERNAL_ASSERT(manager()); - /** @todo lowercase only the extension, once Directory::split() is done */ - const std::string normalized = Utility::String::lowercase(filename); + /** @todo once Directory is std::string-free, use splitExtension(), but + only if we don't detect more than one extension yet */ + const Containers::StringView normalized = Utility::String::lowercase(filename); /* Detect the plugin from extension */ - std::string plugin; - if(Utility::String::endsWith(normalized, ".ply")) - plugin = "StanfordSceneConverter"; + Containers::StringView plugin; + if(normalized.hasSuffix(".ply"_s)) + plugin = "StanfordSceneConverter"_s; else { Error{} << "Trade::AnySceneConverter::convertToFile(): cannot determine the format of" << filename; return false; diff --git a/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp b/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp index c5c7888db..58a512bad 100644 --- a/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp +++ b/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp @@ -27,11 +27,10 @@ #include #include -#include #include #include #include -#include +#include /* for PluginMetadata::name() */ #include #include @@ -122,49 +121,50 @@ Containers::StringView stringForFormat(const Format format) { } Format formatForExtension(const char* prefix, const Containers::StringView filename) { - /** @todo lowercase only the extension, once Directory::split() is done */ - const std::string normalized = Utility::String::lowercase(filename); + /* Can't reliably lowercase just the extension as we detect double + extensions as well */ + const Containers::String normalized = Utility::String::lowercase(filename); /* https://github.com/KhronosGroup/SPIRV-Tools/blob/a715b1b4053519ad0f2bdb2d22ace35d35867cff/README.md#command-line-tools "It's a convention to name SPIR-V assembly and binary files with suffix .spvasm and .spv, respectively." IT'S GREAT THAT I HAD TO SEARCH HALF THE INTERNET TO FIND THIS CONVENTION. Especially when tests in the SPIRV-Cross repo use `.asm.bla` instead, FFS. */ - if(Utility::String::endsWith(normalized, ".spvasm") || + if(normalized.hasSuffix(".spvasm"_s) || /* Not official, used by https://github.com/KhronosGroup/SPIRV-Cross */ - Utility::String::endsWith(normalized, ".asm.vert") || - Utility::String::endsWith(normalized, ".asm.frag") || - Utility::String::endsWith(normalized, ".asm.geom") || - Utility::String::endsWith(normalized, ".asm.comp") || - Utility::String::endsWith(normalized, ".asm.tesc") || - Utility::String::endsWith(normalized, ".asm.tese") || - Utility::String::endsWith(normalized, ".asm.rgen") || - Utility::String::endsWith(normalized, ".asm.rint") || - Utility::String::endsWith(normalized, ".asm.rahit") || - Utility::String::endsWith(normalized, ".asm.rchit") || - Utility::String::endsWith(normalized, ".asm.rmiss") || - Utility::String::endsWith(normalized, ".asm.rcall") || - Utility::String::endsWith(normalized, ".asm.mesh") || - Utility::String::endsWith(normalized, ".asm.task")) + normalized.hasSuffix(".asm.vert"_s) || + normalized.hasSuffix(".asm.frag"_s) || + normalized.hasSuffix(".asm.geom"_s) || + normalized.hasSuffix(".asm.comp"_s) || + normalized.hasSuffix(".asm.tesc"_s) || + normalized.hasSuffix(".asm.tese"_s) || + normalized.hasSuffix(".asm.rgen"_s) || + normalized.hasSuffix(".asm.rint"_s) || + normalized.hasSuffix(".asm.rahit"_s) || + normalized.hasSuffix(".asm.rchit"_s) || + normalized.hasSuffix(".asm.rmiss"_s) || + normalized.hasSuffix(".asm.rcall"_s) || + normalized.hasSuffix(".asm.mesh"_s) || + normalized.hasSuffix(".asm.task"_s)) return Format::SpirvAssembly; /* https://github.com/KhronosGroup/glslang/blob/3ce148638bdc3807316e358dee4a5c9583189ae7/StandAlone/StandAlone.cpp#L260-L274 */ - else if(Utility::String::endsWith(normalized, ".glsl") || - Utility::String::endsWith(normalized, ".vert") || - Utility::String::endsWith(normalized, ".frag") || - Utility::String::endsWith(normalized, ".geom") || - Utility::String::endsWith(normalized, ".comp") || - Utility::String::endsWith(normalized, ".tesc") || - Utility::String::endsWith(normalized, ".tese") || - Utility::String::endsWith(normalized, ".rgen") || - Utility::String::endsWith(normalized, ".rint") || - Utility::String::endsWith(normalized, ".rahit") || - Utility::String::endsWith(normalized, ".rchit") || - Utility::String::endsWith(normalized, ".rmiss") || - Utility::String::endsWith(normalized, ".rcall") || - Utility::String::endsWith(normalized, ".mesh") || - Utility::String::endsWith(normalized, ".task")) + else if(normalized.hasSuffix(".glsl"_s) || + normalized.hasSuffix(".vert"_s) || + normalized.hasSuffix(".frag"_s) || + normalized.hasSuffix(".geom"_s) || + normalized.hasSuffix(".comp"_s) || + normalized.hasSuffix(".tesc"_s) || + normalized.hasSuffix(".tese"_s) || + normalized.hasSuffix(".rgen"_s) || + normalized.hasSuffix(".rint"_s) || + normalized.hasSuffix(".rahit"_s) || + normalized.hasSuffix(".rchit"_s) || + normalized.hasSuffix(".rmiss"_s) || + normalized.hasSuffix(".rcall"_s) || + normalized.hasSuffix(".mesh"_s) || + normalized.hasSuffix(".task"_s)) return Format::Glsl; - else if(Utility::String::endsWith(normalized, ".spv")) + else if(normalized.hasSuffix(".spv"_s)) return Format::Spirv; Error{} << prefix << "cannot determine the format of" << filename;