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 <string_view>
7#include "MRExpected.h"
8#include "MRPch/MRBindingMacros.h"
9
10namespace MR
11{
12
15
17[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::wstring utf8ToWide( const char* utf8 );
18
20[[nodiscard]] MRMESH_API std::string systemToUtf8( const std::string & system );
21
24[[nodiscard]] MRMESH_API std::string utf8ToSystem( const std::string & utf8 );
25
27[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::string wideToUtf8( const wchar_t * wide );
28
29#ifdef _WIN32
31[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::string Utf16ToUtf8( const std::wstring_view & utf16 );
32#endif
33
34#if defined __cpp_lib_char8_t
35
36[[nodiscard]] MR_BIND_IGNORE inline std::string asString( const std::u8string & s ) { return { s.begin(), s.end() }; }
37[[nodiscard]] MR_BIND_IGNORE inline std::u8string asU8String( const std::string & s ) { return { s.begin(), s.end() }; }
38
39#if defined( _LIBCPP_VERSION ) && _LIBCPP_VERSION < 12000
40[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::path( s ); }
41[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::path( std::string( s ) ); }
42#else
43[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::path( asU8String( s ) ); }
44[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::path( asU8String( std::string( s ) ) ); }
45#endif
46
47#else
48
49[[nodiscard]] MR_BIND_IGNORE inline const std::string & asString( const std::string & s ) { return s; }
50[[nodiscard]] MR_BIND_IGNORE inline const std::string & asU8String( const std::string & s ) { return s; }
51
52[[nodiscard]] MR_BIND_IGNORE inline std::string asString( std::string && s ) { return std::move( s ); }
53[[nodiscard]] MR_BIND_IGNORE inline std::string asU8String( std::string && s ) { return std::move( s ); }
54
55[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::u8path( s ); }
56[[nodiscard]] MR_BIND_IGNORE inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::u8path( s ); }
57
58#endif
59
61#if defined( _LIBCPP_VERSION ) && _LIBCPP_VERSION < 12000
62[[nodiscard]] inline std::string utf8string( const std::filesystem::path & path )
63 { return path.u8string(); }
64#else
65[[nodiscard]] inline std::string utf8string( const std::filesystem::path & path )
66 { return asString( path.u8string() ); }
67#endif
68
70std::string utf8string( const std::string & ) = delete;
71
74[[nodiscard]] MRMESH_API std::string utf8substr( const char * s, size_t pos, size_t count );
75
79[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::pair<char32_t, size_t> utf8ToCodepoint( const char* s, size_t size );
80
82[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::u32string utf8ToUtf32( std::string_view str );
83
86MR_BIND_IGNORE MRMESH_API void appendUtf8( std::string& str, char32_t cp );
87
89[[nodiscard]] MR_BIND_IGNORE MRMESH_API std::string utf32ToUtf8( std::u32string_view str );
90
96[[nodiscard]] MRMESH_API std::string bytesString( size_t size );
97
100[[nodiscard]] inline bool isProhibitedChar( char c )
101 { return c == '?' || c == '*' || c == '/' || c == '\\' || c == '"' || c == '<' || c == '>' || c == ':' || c == '|' || (unsigned)c < 32; }
102
104[[nodiscard]] MRMESH_API bool hasProhibitedChars( const std::string& line );
105
107[[nodiscard]] MRMESH_API std::string replaceProhibitedChars( const std::string& line, char replacement = '_' );
108
110template<typename T>
111[[nodiscard]] inline Expected<T> addFileNameInError( Expected<T> v, const std::filesystem::path & file )
112{
113 if ( !v.has_value() )
114 v = unexpected( v.error() + ": " + utf8string( file ) );
115 return v;
116}
117
122[[nodiscard]] MRMESH_API std::string commonFilesName( const std::vector<std::filesystem::path> & files );
123
130[[deprecated("Use `valueToString()` from `MRViewer/MRUnits.h` instead!")]]
131MRMESH_API MR_BIND_IGNORE char * formatNoTrailingZeros( char * fmt, double v, int digitsAfterPoint, int precision = 6 );
132
134[[nodiscard]] MRMESH_API double roundToPrecision( double v, int precision );
135
137[[nodiscard]] inline float roundToPrecision( float v, int precision ) { return (float)roundToPrecision( double(v), precision ); }
138
140[[nodiscard]] inline std::string getCancelMessage( const std::filesystem::path& path )
141{
142 return "Loading canceled: " + utf8string( path );
143}
144
147[[nodiscard]] MR_BIND_IGNORE inline char toLower( char ch )
148{
149 return ( ch >= 'A' && ch <= 'Z' ) ? ch + ( 'a' - 'A' ) : ch;
150}
151
153[[nodiscard]] MR_BIND_IGNORE inline char32_t toLower( char32_t ch )
154{
155 return ( ch >= U'A' && ch <= U'Z' ) ? ch + ( U'a' - U'A' ) : ch;
156}
157
159[[nodiscard]] MRMESH_API std::string toLower( std::string str );
160
162
163}
#define MRMESH_API
Definition MRMeshFwd.h:85
std::string getCancelMessage(const std::filesystem::path &path)
Returns message showed when loading is canceled.
Definition MRStringConvert.h:140
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:111
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
MR_BIND_IGNORE std::string utf32ToUtf8(std::u32string_view str)
Converts the given UTF-32 encoded string to a UTF-8 encoded string.
MR_BIND_IGNORE char toLower(char ch)
Definition MRStringConvert.h:147
std::string utf8string(const std::filesystem::path &path)
returns filename as UTF8-encoded string
Definition MRStringConvert.h:65
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:50
std::string systemToUtf8(const std::string &system)
converts system encoded string to UTF8-encoded string
bool isProhibitedChar(char c)
Definition MRStringConvert.h:100
std::string utf8substr(const char *s, size_t pos, size_t count)
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
MR_BIND_IGNORE std::u32string utf8ToUtf32(std::string_view str)
Converts the given UTF-8 encoded string to a UTF-32 encoded string.
MR_BIND_IGNORE void appendUtf8(std::string &str, char32_t cp)
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:55
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