xref: /aosp_15_r20/external/icing/icing/util/snippet-helpers.cc (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 #include "icing/util/snippet-helpers.h"
16 
17 #include <algorithm>
18 #include <string_view>
19 
20 #include "icing/proto/document.pb.h"
21 #include "icing/proto/search.pb.h"
22 #include "icing/schema/property-util.h"
23 
24 namespace icing {
25 namespace lib {
26 
GetWindows(std::string_view content,const SnippetProto::EntryProto & snippet_proto)27 std::vector<std::string_view> GetWindows(
28     std::string_view content, const SnippetProto::EntryProto& snippet_proto) {
29   std::vector<std::string_view> windows;
30   for (const SnippetMatchProto& match : snippet_proto.snippet_matches()) {
31     windows.push_back(content.substr(match.window_byte_position(),
32                                      match.window_byte_length()));
33   }
34   return windows;
35 }
36 
GetMatches(std::string_view content,const SnippetProto::EntryProto & snippet_proto)37 std::vector<std::string_view> GetMatches(
38     std::string_view content, const SnippetProto::EntryProto& snippet_proto) {
39   std::vector<std::string_view> matches;
40   for (const SnippetMatchProto& match : snippet_proto.snippet_matches()) {
41     matches.push_back(content.substr(match.exact_match_byte_position(),
42                                      match.exact_match_byte_length()));
43   }
44   return matches;
45 }
46 
GetSubMatches(std::string_view content,const SnippetProto::EntryProto & snippet_proto)47 std::vector<std::string_view> GetSubMatches(
48     std::string_view content, const SnippetProto::EntryProto& snippet_proto) {
49   std::vector<std::string_view> matches;
50   for (const SnippetMatchProto& match : snippet_proto.snippet_matches()) {
51     matches.push_back(content.substr(match.exact_match_byte_position(),
52                                      match.submatch_byte_length()));
53   }
54   return matches;
55 }
56 
GetString(const DocumentProto * document,std::string_view property_path_expr)57 std::string_view GetString(const DocumentProto* document,
58                            std::string_view property_path_expr) {
59   std::vector<std::string_view> properties =
60       property_util::SplitPropertyPathExpr(property_path_expr);
61   for (int i = 0; i < properties.size(); ++i) {
62     property_util::PropertyInfo property_info =
63         property_util::ParsePropertyNameExpr(properties.at(i));
64     if (property_info.index == property_util::kWildcardPropertyIndex) {
65       // Use index = 0 by default.
66       property_info.index = 0;
67     }
68 
69     const PropertyProto* prop =
70         property_util::GetPropertyProto(*document, property_info.name);
71     if (prop == nullptr) {
72       // requested property doesn't exist in the document. Return empty string.
73       return "";
74     }
75     if (i == properties.size() - 1) {
76       // The last property. Get the string_value
77       if (prop->string_values_size() - 1 < property_info.index) {
78         // The requested string doesn't exist. Return empty string.
79         return "";
80       }
81       return prop->string_values(property_info.index);
82     } else if (prop->document_values_size() - 1 < property_info.index) {
83       // The requested subproperty doesn't exist. return an empty string.
84       return "";
85     } else {
86       // Go to the next subproperty.
87       document = &prop->document_values(property_info.index);
88     }
89   }
90   return "";
91 }
92 
93 }  // namespace lib
94 }  // namespace icing
95