MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / compare_field_bytes

Function compare_field_bytes

nodedb-query/src/msgpack_scan/compare.rs:54–114  ·  view source on GitHub ↗

Compare two MessagePack values by their decoded content. Comparison order: 1. Null < Bool < Number < String < Binary < Array < Map 2. Within numbers: compare as f64 3. Within strings: lexicographic on raw bytes (valid UTF-8 guarantees byte order = Unicode code-point order for ASCII/Latin-1) 4. Fallback: raw byte comparison

(
    a_buf: &[u8],
    a_range: (usize, usize),
    b_buf: &[u8],
    b_range: (usize, usize),
)

Source from the content-addressed store, hash-verified

52/// byte order = Unicode code-point order for ASCII/Latin-1)
53/// 4. Fallback: raw byte comparison
54pub fn compare_field_bytes(
55 a_buf: &[u8],
56 a_range: (usize, usize),
57 b_buf: &[u8],
58 b_range: (usize, usize),
59) -> Ordering {
60 let a_off = a_range.0;
61 let b_off = b_range.0;
62
63 let a_tag = match a_buf.get(a_off) {
64 Some(&t) => t,
65 None => return Ordering::Less,
66 };
67 let b_tag = match b_buf.get(b_off) {
68 Some(&t) => t,
69 None => return Ordering::Greater,
70 };
71
72 let a_type = type_rank(a_tag);
73 let b_type = type_rank(b_tag);
74
75 if a_type != b_type {
76 return a_type.cmp(&b_type);
77 }
78
79 match a_type {
80 0 => Ordering::Equal, // both null
81 1 => {
82 // bool
83 let a_val = a_tag == 0xc3; // true
84 let b_val = b_tag == 0xc3;
85 a_val.cmp(&b_val)
86 }
87 2 => {
88 // number — compare as f64
89 match (read_f64(a_buf, a_off), read_f64(b_buf, b_off)) {
90 (Some(a), Some(b)) => a.partial_cmp(&b).unwrap_or(Ordering::Equal),
91 (Some(_), None) => Ordering::Greater,
92 (None, Some(_)) => Ordering::Less,
93 (None, None) => Ordering::Equal,
94 }
95 }
96 3 => {
97 // string — compare raw bytes
98 match (str_bounds(a_buf, a_off), str_bounds(b_buf, b_off)) {
99 (Some((a_s, a_l)), Some((b_s, b_l))) => {
100 let a_bytes = &a_buf[a_s..a_s + a_l];
101 let b_bytes = &b_buf[b_s..b_s + b_l];
102 a_bytes.cmp(b_bytes)
103 }
104 _ => Ordering::Equal,
105 }
106 }
107 _ => {
108 // binary, array, map, ext — fallback to raw byte comparison
109 let a_slice = &a_buf[a_range.0..a_range.1];
110 let b_slice = &b_buf[b_range.0..b_range.1];
111 a_slice.cmp(b_slice)

Callers 4

find_minmaxFunction · 0.85
compare_preextractedFunction · 0.85

Calls 6

type_rankFunction · 0.85
str_boundsFunction · 0.85
read_f64Function · 0.70
getMethod · 0.45
cmpMethod · 0.45
partial_cmpMethod · 0.45

Tested by

no test coverage detected