summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/file_util.cpp82
-rw-r--r--src/common/file_util.h13
2 files changed, 55 insertions, 40 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 47ac8368e..13fc31bfc 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -802,66 +802,80 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
802 } 802 }
803} 803}
804 804
805std::vector<std::string> SplitPathComponents(const std::string& filename) { 805std::vector<std::string> SplitPathComponents(std::string_view filename) {
806 auto copy(filename); 806 std::string copy(filename);
807 std::replace(copy.begin(), copy.end(), '\\', '/'); 807 std::replace(copy.begin(), copy.end(), '\\', '/');
808 std::vector<std::string> out; 808 std::vector<std::string> out;
809 809
810 std::stringstream stream(filename); 810 std::stringstream stream(copy);
811 std::string item; 811 std::string item;
812 while (std::getline(stream, item, '/')) 812 while (std::getline(stream, item, '/')) {
813 out.push_back(std::move(item)); 813 out.push_back(std::move(item));
814 }
814 815
815 return out; 816 return out;
816} 817}
817 818
818std::string GetParentPath(const std::string& path) { 819std::string_view GetParentPath(std::string_view path) {
819 auto out = path; 820 const auto name_bck_index = path.rfind('\\');
820 const auto name_bck_index = out.find_last_of('\\'); 821 const auto name_fwd_index = path.rfind('/');
821 const auto name_fwd_index = out.find_last_of('/');
822 size_t name_index; 822 size_t name_index;
823 if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos)
824 name_index = std::min<size_t>(name_bck_index, name_fwd_index);
825 else
826 name_index = std::max<size_t>(name_bck_index, name_fwd_index);
827 823
828 return out.erase(name_index); 824 if (name_bck_index == std::string_view::npos || name_fwd_index == std::string_view::npos) {
825 name_index = std::min(name_bck_index, name_fwd_index);
826 } else {
827 name_index = std::max(name_bck_index, name_fwd_index);
828 }
829
830 return path.substr(0, name_index);
829} 831}
830 832
831std::string GetPathWithoutTop(std::string path) { 833std::string_view GetPathWithoutTop(std::string_view path) {
832 if (path.empty()) 834 if (path.empty()) {
833 return ""; 835 return path;
836 }
837
834 while (path[0] == '\\' || path[0] == '/') { 838 while (path[0] == '\\' || path[0] == '/') {
835 path = path.substr(1); 839 path.remove_suffix(1);
836 if (path.empty()) 840 if (path.empty()) {
837 return ""; 841 return path;
842 }
838 } 843 }
839 const auto name_bck_index = path.find_first_of('\\'); 844
840 const auto name_fwd_index = path.find_first_of('/'); 845 const auto name_bck_index = path.find('\\');
846 const auto name_fwd_index = path.find('/');
841 return path.substr(std::min(name_bck_index, name_fwd_index) + 1); 847 return path.substr(std::min(name_bck_index, name_fwd_index) + 1);
842} 848}
843 849
844std::string GetFilename(std::string path) { 850std::string_view GetFilename(std::string_view path) {
845 std::replace(path.begin(), path.end(), '\\', '/'); 851 const auto name_index = path.find_last_of("\\/");
846 auto name_index = path.find_last_of('/'); 852
847 if (name_index == std::string::npos) 853 if (name_index == std::string_view::npos) {
848 return ""; 854 return {};
855 }
856
849 return path.substr(name_index + 1); 857 return path.substr(name_index + 1);
850} 858}
851 859
852std::string GetExtensionFromFilename(const std::string& name) { 860std::string_view GetExtensionFromFilename(std::string_view name) {
853 size_t index = name.find_last_of('.'); 861 const size_t index = name.rfind('.');
854 if (index == std::string::npos) 862
855 return ""; 863 if (index == std::string_view::npos) {
864 return {};
865 }
856 866
857 return name.substr(index + 1); 867 return name.substr(index + 1);
858} 868}
859 869
860std::string RemoveTrailingSlash(const std::string& path) { 870std::string_view RemoveTrailingSlash(std::string_view path) {
861 if (path.empty()) 871 if (path.empty()) {
862 return path; 872 return path;
863 if (path.back() == '\\' || path.back() == '/') 873 }
864 return path.substr(0, path.size() - 1); 874
875 if (path.back() == '\\' || path.back() == '/') {
876 path.remove_suffix(1);
877 return path;
878 }
865 879
866 return path; 880 return path;
867} 881}
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 090907c03..abfa79eae 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -9,6 +9,7 @@
9#include <fstream> 9#include <fstream>
10#include <functional> 10#include <functional>
11#include <string> 11#include <string>
12#include <string_view>
12#include <type_traits> 13#include <type_traits>
13#include <vector> 14#include <vector>
14#include "common/common_types.h" 15#include "common/common_types.h"
@@ -151,22 +152,22 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
151 152
152// Splits the path on '/' or '\' and put the components into a vector 153// Splits the path on '/' or '\' and put the components into a vector
153// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } 154// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" }
154std::vector<std::string> SplitPathComponents(const std::string& filename); 155std::vector<std::string> SplitPathComponents(std::string_view filename);
155 156
156// Gets all of the text up to the last '/' or '\' in the path. 157// Gets all of the text up to the last '/' or '\' in the path.
157std::string GetParentPath(const std::string& path); 158std::string_view GetParentPath(std::string_view path);
158 159
159// Gets all of the text after the first '/' or '\' in the path. 160// Gets all of the text after the first '/' or '\' in the path.
160std::string GetPathWithoutTop(std::string path); 161std::string_view GetPathWithoutTop(std::string_view path);
161 162
162// Gets the filename of the path 163// Gets the filename of the path
163std::string GetFilename(std::string path); 164std::string_view GetFilename(std::string_view path);
164 165
165// Gets the extension of the filename 166// Gets the extension of the filename
166std::string GetExtensionFromFilename(const std::string& name); 167std::string_view GetExtensionFromFilename(std::string_view name);
167 168
168// Removes the final '/' or '\' if one exists 169// Removes the final '/' or '\' if one exists
169std::string RemoveTrailingSlash(const std::string& path); 170std::string_view RemoveTrailingSlash(std::string_view path);
170 171
171// Creates a new vector containing indices [first, last) from the original. 172// Creates a new vector containing indices [first, last) from the original.
172template <typename T> 173template <typename T>