diff options
| author | 2018-07-22 01:23:29 -0400 | |
|---|---|---|
| committer | 2018-07-22 03:22:21 -0400 | |
| commit | 398444e67635c158a20301d1a32a1c45bfdd4056 (patch) | |
| tree | db638ebe09199ae64b69f5911136effa9756571f /src/common/file_util.cpp | |
| parent | Merge pull request #764 from lioncash/move (diff) | |
| download | yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.gz yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.xz yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.zip | |
file_util, vfs: Use std::string_view where applicable
Avoids unnecessary construction of std::string instances where
applicable.
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 82 |
1 files changed, 48 insertions, 34 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 | ||
| 805 | std::vector<std::string> SplitPathComponents(const std::string& filename) { | 805 | std::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 | ||
| 818 | std::string GetParentPath(const std::string& path) { | 819 | std::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 | ||
| 831 | std::string GetPathWithoutTop(std::string path) { | 833 | std::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 | ||
| 844 | std::string GetFilename(std::string path) { | 850 | std::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 | ||
| 852 | std::string GetExtensionFromFilename(const std::string& name) { | 860 | std::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 | ||
| 860 | std::string RemoveTrailingSlash(const std::string& path) { | 870 | std::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 | } |