OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_LOOKUP_STRING_IN_FIXED_SET_H_ | 5 #ifndef NET_BASE_LOOKUP_STRING_IN_FIXED_SET_H_ |
6 #define NET_BASE_LOOKUP_STRING_IN_FIXED_SET_H_ | 6 #define NET_BASE_LOOKUP_STRING_IN_FIXED_SET_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 // Looks up the string |key| with length |key_length| in a fixed set of | 24 // Looks up the string |key| with length |key_length| in a fixed set of |
25 // strings. The set of strings must be known at compile time. It is converted to | 25 // strings. The set of strings must be known at compile time. It is converted to |
26 // a graph structure named a DAFSA (Deterministic Acyclic Finite State | 26 // a graph structure named a DAFSA (Deterministic Acyclic Finite State |
27 // Automaton) by the script make_dafsa.py during compilation. This permits | 27 // Automaton) by the script make_dafsa.py during compilation. This permits |
28 // efficient (in time and space) lookup. The graph generated by make_dafsa.py | 28 // efficient (in time and space) lookup. The graph generated by make_dafsa.py |
29 // takes the form of a constant byte array which should be supplied via the | 29 // takes the form of a constant byte array which should be supplied via the |
30 // |graph| and |length| parameters. The return value is kDafsaNotFound, | 30 // |graph| and |length| parameters. The return value is kDafsaNotFound, |
31 // kDafsaFound, or a bitmap consisting of one or more of kDafsaExceptionRule, | 31 // kDafsaFound, or a bitmap consisting of one or more of kDafsaExceptionRule, |
32 // kDafsaWildcardRule and kDafsaPrivateRule ORed together. | 32 // kDafsaWildcardRule and kDafsaPrivateRule ORed together. |
33 // | |
34 // TODO(nick): Replace this with FixedSetIncrementalLookup everywhere. | |
33 NET_EXPORT int LookupStringInFixedSet(const unsigned char* graph, | 35 NET_EXPORT int LookupStringInFixedSet(const unsigned char* graph, |
34 size_t length, | 36 size_t length, |
35 const char* key, | 37 const char* key, |
36 size_t key_length); | 38 size_t key_length); |
37 | 39 |
40 // FixedSetIncrementalLookup provides efficient membership and prefix queries | |
41 // against a fixed set of strings. The set of strings must be known at compile | |
42 // time. The set is converted to a graph structure named a DAFSA (Deterministic | |
43 // Acyclic Finite State Automaton) by the script //net/tools/dafsa/make_dafsa.py | |
44 // during compilation. The conversion generates a C++ header file defining the | |
45 // encoded graph as a constant byte array. This class provides a fast, constant- | |
46 // space lookup operation against such byte arrays. | |
47 // | |
48 // The lookup proceeds incrementally, with input characters provided one at a | |
49 // time. This approach allow queries of the form: "given an input string, which | |
50 // prefixes of that string that appear in the fixed set?" As the matching | |
51 // prefixes (and their result codes) are enumerated, the most suitable match | |
52 // among them can be selected in a single pass. | |
53 // | |
54 // This class can also be used to perform suffix queries (instead of prefix | |
55 // queries) against a fixed set, so long as the DAFSA is constructed on reversed | |
56 // values, and the input is provided in reverse order. | |
57 // | |
58 // Example usage for simple membership query; |input| is a std::string: | |
59 // | |
60 // FixedSetIncrementalLookup lookup(kDafsa, sizeof(kDafsa)); | |
61 // for (char c : input) { | |
62 // if (!lookup.Advance(c)) | |
63 // return false; | |
64 // } | |
65 // return lookup.GetResultForCurrentSequence() != kDafsaNotFound; | |
66 // | |
67 // Example usage for 'find longest prefix in set with result code == 3' query: | |
68 // | |
69 // FixedSetIncrementalLookup prefix_lookup(kDafsa, sizeof(kDafsa)); | |
70 // size_t longest_match = std::string::npos; | |
71 // for (size_t i = 0; i < input.length(); ++i) { | |
72 // if (!prefix_lookup.Advance(input[i])) | |
73 // break; | |
74 // if (prefix_lookup.GetResultForCurrentSequence() == 3) | |
75 // longest_match = i; | |
76 // } | |
77 // return input.substr(0, longest_match + 1); | |
Ryan Sleevi
2017/01/27 00:08:24
The "longest_match + 1" is a bit of a red flag, mo
ncarter (slow)
2017/02/15 23:42:11
Fixed.
| |
78 class NET_EXPORT FixedSetIncrementalLookup { | |
79 public: | |
80 // Begin a lookup against the provided fixed set. |graph| and |length| | |
81 // describe a byte buffer generated by the make_dafsa.py script, as described | |
82 // in the class comment. | |
83 // | |
84 // FixedSetIncrementalLookup is initialized to a state corresponding to the | |
85 // empty input sequence. Calling GetResultForCurrentSequence() in the initial | |
86 // state would indicate whether the empty string appears in the fixed set. | |
87 // Characters can be added to the sequence by calling Advance(), and the | |
88 // lookup result can be checked after each addition by calling | |
89 // GetResultForCurrentSequence(). | |
90 FixedSetIncrementalLookup(const unsigned char* graph, size_t length); | |
91 | |
92 // FixedSetIncrementalLookup is copyable so that callers can save/restore | |
93 // their position in the search. This is for cases where branching or | |
94 // backtracking might be required (e.g. to probe for the presence of a | |
95 // designated wildcard character). | |
96 FixedSetIncrementalLookup(const FixedSetIncrementalLookup&); | |
97 FixedSetIncrementalLookup& operator=(const FixedSetIncrementalLookup&); | |
98 | |
99 ~FixedSetIncrementalLookup(); | |
100 | |
101 // Advance the query by adding a character to the input sequence. |input| can | |
102 // be any char value, but only ASCII characters will ever result in matches, | |
103 // since the fixed set itself is limited to ASCII strings. | |
104 // | |
105 // Returns true if the resulting input sequence either appears in the fixed | |
106 // set itself, or is a prefix of some longer string in the fixed set. Returns | |
107 // false otherwise, implying that the graph is exhausted and | |
108 // GetResultForCurrentSequence() will return kDafsaNotFound. | |
109 // | |
110 // Once Advance() has returned false, the caller can safely stop feeding more | |
111 // characters, as subsequent calls to Advance() will return false and have no | |
112 // effect. | |
113 bool Advance(char input); | |
114 | |
115 // Returns the result code corresponding to the input sequence provided thus | |
116 // far to Advance(). | |
117 // | |
118 // If the sequence does not appear in the fixed set, the return value is | |
119 // kDafsaNotFound. Otherwise, the value is a non-negative integer (currently | |
120 // limited to 0-7) corresponding to the result code for that string, as listed | |
121 // in the .gperf file from which the DAFSA was generated. For | |
122 // GetDomainAndRegistry DAFSAs, these values should be interpreted as a | |
123 // bitmask of kDafsaExceptionRule, kDafsaWildcardRule, and kDafsaPrivateRule. | |
124 // | |
125 // It is okay to call this function, and then extend the sequence further by | |
126 // calling Advance(). | |
127 int GetResultForCurrentSequence() const; | |
128 | |
129 private: | |
130 // Pointer to the current position in the graph indicating the current state | |
131 // of the automaton, or nullptr if the graph is exhausted. | |
132 const unsigned char* pos_; | |
133 | |
134 // Pointer just past the end of the graph. |pos_| should never get here. This | |
135 // is used only in DCHECKs. | |
136 const unsigned char* end_; | |
137 | |
138 // Contains the current decoder state. If true, |pos_| points to a label | |
139 // character or a return code. If false, |pos_| points to a sequence of | |
140 // offsets that indicate the child nodes of the current state. | |
141 bool pos_is_label_character_; | |
142 }; | |
143 | |
38 } // namespace net | 144 } // namespace net |
39 | 145 |
40 #endif // NET_BASE_LOOKUP_STRING_IN_FIXED_SET_H_ | 146 #endif // NET_BASE_LOOKUP_STRING_IN_FIXED_SET_H_ |
OLD | NEW |