summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2018-07-22 11:32:28 -0700
committerGravatar GitHub2018-07-22 11:32:28 -0700
commit5ee4c49c3049497fe1880e7edd722b931c688dee (patch)
treeb19bbe84497f300318b99ceaf2ec2aedf04ac6b5 /src/common/file_util.cpp
parentMerge pull request #770 from lioncash/construct (diff)
parentvfs: Correct file_p variable usage within InterpretAsDirectory() (diff)
downloadyuzu-5ee4c49c3049497fe1880e7edd722b931c688dee.tar.gz
yuzu-5ee4c49c3049497fe1880e7edd722b931c688dee.tar.xz
yuzu-5ee4c49c3049497fe1880e7edd722b931c688dee.zip
Merge pull request #768 from lioncash/string-view
file_util, vfs: Use std::string_view where applicable
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r--src/common/file_util.cpp82
1 files changed, 48 insertions, 34 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index a427372c9..1bc291cf9 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -792,66 +792,80 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
792 } 792 }
793} 793}
794 794
795std::vector<std::string> SplitPathComponents(const std::string& filename) { 795std::vector<std::string> SplitPathComponents(std::string_view filename) {
796 auto copy(filename); 796 std::string copy(filename);
797 std::replace(copy.begin(), copy.end(), '\\', '/'); 797 std::replace(copy.begin(), copy.end(), '\\', '/');
798 std::vector<std::string> out; 798 std::vector<std::string> out;
799 799
800 std::stringstream stream(filename); 800 std::stringstream stream(copy);
801 std::string item; 801 std::string item;
802 while (std::getline(stream, item, '/')) 802 while (std::getline(stream, item, '/')) {
803 out.push_back(std::move(item)); 803 out.push_back(std::move(item));
804 }
804 805
805 return out; 806 return out;
806} 807}
807 808
808std::string GetParentPath(const std::string& path) { 809std::string_view GetParentPath(std::string_view path) {
809 auto out = path; 810 const auto name_bck_index = path.rfind('\\');
810 const auto name_bck_index = out.find_last_of('\\'); 811 const auto name_fwd_index = path.rfind('/');
811 const auto name_fwd_index = out.find_last_of('/');
812 size_t name_index; 812 size_t name_index;
813 if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos)
814 name_index = std::min<size_t>(name_bck_index, name_fwd_index);
815 else
816 name_index = std::max<size_t>(name_bck_index, name_fwd_index);
817 813
818 return out.erase(name_index); 814 if (name_bck_index == std::string_view::npos || name_fwd_index == std::string_view::npos) {
815 name_index = std::min(name_bck_index, name_fwd_index);
816 } else {
817 name_index = std::max(name_bck_index, name_fwd_index);
818 }
819
820 return path.substr(0, name_index);
819} 821}
820 822
821std::string GetPathWithoutTop(std::string path) { 823std::string_view GetPathWithoutTop(std::string_view path) {
822 if (path.empty()) 824 if (path.empty()) {
823 return ""; 825 return path;
826 }
827
824 while (path[0] == '\\' || path[0] == '/') { 828 while (path[0] == '\\' || path[0] == '/') {
825 path = path.substr(1); 829 path.remove_suffix(1);
826 if (path.empty()) 830 if (path.empty()) {
827 return ""; 831 return path;
832 }
828 } 833 }
829 const auto name_bck_index = path.find_first_of('\\'); 834
830 const auto name_fwd_index = path.find_first_of('/'); 835 const auto name_bck_index = path.find('\\');
836 const auto name_fwd_index = path.find('/');
831 return path.substr(std::min(name_bck_index, name_fwd_index) + 1); 837 return path.substr(std::min(name_bck_index, name_fwd_index) + 1);
832} 838}
833 839
834std::string GetFilename(std::string path) { 840std::string_view GetFilename(std::string_view path) {
835 std::replace(path.begin(), path.end(), '\\', '/'); 841 const auto name_index = path.find_last_of("\\/");
836 auto name_index = path.find_last_of('/'); 842
837 if (name_index == std::string::npos) 843 if (name_index == std::string_view::npos) {
838 return ""; 844 return {};
845 }
846
839 return path.substr(name_index + 1); 847 return path.substr(name_index + 1);
840} 848}
841 849
842std::string GetExtensionFromFilename(const std::string& name) { 850std::string_view GetExtensionFromFilename(std::string_view name) {
843 size_t index = name.find_last_of('.'); 851 const size_t index = name.rfind('.');
844 if (index == std::string::npos) 852
845 return ""; 853 if (index == std::string_view::npos) {
854 return {};
855 }
846 856
847 return name.substr(index + 1); 857 return name.substr(index + 1);
848} 858}
849 859
850std::string RemoveTrailingSlash(const std::string& path) { 860std::string_view RemoveTrailingSlash(std::string_view path) {
851 if (path.empty()) 861 if (path.empty()) {
852 return path; 862 return path;
853 if (path.back() == '\\' || path.back() == '/') 863 }
854 return path.substr(0, path.size() - 1); 864
865 if (path.back() == '\\' || path.back() == '/') {
866 path.remove_suffix(1);
867 return path;
868 }
855 869
856 return path; 870 return path;
857} 871}