| 808 | namespace path { |
| 809 | |
| 810 | Try<hashmap<string, string>> parse(const string& pattern, const string& path) |
| 811 | { |
| 812 | // Split the pattern by '/' into keys. |
| 813 | vector<string> keys = strings::tokenize(pattern, "/"); |
| 814 | |
| 815 | // Split the path by '/' into segments. |
| 816 | vector<string> segments = strings::tokenize(path, "/"); |
| 817 | |
| 818 | hashmap<string, string> result; |
| 819 | |
| 820 | while (!segments.empty()) { |
| 821 | if (keys.empty()) { |
| 822 | return Error( |
| 823 | "Not expecting suffix '" + strings::join("/", segments) + "'"); |
| 824 | } |
| 825 | |
| 826 | string key = keys.front(); |
| 827 | |
| 828 | if (strings::startsWith(key, "{") && |
| 829 | strings::endsWith(key, "}")) { |
| 830 | key = strings::remove(key, "{", strings::PREFIX); |
| 831 | key = strings::remove(key, "}", strings::SUFFIX); |
| 832 | } else if (key != segments.front()) { |
| 833 | return Error("Expecting '" + key + "' not '" + segments.front() + "'"); |
| 834 | } |
| 835 | |
| 836 | result[key] = segments.front(); |
| 837 | |
| 838 | keys.erase(keys.begin()); |
| 839 | segments.erase(segments.begin()); |
| 840 | } |
| 841 | |
| 842 | return result; |
| 843 | } |
| 844 | |
| 845 | } // namespace path { |
| 846 | |