Repository navigation
Expand file tree
/
Copy pathcodec.rs
More file actions
98 lines (86 loc) Β· 3.1 KB
/
Copy pathcodec.rs
File metadata and controls
98 lines (86 loc) Β· 3.1 KB
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
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub const NAME_VALUE_SEPARATOR: char = '\u{0001}';
pub const PROPERTY_SEPARATOR: char = '\u{0002}';
use std::collections::HashMap;
use cheetah_string::CheetahString;
const HEX: &[u8; 16] = b"0123456789ABCDEF";
#[inline]
pub(crate) fn upper_hex_pair(byte: u8) -> [u8; 2] {
[HEX[usize::from(byte >> 4)], HEX[usize::from(byte & 0x0f)]]
}
pub fn bytes_to_string(bytes: &[u8]) -> String {
let mut value = String::with_capacity(bytes.len() * 2);
for byte in bytes {
value.push(HEX[(byte >> 4) as usize] as char);
value.push(HEX[(byte & 0x0f) as usize] as char);
}
value
}
pub fn string_to_bytes(value: &str) -> Option<Vec<u8>> {
if value.is_empty() || !value.len().is_multiple_of(2) {
return None;
}
value
.as_bytes()
.as_chunks::<2>()
.0
.iter()
.map(|pair| {
let high = hex_value(pair[0])?;
let low = hex_value(pair[1])?;
Some((high << 4) | low)
})
.collect()
}
pub fn write_int(buffer: &mut [char], position: usize, value: i32) {
for (offset, shift) in (0..8).zip((0..=28).rev().step_by(4)) {
buffer[position + offset] = HEX[((value >> shift) & 0x0f) as usize] as char;
}
}
pub fn write_short(buffer: &mut [char], position: usize, value: i16) {
for (offset, shift) in (0..4).zip((0..=12).rev().step_by(4)) {
buffer[position + offset] = HEX[((value >> shift) & 0x0f) as usize] as char;
}
}
pub fn message_properties_to_string(properties: &HashMap<CheetahString, CheetahString>) -> CheetahString {
let mut value = String::new();
for (name, property) in properties {
value.push_str(name);
value.push(NAME_VALUE_SEPARATOR);
value.push_str(property);
value.push(PROPERTY_SEPARATOR);
}
CheetahString::from_string(value)
}
pub fn string_to_message_properties(properties: Option<&CheetahString>) -> HashMap<CheetahString, CheetahString> {
let Some(properties) = properties else {
return HashMap::new();
};
properties
.split_char(PROPERTY_SEPARATOR)
.filter_map(|entry| {
let (name, value) = entry.split_once(NAME_VALUE_SEPARATOR)?;
(!name.is_empty()).then(|| (CheetahString::from_slice(name), CheetahString::from_slice(value)))
})
.collect()
}
fn hex_value(byte: u8) -> Option<u8> {
match byte {
b'0'..=b'9' => Some(byte - b'0'),
b'a'..=b'f' => Some(byte - b'a' + 10),
b'A'..=b'F' => Some(byte - b'A' + 10),
_ => None,
}
}