1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
//! ref: composer/vendor/symfony/console/Input/StringInput.php
use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::input::argv_input::ArgvInput;
use crate::symfony::console::input::input_definition::InputDefinition;
use crate::symfony::console::input::input_interface::InputInterface;
use indexmap::IndexMap;
use shirabe_php_shim::{CaptureKey, PhpMixed};
/// StringInput represents an input provided as a string.
///
/// Usage:
///
/// $input = new StringInput('foo --bar="foobar"');
#[derive(Debug, Clone)]
pub struct StringInput {
pub(crate) inner: ArgvInput,
}
impl StringInput {
pub const REGEX_STRING: &'static str = r#"([^\s]+?)(?:\s|(?<!\\)"|(?<!\\)'|$)"#;
pub const REGEX_UNQUOTED_STRING: &'static str = r#"([^\s\\]+?)"#;
pub const REGEX_QUOTED_STRING: &'static str =
r#"(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)')"#;
pub fn new(input: &str) -> anyhow::Result<Self> {
// parent::__construct([])
let inner = ArgvInput::new(Some(vec![]), None)?;
let mut string_input = StringInput { inner };
let tokens = string_input.tokenize(input)?;
string_input.inner.set_tokens(tokens);
Ok(string_input)
}
/// Tokenizes a string.
fn tokenize(&self, input: &str) -> anyhow::Result<Vec<String>> {
let bytes = input.as_bytes();
let mut tokens: Vec<String> = vec![];
let length = shirabe_php_shim::strlen(input);
let mut cursor: i64 = 0;
let mut token: Option<String> = None;
while cursor < length {
if bytes[cursor as usize] == b'\\' {
cursor += 1;
let next: String = match bytes.get(cursor as usize) {
Some(b) => String::from_utf8_lossy(&[*b]).into_owned(),
None => String::new(),
};
token = Some(format!("{}{}", token.unwrap_or_default(), next));
cursor += 1;
continue;
}
let mut m: IndexMap<CaptureKey, Option<String>> = IndexMap::new();
if shirabe_php_shim::preg_match2(r"/\s+/A", input, &mut m, 0, cursor as usize) {
if token.is_some() {
tokens.push(token.take().unwrap());
}
cursor +=
shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or(""));
} else if shirabe_php_shim::preg_match2(
&format!(r#"/([^="'\s]+?)(=?)({}+)/A"#, Self::REGEX_QUOTED_STRING),
input,
&mut m,
0,
cursor as usize,
) {
let inner = shirabe_php_shim::substr(
m[&CaptureKey::ByIndex(3)].as_deref().unwrap_or(""),
1,
Some(-1),
);
let replaced =
shirabe_php_shim::str_replace_arr(&["\"'", "'\"", "''", "\"\""], "", &inner);
token = Some(format!(
"{}{}{}{}",
token.unwrap_or_default(),
m[&CaptureKey::ByIndex(1)].as_deref().unwrap_or(""),
m[&CaptureKey::ByIndex(2)].as_deref().unwrap_or(""),
shirabe_php_shim::stripcslashes(&replaced)
));
cursor +=
shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or(""));
} else if shirabe_php_shim::preg_match2(
&format!(r"/{}/A", Self::REGEX_QUOTED_STRING),
input,
&mut m,
0,
cursor as usize,
) {
token = Some(format!(
"{}{}",
token.unwrap_or_default(),
shirabe_php_shim::stripcslashes(&shirabe_php_shim::substr(
m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or(""),
1,
Some(-1)
))
));
cursor +=
shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or(""));
} else if shirabe_php_shim::preg_match2(
&format!(r"/{}/A", Self::REGEX_UNQUOTED_STRING),
input,
&mut m,
0,
cursor as usize,
) {
token = Some(format!(
"{}{}",
token.unwrap_or_default(),
m[&CaptureKey::ByIndex(1)].as_deref().unwrap_or("")
));
cursor +=
shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or(""));
} else {
// should never happen
return Err(
InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
message: format!(
"Unable to parse input near \"... {} ...\".",
PhpMixed::String(shirabe_php_shim::substr(input, cursor, Some(10),)),
),
code: 0,
})
.into(),
);
}
}
if let Some(token) = token {
tokens.push(token);
}
Ok(tokens)
}
}
impl InputInterface for StringInput {
fn dup(&self) -> std::rc::Rc<std::cell::RefCell<dyn InputInterface>> {
std::rc::Rc::new(std::cell::RefCell::new(self.clone()))
}
fn get_first_argument(&self) -> Option<String> {
self.inner.get_first_argument()
}
fn has_parameter_option(&self, values: PhpMixed, only_params: bool) -> bool {
InputInterface::has_parameter_option(&self.inner, values, only_params)
}
fn get_parameter_option(
&self,
values: PhpMixed,
default: PhpMixed,
only_params: bool,
) -> PhpMixed {
InputInterface::get_parameter_option(&self.inner, values, default, only_params)
}
fn bind(&mut self, definition: &InputDefinition) -> anyhow::Result<()> {
InputInterface::bind(&mut self.inner, definition)
}
fn validate(&mut self) -> anyhow::Result<()> {
self.inner.validate()
}
fn get_arguments(&self) -> IndexMap<String, PhpMixed> {
InputInterface::get_arguments(&self.inner)
}
fn get_argument(&self, name: &str) -> anyhow::Result<PhpMixed> {
self.inner.get_argument(name)
}
fn set_argument(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()> {
self.inner.set_argument(name, value)
}
fn has_argument(&self, name: &str) -> bool {
self.inner.has_argument(name)
}
fn get_options(&self) -> IndexMap<String, PhpMixed> {
InputInterface::get_options(&self.inner)
}
fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
self.inner.get_option(name)
}
fn set_option(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()> {
self.inner.set_option(name, value)
}
fn has_option(&self, name: &str) -> bool {
self.inner.has_option(name)
}
fn is_interactive(&self) -> bool {
self.inner.is_interactive()
}
fn set_interactive(&mut self, interactive: bool) {
self.inner.set_interactive(interactive)
}
}
|