Exemple #1
0
bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name, int32_t *num) {
  upb_value v;
  if (!upb_strtable_lookup(&def->ntoi, name, &v)) {
    return false;
  }
  if (num) *num = upb_value_getint32(v);
  return true;
}
Exemple #2
0
static upb_flow_t upb_textprinter_value(void *_p, upb_value fval,
                                        upb_value val) {
  upb_textprinter *p = _p;
  upb_fielddef *f = upb_value_getfielddef(fval);
  upb_textprinter_indent(p);
  CHECK(upb_bytesink_printf(p->bytesink, &p->status, UPB_STRFMT ": ", UPB_STRARG(f->name)));
#define CASE(fmtstr, member) \
    CHECK(upb_bytesink_printf(p->bytesink, &p->status, fmtstr, upb_value_get ## member(val))); break;
  switch(f->type) {
    case UPB_TYPE(DOUBLE):
      CASE("%0.f", double);
    case UPB_TYPE(FLOAT):
      CASE("%0.f", float)
    case UPB_TYPE(INT64):
    case UPB_TYPE(SFIXED64):
    case UPB_TYPE(SINT64):
      CASE("%" PRId64, int64)
    case UPB_TYPE(UINT64):
    case UPB_TYPE(FIXED64):
      CASE("%" PRIu64, uint64)
    case UPB_TYPE(UINT32):
    case UPB_TYPE(FIXED32):
      CASE("%" PRIu32, uint32);
    case UPB_TYPE(ENUM): {
      upb_enumdef *enum_def = upb_downcast_enumdef(f->def);
      upb_string *enum_label =
          upb_enumdef_iton(enum_def, upb_value_getint32(val));
      if (enum_label) {
        // We found a corresponding string for this enum.  Otherwise we fall
        // through to the int32 code path.
        CHECK(upb_bytesink_putstr(p->bytesink, enum_label, &p->status));
        break;
      }
    }
    case UPB_TYPE(INT32):
    case UPB_TYPE(SFIXED32):
    case UPB_TYPE(SINT32):
      CASE("%" PRId32, int32)
    case UPB_TYPE(BOOL):
      CASE("%hhu", bool);
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES):
      CHECK(upb_bytesink_putstr(p->bytesink, UPB_STRLIT("\""), &p->status));
      CHECK(upb_textprinter_putescaped(p, upb_value_getstr(val),
                                       f->type == UPB_TYPE(STRING)));
      CHECK(upb_bytesink_putstr(p->bytesink, UPB_STRLIT("\""), &p->status));
      break;
  }
  upb_textprinter_endfield(p);
  return UPB_CONTINUE;
err:
  return UPB_BREAK;
}
Exemple #3
0
static void visit_check(const upb_refcounted *obj, const upb_refcounted *subobj,
                        void *closure) {
  check_state *s = closure;
  assert(obj == s->obj);
  assert(subobj);
  upb_inttable *ref2 = &s->ref2;
  upb_value v;
  bool removed = upb_inttable_removeptr(ref2, subobj, &v);
  // The following assertion will fail if the visit() function visits a subobj
  // that it did not have a ref2 on, or visits the same subobj too many times.
  assert(removed);
  int32_t newcount = upb_value_getint32(v) - 1;
  if (newcount > 0) {
    upb_inttable_insert(ref2, (uintptr_t)subobj, upb_value_int32(newcount));
  }
}
Exemple #4
0
int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  return upb_value_getint32(upb_strtable_iter_value(iter));
}
Exemple #5
0
bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name, int32_t *num) {
  const upb_value *v = upb_strtable_lookup(&def->ntoi, name);
  if (!v) return false;
  if (num) *num = upb_value_getint32(*v);
  return true;
}