| 737 | namespace path { |
| 738 | |
| 739 | Try<hashmap<string, string>> parse(const string& pattern, const string& path) |
| 740 | { |
| 741 | // Split the pattern by '/' into keys. |
| 742 | vector<string> keys = strings::tokenize(pattern, "/"); |
| 743 | |
| 744 | // Split the path by '/' into segments. |
| 745 | vector<string> segments = strings::tokenize(path, "/"); |
| 746 | |
| 747 | hashmap<string, string> result; |
| 748 | |
| 749 | while (!segments.empty()) { |
| 750 | if (keys.empty()) { |
| 751 | return Error( |
| 752 | "Not expecting suffix '" + strings::join("/", segments) + "'"); |
| 753 | } |
| 754 | |
| 755 | string key = keys.front(); |
| 756 | |
| 757 | if (strings::startsWith(key, "{") && |
| 758 | strings::endsWith(key, "}")) { |
| 759 | key = strings::remove(key, "{", strings::PREFIX); |
| 760 | key = strings::remove(key, "}", strings::SUFFIX); |
| 761 | } else if (key != segments.front()) { |
| 762 | return Error("Expecting '" + key + "' not '" + segments.front() + "'"); |
| 763 | } |
| 764 | |
| 765 | result[key] = segments.front(); |
| 766 | |
| 767 | keys.erase(keys.begin()); |
| 768 | segments.erase(segments.begin()); |
| 769 | } |
| 770 | |
| 771 | return result; |
| 772 | } |
| 773 | |
| 774 | } // namespace path { |
| 775 | |