| 5107 | } |
| 5108 | |
| 5109 | String String::path_to(const String &p_path) const { |
| 5110 | String src = replace_char('\\', '/'); |
| 5111 | String dst = p_path.replace_char('\\', '/'); |
| 5112 | if (!src.ends_with("/")) { |
| 5113 | src += "/"; |
| 5114 | } |
| 5115 | if (!dst.ends_with("/")) { |
| 5116 | dst += "/"; |
| 5117 | } |
| 5118 | |
| 5119 | if (src.begins_with("res://") && dst.begins_with("res://")) { |
| 5120 | src = src.replace("res://", "/"); |
| 5121 | dst = dst.replace("res://", "/"); |
| 5122 | |
| 5123 | } else if (src.begins_with("user://") && dst.begins_with("user://")) { |
| 5124 | src = src.replace("user://", "/"); |
| 5125 | dst = dst.replace("user://", "/"); |
| 5126 | |
| 5127 | } else if (src.begins_with("/") && dst.begins_with("/")) { |
| 5128 | //nothing |
| 5129 | } else { |
| 5130 | //dos style |
| 5131 | String src_begin = src.get_slicec('/', 0); |
| 5132 | String dst_begin = dst.get_slicec('/', 0); |
| 5133 | |
| 5134 | if (src_begin != dst_begin) { |
| 5135 | return p_path; //impossible to do this |
| 5136 | } |
| 5137 | |
| 5138 | src = src.substr(src_begin.length()); |
| 5139 | dst = dst.substr(dst_begin.length()); |
| 5140 | } |
| 5141 | |
| 5142 | //remove leading and trailing slash and split |
| 5143 | Vector<String> src_dirs = src.substr(1, src.length() - 2).split("/"); |
| 5144 | Vector<String> dst_dirs = dst.substr(1, dst.length() - 2).split("/"); |
| 5145 | |
| 5146 | //find common parent |
| 5147 | int common_parent = 0; |
| 5148 | |
| 5149 | while (true) { |
| 5150 | if (src_dirs.size() == common_parent) { |
| 5151 | break; |
| 5152 | } |
| 5153 | if (dst_dirs.size() == common_parent) { |
| 5154 | break; |
| 5155 | } |
| 5156 | if (src_dirs[common_parent] != dst_dirs[common_parent]) { |
| 5157 | break; |
| 5158 | } |
| 5159 | common_parent++; |
| 5160 | } |
| 5161 | |
| 5162 | common_parent--; |
| 5163 | |
| 5164 | int dirs_to_backtrack = (src_dirs.size() - 1) - common_parent; |
| 5165 | String dir = String("../").repeat(dirs_to_backtrack); |
| 5166 |
no test coverage detected