Make index zero-base, and also support relative index.
| 798 | |
| 799 | // Make index zero-base, and also support relative index. |
| 800 | static inline bool fixIndex(int idx, int n, int *ret) { |
| 801 | if (!ret) { |
| 802 | return false; |
| 803 | } |
| 804 | |
| 805 | if (idx > 0) { |
| 806 | (*ret) = idx - 1; |
| 807 | return true; |
| 808 | } |
| 809 | |
| 810 | if (idx == 0) { |
| 811 | // zero is not allowed according to the spec. |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | if (idx < 0) { |
| 816 | (*ret) = n + idx; // negative value = relative |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | return false; // never reach here. |
| 821 | } |
| 822 | |
| 823 | static inline std::string parseString(const char **token) { |
| 824 | std::string s; |