From 8e013baee352f4a306dab845c9e294f2672ad077 Mon Sep 17 00:00:00 2001 From: Hugo Amiard Date: Wed, 10 Aug 2022 12:11:49 +0200 Subject: [PATCH] Utility: use Corrade String and StringView in ConfigurationValue --- src/Magnum/DebugTools/FrameProfiler.cpp | 10 +-- src/Magnum/DebugTools/FrameProfiler.h | 8 +- src/Magnum/Math/ConfigurationValue.h | 99 +++++++++++++------------ src/Magnum/Mesh.cpp | 10 +-- src/Magnum/Mesh.h | 9 +-- src/Magnum/PixelFormat.cpp | 10 +-- src/Magnum/PixelFormat.h | 9 +-- src/Magnum/VertexFormat.cpp | 6 +- src/Magnum/VertexFormat.h | 5 +- 9 files changed, 83 insertions(+), 83 deletions(-) diff --git a/src/Magnum/DebugTools/FrameProfiler.cpp b/src/Magnum/DebugTools/FrameProfiler.cpp index 34f5196691..db33b80c95 100644 --- a/src/Magnum/DebugTools/FrameProfiler.cpp +++ b/src/Magnum/DebugTools/FrameProfiler.cpp @@ -682,14 +682,14 @@ namespace Corrade { namespace Utility { using namespace Magnum; #ifdef MAGNUM_TARGET_GL -std::string ConfigurationValue::toString(const DebugTools::FrameProfilerGL::Value value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(const DebugTools::FrameProfilerGL::Value value, ConfigurationValueFlags) { const UnsignedInt bit = Math::log2(UnsignedShort(value)); if(1 << bit == UnsignedShort(value)) return DebugTools::FrameProfilerGLValueNames[bit]; return ""; } -DebugTools::FrameProfilerGL::Value ConfigurationValue::fromString(const std::string& value, ConfigurationValueFlags) { +DebugTools::FrameProfilerGL::Value ConfigurationValue::fromString(Containers::StringView value, ConfigurationValueFlags) { for(std::size_t i = 0; i != Containers::arraySize(DebugTools::FrameProfilerGLValueNames); ++i) if(DebugTools::FrameProfilerGLValueNames[i] == value) return DebugTools::FrameProfilerGL::Value(1 << i); @@ -697,7 +697,7 @@ DebugTools::FrameProfilerGL::Value ConfigurationValue::toString(const DebugTools::FrameProfilerGL::Values value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(const DebugTools::FrameProfilerGL::Values value, ConfigurationValueFlags) { std::string out; for(std::size_t i = 0; i != Containers::arraySize(DebugTools::FrameProfilerGLValueNames); ++i) { @@ -711,8 +711,8 @@ std::string ConfigurationValue::toString(co return out; } -DebugTools::FrameProfilerGL::Values ConfigurationValue::fromString(const std::string& value, ConfigurationValueFlags) { - const std::vector bits = Utility::String::splitWithoutEmptyParts(value); +DebugTools::FrameProfilerGL::Values ConfigurationValue::fromString(Containers::StringView value, ConfigurationValueFlags) { + const Containers::Array bits = value.splitWithoutEmptyParts(); DebugTools::FrameProfilerGL::Values values; for(const std::string& bit: bits) diff --git a/src/Magnum/DebugTools/FrameProfiler.h b/src/Magnum/DebugTools/FrameProfiler.h index 1241c68d85..597fc69b16 100644 --- a/src/Magnum/DebugTools/FrameProfiler.h +++ b/src/Magnum/DebugTools/FrameProfiler.h @@ -735,14 +735,14 @@ template<> struct MAGNUM_DEBUGTOOLS_EXPORT ConfigurationValue struct MAGNUM_DEBUGTOOLS_EXPORT ConfigurationValue struct MAGNUM_DEBUGTOOLS_EXPORT ConfigurationValue +#include #include #include "Magnum/Math/Angle.h" @@ -57,12 +57,12 @@ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes degrees as a number */ - static std::string toString(const Magnum::Math::Deg& value, ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::Deg& value, ConfigurationValueFlags flags) { return ConfigurationValue::toString(T(value), flags); } /** @brief Reads degrees as a number */ - static Magnum::Math::Deg fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::Deg fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { return Magnum::Math::Deg(ConfigurationValue::fromString(stringValue, flags)); } }; @@ -72,12 +72,12 @@ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes degrees as a number */ - static std::string toString(const Magnum::Math::Rad& value, ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::Rad& value, ConfigurationValueFlags flags) { return ConfigurationValue::toString(T(value), flags); } /** @brief Reads degrees as a number */ - static Magnum::Math::Rad fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::Rad fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { return Magnum::Math::Rad(ConfigurationValue::fromString(stringValue, flags)); } }; @@ -87,33 +87,34 @@ template struct ConfigurationValue& value, ConfigurationValueFlags flags) { - std::string output; + static Containers::String toString(const Magnum::Math::Vector& value, ConfigurationValueFlags flags) { + Containers::String output; for(std::size_t i = 0; i != size; ++i) { - if(!output.empty()) output += ' '; - output += ConfigurationValue::toString(value[i], flags); + if(!output.isEmpty()) output = output + " "; + output = output + ConfigurationValue::toString(value[i], flags); } return output; } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::Vector fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::Vector fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { Magnum::Math::Vector result; - std::size_t oldpos = 0, pos = std::string::npos, i = 0; + Containers::StringView next, cursor = stringValue; + std::size_t i = 0; do { - pos = stringValue.find(' ', oldpos); - std::string part = stringValue.substr(oldpos, pos-oldpos); + next = cursor.find(" "); + Containers::StringView part = cursor.exceptSuffix(next); - if(!part.empty()) { + if(!part.isEmpty()) { result[i] = ConfigurationValue::fromString(part, flags); ++i; } - oldpos = pos+1; - } while(pos != std::string::npos && i != size); + cursor = cursor.exceptPrefix(next); + } while(cursor); return result; } @@ -158,13 +159,13 @@ template struct ConfigurationValue< ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ - static std::string toString(const Magnum::Math::RectangularMatrix& value, ConfigurationValueFlags flags) { - std::string output; + static Containers::String toString(const Magnum::Math::RectangularMatrix& value, ConfigurationValueFlags flags) { + Containers::String output; for(std::size_t row = 0; row != rows; ++row) { for(std::size_t col = 0; col != cols; ++col) { - if(!output.empty()) output += ' '; - output += ConfigurationValue::toString(value[col][row], flags); + if(!output.isEmpty()) output = output + " "; + output = output + ConfigurationValue::toString(value[col][row], flags); } } @@ -172,21 +173,22 @@ template struct ConfigurationValue< } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::RectangularMatrix fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::RectangularMatrix fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { Magnum::Math::RectangularMatrix result; - std::size_t oldpos = 0, pos = std::string::npos, i = 0; + Containers::StringView next, cursor = stringValue; + std::size_t i = 0; do { - pos = stringValue.find(' ', oldpos); - std::string part = stringValue.substr(oldpos, pos-oldpos); + next = cursor.find(" "); + Containers::StringView part = cursor.exceptSuffix(next); - if(!part.empty()) { + if(!part.isEmpty()) { result[i%cols][i/cols] = ConfigurationValue::fromString(part, flags); ++i; } - oldpos = pos+1; - } while(pos != std::string::npos && i != cols*rows); + cursor = cursor.exceptPrefix(next); + } while(cursor); return result; } @@ -234,13 +236,13 @@ template struct ConfigurationValue& value, const ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::Range& value, const ConfigurationValueFlags flags) { return ConfigurationValue>::toString( reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::Range fromString(const std::string& stringValue, const ConfigurationValueFlags flags) { + static Magnum::Math::Range fromString(Containers::StringView stringValue, const ConfigurationValueFlags flags) { const auto vec = ConfigurationValue>::fromString(stringValue, flags); return *reinterpret_cast*>(vec.data()); } @@ -267,12 +269,12 @@ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ - static std::string toString(const Magnum::Math::Complex& value, ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::Complex& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::Complex fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::Complex fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<2, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } @@ -283,12 +285,12 @@ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ - static std::string toString(const Magnum::Math::DualComplex& value, ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::DualComplex& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::DualComplex fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::DualComplex fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<4, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } @@ -302,12 +304,12 @@ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ - static std::string toString(const Magnum::Math::Quaternion& value, ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::Quaternion& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::Quaternion fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::Quaternion fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<4, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } @@ -321,12 +323,12 @@ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ - static std::string toString(const Magnum::Math::DualQuaternion& value, ConfigurationValueFlags flags) { + static Containers::String toString(const Magnum::Math::DualQuaternion& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::DualQuaternion fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::DualQuaternion fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<8, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } @@ -343,13 +345,13 @@ template str ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ - static std::string toString(const Magnum::Math::Bezier& value, ConfigurationValueFlags flags) { - std::string output; + static Containers::String toString(const Magnum::Math::Bezier& value, ConfigurationValueFlags flags) { + Containers::String output; for(std::size_t o = 0; o != order + 1; ++o) { for(std::size_t i = 0; i != dimensions; ++i) { - if(!output.empty()) output += ' '; - output += ConfigurationValue::toString(value[o][i], flags); + if(!output.isEmpty()) output = output + " "; + output = output + ConfigurationValue::toString(value[o][i], flags); } } @@ -357,21 +359,22 @@ template str } /** @brief Reads elements separated with whitespace */ - static Magnum::Math::Bezier fromString(const std::string& stringValue, ConfigurationValueFlags flags) { + static Magnum::Math::Bezier fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { Magnum::Math::Bezier result; - std::size_t oldpos = 0, pos = std::string::npos, i = 0; + Containers::StringView next, cursor = stringValue; + std::size_t i = 0; do { - pos = stringValue.find(' ', oldpos); - std::string part = stringValue.substr(oldpos, pos-oldpos); + next = cursor.find(" "); + Containers::StringView part = cursor.exceptSuffix(next); - if(!part.empty()) { + if(!part.isEmpty()) { result[i/dimensions][i%dimensions] = ConfigurationValue::fromString(part, flags); ++i; } - oldpos = pos+1; - } while(pos != std::string::npos); + cursor = cursor.exceptPrefix(next); + } while(cursor); return result; } diff --git a/src/Magnum/Mesh.cpp b/src/Magnum/Mesh.cpp index 7fbcf5b8f0..0e30d0538b 100644 --- a/src/Magnum/Mesh.cpp +++ b/src/Magnum/Mesh.cpp @@ -25,7 +25,7 @@ #include "Mesh.h" -#include +#include #include #include #include @@ -105,28 +105,28 @@ UnsignedInt meshIndexTypeSize(const MeshIndexType type) { namespace Corrade { namespace Utility { -std::string ConfigurationValue::toString(Magnum::MeshPrimitive value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(Magnum::MeshPrimitive value, ConfigurationValueFlags) { if(Magnum::UnsignedInt(value) - 1 < Containers::arraySize(Magnum::MeshPrimitiveNames)) return Magnum::MeshPrimitiveNames[Magnum::UnsignedInt(value) - 1]; return {}; } -Magnum::MeshPrimitive ConfigurationValue::fromString(const std::string& stringValue, ConfigurationValueFlags) { +Magnum::MeshPrimitive ConfigurationValue::fromString(Containers::StringView stringValue, ConfigurationValueFlags) { for(std::size_t i = 0; i != Containers::arraySize(Magnum::MeshPrimitiveNames); ++i) if(stringValue == Magnum::MeshPrimitiveNames[i]) return Magnum::MeshPrimitive(i + 1); return {}; } -std::string ConfigurationValue::toString(Magnum::MeshIndexType value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(Magnum::MeshIndexType value, ConfigurationValueFlags) { if(Magnum::UnsignedInt(value) - 1 < Containers::arraySize(Magnum::MeshIndexTypeNames)) return Magnum::MeshIndexTypeNames[Magnum::UnsignedInt(value) - 1]; return {}; } -Magnum::MeshIndexType ConfigurationValue::fromString(const std::string& stringValue, ConfigurationValueFlags) { +Magnum::MeshIndexType ConfigurationValue::fromString(Containers::StringView stringValue, ConfigurationValueFlags) { for(std::size_t i = 0; i != Containers::arraySize(Magnum::MeshIndexTypeNames); ++i) if(stringValue == Magnum::MeshIndexTypeNames[i]) return Magnum::MeshIndexType(i + 1); diff --git a/src/Magnum/Mesh.h b/src/Magnum/Mesh.h index 3902ba5c52..3317547939 100644 --- a/src/Magnum/Mesh.h +++ b/src/Magnum/Mesh.h @@ -30,7 +30,6 @@ */ #include -#include #include "Magnum/Magnum.h" #include "Magnum/visibility.h" @@ -365,14 +364,14 @@ template<> struct MAGNUM_EXPORT ConfigurationValue { * * If the value is invalid, returns empty string. */ - static std::string toString(Magnum::MeshPrimitive value, ConfigurationValueFlags); + static Containers::String toString(Magnum::MeshPrimitive value, ConfigurationValueFlags); /** * @brief Reads enum value as string * * If the value is invalid, returns a zero (invalid) primitive. */ - static Magnum::MeshPrimitive fromString(const std::string& stringValue, ConfigurationValueFlags); + static Magnum::MeshPrimitive fromString(Containers::StringView stringValue, ConfigurationValueFlags); }; /** @configurationvalue{Magnum::MeshIndexType} */ @@ -384,14 +383,14 @@ template<> struct MAGNUM_EXPORT ConfigurationValue { * * If the value is invalid, returns empty string. */ - static std::string toString(Magnum::MeshIndexType value, ConfigurationValueFlags); + static Containers::String toString(Magnum::MeshIndexType value, ConfigurationValueFlags); /** * @brief Read enum value as string * * If the value is invalid, returns a zero (invalid) type. */ - static Magnum::MeshIndexType fromString(const std::string& stringValue, ConfigurationValueFlags); + static Magnum::MeshIndexType fromString(Containers::StringView stringValue, ConfigurationValueFlags); }; }} diff --git a/src/Magnum/PixelFormat.cpp b/src/Magnum/PixelFormat.cpp index da887735ae..40afec2531 100644 --- a/src/Magnum/PixelFormat.cpp +++ b/src/Magnum/PixelFormat.cpp @@ -25,7 +25,7 @@ #include "PixelFormat.h" -#include +#include #include #include #include @@ -556,14 +556,14 @@ Debug& operator<<(Debug& debug, const CompressedPixelFormat value) { namespace Corrade { namespace Utility { -std::string ConfigurationValue::toString(Magnum::PixelFormat value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(Magnum::PixelFormat value, ConfigurationValueFlags) { if(Magnum::UnsignedInt(value) - 1 < Containers::arraySize(Magnum::PixelFormatNames)) return Magnum::PixelFormatNames[Magnum::UnsignedInt(value) - 1]; return {}; } -Magnum::PixelFormat ConfigurationValue::fromString(const std::string& stringValue, ConfigurationValueFlags) { +Magnum::PixelFormat ConfigurationValue::fromString(Containers::StringView stringValue, ConfigurationValueFlags) { /** @todo This is extremely slow with >100 values. Do a binary search on a sorted index list instead (extracted into a common utility) */ for(std::size_t i = 0; i != Containers::arraySize(Magnum::PixelFormatNames); ++i) @@ -572,14 +572,14 @@ Magnum::PixelFormat ConfigurationValue::fromString(const st return {}; } -std::string ConfigurationValue::toString(Magnum::CompressedPixelFormat value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(Magnum::CompressedPixelFormat value, ConfigurationValueFlags) { if(Magnum::UnsignedInt(value) - 1 < Containers::arraySize(Magnum::CompressedPixelFormatNames)) return Magnum::CompressedPixelFormatNames[Magnum::UnsignedInt(value) - 1]; return {}; } -Magnum::CompressedPixelFormat ConfigurationValue::fromString(const std::string& stringValue, ConfigurationValueFlags) { +Magnum::CompressedPixelFormat ConfigurationValue::fromString(Containers::StringView stringValue, ConfigurationValueFlags) { /** @todo This is extremely slow with >100 values. Do a binary search on a sorted index list instead (extracted into a common utility) */ for(std::size_t i = 0; i != Containers::arraySize(Magnum::CompressedPixelFormatNames); ++i) diff --git a/src/Magnum/PixelFormat.h b/src/Magnum/PixelFormat.h index 1cf1cd2983..290f5aa28e 100644 --- a/src/Magnum/PixelFormat.h +++ b/src/Magnum/PixelFormat.h @@ -30,7 +30,6 @@ */ #include -#include #include "Magnum/Magnum.h" #include "Magnum/visibility.h" @@ -2491,14 +2490,14 @@ template<> struct MAGNUM_EXPORT ConfigurationValue { * * If the value is invalid, returns empty string. */ - static std::string toString(Magnum::PixelFormat value, ConfigurationValueFlags); + static Containers::String toString(Magnum::PixelFormat value, ConfigurationValueFlags); /** * @brief Reads enum value as string * * If the value is invalid, returns a zero (invalid) format. */ - static Magnum::PixelFormat fromString(const std::string& stringValue, ConfigurationValueFlags); + static Magnum::PixelFormat fromString(Containers::StringView stringValue, ConfigurationValueFlags); }; /** @@ -2513,14 +2512,14 @@ template<> struct MAGNUM_EXPORT ConfigurationValue +#include #include #include #include @@ -930,14 +930,14 @@ Debug& operator<<(Debug& debug, const VertexFormat value) { namespace Corrade { namespace Utility { -std::string ConfigurationValue::toString(Magnum::VertexFormat value, ConfigurationValueFlags) { +Containers::String ConfigurationValue::toString(Magnum::VertexFormat value, ConfigurationValueFlags) { if(Magnum::UnsignedInt(value) - 1 < Containers::arraySize(Magnum::VertexFormatNames)) return Magnum::VertexFormatNames[Magnum::UnsignedInt(value) - 1]; return {}; } -Magnum::VertexFormat ConfigurationValue::fromString(const std::string& stringValue, ConfigurationValueFlags) { +Magnum::VertexFormat ConfigurationValue::fromString(Containers::StringView stringValue, ConfigurationValueFlags) { for(std::size_t i = 0; i != Containers::arraySize(Magnum::VertexFormatNames); ++i) if(stringValue == Magnum::VertexFormatNames[i]) return Magnum::VertexFormat(i + 1); diff --git a/src/Magnum/VertexFormat.h b/src/Magnum/VertexFormat.h index 9cbcc91084..56dcbc3d2c 100644 --- a/src/Magnum/VertexFormat.h +++ b/src/Magnum/VertexFormat.h @@ -30,7 +30,6 @@ */ #include -#include #include "Magnum/Magnum.h" #include "Magnum/visibility.h" @@ -1473,14 +1472,14 @@ template<> struct MAGNUM_EXPORT ConfigurationValue { * * If the value is invalid, returns empty string. */ - static std::string toString(Magnum::VertexFormat value, ConfigurationValueFlags); + static Containers::String toString(Magnum::VertexFormat value, ConfigurationValueFlags); /** * @brief Read enum value as string * * If the value is invalid, returns a zero (invalid) format. */ - static Magnum::VertexFormat fromString(const std::string& stringValue, ConfigurationValueFlags); + static Magnum::VertexFormat fromString(Containers::StringView stringValue, ConfigurationValueFlags); }; }}