Skip to content

Commit d6bae9b

Browse files
Port hex_to_int from C to Rust
1 parent c91f806 commit d6bae9b

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/lib_ccx/utility.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ extern void ccxr_timestamp_to_srttime(uint64_t timestamp, char *buffer);
1616
extern void ccxr_timestamp_to_vtttime(uint64_t timestamp, char *buffer);
1717
extern void ccxr_millis_to_date(uint64_t timestamp, char *buffer, enum ccx_output_date_format date_format, char millis_separator);
1818
extern int ccxr_stringztoms(const char *s, struct ccx_boundary_time *bt);
19+
#ifndef DISABLE_RUST
20+
extern int ccxr_hex_to_int(char high, char low);
21+
#endif
1922

2023
static uint32_t crc32_table[] = {
2124
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
@@ -266,6 +269,9 @@ void sleep_secs(int secs)
266269

267270
int hex_to_int(char high, char low)
268271
{
272+
#ifndef DISABLE_RUST
273+
return ccxr_hex_to_int(high, low);
274+
#else
269275
unsigned char h, l;
270276
if (high >= '0' && high <= '9')
271277
h = high - '0';
@@ -280,6 +286,7 @@ int hex_to_int(char high, char low)
280286
else
281287
return -1;
282288
return h * 16 + l;
289+
#endif
283290
}
284291
int hex_string_to_int(char *string, int len)
285292
{

src/rust/lib_ccxr/src/util/hex.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub fn hex_to_int(high: char, low: char) -> i32 {
2+
let h = match high {
3+
'0'..='9' => high as i32 - '0' as i32,
4+
'a'..='f' => high as i32 - 'a' as i32 + 10,
5+
_ => return -1,
6+
};
7+
let l = match low {
8+
'0'..='9' => low as i32 - '0' as i32,
9+
'a'..='f' => low as i32 - 'a' as i32 + 10,
10+
_ => return -1,
11+
};
12+
h * 16 + l
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn test_hex_to_int() {
21+
assert_eq!(hex_to_int('0', '0'), 0);
22+
assert_eq!(hex_to_int('f', 'f'), 255);
23+
assert_eq!(hex_to_int('a', '0'), 160);
24+
assert_eq!(hex_to_int('0', 'a'), 10);
25+
assert_eq!(hex_to_int('g', '0'), -1);
26+
assert_eq!(hex_to_int('0', 'g'), -1);
27+
}
28+
}

src/rust/lib_ccxr/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
pub mod bits;
1414
pub mod encoders_helper;
1515
pub mod encoding;
16+
pub mod hex;
1617
pub mod levenshtein;
1718
pub mod log;
1819
pub mod time;

src/rust/src/libccxr_exports/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod demuxer;
55
pub mod demuxerdata;
66
pub mod net;
77
pub mod time;
8+
pub mod util;
89
use crate::ccx_options;
910
use lib_ccxr::util::log::*;
1011
use lib_ccxr::util::{bits::*, levenshtein::*};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use lib_ccxr::util::hex;
2+
use std::ffi::c_char;
3+
4+
#[no_mangle]
5+
pub extern "C" fn ccxr_hex_to_int(high: c_char, low: c_char) -> i32 {
6+
hex::hex_to_int(high as u8 as char, low as u8 as char)
7+
}

0 commit comments

Comments
 (0)