Radium Engine  1.7.2
Loading...
Searching...
No Matches
StringUtils.cpp
1#include <Core/Utils/StringUtils.hpp>
2#include <cstddef>
3#include <map>
4#include <string>
5#include <string_view>
6
7/*
8 * NOMINMAX is already defined in CoreMacros.hpp ...
9
10#ifdef COMPILER_MSVC
11# define NOMINMAX // Avoid C2039 MSVC compiler error
12//#undef vsnprintf
13//#define vsnprintf(buffer, count, format, argptr) vsnprintf_s(buffer, count, count, format, argptr)
14#endif
15*/
16
17namespace Ra {
18namespace Core {
19namespace Utils {
20
21std::string getFileExt( const std::string& str ) {
22 auto pos = str.find_last_of( '.' );
23 std::string res = pos < str.length() ? str.substr( pos + 1 ) : "";
24 return res;
25}
26
27std::string getDirName( const std::string& path ) {
28 // We remove any trailing slashes.
29 auto pos = path.find_last_not_of( '/' );
30
31 // Don't strip the last / from "/"
32 if ( pos >= path.length() ) { pos = path.find_first_of( "/" ); }
33 std::string res = path.substr( 0, pos + 1 );
34
35 // Now find the previous slash and cut the string.
36 pos = res.find_last_of( '/' );
37
38 // The directory is actually "/" because the last slash is in first position.
39 // In that case we should return "/"
40 if ( pos == 0 ) { res = "/"; }
41 else if ( pos < res.length() ) { res.resize( pos ); }
42 else { res = "."; }
43
44 return res;
45}
46
47std::string getBaseName( const std::string& path, bool keepExtension ) {
48 // We remove any trailing slashes.
49 auto pos = path.find_last_not_of( '/' );
50 // Don't strip the last / from "/"
51 if ( pos >= path.length() ) { pos = path.find_first_of( "/" ); }
52 std::string res = path.substr( 0, pos + 1 );
53
54 // Now find the previous slash and cut the string.
55 pos = res.find_last_of( '/' );
56 if ( pos < res.length() ) { res = res.substr( pos + 1 ); }
57 if ( !keepExtension ) {
58 pos = res.find_last_of( '.' );
59 if ( pos < res.length() ) { res.resize( pos ); }
60 }
61
62 return res;
63}
64
65std::size_t replaceAllInString( std::string& inout, std::string_view what, std::string_view with ) {
67 for ( std::string::size_type pos {};
68 inout.npos != ( pos = inout.find( what.data(), pos, what.length() ) );
69 pos += with.length(), ++count ) {
70 inout.replace( pos, what.length(), with.data(), with.length() );
71 }
72 return count;
73}
74
75std::size_t removeAllInString( std::string& inout, std::string_view what ) {
76 return replaceAllInString( inout, what, "" );
77}
78
79void remove_bracket_block( std::string& inout, const std::string& what, char open_sep ) {
80 replace_bracket_block( inout, what, "", open_sep );
81}
82
83void replace_bracket_block( std::string& inout,
84 const std::string& what,
85 const std::string& with,
86 char open_sep ) {
87 const std::map<char, char> open_to_close = {
88 { '<', '>' },
89 { '(', ')' },
90 { '[', ']' },
91 { '{', '}' },
92 };
93
94 const auto it = open_to_close.find( open_sep );
95 if ( it == open_to_close.end() ) throw std::invalid_argument( "Unsupported open separator" );
96
97 const char close_sep = it->second;
98
99 std::string target = what + open_sep;
100
101 size_t i = 0;
102
103 for ( size_t pos = inout.find( target, i ); pos != std::string::npos;
104 pos = inout.find( target, i ) ) {
105
106 size_t j = pos + target.size();
107 int depth = 1;
108 while ( j < inout.size() && depth > 0 ) {
109 if ( inout[j] == open_sep )
110 ++depth;
111 else if ( inout[j] == close_sep )
112 --depth;
113 ++j;
114 }
115 // If unmatched open_sep, abort safely
116 if ( depth != 0 ) break;
117
118 while ( j < inout.size() && std::isspace( static_cast<unsigned char>( inout[j] ) ) ) {
119 ++j;
120 }
121 // Replace the full "what<...>" block with "with"
122 inout.replace( pos, j - pos, with );
123 }
124}
125
126} // namespace Utils
127} // namespace Core
128} // namespace Ra
T count(T... args)
T end(T... args)
T find_first_of(T... args)
T find(T... args)
T find_last_not_of(T... args)
T find_last_of(T... args)
T isspace(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4
T replace(T... args)
T resize(T... args)
T length(T... args)
T substr(T... args)