Skip to content

Instantly share code, notes, and snippets.

@neandrake

neandrake/stringparse.rs Secret

Created Sep 11, 2019
Embed
What would you like to do?
/// Returns the value as a slice with the padding character
/// removed per the specification of whether the VR indicates leading/trailing
/// padding is significant.
fn get_string_bytes_without_padding(&self, vr: VRRef) -> &[u8] {
// grab the position to start reading bytes from prior to computing the new bytes_read
let mut lindex: usize = 0;
let data: &[u8] = self.data.as_slice();
if data.is_empty() {
return data;
}
let mut rindex: usize = data.len() - 1;
if vr.can_pad_end {
if vr.padding == vr::SPACE_PADDING {
// space padding should strip all trailing spaces
while rindex > lindex {
if data[rindex] == vr.padding {
rindex -= 1;
} else {
break;
}
}
} else if vr.padding == vr::NULL_PADDING {
// null byte padding is only singular and only if used to achieve even length
if data.len() % 2 == 0 && data[rindex] == vr.padding {
rindex -= 1;
}
}
}
if vr.can_pad_front {
// space padding should strip all leading spaces
if vr.padding == vr::SPACE_PADDING {
while lindex < rindex {
if data[lindex] == vr.padding {
lindex += 1;
} else {
break;
}
}
}
// no such thing as leading padding of null bytes
}
&data[lindex..=rindex]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment