xref: /aosp_15_r20/external/icing/icing/util/snippet-helpers.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_TESTING_SNIPPET_HELPERS_H_
16 #define ICING_TESTING_SNIPPET_HELPERS_H_
17 
18 #include <string>
19 
20 #include "icing/proto/document.pb.h"
21 #include "icing/proto/search.pb.h"
22 
23 namespace icing {
24 namespace lib {
25 
26 // Retrieve pointer to the PropertyProto identified by property_name.
27 // Returns nullptr if no such property exists.
28 //
29 // NOTE: This function does not handle nesting or indexes. "foo.bar" will return
30 // a nullptr even if document contains a property called "foo" that contains a
31 // subproperty called "bar".
32 const PropertyProto* GetProperty(const DocumentProto& document,
33                                  const std::string& property_name);
34 
35 // Retrieves all windows defined by the snippet_proto for the content.
36 std::vector<std::string_view> GetWindows(
37     std::string_view content, const SnippetProto::EntryProto& snippet_proto);
38 
39 // Retrieves all matches defined by the snippet_proto for the content.
40 std::vector<std::string_view> GetMatches(
41     std::string_view content, const SnippetProto::EntryProto& snippet_proto);
42 
43 // Retrieves all submatches defined by the snippet_proto for the content.
44 std::vector<std::string_view> GetSubMatches(
45     std::string_view content, const SnippetProto::EntryProto& snippet_proto);
46 
47 // Retrieves the string value held in the document corresponding to the
48 // property_path_expr.
49 // Example:
50 //   - GetString(doc, "foo") will retrieve the first string value in the
51 //     property "foo" in document or an empty string if it doesn't exist.
52 //   - GetString(doc, "foo[1].bar[2]") will retrieve the third string value in
53 //     the subproperty "bar" of the second document value in the property "foo".
54 std::string_view GetString(const DocumentProto* document,
55                            std::string_view property_path_expr);
56 
57 }  // namespace lib
58 }  // namespace icing
59 
60 #endif  // ICING_TESTING_SNIPPET_HELPERS_H_
61