zipora 2.1.4

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Hexadecimal encoding and decoding utilities
//!
//! Provides efficient hex encoding and decoding functions.

use crate::error::{Result, ZiporaError};

/// Decode a hex character to its nibble value (0-15)
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_char_to_nibble;
///
/// assert_eq!(hex_char_to_nibble(b'0'), Some(0));
/// assert_eq!(hex_char_to_nibble(b'9'), Some(9));
/// assert_eq!(hex_char_to_nibble(b'a'), Some(10));
/// assert_eq!(hex_char_to_nibble(b'F'), Some(15));
/// assert_eq!(hex_char_to_nibble(b'g'), None);
/// ```
#[inline]
pub const fn hex_char_to_nibble(c: u8) -> Option<u8> {
    match c {
        b'0'..=b'9' => Some(c - b'0'),
        b'a'..=b'f' => Some(c - b'a' + 10),
        b'A'..=b'F' => Some(c - b'A' + 10),
        _ => None,
    }
}

/// Encode a nibble value (0-15) to a hex character (lowercase)
///
/// # Panics
///
/// Panics if value >= 16
#[inline]
pub const fn nibble_to_hex_lower(value: u8) -> u8 {
    const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";
    HEX_CHARS[value as usize]
}

/// Encode a nibble value (0-15) to a hex character (uppercase)
///
/// # Panics
///
/// Panics if value >= 16
#[inline]
pub const fn nibble_to_hex_upper(value: u8) -> u8 {
    const HEX_CHARS: &[u8; 16] = b"0123456789ABCDEF";
    HEX_CHARS[value as usize]
}

/// Decode a hexadecimal string to bytes
///
/// The input must have an even number of characters.
/// Supports both uppercase and lowercase hex digits.
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_decode;
///
/// let result = hex_decode("48656c6c6f").unwrap();
/// assert_eq!(result, b"Hello");
///
/// let result = hex_decode("DEADBEEF").unwrap();
/// assert_eq!(result, vec![0xDE, 0xAD, 0xBE, 0xEF]);
/// ```
pub fn hex_decode(hex: &str) -> Result<Vec<u8>> {
    hex_decode_bytes(hex.as_bytes())
}

/// Decode a hexadecimal byte slice to bytes
///
/// The input must have an even number of bytes.
/// Supports both uppercase and lowercase hex digits.
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_decode_bytes;
///
/// let result = hex_decode_bytes(b"48656c6c6f").unwrap();
/// assert_eq!(result, b"Hello");
/// ```
pub fn hex_decode_bytes(hex: &[u8]) -> Result<Vec<u8>> {
    if hex.len() % 2 != 0 {
        return Err(ZiporaError::InvalidData {
            message: "hex string must have even length".to_string(),
        });
    }

    let mut result = Vec::with_capacity(hex.len() / 2);

    for chunk in hex.chunks_exact(2) {
        let high = hex_char_to_nibble(chunk[0]).ok_or_else(|| ZiporaError::InvalidData {
            message: format!("invalid hex character: {:?}", chunk[0] as char),
        })?;
        let low = hex_char_to_nibble(chunk[1]).ok_or_else(|| ZiporaError::InvalidData {
            message: format!("invalid hex character: {:?}", chunk[1] as char),
        })?;

        result.push((high << 4) | low);
    }

    Ok(result)
}

/// Decode hex string into an existing buffer
///
/// Returns the number of bytes written. The buffer must be at least `hex.len() / 2` bytes.
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_decode_to_slice;
///
/// let mut buf = [0u8; 5];
/// let len = hex_decode_to_slice(b"48656c6c6f", &mut buf).unwrap();
/// assert_eq!(len, 5);
/// assert_eq!(&buf, b"Hello");
/// ```
pub fn hex_decode_to_slice(hex: &[u8], output: &mut [u8]) -> Result<usize> {
    if hex.len() % 2 != 0 {
        return Err(ZiporaError::InvalidData {
            message: "hex string must have even length".to_string(),
        });
    }

    let output_len = hex.len() / 2;
    if output.len() < output_len {
        return Err(ZiporaError::OutOfBounds {
            index: output_len,
            size: output.len(),
        });
    }

    for (i, chunk) in hex.chunks_exact(2).enumerate() {
        let high = hex_char_to_nibble(chunk[0]).ok_or_else(|| ZiporaError::InvalidData {
            message: format!("invalid hex character: {:?}", chunk[0] as char),
        })?;
        let low = hex_char_to_nibble(chunk[1]).ok_or_else(|| ZiporaError::InvalidData {
            message: format!("invalid hex character: {:?}", chunk[1] as char),
        })?;

        output[i] = (high << 4) | low;
    }

    Ok(output_len)
}

