Merge pull request #89 from google/stringview

Added upb_stringview, the string representation for upb_msg.
pull/13171/head
Joshua Haberman 7 years ago committed by GitHub
commit 0af928564e
  1. 4
      upb/bindings/lua/msg.c
  2. 26
      upb/msg.c
  3. 38
      upb/msg.h

@ -434,7 +434,7 @@ static upb_msgval lupb_tomsgval(lua_State *L, upb_fieldtype_t type, int narg,
case UPB_TYPE_BYTES: {
size_t len;
const char *ptr = lupb_checkstring(L, narg, &len);
return upb_msgval_str(ptr, len);
return upb_msgval_makestr(ptr, len);
}
case UPB_TYPE_MESSAGE:
UPB_ASSERT(lmsgclass);
@ -1014,7 +1014,7 @@ static int lupb_msg_index(lua_State *L) {
if (upb_msg_has(lmsg->msg, field_index, l)) {
upb_msgval val = upb_msg_get(lmsg->msg, field_index, l);
lua_pop(L, 1);
lua_pushlstring(L, val.str.ptr, val.str.len);
lua_pushlstring(L, val.str.data, val.str.size);
lupb_uservalseti(L, 1, lupb_fieldindex(f), -1);
}
}

@ -135,7 +135,7 @@ static upb_msgval upb_msgval_fromdefault(const upb_fielddef *f) {
case UPB_TYPE_BYTES: {
size_t len;
const char *ptr = upb_fielddef_defaultstr(f, &len);
return upb_msgval_str(ptr, len);
return upb_msgval_makestr(ptr, len);
}
case UPB_TYPE_MESSAGE:
return upb_msgval_msg(NULL);
@ -423,9 +423,9 @@ void *upb_msg_startstr(void *msg, const void *hd, size_t size_hint) {
val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING));
upb_free(alloc, (void*)val.str.ptr);
val.str.ptr = NULL;
val.str.len = 0;
upb_free(alloc, (void*)val.str.data);
val.str.data = NULL;
val.str.size = 0;
upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING));
return msg;
@ -441,15 +441,15 @@ size_t upb_msg_str(void *msg, const void *hd, const char *ptr, size_t size,
val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING));
newsize = val.str.len + size;
val.str.ptr = upb_realloc(alloc, (void*)val.str.ptr, val.str.len, newsize);
newsize = val.str.size + size;
val.str.data = upb_realloc(alloc, (void*)val.str.data, val.str.size, newsize);
if (!val.str.ptr) {
if (!val.str.data) {
return false;
}
memcpy((char*)val.str.ptr + val.str.len, ptr, size);
val.str.len = newsize;
memcpy((char*)val.str.data + val.str.size, ptr, size);
val.str.size = newsize;
upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING));
return size;
}
@ -540,7 +540,7 @@ static bool upb_visitor_hasfield(const upb_msg *msg, const upb_fielddef *f,
return upb_msgval_getuint64(val) != 0;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
return upb_msgval_getstr(val) && upb_msgval_getstrlen(val) > 0;
return upb_msgval_getstr(val).size > 0;
case UPB_TYPE_MESSAGE:
return upb_msgval_getmsg(val) != NULL;
}
@ -899,8 +899,8 @@ static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key,
switch (type) {
case UPB_TYPE_STRING:
/* Point to string data of the input key. */
*out_key = key->str.ptr;
*out_len = key->str.len;
*out_key = key->str.data;
*out_len = key->str.size;
return;
case UPB_TYPE_BOOL:
case UPB_TYPE_INT32:
@ -925,7 +925,7 @@ static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key,
size_t len) {
switch (type) {
case UPB_TYPE_STRING:
return upb_msgval_str(key, len);
return upb_msgval_makestr(key, len);
case UPB_TYPE_BOOL:
case UPB_TYPE_INT32:
case UPB_TYPE_UINT32:

@ -110,6 +110,23 @@ const upb_visitorplan *upb_msgfactory_getvisitorplan(upb_msgfactory *f,
const upb_handlers *h);
/** upb_stringview ************************************************************/
typedef struct {
const char *data;
size_t size;
} upb_stringview;
UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) {
upb_stringview ret;
ret.data = data;
ret.size = size;
return ret;
}
#define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len}
/** upb_msgval ****************************************************************/
/* A union representing all possible protobuf values. Used for generic get/set
@ -127,10 +144,7 @@ typedef union {
const upb_msg* msg;
const upb_array* arr;
const void* ptr;
struct {
const char *ptr;
size_t len;
} str;
upb_stringview str;
} upb_msgval;
#define ACCESSORS(name, membername, ctype) \
@ -157,22 +171,12 @@ ACCESSORS(map, map, const upb_map*)
ACCESSORS(msg, msg, const upb_msg*)
ACCESSORS(ptr, ptr, const void*)
ACCESSORS(arr, arr, const upb_array*)
ACCESSORS(str, str, upb_stringview)
#undef ACCESSORS
UPB_INLINE upb_msgval upb_msgval_str(const char *ptr, size_t len) {
upb_msgval ret;
ret.str.ptr = ptr;
ret.str.len = len;
return ret;
}
UPB_INLINE const char* upb_msgval_getstr(upb_msgval val) {
return val.str.ptr;
}
UPB_INLINE size_t upb_msgval_getstrlen(upb_msgval val) {
return val.str.len;
UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
return upb_msgval_str(upb_stringview_make(data, size));
}

Loading…
Cancel
Save