diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2016-07-25 10:09:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-25 10:09:15 -0700 |
commit | 44bd6bda58d3889c06e82c5060609182f124aba3 (patch) | |
tree | facc0a600e9b52dc5c94d48f891c8c4a845eca67 /ruby/ext/google/protobuf_c/map.c | |
parent | 06220303323f3cce425706540defcd7a29d42ec2 (diff) | |
parent | d07a9963df7c01d978c0749eb5753f98a3f0dc65 (diff) | |
download | protobuf-44bd6bda58d3889c06e82c5060609182f124aba3.tar.gz protobuf-44bd6bda58d3889c06e82c5060609182f124aba3.tar.bz2 protobuf-44bd6bda58d3889c06e82c5060609182f124aba3.zip |
Merge pull request #1821 from haberman/rubyfreezestr
Ruby: encode and freeze strings when the are assigned or decoded.
Diffstat (limited to 'ruby/ext/google/protobuf_c/map.c')
-rw-r--r-- | ruby/ext/google/protobuf_c/map.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/ruby/ext/google/protobuf_c/map.c b/ruby/ext/google/protobuf_c/map.c index 92fc7286..12f1f9dd 100644 --- a/ruby/ext/google/protobuf_c/map.c +++ b/ruby/ext/google/protobuf_c/map.c @@ -63,16 +63,16 @@ // construct a key byte sequence if needed. |out_key| and |out_length| provide // the resulting key data/length. #define TABLE_KEY_BUF_LENGTH 8 // sizeof(uint64_t) -static void table_key(Map* self, VALUE key, - char* buf, - const char** out_key, - size_t* out_length) { +static VALUE table_key(Map* self, VALUE key, + char* buf, + const char** out_key, + size_t* out_length) { switch (self->key_type) { case UPB_TYPE_BYTES: case UPB_TYPE_STRING: // Strings: use string content directly. Check_Type(key, T_STRING); - native_slot_validate_string_encoding(self->key_type, key); + key = native_slot_encode_and_freeze_string(self->key_type, key); *out_key = RSTRING_PTR(key); *out_length = RSTRING_LEN(key); break; @@ -93,6 +93,8 @@ static void table_key(Map* self, VALUE key, assert(false); break; } + + return key; } static VALUE table_key_to_ruby(Map* self, const char* buf, size_t length) { @@ -357,7 +359,7 @@ VALUE Map_index(VALUE _self, VALUE key) { const char* keyval = NULL; size_t length = 0; upb_value v; - table_key(self, key, keybuf, &keyval, &length); + key = table_key(self, key, keybuf, &keyval, &length); if (upb_strtable_lookup2(&self->table, keyval, length, &v)) { void* mem = value_memory(&v); @@ -383,7 +385,7 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) { size_t length = 0; upb_value v; void* mem; - table_key(self, key, keybuf, &keyval, &length); + key = table_key(self, key, keybuf, &keyval, &length); mem = value_memory(&v); native_slot_set(self->value_type, self->value_type_class, mem, value); @@ -411,7 +413,7 @@ VALUE Map_has_key(VALUE _self, VALUE key) { char keybuf[TABLE_KEY_BUF_LENGTH]; const char* keyval = NULL; size_t length = 0; - table_key(self, key, keybuf, &keyval, &length); + key = table_key(self, key, keybuf, &keyval, &length); if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) { return Qtrue; @@ -434,7 +436,7 @@ VALUE Map_delete(VALUE _self, VALUE key) { const char* keyval = NULL; size_t length = 0; upb_value v; - table_key(self, key, keybuf, &keyval, &length); + key = table_key(self, key, keybuf, &keyval, &length); if (upb_strtable_remove2(&self->table, keyval, length, &v)) { void* mem = value_memory(&v); |