/// Encode bytes to hexadecimal string (lowercase)
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_encode;
///
/// let result = hex_encode(b"Hello");
/// assert_eq!(result, "48656c6c6f");
/// ```
pub fn hex_encode(bytes: &[u8]) -> String {
    let mut result = String::with_capacity(bytes.len() * 2);

    for &byte in bytes {
        result.push(nibble_to_hex_lower(byte >> 4) as char);
        result.push(nibble_to_hex_lower(byte & 0x0f) as char);
    }

    result
}

/// Encode bytes to hexadecimal string (uppercase)
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_encode_upper;
///
/// let result = hex_encode_upper(b"Hello");
/// assert_eq!(result, "48656C6C6F");
/// ```
pub fn hex_encode_upper(bytes: &[u8]) -> String {
    let mut result = String::with_capacity(bytes.len() * 2);

    for &byte in bytes {
        result.push(nibble_to_hex_upper(byte >> 4) as char);
        result.push(nibble_to_hex_upper(byte & 0x0f) as char);
    }

    result
}

/// Encode bytes to hex bytes (lowercase)
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_encode_to_bytes;
///
/// let result = hex_encode_to_bytes(b"\xDE\xAD");
/// assert_eq!(result, b"dead");
/// ```
pub fn hex_encode_to_bytes(bytes: &[u8]) -> Vec<u8> {
    let mut result = Vec::with_capacity(bytes.len() * 2);

    for &byte in bytes {
        result.push(nibble_to_hex_lower(byte >> 4));
        result.push(nibble_to_hex_lower(byte & 0x0f));
    }

    result
}

/// Encode bytes into an existing buffer
///
/// Returns the number of bytes written. The buffer must be at least `bytes.len() * 2` bytes.
///
/// # Examples
///
/// ```rust
/// use zipora::string::hex_encode_to_slice;
///
/// let mut buf = [0u8; 10];
/// let len = hex_encode_to_slice(b"Hello", &mut buf).unwrap();
/// assert_eq!(len, 10);
/// assert_eq!(&buf, b"48656c6c6f");
/// ```
pub fn hex_encode_to_slice(bytes: &[u8], output: &mut [u8]) -> Result<usize> {
    let output_len = bytes.len() * 2;
    if output.len() < output_len {
        return Err(ZiporaError::OutOfBounds {
            index: output_len,
            size: output.len(),
        });
    }

    for (i, &byte) in bytes.iter().enumerate() {
        output[i * 2] = nibble_to_hex_lower(byte >> 4);
        output[i * 2 + 1] = nibble_to_hex_lower(byte & 0x0f);
    }

    Ok(output_len)
}

/// Check if a string is valid hexadecimal
///
/// # Examples
///
/// ```rust
/// use zipora::string::is_valid_hex;
///
/// assert!(is_valid_hex("48656c6c6f"));
/// assert!(is_valid_hex("DEADBEEF"));
/// assert!(!is_valid_hex("hello"));
/// assert!(!is_valid_hex("123"));  // Odd length
/// ```
pub fn is_valid_hex(s: &str) -> bool {
    if s.len() % 2 != 0 {
        return false;
    }
    s.bytes().all(|c| hex_char_to_nibble(c).is_some())
}

