MeshLib C++ Docs
Loading...
Searching...
No Matches
MRStringConvert.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include <filesystem>
5#include <string>
6#include "MRExpected.h"
7#include "MRPch/MRBindingMacros.h"
8
9namespace MR
10{
11
14
16[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::wstring utf8ToWide( const char* utf8 );
17
19[[nodiscard]] MRMESH_API std::string systemToUtf8( const std::string & system );
20
23[[nodiscard]] MRMESH_API std::string utf8ToSystem( const std::string & utf8 );
24
26[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::string wideToUtf8( const wchar_t * wide );
27
28#ifdef _WIN32
30[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::string Utf16ToUtf8( const std::wstring_view & utf16 );
31#endif
32
33#if defined __cpp_lib_char8_t
34
35[[nodiscard]] MR_BIND_IGNORE inline std::string asString( const std::u8string & s ) { return { s.begin(), s.end() }; }
36[[nodiscard]] MR_BIND_IGNORE inline std::u8string asU8String( const std::string & s ) { return { s.begin(), s.end() }; }
37
38#if defined( _LIBCPP_VERSION ) && _LIBCPP_VERSION < 12000
39[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::path( s ); }
40[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::path( std::string( s ) ); }
41#else
42[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::path( asU8String( s ) ); }
43[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::path( asU8String( std::string( s ) ) ); }
44#endif
45
46#else
47
48[[nodiscard]] MR_BIND_IGNORE inline const std::string & asString( const std::string & s ) { return s; }
49[[nodiscard]] MR_BIND_IGNORE inline const std::string & asU8String( const std::string & s ) { return s; }
50
51[[nodiscard]] MR_BIND_IGNORE inline std::string asString( std::string && s ) { return std::move( s ); }
52[[nodiscard]] MR_BIND_IGNORE inline std::string asU8String( std::string && s ) { return std::move( s ); }
53
54[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::u8path( s ); }
55[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::u8path( s ); }
56
57#endif
58
60#if defined( _LIBCPP_VERSION ) && _LIBCPP_VERSION < 12000
61[[nodiscard]] inline std::string utf8string( const std::filesystem::path & path )
62 { return path.u8string(); }
63#else
64[[nodiscard]] inline std::string utf8string( const std::filesystem::path & path )
65 { return asString( path.u8string() ); }
66#endif
67
69std::string utf8string( const std::string & ) = delete;
70
73[[nodiscard]] MRMESH_API std::string utf8substr( const char * s, size_t pos, size_t count );
74
78[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::pair<char32_t, size_t> utf8ToCodepoint( const char* s, size_t size );
79
81[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::u32string utf8ToUtf32( const std::string& str );
82
88[[nodiscard]] MRMESH_API std::string bytesString( size_t size );
89
92[[nodiscard]] inline bool isProhibitedChar( char c )
93 { return c == '?' || c == '*' || c == '/' || c == '\\' || c == '"' || c == '<' || c == '>' || c == ':' || c == '|' || (unsigned)c < 32; }
94
96[[nodiscard]] MRMESH_API bool hasProhibitedChars( const std::string& line );
97
99[[nodiscard]] MRMESH_API std::string replaceProhibitedChars( const std::string& line, char replacement = '_' );
100
102template<typename T>
103[[nodiscard]] inline Expected<T> addFileNameInError( Expected<T> v, const std::filesystem::path & file )
104{
105 if ( !v.has_value() )
106 v = unexpected( v.error() + ": " + utf8string( file ) );
107 return v;
108}
109
114[[nodiscard]] MRMESH_API std::string commonFilesName( const std::vector<std::filesystem::path> & files );
115
122[[deprecated("Use `valueToString()` from `MRViewer/MRUnits.h` instead!")]]
123MRMESH_API MR_BIND_IGNORE char * formatNoTrailingZeros( char * fmt, double v, int digitsAfterPoint, int precision = 6 );
124
126[[nodiscard]] MRMESH_API double roundToPrecision( double v, int precision );
127
129[[nodiscard]] inline float roundToPrecision( float v, int precision ) { return (float)roundToPrecision( double(v), precision ); }
130
132[[nodiscard]] inline std::string getCancelMessage( const std::filesystem::path& path )
133{
134 return "Loading canceled: " + utf8string( path );
135}
136
138[[nodiscard]] MRMESH_API std::string toLower( std::string str );
139
141
142}
#define MRMESH_API
Definition MRMeshFwd.h:82
std::string getCancelMessage(const std::filesystem::path &path)
Returns message showed when loading is canceled.
Definition MRStringConvert.h:132
std::string utf8ToSystem(const std::string &utf8)
Expected< T > addFileNameInError(Expected< T > v, const std::filesystem::path &file)
if (v) contains an error, then appends given file name to that error
Definition MRStringConvert.h:103
MR_BIND_IGNORE std::pair< char32_t, size_t > utf8ToCodepoint(const char *s, size_t size)
bool hasProhibitedChars(const std::string &line)
returns true if line contains at least one character (c) for which isProhibitedChar(c)==true
std::string toLower(std::string str)
return a copy of the string with all alphabetic ASCII characters replaced with upper-case variants
std::string utf8string(const std::filesystem::path &path)
returns filename as UTF8-encoded string
Definition MRStringConvert.h:64
std::string replaceProhibitedChars(const std::string &line, char replacement='_')
replace all characters (c), where isProhibitedChar(c)==true, with replacement char
MR_BIND_IGNORE const std::string & asU8String(const std::string &s)
Definition MRStringConvert.h:49
std::string systemToUtf8(const std::string &system)
converts system encoded string to UTF8-encoded string
bool isProhibitedChar(char c)
Definition MRStringConvert.h:92
std::string utf8substr(const char *s, size_t pos, size_t count)
MR_BIND_IGNORE std::u32string utf8ToUtf32(const std::string &str)
Converts the given UTF-8 encoded string to a UTF-32 encoded string.
std::string commonFilesName(const std::vector< std::filesystem::path > &files)
MR_BIND_IGNORE std::wstring utf8ToWide(const char *utf8)
converts UTF8-encoded string into UTF16-encoded string
MR_BIND_IGNORE std::string wideToUtf8(const wchar_t *wide)
converts wide null terminating string to UTF8-encoded string
double roundToPrecision(double v, int precision)
returns given value rounded to given number of decimal digits
std::string bytesString(size_t size)
MR_BIND_IGNORE char * formatNoTrailingZeros(char *fmt, double v, int digitsAfterPoint, int precision=6)
MR_BIND_IGNORE std::filesystem::path pathFromUtf8(const std::string &s)
Definition MRStringConvert.h:54
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
MR_BIND_IGNORE auto unexpected(E &&e)
Definition MRExpected.h:37
tl::expected< T, E > Expected
Definition MRExpected.h:31
const char * asString(ColoringType ct)
returns string representation of enum values
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:13
only for bindings generation
Definition MRCameraOrientationPlugin.h:8