Beispiel #1
0
/**
 * Converts the milliseconds time to the raw BSON bytes. We need to
 * explicitly convert using 64 bit here.
 *
 * @example Convert the milliseconds value to BSON bytes.
 *    rb_time_to_bson(time, 2124132340000, encoded);
 *
 * @param [ Time ] self The Ruby Time object.
 * @param [ Integer ] milliseconds The milliseconds pre/post epoch.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with time BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_time_to_bson(int argc, VALUE *argv, VALUE self)
{
  int64_t t = NUM2INT64(rb_funcall(self, rb_intern("to_i"), 0));
  int64_t milliseconds = (int64_t)(t * 1000);
  int32_t micro = NUM2INT(rb_funcall(self, rb_intern("usec"), 0));
  int64_t time = milliseconds + (micro / 1000);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  return int64_t_to_bson(time, encoded);
}
Beispiel #2
0
/**
 * Check if the integer is a 32 bit integer.
 *
 * @example Check if the integer is 32 bit.
 *    rb_integer_is_bson_int32(integer);
 *
 * @param [ Integer ] self The ruby integer.
 *
 * @return [ true, false ] If the integer is 32 bit.
 *
 * @since 2.0.0
 */
static VALUE rb_integer_is_bson_int32(VALUE self)
{
  const int64_t v = NUM2INT64(self);
  if (INT_MIN <= v && v <= INT_MAX) {
    return Qtrue;
  }
  else {
    return Qfalse;
  }
}
Beispiel #3
0
/**
 * Convert the Ruby integer into a character string and append with nullchar to encoded BSON.
 *
 * @example Convert the integer to string and append with nullchar.
 *    rb_integer_to_bson_key(128, encoded);
 *
 * @param [ Integer ] self The Ruby integer.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_integer_to_bson_key(int argc, VALUE *argv, VALUE self)
{
  char bytes[INTEGER_CHAR_SIZE];
  const int64_t v = NUM2INT64(self);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  int length;
  if (v < BSON_INDEX_SIZE)
    return rb_str_cat(encoded, rb_bson_array_indexes[v], strlen(rb_bson_array_indexes[v]) + 1);
  length = snprintf(bytes, INTEGER_CHAR_SIZE, "%ld", (long)v);
  return rb_str_cat(encoded, bytes, length + 1);
}
/**
 * Convert the Ruby integer into a BSON as per the 64 bit specification,
 * which is 8 bytes.
 *
 * @example Convert the integer to 64bit BSON.
 *    rb_integer_to_bson_int64(128, encoded);
 *
 * @param [ Integer ] self The Ruby integer.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_integer_to_bson_int64(VALUE self, VALUE encoded)
{
  const int64_t v = NUM2INT64(self);
  const char bytes[8] = {
    v & 255,
    (v >> 8) & 255,
    (v >> 16) & 255,
    (v >> 24) & 255,
    (v >> 32) & 255,
    (v >> 40) & 255,
    (v >> 48) & 255,
    (v >> 56) & 255
  };
  return rb_str_cat(encoded, bytes, 8);
}
Beispiel #5
0
/**
 * Convert the Ruby integer into a BSON as per the 64 bit specification,
 * which is 8 bytes.
 *
 * @example Convert the integer to 64bit BSON.
 *    rb_integer_to_bson_int64(128, encoded);
 *
 * @param [ Integer ] self The Ruby integer.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_integer_to_bson_int64(VALUE self, VALUE encoded)
{
  return int64_t_to_bson(NUM2INT64(self), encoded);
}