xref: /aosp_15_r20/external/icing/icing/join/qualified-id.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2022 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_JOIN_QUALIFIED_ID_H_
16 #define ICING_JOIN_QUALIFIED_ID_H_
17 
18 #include <string>
19 #include <string_view>
20 
21 #include "icing/text_classifier/lib3/utils/base/statusor.h"
22 
23 namespace icing {
24 namespace lib {
25 
26 // QualifiedId definition: namespace and uri.
27 // This is a wrapper class for parsing qualified id string.
28 //
29 // Qualified id string format: escape(namespace) + '#' + escape(uri).
30 // - Use '#' as the separator to concat namespace and uri
31 // - Use '\' to escape '\' and '#' in namespace and uri.
32 // - There should be 1 separator '#' in a qualified string, and the rest part
33 //   should have correct escape format.
34 // - Raw namespace and uri cannot be empty.
35 class QualifiedId {
36  public:
37   static constexpr char kEscapeChar = '\\';
38   static constexpr char kNamespaceUriSeparator = '#';
39 
40   // Parses a qualified id string "<escaped(namespace)>#<escaped(uri)>" and
41   // creates an instance of QualifiedId.
42   //
43   // qualified_id_str: a qualified id string having the format mentioned above.
44   //
45   // Returns:
46   //   - A QualifiedId instance with raw namespace and uri, on success.
47   //   - INVALID_ARGUMENT_ERROR if the format of qualified_id_str is incorrect.
48   static libtextclassifier3::StatusOr<QualifiedId> Parse(
49       std::string_view qualified_id_str);
50 
QualifiedId(std::string name_space,std::string uri)51   explicit QualifiedId(std::string name_space, std::string uri)
52       : name_space_(std::move(name_space)), uri_(std::move(uri)) {}
53 
name_space()54   const std::string& name_space() const { return name_space_; }
uri()55   const std::string& uri() const { return uri_; }
56 
57  private:
58   std::string name_space_;
59   std::string uri_;
60 };
61 
62 }  // namespace lib
63 }  // namespace icing
64 
65 #endif  // ICING_JOIN_QUALIFIED_ID_H_
66