1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #ifndef UPB_MESSAGE_COMPARE_H_
9 #define UPB_MESSAGE_COMPARE_H_
10
11 #include <stddef.h>
12
13 #include "upb/base/descriptor_constants.h"
14 #include "upb/message/message.h"
15 #include "upb/message/value.h"
16 #include "upb/mini_table/extension.h"
17 #include "upb/mini_table/field.h"
18 #include "upb/mini_table/message.h"
19
20 // Must be last.
21 #include "upb/port/def.inc"
22
23 enum {
24 // If set, upb_Message_IsEqual() will attempt to compare unknown fields.
25 // By its very nature this comparison is inexact.
26 kUpb_CompareOption_IncludeUnknownFields = (1 << 0)
27 };
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 // Returns true if no known fields or extensions are set in the message.
34 UPB_API bool upb_Message_IsEmpty(const upb_Message* msg,
35 const upb_MiniTable* m);
36
37 UPB_API bool upb_Message_IsEqual(const upb_Message* msg1,
38 const upb_Message* msg2,
39 const upb_MiniTable* m, int options);
40
41 // If |ctype| is a message then |m| must point to its minitable.
upb_MessageValue_IsEqual(upb_MessageValue val1,upb_MessageValue val2,upb_CType ctype,const upb_MiniTable * m,int options)42 UPB_API_INLINE bool upb_MessageValue_IsEqual(upb_MessageValue val1,
43 upb_MessageValue val2,
44 upb_CType ctype,
45 const upb_MiniTable* m,
46 int options) {
47 switch (ctype) {
48 case kUpb_CType_Bool:
49 return val1.bool_val == val2.bool_val;
50
51 case kUpb_CType_Float:
52 case kUpb_CType_Int32:
53 case kUpb_CType_UInt32:
54 case kUpb_CType_Enum:
55 return val1.int32_val == val2.int32_val;
56
57 case kUpb_CType_Double:
58 case kUpb_CType_Int64:
59 case kUpb_CType_UInt64:
60 return val1.int64_val == val2.int64_val;
61
62 case kUpb_CType_String:
63 case kUpb_CType_Bytes:
64 return upb_StringView_IsEqual(val1.str_val, val2.str_val);
65
66 case kUpb_CType_Message:
67 return upb_Message_IsEqual(val1.msg_val, val2.msg_val, m, options);
68
69 default:
70 UPB_UNREACHABLE();
71 return false;
72 }
73 }
74
75 #ifdef __cplusplus
76 } /* extern "C" */
77 #endif
78
79 #include "upb/port/undef.inc"
80
81 #endif // UPB_MESSAGE_COMPARE_H_
82