| 800 | */ |
| 801 | #endif |
| 802 | void Parse(const Ch* source, size_t length) { |
| 803 | RAPIDJSON_ASSERT(source != NULL); |
| 804 | RAPIDJSON_ASSERT(nameBuffer_ == 0); |
| 805 | RAPIDJSON_ASSERT(tokens_ == 0); |
| 806 | |
| 807 | // Create own allocator if user did not supply. |
| 808 | if (!allocator_) |
| 809 | ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); |
| 810 | |
| 811 | // Count number of '/' as tokenCount |
| 812 | tokenCount_ = 0; |
| 813 | for (const Ch* s = source; s != source + length; s++) |
| 814 | if (*s == '/') |
| 815 | tokenCount_++; |
| 816 | |
| 817 | Token* token = tokens_ = static_cast<Token *>(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch))); |
| 818 | Ch* name = nameBuffer_ = reinterpret_cast<Ch *>(tokens_ + tokenCount_); |
| 819 | size_t i = 0; |
| 820 | |
| 821 | // Detect if it is a URI fragment |
| 822 | bool uriFragment = false; |
| 823 | if (source[i] == '#') { |
| 824 | uriFragment = true; |
| 825 | i++; |
| 826 | } |
| 827 | |
| 828 | if (i != length && source[i] != '/') { |
| 829 | parseErrorCode_ = kPointerParseErrorTokenMustBeginWithSolidus; |
| 830 | goto error; |
| 831 | } |
| 832 | |
| 833 | while (i < length) { |
| 834 | RAPIDJSON_ASSERT(source[i] == '/'); |
| 835 | i++; // consumes '/' |
| 836 | |
| 837 | token->name = name; |
| 838 | bool isNumber = true; |
| 839 | |
| 840 | while (i < length && source[i] != '/') { |
| 841 | Ch c = source[i]; |
| 842 | if (uriFragment) { |
| 843 | // Decoding percent-encoding for URI fragment |
| 844 | if (c == '%') { |
| 845 | PercentDecodeStream is(&source[i], source + length); |
| 846 | GenericInsituStringStream<EncodingType> os(name); |
| 847 | Ch* begin = os.PutBegin(); |
| 848 | if (!Transcoder<UTF8<>, EncodingType>().Validate(is, os) || !is.IsValid()) { |
| 849 | parseErrorCode_ = kPointerParseErrorInvalidPercentEncoding; |
| 850 | goto error; |
| 851 | } |
| 852 | size_t len = os.PutEnd(begin); |
| 853 | i += is.Tell() - 1; |
| 854 | if (len == 1) |
| 855 | c = *name; |
| 856 | else { |
| 857 | name += len; |
| 858 | isNumber = false; |
| 859 | i++; |