1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #ifndef UPB_BASE_STRING_VIEW_H_
9 #define UPB_BASE_STRING_VIEW_H_
10
11 #include <string.h>
12
13 // Must be last.
14 #include "upb/port/def.inc"
15
16 #define UPB_STRINGVIEW_INIT(ptr, len) \
17 { ptr, len }
18
19 #define UPB_STRINGVIEW_FORMAT "%.*s"
20 #define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data
21
22 // LINT.IfChange(struct_definition)
23 typedef struct {
24 const char* data;
25 size_t size;
26 } upb_StringView;
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
upb_StringView_FromDataAndSize(const char * data,size_t size)32 UPB_API_INLINE upb_StringView upb_StringView_FromDataAndSize(const char* data,
33 size_t size) {
34 upb_StringView ret;
35 ret.data = data;
36 ret.size = size;
37 return ret;
38 }
39
upb_StringView_FromString(const char * data)40 UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) {
41 return upb_StringView_FromDataAndSize(data, strlen(data));
42 }
43
upb_StringView_IsEqual(upb_StringView a,upb_StringView b)44 UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
45 return (a.size == b.size) && (!a.size || !memcmp(a.data, b.data, a.size));
46 }
47
48 // LINT.ThenChange(
49 // GoogleInternalName1,
50 // //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string,
51 // //depot/google3/third_party/upb/bits/typescript/string_view.ts
52 // )
53
54 #ifdef __cplusplus
55 } /* extern "C" */
56 #endif
57
58 #include "upb/port/undef.inc"
59
60 #endif /* UPB_BASE_STRING_VIEW_H_ */
61