xref: /5.5.2/subjson/subdoc/hkesc.h (revision aef8453a)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3*     Copyright 2015 Couchbase, Inc
4*
5*   Licensed under the Apache License, Version 2.0 (the "License");
6*   you may not use this file except in compliance with the License.
7*   You may obtain a copy of the License at
8*
9*       http://www.apache.org/licenses/LICENSE-2.0
10*
11*   Unless required by applicable law or agreed to in writing, software
12*   distributed under the License is distributed on an "AS IS" BASIS,
13*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*   See the License for the specific language governing permissions and
15*   limitations under the License.
16*/
17
18// Template class for dealing with escaped hash keys (specifically, those
19// which contain 'u-escapes'.
20// This is required for comparison: All JSON characters have a single form
21// of representation, except for those which must be escaped.
22// Implemented as a template because we might 'customize' the ABI for jsonsl
23// depending on the parsing mode.
24#include "uescape.h"
25#include "loc.h"
26
27namespace Subdoc {
28class HashKey {
29public:
30    template <typename StateType>
31    void set_hk_begin(const StateType *, const char *at) {
32        m_strvalid = false;
33        m_hkbuf = at + 1;
34    }
35
36    template <typename StateType>
37    void set_hk_end(const StateType *state) {
38        m_hklen = state->pos_cur - (state->pos_begin + 1);
39        if (!state->nescapes) {
40            m_hkesc = false;
41        } else {
42            m_hkesc = true;
43        }
44    }
45
46    const char *get_hk(size_t &nkey) {
47        if (!m_hkesc) {
48            nkey = m_hklen;
49            return m_hkbuf;
50        }
51
52        if (m_strvalid) {
53            nkey = m_hkstr.size();
54            return m_hkstr.c_str();
55        }
56
57        m_hkstr.clear();
58
59        // TODO: use jsonsl_util_unescape_ex() instead. However this requires
60        // a dedicated character table
61        UescapeConverter::convert(m_hkbuf, m_hklen, m_hkstr);
62        m_strvalid = true;
63        return get_hk(nkey);
64    }
65
66    void hk_rawloc(Loc &loc) const {
67        loc.at = m_hkbuf - 1;
68        loc.length = m_hklen + 2;
69    }
70
71private:
72    const char *m_hkbuf = NULL;
73    size_t m_hklen = 0;
74    bool m_hkesc = false;
75    bool m_strvalid = false;
76    std::string m_hkstr;
77};
78}
79