tinybits 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/tinybits/extconf.rb +1 -0
- data/ext/tinybits/tinybits.h +7 -4
- data/ext/tinybits/tinybits_ext.c +50 -1
- data/lib/tinybits/version.rb +1 -1
- data/lib/tinybits.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 579aa574466ff4f72f4e9fa59940d3d14fc773404cb73296b2b1498551da6d2f
|
4
|
+
data.tar.gz: 5edf98872ab8d59a3868b0dc6e3b05271d8dbdba7664afc039ca219820a7b247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 887b0b5c435cd7da039af2b370f680c5266c06cad1db665e54436f81a22172837495739c051ccea3050a446e055ca24a9198269a51bdb7005fec79d398c37758
|
7
|
+
data.tar.gz: 44c1cd7a40ccf330d8ca96a5c7203b6ce60c07c00c3580fe28440ef0b62c6c083ad4417143a52afce3ae0553eefbc7a9d9035e7011d13557f682704be6f5ad5d
|
data/ext/tinybits/extconf.rb
CHANGED
data/ext/tinybits/tinybits.h
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/**
|
2
2
|
* TinyBits Amalgamated Header
|
3
|
-
* Generated on: Sun May 4
|
3
|
+
* Generated on: Sun May 4 05:43:30 PM CEST 2025
|
4
4
|
*/
|
5
5
|
|
6
6
|
#ifndef TINY_BITS_H
|
@@ -89,8 +89,11 @@ typedef struct HashTable {
|
|
89
89
|
|
90
90
|
static inline uint32_t fast_hash_32(const char* str, uint16_t len) {
|
91
91
|
uint32_t hash = len;
|
92
|
-
hash = (hash <<
|
93
|
-
|
92
|
+
hash = (hash << 24) |
|
93
|
+
((unsigned char)str[0] << 16) |
|
94
|
+
((unsigned char)str[1] << 8 ) |
|
95
|
+
((unsigned char)str[len-1]);
|
96
|
+
//hash ^= (((unsigned char)str[len-2] << 24) | ((unsigned char)str[len-1] << 16));
|
94
97
|
return hash;
|
95
98
|
}
|
96
99
|
|
@@ -652,7 +655,7 @@ static inline int pack_str(tiny_bits_packer *encoder, char* str, uint32_t str_le
|
|
652
655
|
HashEntry entry = encoder->encode_table.cache[index - 1];
|
653
656
|
if (hash_code == entry.hash
|
654
657
|
&& str_len == entry.length
|
655
|
-
&&
|
658
|
+
&& fast_memcmp(str, encoder->buffer + entry.offset, str_len) == 0 ) {
|
656
659
|
id = index - 1;
|
657
660
|
found = 1;
|
658
661
|
break;
|
data/ext/tinybits/tinybits_ext.c
CHANGED
@@ -4,8 +4,23 @@
|
|
4
4
|
#include "tinybits.h"
|
5
5
|
|
6
6
|
// Ruby module and classes
|
7
|
+
/*
|
8
|
+
* Document-module: TinyBits
|
9
|
+
*
|
10
|
+
* A Ruby extension for fast binary serialization and deserialization of Ruby objects.
|
11
|
+
*/
|
7
12
|
VALUE rb_mTinyBits;
|
13
|
+
/*
|
14
|
+
* Document-class: TinyBits::Packer
|
15
|
+
*
|
16
|
+
* The Packer class handles serialization of Ruby objects to the TinyBits binary format.
|
17
|
+
*/
|
8
18
|
VALUE rb_cPacker;
|
19
|
+
/*
|
20
|
+
* Document-class: TinyBits::Unpacker
|
21
|
+
*
|
22
|
+
* The Unpacker class handles deserialization of TinyBits binary format to Ruby objects.
|
23
|
+
*/
|
9
24
|
VALUE rb_cUnpacker;
|
10
25
|
|
11
26
|
// Forward declarations
|
@@ -75,6 +90,13 @@ static VALUE rb_packer_alloc(VALUE klass) {
|
|
75
90
|
return TypedData_Wrap_Struct(klass, &packer_data_type, packer_data);
|
76
91
|
}
|
77
92
|
|
93
|
+
/*
|
94
|
+
* Document-method: initialize
|
95
|
+
*
|
96
|
+
* Initializes a new Packer object
|
97
|
+
*
|
98
|
+
* @return [Packer] The initialized packer object.
|
99
|
+
*/
|
78
100
|
static VALUE rb_packer_init(VALUE self) {
|
79
101
|
PackerData* packer_data;
|
80
102
|
TypedData_Get_Struct(self, PackerData, &packer_data_type, packer_data);
|
@@ -141,7 +163,17 @@ static inline int pack_ruby_object_recursive(tiny_bits_packer* packer, VALUE obj
|
|
141
163
|
}
|
142
164
|
}
|
143
165
|
|
144
|
-
|
166
|
+
/*
|
167
|
+
* Document-method: pack
|
168
|
+
*
|
169
|
+
* Packs a Ruby object into a binary string.
|
170
|
+
* Supports Ruby types: String, Array, Hash, Integer, Float, nil, true, false, Symbol, and Time.
|
171
|
+
* Objects can implement a `to_tinybits` method to provide custom serialization.
|
172
|
+
*
|
173
|
+
* @param obj [Object] The Ruby object to pack.
|
174
|
+
* @return [String] The packed binary string (frozen).
|
175
|
+
* @raise [RuntimeError] If packing fails due to unsupported types or other errors.
|
176
|
+
*/
|
145
177
|
static VALUE rb_pack(VALUE self, VALUE obj) {
|
146
178
|
PackerData* packer_data;
|
147
179
|
TypedData_Get_Struct(self, PackerData, &packer_data_type, packer_data);
|
@@ -169,6 +201,16 @@ static VALUE rb_pack(VALUE self, VALUE obj) {
|
|
169
201
|
return result;
|
170
202
|
}
|
171
203
|
|
204
|
+
/*
|
205
|
+
* Document-method: push
|
206
|
+
*
|
207
|
+
* Appends a packed object to the current buffer.
|
208
|
+
* Inserts a separator when appending to non-empty buffer.
|
209
|
+
*
|
210
|
+
* @param obj [Object] The Ruby object to append.
|
211
|
+
* @return [Integer] The number of bytes added to the buffer.
|
212
|
+
* @raise [RuntimeError] If packing fails.
|
213
|
+
*/
|
172
214
|
static VALUE rb_push(VALUE self, VALUE obj) {
|
173
215
|
PackerData* packer_data;
|
174
216
|
TypedData_Get_Struct(self, PackerData, &packer_data_type, packer_data);
|
@@ -199,6 +241,13 @@ static VALUE rb_push(VALUE self, VALUE obj) {
|
|
199
241
|
return INT2FIX(packer_data->packer->current_pos - initial_pos);
|
200
242
|
}
|
201
243
|
|
244
|
+
/*
|
245
|
+
* Document-method: to_s
|
246
|
+
*
|
247
|
+
* Returns the current packed buffer as a string.
|
248
|
+
*
|
249
|
+
* @return [String] The current packed buffer contents (frozen).
|
250
|
+
*/
|
202
251
|
static VALUE rb_to_s(VALUE self){
|
203
252
|
PackerData* packer_data;
|
204
253
|
TypedData_Get_Struct(self, PackerData, &packer_data_type, packer_data);
|
data/lib/tinybits/version.rb
CHANGED
data/lib/tinybits.rb
CHANGED
@@ -2,6 +2,19 @@ require_relative './tinybits/version'
|
|
2
2
|
require 'tinybits_ext'
|
3
3
|
|
4
4
|
module TinyBits
|
5
|
+
# packs an object to a binary string
|
6
|
+
# @param obj [Object] The Ruby object to pack.
|
7
|
+
# @return [String] The packed binary string (frozen).
|
8
|
+
# @raise [RuntimeError] If packing fails due to unsupported types or other errors.
|
9
|
+
# this is a convinience interface, a better way is to instantiate a TinyBits::Packer
|
10
|
+
# object and use its #pack method
|
5
11
|
def self.pack(object) = Packer.new.pack(object)
|
12
|
+
|
13
|
+
# unpacks an object from a binary string
|
14
|
+
# @param buffer [String] The Ruby string holding the packed buffer.
|
15
|
+
# @return [Object] The unpacked Ruby Object (all strings within the object will be frozen).
|
16
|
+
# @raise [RuntimeError] If unpacking fails due to unsupported types or malformed data.
|
17
|
+
# this is a convinience interface, a better way is to instantiate a TinyBits::Unpacker
|
18
|
+
# object and use its #unpack method
|
6
19
|
def self.unpack(buffer) = Unpacker.new.unpack(buffer)
|
7
20
|
end
|