split out upb_StringView from upb.h

PiperOrigin-RevId: 456858455
pull/13171/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 8d0d13f2bc
commit e153b52394
  1. 3
      BUILD
  2. 73
      upb/string_view.h
  3. 30
      upb/upb.h

@ -123,6 +123,7 @@ cc_library(
"upb/extension_registry.h",
"upb/msg.h",
"upb/status.h",
"upb/string_view.h",
"upb/upb.h",
"upb/upb.hpp",
],
@ -143,6 +144,7 @@ cc_library(
"upb/extension_registry.c",
"upb/msg.h",
"upb/msg_internal.h",
"upb/string_view.h",
"upb/upb.h",
],
hdrs = [
@ -728,6 +730,7 @@ cc_library(
"upb/arena.h",
"upb/internal/table.h",
"upb/status.h",
"upb/string_view.h",
"upb/table_internal.h",
"upb/upb.h",
],

@ -0,0 +1,73 @@
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google LLC nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UPB_STRING_VIEW_H_
#define UPB_STRING_VIEW_H_
#include <string.h>
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
const char* data;
size_t size;
} upb_StringView;
UPB_INLINE upb_StringView upb_StringView_FromDataAndSize(const char* data,
size_t size) {
upb_StringView ret;
ret.data = data;
ret.size = size;
return ret;
}
UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) {
return upb_StringView_FromDataAndSize(data, strlen(data));
}
UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
}
#define UPB_STRINGVIEW_INIT(ptr, len) \
{ ptr, len }
#define UPB_STRINGVIEW_FORMAT "%.*s"
#define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data
#include "upb/port_undef.inc"
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* UPB_STRING_VIEW_H_ */

@ -42,6 +42,7 @@
// TODO(b/232091617): Remove these and fix everything that breaks as a result.
#include "upb/arena.h"
#include "upb/status.h"
#include "upb/string_view.h"
// Must be last.
#include "upb/port_def.inc"
@ -50,35 +51,6 @@
extern "C" {
#endif
/** upb_StringView ************************************************************/
typedef struct {
const char* data;
size_t size;
} upb_StringView;
UPB_INLINE upb_StringView upb_StringView_FromDataAndSize(const char* data,
size_t size) {
upb_StringView ret;
ret.data = data;
ret.size = size;
return ret;
}
UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) {
return upb_StringView_FromDataAndSize(data, strlen(data));
}
UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
}
#define UPB_STRINGVIEW_INIT(ptr, len) \
{ ptr, len }
#define UPB_STRINGVIEW_FORMAT "%.*s"
#define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data
/* Constants ******************************************************************/
/* A list of types as they are encoded on-the-wire. */

Loading…
Cancel
Save