WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
string_view.h
Go to the documentation of this file.
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)
23typedef struct {
24 const char* data;
25 size_t size;
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
33 size_t size) {
35 ret.data = data;
36 ret.size = size;
37 return ret;
38}
39
41 return upb_StringView_FromDataAndSize(data, strlen(data));
42}
43
45 return (a.size == b.size) && (!a.size || !memcmp(a.data, b.data, a.size));
46}
47
48// Compares StringViews following strcmp rules.
49// Please note this comparison is neither unicode nor locale aware.
51 int result = memcmp(a.data, b.data, UPB_MIN(a.size, b.size));
52 if (result != 0) return result;
53 if (a.size < b.size) {
54 return -1;
55 } else if (a.size > b.size) {
56 return 1;
57 } else {
58 return 0;
59 }
60}
61
62// LINT.ThenChange(
63// GoogleInternalName1,
64// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string,
65// //depot/google3/third_party/upb/bits/typescript/string_view.ts
66// )
67
68#ifdef __cplusplus
69} /* extern "C" */
70#endif
71
72#include "upb/port/undef.inc"
73
74#endif /* UPB_BASE_STRING_VIEW_H_ */
#define UPB_API_INLINE
Definition def.inc:163
#define UPB_INLINE
Definition def.inc:144
#define UPB_MIN(x, y)
Definition def.inc:301
UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b)
Definition string_view.h:44
UPB_INLINE int upb_StringView_Compare(upb_StringView a, upb_StringView b)
Definition string_view.h:50
UPB_API_INLINE upb_StringView upb_StringView_FromDataAndSize(const char *data, size_t size)
Definition string_view.h:32
UPB_INLINE upb_StringView upb_StringView_FromString(const char *data)
Definition string_view.h:40
Definition string_view.h:23
const char * data
Definition string_view.h:24
size_t size
Definition string_view.h:25