diff --git a/BUILD b/BUILD index b14bad4a94..693cfc5e22 100644 --- a/BUILD +++ b/BUILD @@ -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", ], diff --git a/upb/string_view.h b/upb/string_view.h new file mode 100644 index 0000000000..6a3395148e --- /dev/null +++ b/upb/string_view.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 + +// 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_ */ diff --git a/upb/upb.h b/upb/upb.h index 7398d09fff..a2345eb1b1 100644 --- a/upb/upb.h +++ b/upb/upb.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. */