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