/**************************************************************************** ** Copyright (c) 2021, Fougue Ltd. ** All rights reserved. ** See license at https://github.com/fougue/mayo/blob/master/LICENSE.txt ****************************************************************************/ #pragma once #include "occ_handle.h" #include #include #include #include #include #include #include namespace Mayo { // Base converter between string types // Note: unicode(utf8/utf16) conversion is preferred when possible template struct StringConv {}; // -- // -- General API // -- // X -> Y // Note: 'InputStringType' should be automatically deduced by the compiler template OutputStringType string_conv(const InputStringType& str) { return StringConv::to(str); } // X -> std::string template std::string to_stdString(const StringType& str) { return string_conv(str); } // X -> std::string_view template std::string_view to_stdStringView(const StringType& str) { return string_conv(str); } // X -> TCollection_AsciiString template TCollection_AsciiString to_OccAsciiString(const StringType& str) { return string_conv(str); } // X -> Handle(TCollection_HAsciiString) template OccHandle to_OccHandleHAsciiString(const StringType& str) { return string_conv>(str); } // X -> TCollection_ExtendedString template TCollection_ExtendedString to_OccExtString(const StringType& str) { return string_conv(str); } // double -> std::string struct DoubleToStringOptions { std::locale locale; int decimalCount = 6; bool removeTrailingZeroes = true; bool roundToZero = true; bool toUtf8 = true; // double zeroPrecision = 0.000000000001; }; class DoubleToStringOperation { public: DoubleToStringOperation(double value); DoubleToStringOperation& locale(const std::locale& l); DoubleToStringOperation& decimalCount(int c); DoubleToStringOperation& removeTrailingZeroes(bool on); DoubleToStringOperation& roundToZero(bool on); DoubleToStringOperation& toUtf8(bool on); operator std::string(); std::string get() const; private: double m_value; DoubleToStringOptions m_opts; }; std::string to_stdString(double value, const DoubleToStringOptions& opts); DoubleToStringOperation to_stdString(double value); // -- // -- Converters(misc) // -- // const char* -> TCollection_ExtendedString template<> struct StringConv { static auto to(const char* str) { return TCollection_ExtendedString(str, true/*multi-byte*/); } }; // const char[N] -> TCollection_ExtendedString template struct StringConv { static auto to(const char(&str)[N]) { return TCollection_ExtendedString(str, true/*multi-byte*/); } }; #if 0 // std::string_view -> TCollection_ExtendedString template<> struct StringConv { static auto to(std::string_view str) { return TCollection_ExtendedString(str.data(), true/*multi-byte*/); } }; #endif // -- // -- std::string_view -> X // -- // std::string_view -> TCollection_AsciiString template<> struct StringConv { static auto to(std::string_view str) { return TCollection_AsciiString(str.data(), int(str.size())); } }; // std::string_view -> Handle(TCollection_HAsciiString) template<> struct StringConv> { static auto to(std::string_view str) { return makeOccHandle(to_OccAsciiString(str)); } }; // std::string_view -> NCollection_Utf8String template<> struct StringConv { static auto to(std::string_view str) { return NCollection_Utf8String(str.data(), static_cast(str.size())); } }; // -- // -- Handle(TCollection_HAsciiString) -> X // -- // Handle(TCollection_HAsciiString) -> std::string template<> struct StringConv, std::string> { static auto to(const OccHandle& str) { return string_conv(str ? str->String() : TCollection_AsciiString()); } }; // Handle(TCollection_HAsciiString) -> std::string_view template<> struct StringConv, std::string_view> { static auto to(const OccHandle& str) { return string_conv(str ? str->String() : TCollection_AsciiString()); } }; // -- // -- std::string -> X // -- // std::string -> TCollection_AsciiString template<> struct StringConv { static auto to(const std::string& str) { return TCollection_AsciiString(str.c_str(), int(str.length())); } }; // std::string -> Handle(TCollection_HAsciiString) template<> struct StringConv> { static auto to(const std::string& str) { return makeOccHandle(str.c_str()); } }; // std::string -> TCollection_ExtendedString template<> struct StringConv { static auto to(const std::string& str) { return TCollection_ExtendedString(str.c_str(), true/*multi-byte*/); } }; // std::string -> NCollection_Utf8String template<> struct StringConv { static auto to(const std::string& str) { return NCollection_Utf8String(str.c_str(), static_cast(str.size())); } }; // -- // -- TCollection_AsciiString -> X // -- // TCollection_AsciiString -> std::string template<> struct StringConv { static auto to(const TCollection_AsciiString& str) { return std::string(str.ToCString(), str.Length()); } }; // TCollection_AsciiString -> std::string_view template<> struct StringConv { static auto to(const TCollection_AsciiString& str) { return std::string_view(str.ToCString(), str.Length()); } }; // -- // -- TCollection_ExtendedString -> X // -- // TCollection_ExtendedString -> std::string template<> struct StringConv { static auto to(const TCollection_ExtendedString& str) { std::string u8; u8.resize(str.LengthOfCString()); char* u8Data = u8.data(); str.ToUTF8CString(u8Data); return u8; } }; // TCollection_ExtendedString -> std::u16string template<> struct StringConv { static auto to(const TCollection_ExtendedString& str) { return std::u16string(str.ToExtString(), str.Length()); } }; // TCollection_ExtendedString -> std::u16string_view template<> struct StringConv { static auto to(const TCollection_ExtendedString& str) { return std::u16string_view(str.ToExtString(), str.Length()); } }; } // namespace Mayo