/// Parse a single hex byte from two characters
///
/// # Examples
///
/// ```rust
/// use zipora::string::parse_hex_byte;
///
/// assert_eq!(parse_hex_byte(b'4', b'8'), Some(0x48));
/// assert_eq!(parse_hex_byte(b'f', b'f'), Some(0xff));
/// assert_eq!(parse_hex_byte(b'g', b'0'), None);
/// ```
#[inline]
pub fn parse_hex_byte(high: u8, low: u8) -> Option<u8> {
    let h = hex_char_to_nibble(high)?;
    let l = hex_char_to_nibble(low)?;
    Some((h << 4) | l)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hex_char_to_nibble() {
        assert_eq!(hex_char_to_nibble(b'0'), Some(0));
        assert_eq!(hex_char_to_nibble(b'9'), Some(9));
        assert_eq!(hex_char_to_nibble(b'a'), Some(10));
        assert_eq!(hex_char_to_nibble(b'f'), Some(15));
        assert_eq!(hex_char_to_nibble(b'A'), Some(10));
        assert_eq!(hex_char_to_nibble(b'F'), Some(15));
        assert_eq!(hex_char_to_nibble(b'g'), None);
        assert_eq!(hex_char_to_nibble(b' '), None);
    }

    #[test]
    fn test_nibble_to_hex() {
        assert_eq!(nibble_to_hex_lower(0), b'0');
        assert_eq!(nibble_to_hex_lower(9), b'9');
        assert_eq!(nibble_to_hex_lower(10), b'a');
        assert_eq!(nibble_to_hex_lower(15), b'f');

        assert_eq!(nibble_to_hex_upper(0), b'0');
        assert_eq!(nibble_to_hex_upper(10), b'A');
        assert_eq!(nibble_to_hex_upper(15), b'F');
    }

    #[test]
    fn test_hex_decode() {
        let result = hex_decode("48656c6c6f").unwrap();
        assert_eq!(result, b"Hello");

        let result = hex_decode("DEADBEEF").unwrap();
        assert_eq!(result, vec![0xDE, 0xAD, 0xBE, 0xEF]);

        let result = hex_decode("").unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_hex_decode_mixed_case() {
        let result = hex_decode("DeAdBeEf").unwrap();
        assert_eq!(result, vec![0xDE, 0xAD, 0xBE, 0xEF]);
    }

    #[test]
    fn test_hex_decode_error_odd_length() {
        let result = hex_decode("123");
        assert!(result.is_err());
    }

    #[test]
    fn test_hex_decode_error_invalid_char() {
        let result = hex_decode("gg");
        assert!(result.is_err());
    }

    #[test]
    fn test_hex_decode_to_slice() {
        let mut buf = [0u8; 5];
        let len = hex_decode_to_slice(b"48656c6c6f", &mut buf).unwrap();
        assert_eq!(len, 5);
        assert_eq!(&buf, b"Hello");
    }

    #[test]
    fn test_hex_decode_to_slice_error_small_buffer() {
        let mut buf = [0u8; 2];
        let result = hex_decode_to_slice(b"48656c6c6f", &mut buf);
        assert!(result.is_err());
    }

    #[test]
    fn test_hex_encode() {
        let result = hex_encode(b"Hello");
        assert_eq!(result, "48656c6c6f");

        let result = hex_encode(b"\xDE\xAD\xBE\xEF");
        assert_eq!(result, "deadbeef");

        let result = hex_encode(b"");
        assert!(result.is_empty());
    }

    #[test]
    fn test_hex_encode_upper() {
        let result = hex_encode_upper(b"Hello");
        assert_eq!(result, "48656C6C6F");

        let result = hex_encode_upper(b"\xDE\xAD\xBE\xEF");
        assert_eq!(result, "DEADBEEF");
    }

    #[test]
    fn test_hex_encode_to_bytes() {
        let result = hex_encode_to_bytes(b"\xDE\xAD");
        assert_eq!(result, b"dead");
    }

    #[test]
    fn test_hex_encode_to_slice() {
        let mut buf = [0u8; 10];
        let len = hex_encode_to_slice(b"Hello", &mut buf).unwrap();
        assert_eq!(len, 10);
        assert_eq!(&buf, b"48656c6c6f");
    }

    #[test]
    fn test_hex_encode_to_slice_error_small_buffer() {
        let mut buf = [0u8; 4];
        let result = hex_encode_to_slice(b"Hello", &mut buf);
        assert!(result.is_err());
    }

    #[test]
    fn test_is_valid_hex() {
        assert!(is_valid_hex("48656c6c6f"));
        assert!(is_valid_hex("DEADBEEF"));
        assert!(is_valid_hex(""));

        assert!(!is_valid_hex("hello"));
        assert!(!is_valid_hex("123")); // Odd length
        assert!(!is_valid_hex("12gh"));
    }

    #[test]
    fn test_parse_hex_byte() {
        assert_eq!(parse_hex_byte(b'4', b'8'), Some(0x48));
        assert_eq!(parse_hex_byte(b'f', b'f'), Some(0xff));
        assert_eq!(parse_hex_byte(b'0', b'0'), Some(0x00));
        assert_eq!(parse_hex_byte(b'g', b'0'), None);
        assert_eq!(parse_hex_byte(b'0', b'g'), None);
    }

    #[test]
    fn test_roundtrip() {
        let original = b"Hello, World! \x00\xFF\x12\x34";
        let encoded = hex_encode(original);
        let decoded = hex_decode(&encoded).unwrap();
        assert_eq!(decoded, original);
    }
}