Пример #1
0
size_t pydynd::get_nonragged_dim_count(const ndt::type &tp, size_t max_count)
{
  if (tp.is_symbolic()) {
    if (tp.is_scalar()) {
      return 0;
    }
  }

  if (!tp.is_scalar()) {
    if (max_count <= 1) {
      return max_count;
    }
    else {
      return min(max_count,
                 1 + get_nonragged_dim_count(
                         static_cast<const ndt::base_dim_type *>(tp.extended())
                             ->get_element_type(),
                         max_count - 1));
    }
  }

  switch (tp.get_id()) {
  case struct_id:
  case tuple_id:
    if (max_count <= 1) {
      return max_count;
    }
    else {
      auto bsd = tp.extended<ndt::tuple_type>();
      size_t field_count = bsd->get_field_count();
      for (size_t i = 0; i != field_count; ++i) {
        size_t candidate =
            1 + get_nonragged_dim_count(bsd->get_field_type(i), max_count - 1);
        if (candidate < max_count) {
          max_count = candidate;
          if (max_count <= 1) {
            return max_count;
          }
        }
      }
      return max_count;
    }
  default:
    return 0;
  }
}
Пример #2
0
static void parse_option_json(const ndt::type &tp, const char *arrmeta, char *out_data, const char *&begin,
                              const char *end, const eval::eval_context *ectx)
{
  begin = skip_whitespace(begin, end);
  const char *saved_begin = begin;
  if (tp.is_scalar()) {
    if (parse_token(begin, end, "null")) {
      tp.extended<ndt::option_type>()->assign_na(arrmeta, out_data, ectx);
      return;
    } else {
      const ndt::type &value_tp = tp.extended<ndt::option_type>()->get_value_type();
      const char *strbegin, *strend;
      bool escaped;
      if (parse::parse_doublequote_string_no_ws(begin, end, strbegin, strend, escaped)) {
        try
        {
          if (!escaped) {
            tp.extended()->set_from_utf8_string(arrmeta, out_data, strbegin, strend, ectx);
          } else {
            std::string val;
            parse::unescape_string(strbegin, strend, val);
            tp.extended()->set_from_utf8_string(arrmeta, out_data, val, ectx);
          }
          return;
        }
        catch (const exception &e)
        {
          throw json_parse_error(saved_begin, e.what(), tp);
        }
        catch (const dynd::dynd_exception &e)
        {
          throw json_parse_error(saved_begin, e.what(), tp);
        }
      } else if (value_tp.get_kind() == bool_kind) {
        if (parse_token(begin, end, "true")) {
          *out_data = 1;
        } else if (parse_token(begin, end, "false")) {
          *out_data = 0;
        } else if (parse::parse_json_number_no_ws(begin, end, strbegin, strend)) {
          if (parse::compare_range_to_literal(strbegin, strend, "1")) {
            *out_data = 1;
          } else if (parse::compare_range_to_literal(strbegin, strend, "0")) {
            *out_data = 0;
          } else {
            throw json_parse_error(begin, "expected a boolean", tp);
          }
        } else {
          throw json_parse_error(begin, "expected a boolean", tp);
        }
        return;
      } else if (value_tp.get_kind() == sint_kind || value_tp.get_kind() == uint_kind ||
                 value_tp.get_kind() == real_kind || value_tp.get_kind() == complex_kind) {
        if (parse::parse_json_number_no_ws(begin, end, strbegin, strend)) {
          parse::string_to_number(out_data, value_tp.get_type_id(), strbegin, strend, false, ectx->errmode);
        } else {
          throw json_parse_error(begin, "expected a number", tp);
        }
        return;
      } else {
        throw json_parse_error(begin, "expected a string", tp);
      }
    }
  }

  stringstream ss;
  ss << "parse_json: unsupported dynd type \"" << tp << "\"";
  throw runtime_error(ss.str());
}