shellharden/src/sitstrdq.rs

66 lines
1.8 KiB
Rust
Raw Normal View History

2018-06-12 19:32:55 +00:00
/*
* Copyright 2016 - 2019 Andreas Nordal
2018-06-12 19:32:55 +00:00
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
use crate::situation::Situation;
use crate::situation::Transition;
use crate::situation::WhatNow;
use crate::situation::flush;
2018-06-12 19:32:55 +00:00
use crate::commonstrcmd::CommonStrCmdResult;
use crate::commonstrcmd::common_str_cmd;
2018-06-12 19:32:55 +00:00
pub struct SitStrDq {}
impl Situation for SitStrDq {
fn whatnow(&mut self, horizon: &[u8], is_horizon_lengthenable: bool) -> WhatNow {
for (i, &a) in horizon.iter().enumerate() {
if a == b'\"' {
return WhatNow{tri: Transition::Pop, pre: i, len: 1, alt: None};
2018-06-12 19:32:55 +00:00
}
match common_str_cmd(&horizon, i, is_horizon_lengthenable, false) {
2020-02-23 17:51:01 +00:00
CommonStrCmdResult::None => {}
CommonStrCmdResult::Some(x) |
2020-02-23 17:51:01 +00:00
CommonStrCmdResult::OnlyWithQuotes(x) => { return x; }
2018-06-12 19:32:55 +00:00
}
}
flush(horizon.len())
2018-06-12 19:32:55 +00:00
}
2020-02-23 19:39:27 +00:00
fn get_color(&self) -> u32 {
0x00_ff0000
2018-06-12 19:32:55 +00:00
}
}
2019-08-03 12:58:32 +00:00
#[cfg(test)]
use crate::testhelpers::*;
2019-08-03 12:58:32 +00:00
#[cfg(test)]
use crate::sitcmd::SitNormal;
2019-08-03 12:58:32 +00:00
#[cfg(test)]
use crate::sitvec::SitVec;
2019-08-03 12:58:32 +00:00
#[cfg(test)]
use crate::situation::COLOR_MAGIC;
2019-08-03 12:58:32 +00:00
#[test]
fn test_sit_strdq() {
let found_cmdsub = WhatNow{
2019-08-03 12:58:32 +00:00
tri: Transition::Push(Box::new(SitNormal{
end_trigger: u16::from(b')'), end_replace: None,
})), pre: 0, len: 2, alt: None
};
let found_math = WhatNow{
2019-08-03 12:58:32 +00:00
tri: Transition::Push(Box::new(SitVec{
terminator: vec!{b')', b')'},
color: COLOR_MAGIC,
})), pre: 0, len: 3, alt: None
};
sit_expect!(SitStrDq{}, b"", &flush(0));
sit_expect!(SitStrDq{}, b"$", &flush(0), &flush(1));
sit_expect!(SitStrDq{}, b"$(", &flush(0), &found_cmdsub);
2019-08-03 12:58:32 +00:00
sit_expect!(SitStrDq{}, b"$( ", &found_cmdsub);
sit_expect!(SitStrDq{}, b"$((", &found_math);
}