-
Notifications
You must be signed in to change notification settings - Fork 62
make match_any also return index of matched needle #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
description = "Interact with unix processes/bash the same way as pexpect or Don libes expect does" | ||
name = "rexpect" | ||
version = "0.3.0" | ||
version = "0.4.0" | ||
authors = ["Philipp Keller <[email protected]>"] | ||
edition = "2018" | ||
repository = "https://github.com/philippkeller/rexpect" | ||
|
@@ -20,4 +20,4 @@ error-chain = "0.12" | |
tempfile = "3" | ||
|
||
[badges] | ||
travis-ci = { repository = "philippkeller/rexpect" } | ||
travis-ci = { repository = "philippkeller/rexpect" } |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -93,6 +93,15 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub fn find_any(needle: &Vec<ReadUntil>, buffer: &str, eof: bool) -> Option<(usize, usize, usize)> { | ||||||||||||
for (pos, read_until) in needle.iter().enumerate() { | ||||||||||||
if let Some((before, after)) = find(&read_until, buffer, eof) { | ||||||||||||
return Some((before, after, pos)); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
None | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// Non blocking reader | ||||||||||||
/// | ||||||||||||
/// Typically you'd need that to check for output of a process without blocking your thread. | ||||||||||||
|
@@ -222,14 +231,55 @@ impl NBReader { | |||||||||||
/// ``` | ||||||||||||
/// | ||||||||||||
pub fn read_until(&mut self, needle: &ReadUntil) -> Result<(String, String)> { | ||||||||||||
match self.read_until_tuple_pos(needle) { | ||||||||||||
Ok(tuple_pos) => { | ||||||||||||
let first = self.buffer.drain(..tuple_pos.0).collect(); | ||||||||||||
let second = self.buffer.drain(..tuple_pos.1 - tuple_pos.0).collect(); | ||||||||||||
Comment on lines
+236
to
+237
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might it would be better to move it in |
||||||||||||
Ok((first, second)) | ||||||||||||
}, | ||||||||||||
Err(e) => Err(e), | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// Read until any needle is found (blocking!) and return tuple with: | ||||||||||||
/// 1. yet unread string until and without needle | ||||||||||||
/// 2. matched needle | ||||||||||||
/// 3. index of which needle is found | ||||||||||||
/// | ||||||||||||
/// this is the same function as read_until with the difference that the index | ||||||||||||
/// is also returned | ||||||||||||
pub fn read_until_any(&mut self, needle: Vec<ReadUntil>) -> Result<(String, String, usize)> { | ||||||||||||
match self.read_until_tuple_pos(&ReadUntil::Any(needle)) { | ||||||||||||
Ok(tuple_pos) => { | ||||||||||||
let first = self.buffer.drain(..tuple_pos.0).collect(); | ||||||||||||
let second = self.buffer.drain(..tuple_pos.1 - tuple_pos.0).collect(); | ||||||||||||
Ok((first, second, tuple_pos.2)) | ||||||||||||
}, | ||||||||||||
Err(e) => Err(e), | ||||||||||||
} | ||||||||||||
} | ||||||||||||
/// Read until needle is found and return | ||||||||||||
Comment on lines
+260
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
/// 1. yet unread string until and without needle | ||||||||||||
/// 2. matched needle | ||||||||||||
/// 3. index of which needle is found. This is only making sense for ReadUntil::Any | ||||||||||||
/// for the other types this is redundant but I didn't find any more elegant way | ||||||||||||
/// to solve this. | ||||||||||||
fn read_until_tuple_pos(&mut self, needle: &ReadUntil) -> Result<(usize, usize, usize)> { | ||||||||||||
let start = time::Instant::now(); | ||||||||||||
|
||||||||||||
loop { | ||||||||||||
self.read_into_buffer()?; | ||||||||||||
if let Some(tuple_pos) = find(needle, &self.buffer, self.eof) { | ||||||||||||
let first = self.buffer.drain(..tuple_pos.0).collect(); | ||||||||||||
let second = self.buffer.drain(..tuple_pos.1 - tuple_pos.0).collect(); | ||||||||||||
return Ok((first, second)); | ||||||||||||
match needle { | ||||||||||||
ReadUntil::Any(n) => { | ||||||||||||
if let Some(tuple_pos) = find_any(n, &self.buffer, self.eof) { | ||||||||||||
return Ok(tuple_pos) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
needle => { | ||||||||||||
if let Some((before, after)) = find(needle, &self.buffer, self.eof) { | ||||||||||||
return Ok((before, after, 0)) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// reached end of stream and didn't match -> error | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.