Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Macros: Explicitely cast the input as expected #67

Merged
merged 1 commit into from
Aug 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,28 @@ macro_rules! itag(
{
use std::ascii::AsciiExt;

let input = $input as &[u8];

#[inline(always)]
fn as_bytes<T: ::nom::AsBytes>(datum: &T) -> &[u8] {
datum.as_bytes()
}

let expected = $string;
let bytes = as_bytes(&expected);
let input_length = $input.len();
let input_length = input.len();
let bytes_length = bytes.len();
let length = ::std::cmp::min(input_length, bytes_length);
let reduced_input = &$input[..length];
let reduced_input = &input[..length];
let reduced_bytes = &bytes[..length];

let output: $crate::Result<_, _> =
if !reduced_input.eq_ignore_ascii_case(reduced_bytes) {
$crate::Result::Error($crate::Error::Position($crate::ErrorKind::Custom($crate::macros::ErrorKindCustom::ITag as u32), $input))
$crate::Result::Error($crate::Error::Position($crate::ErrorKind::Custom($crate::macros::ErrorKindCustom::ITag as u32), input))
} else if length < bytes_length {
$crate::Result::Incomplete($crate::Needed::Size(bytes_length))
} else {
$crate::Result::Done(&$input[bytes_length..], $string)
$crate::Result::Done(&input[bytes_length..], $string)
};

output
Expand Down