MeshLib Documentation
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
8namespace MR
9{
10
13
15[[nodiscard]] MRMESH_API std::wstring utf8ToWide( const char* utf8 );
16
18[[nodiscard]] MRMESH_API std::string systemToUtf8( const std::string & system );
19
22[[nodiscard]] MRMESH_API std::string utf8ToSystem( const std::string & utf8 );
23
25[[nodiscard]] MRMESH_API std::string wideToUtf8( const wchar_t * wide );
26
27#ifdef _WIN32
29[[nodiscard]] MRMESH_API std::string Utf16ToUtf8( const std::wstring_view & utf16 );
30#endif
31
32#if defined __cpp_lib_char8_t
33
34[[nodiscard]] inline std::string asString( const std::u8string & s ) { return { s.begin(), s.end() }; }
35[[nodiscard]] inline std::u8string asU8String( const std::string & s ) { return { s.begin(), s.end() }; }
36
37#if defined( _LIBCPP_VERSION ) && _LIBCPP_VERSION < 12000
38[[nodiscard]] inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::path( s ); }
39[[nodiscard]] inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::path( std::string( s ) ); }
40#else
41[[nodiscard]] inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::path( asU8String( s ) ); }
42[[nodiscard]] inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::path( asU8String( std::string( s ) ) ); }
43#endif
44
45#else // std::u8string is not defined
46
47[[nodiscard]] inline const std::string & asString( const std::string & s ) { return s; }
48[[nodiscard]] inline const std::string & asU8String( const std::string & s ) { return s; }
49
50[[nodiscard]] inline std::string asString( std::string && s ) { return std::move( s ); }
51[[nodiscard]] inline std::string asU8String( std::string && s ) { return std::move( s ); }
52
53[[nodiscard]] inline std::filesystem::path pathFromUtf8( const std::string & s ) { return std::filesystem::u8path( s ); }
54[[nodiscard]] inline std::filesystem::path pathFromUtf8( const char * s ) { return std::filesystem::u8path( s ); }
55
56#endif
57
59#if defined( _LIBCPP_VERSION ) && _LIBCPP_VERSION < 12000
60[[nodiscard]] inline std::string utf8string( const std::filesystem::path & path )
61 { return path.u8string(); }
62#else
63[[nodiscard]] inline std::string utf8string( const std::filesystem::path & path )
64 { return asString( path.u8string() ); }
65#endif
66
68
74[[nodiscard]] MRMESH_API std::string bytesString( size_t size );
75
77[[nodiscard]] MRMESH_API bool hasProhibitedChars( const std::string& line );
78
80[[nodiscard]] MRMESH_API std::string replaceProhibitedChars( const std::string& line, char replacement = '_' );
81
83template<typename T>
84[[nodiscard]] inline Expected<T> addFileNameInError( Expected<T> v, const std::filesystem::path & file )
85{
86 if ( !v.has_value() )
87 v = unexpected( v.error() + ": " + utf8string( file ) );
88 return v;
89}
90
95[[nodiscard]] MRMESH_API std::string commonFilesName( const std::vector<std::filesystem::path> & files );
96
103[[deprecated("Use `valueToString()` from `MRViewer/MRUnits.h` instead!")]]
104MRMESH_API char * formatNoTrailingZeros( char * fmt, double v, int digitsAfterPoint, int precision = 6 );
105
107[[nodiscard]] MRMESH_API double roundToPrecision( double v, int precision );
108
110[[nodiscard]] inline float roundToPrecision( float v, int precision ) { return (float)roundToPrecision( double(v), precision ); }
111
112// Returns message showed when loading is canceled
113[[nodiscard]] inline std::string getCancelMessage( const std::filesystem::path& path )
114{
115 return "Loading canceled: " + utf8string( path );
116}
117
119[[nodiscard]] MRMESH_API std::string toLower( std::string str );
120
121} //namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
MRMESH_API std::string utf8ToSystem(const std::string &utf8)
std::filesystem::path pathFromUtf8(const std::string &s)
Definition MRStringConvert.h:53
const std::string & asU8String(const std::string &s)
Definition MRStringConvert.h:48
MRMESH_API std::wstring utf8ToWide(const char *utf8)
converts UTF8-encoded string into UTF16-encoded string
std::string utf8string(const std::filesystem::path &path)
returns filename as UTF8-encoded string
Definition MRStringConvert.h:63
MRMESH_API std::string systemToUtf8(const std::string &system)
converts system encoded string to UTF8-encoded string
MRMESH_API std::string wideToUtf8(const wchar_t *wide)
converts wide null terminating string to UTF8-encoded string
Definition MRCameraOrientationPlugin.h:8
std::string getCancelMessage(const std::filesystem::path &path)
Definition MRStringConvert.h:113
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
MR_BIND_IGNORE auto unexpected(E &&e)
Definition MRExpected.h:61
MRMESH_API std::string bytesString(size_t size)
MRMESH_API char * formatNoTrailingZeros(char *fmt, double v, int digitsAfterPoint, int precision=6)
MRMESH_API double roundToPrecision(double v, int precision)
returns given value rounded to given number of decimal digits
MRMESH_API bool hasProhibitedChars(const std::string &line)
returns true if line contains any of OS prohibited chars ('?', '*', '/', '\', '"',...
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:84
MRMESH_API std::string toLower(std::string str)
return a copy of the string with all alphabetic characters replaced with upper-case variants
tl::expected< T, E > Expected
Definition MRExpected.h:58
MRMESH_API std::string replaceProhibitedChars(const std::string &line, char replacement='_')
replace OS prohibited chars ('?', '*', '/', '\', '"', '<', '>') with replacement char
MRMESH_API std::string commonFilesName(const std::vector< std::filesystem::path > &files)
MRMESH_API const char * asString(SignDetectionMode m)
returns string representation of enum values