Loading [MathJax]/jax/output/HTML-CSS/config.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
15 namespace Ra {
16 namespace Core {
17 namespace Utils {
18 
19 std::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 
25 std::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 
45 std::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 
63 std::size_t replaceAllInString( std::string& inout, std::string_view what, std::string_view with ) {
64  std::size_t count {};
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 
73 std::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
Definition: Cage.cpp:3