Radium Engine  1.5.20
Loading...
Searching...
No Matches
StringUtils.cpp
1#include <Core/Utils/StringUtils.hpp>
2
3/*
4 * NOMINMAX is already defined in CoreMacros.hpp ...
5
6#ifdef COMPILER_MSVC
7# define NOMINMAX // Avoid C2039 MSVC compiler error
8//#undef vsnprintf
9//#define vsnprintf(buffer, count, format, argptr) vsnprintf_s(buffer, count, count, format, argptr)
10#endif
11*/
12
13#include <memory>
14
15namespace Ra {
16namespace Core {
17namespace Utils {
18
19std::string getFileExt( const std::string& str ) {
20 auto pos = str.find_last_of( '.' );
21 std::string res = pos < str.length() ? str.substr( pos + 1 ) : "";
22 return res;
23}
24
25std::string getDirName( const std::string& path ) {
26 // We remove any trailing slashes.
27 auto pos = path.find_last_not_of( '/' );
28
29 // Don't strip the last / from "/"
30 if ( pos >= path.length() ) { pos = path.find_first_of( "/" ); }
31 std::string res = path.substr( 0, pos + 1 );
32
33 // Now find the previous slash and cut the string.
34 pos = res.find_last_of( '/' );
35
36 // The directory is actually "/" because the last slash is in first position.
37 // In that case we should return "/"
38 if ( pos == 0 ) { res = "/"; }
39 else if ( pos < res.length() ) { res.resize( pos ); }
40 else { res = "."; }
41
42 return res;
43}
44
45std::string getBaseName( const std::string& path, bool keepExtension ) {
46 // We remove any trailing slashes.
47 auto pos = path.find_last_not_of( '/' );
48 // Don't strip the last / from "/"
49 if ( pos >= path.length() ) { pos = path.find_first_of( "/" ); }
50 std::string res = path.substr( 0, pos + 1 );
51
52 // Now find the previous slash and cut the string.
53 pos = res.find_last_of( '/' );
54 if ( pos < res.length() ) { res = res.substr( pos + 1 ); }
55 if ( !keepExtension ) {
56 pos = res.find_last_of( '.' );
57 if ( pos < res.length() ) { res.resize( pos ); }
58 }
59
60 return res;
61}
62
63std::size_t replaceAllInString( std::string& inout, std::string_view what, std::string_view with ) {
65 for ( std::string::size_type pos {};
66 inout.npos != ( pos = inout.find( what.data(), pos, what.length() ) );
67 pos += with.length(), ++count ) {
68 inout.replace( pos, what.length(), with.data(), with.length() );
69 }
70 return count;
71}
72
73std::size_t removeAllInString( std::string& inout, std::string_view what ) {
74 return replaceAllInString( inout, what, "" );
75}
76} // namespace Utils
77} // namespace Core
78} // namespace Ra
T count(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)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T replace(T... args)
T resize(T... args)
T length(T... args)
T substr(T... args)