| 227 | } |
| 228 | |
| 229 | bool IsAncestorOf(std::string_view ancestor, std::string_view descendant) { |
| 230 | ancestor = RemoveTrailingSlash(ancestor); |
| 231 | if (ancestor == "") { |
| 232 | // everything is a descendant of the root directory |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | descendant = RemoveTrailingSlash(descendant); |
| 237 | if (!descendant.starts_with(ancestor)) { |
| 238 | // an ancestor path is a prefix of descendant paths |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | descendant.remove_prefix(ancestor.size()); |
| 243 | |
| 244 | if (descendant.empty()) { |
| 245 | // "/hello" is an ancestor of "/hello" |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | // "/hello/w" is not an ancestor of "/hello/world" |
| 250 | return descendant.starts_with(std::string{kSep}); |
| 251 | } |
| 252 | |
| 253 | std::optional<std::string_view> RemoveAncestor(std::string_view ancestor, |
| 254 | std::string_view descendant) { |