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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::borrow::Cow;
// this import will become unused in future rust versions
// but won't be removed for now for supporting current
// rust versions
#[allow(unused_imports, deprecated)]
use std::ascii::AsciiExt;

use error::CoreError;
use spec::{
    QuotingClassifier,
    QuotingClass,
    WithoutQuotingValidator,
    PartialCodePoint,
    GeneralQSSpec
};



/// quotes the input string returning the quoted string
///
/// # Example
///
/// ```
/// // use your own Spec instead
/// use quoted_string::test_utils::TestSpec;
/// use quoted_string::quote;
///
/// let qs = quote::<TestSpec>("some\"text").unwrap();
/// assert_eq!(qs, "\"some\\\"text\"");
/// ```
///
#[inline]
pub fn quote<Spec: GeneralQSSpec>(
    input: &str
) -> Result<String, CoreError>
{
    let mut out = String::with_capacity(input.len()+2);
    out.push('"');
    quote_inner::<Spec>(input, &mut out)?;
    out.push('"');
    Ok(out)
}

/// quotes a input writing it into the output buffer, does not add surrounding '"'
///
/// if ascii_only is true and non ascii chars a found an error is returned.
///
/// If no error is returned a boolean indicating if the whole input was ascii is
/// returned.
fn quote_inner<Spec: GeneralQSSpec>(
    input: &str,
    out: &mut String,
) -> Result<(), CoreError>
{
    use self::QuotingClass::*;
    for ch in input.chars() {
        match Spec::Quoting::classify_for_quoting(PartialCodePoint::from_code_point(ch as u32)) {
            QText => out.push(ch),
            NeedsQuoting => { out.push('\\'); out.push(ch); }
            Invalid => return Err(CoreError::InvalidChar)
        }
    }
    Ok(())
}

/// quotes the input string if needed
///
/// The `validator` decides if the value is valid without
/// quoting it, the `Spec` type decides how quoting is done if
/// needed. The `Spec` only specifies the format of quoting
/// e.g. which values are allowed in a quoted-string but
/// wether or not a string needs quoting can often depend
/// on additional factor.
///
/// Note that this implementation expects the
/// validator and spec to be in sync, i.e. what
/// is valid without quoting does not need to
/// be escaped when appearing in a quoted string.
///
/// # Example
///
/// ```
/// # use std::borrow::Cow;
/// // use your own Spec
/// use quoted_string::test_utils::{TestSpec, TestUnquotedValidator};
/// use quoted_string::quote_if_needed;
///
/// let mut without_quoting = TestUnquotedValidator::new();
/// let quoted = quote_if_needed::<TestSpec, _>("simple", &mut without_quoting)
///     .expect("only fails if input can not be represented as quoted string with used Spec");
///
/// // The used spec states a 6 character us-ascii word does not need to be represented as
/// // quoted string
/// assert_eq!(quoted, Cow::Borrowed("simple"));
///
/// let mut without_quoting = TestUnquotedValidator::new();
/// let quoted2 = quote_if_needed::<TestSpec, _>("more complex", &mut without_quoting).unwrap();
/// let expected: Cow<'static, str> = Cow::Owned("\"more complex\"".into());
/// assert_eq!(quoted2, expected);
/// ```
///
pub fn quote_if_needed<'a, Spec, WQImpl>(
    input: &'a str,
    validator: &mut WQImpl
) -> Result<Cow<'a, str>, CoreError>
    where Spec: GeneralQSSpec,
          WQImpl: WithoutQuotingValidator
{
    let mut needs_quoting_from = None;
    for (idx, ch) in input.char_indices() {
        let pcp = PartialCodePoint::from_code_point(ch as u32);
        if !validator.next(pcp) {
            needs_quoting_from = Some(idx);
            break;
        } else {
            //FIXME check if is this even enabled in the right context
            #[cfg(debug_assertions)]
            {
                use self::QuotingClass::*;
                match Spec::Quoting::classify_for_quoting(pcp) {
                    QText => {},
                    Invalid => panic!(concat!("[BUG] representable without quoted string,",
                                            "but invalid in quoted string: {}"), ch),
                    NeedsQuoting => panic!(concat!("[BUG] representable without quoted string,",
                                            "but not without escape in quoted string: {}"), ch)
                }
            }
        }
    }

    let start_quoting_from =
        if input.len() == 0 {
            0
        } else if let Some(offset) = needs_quoting_from {
            offset
        } else {
            return if validator.end() {
                Ok(Cow::Borrowed(input))
            } else {
                let mut out = String::with_capacity(input.len() + 2);
                out.push('"');
                out.push_str(input);
                out.push('"');
                Ok(Cow::Owned(out))
            };
        };


    let mut out = String::with_capacity(input.len() + 3);
    out.push('"');
    out.push_str(&input[0..start_quoting_from]);
    quote_inner::<Spec>(&input[start_quoting_from..], &mut out)?;
    out.push('"');
    Ok(Cow::Owned(out))
}


#[cfg(test)]
mod test {
    // this import will become unused in future rust versions
    // but won't be removed for now for supporting current
    // rust versions
    #[allow(warnings)]
    use std::ascii::AsciiExt;
    use test_utils::*;
    use error::CoreError;
    use super::*;

    #[test]
    fn quote_simple() {
        let data = &[
            ("this is simple", "\"this is simple\""),
            ("with quotes\"  ", "\"with quotes\\\"  \""),
            ("with slash\\  ", "\"with slash\\\\  \"")
        ];
        for &(unquoted, quoted) in data.iter() {
            let got_quoted = quote::<TestSpec>(unquoted).unwrap();
            assert_eq!(got_quoted, quoted);
        }
    }

    #[test]
    fn quote_unquotable() {
        let res = quote::<TestSpec>("→");
        assert_eq!(res.unwrap_err(), CoreError::InvalidChar);
    }

    #[test]
    fn quote_if_needed_unneeded() {
        let mut without_quoting = TestUnquotedValidator::new();
        let out= quote_if_needed::<TestSpec, _>("abcdef", &mut without_quoting).unwrap();
        assert_eq!(out, Cow::Borrowed("abcdef"));
    }

    #[test]
    fn quote_if_needed_state() {
        let mut without_quoting = TestUnquotedValidator::new();
        let out = quote_if_needed::<TestSpec, _>("abcd.e", &mut without_quoting).unwrap();
        assert_eq!(out, Cow::Borrowed("abcd.e"));
        assert_eq!(without_quoting.count, 6);
        assert_eq!(without_quoting.last_was_dot, false)
    }

    #[test]
    fn quote_if_needed_needed_because_char() {
        let mut without_quoting = TestUnquotedValidator::new();
        let out = quote_if_needed::<TestSpec, _>("ab def", &mut without_quoting).unwrap();
        let expected: Cow<'static, str> = Cow::Owned("\"ab def\"".into());
        assert_eq!(out, expected);
        assert!(without_quoting.count >= 2);
    }

    #[test]
    fn quote_if_needed_needed_because_state() {
        let mut without_quoting = TestUnquotedValidator::new();
        let out = quote_if_needed::<TestSpec, _>("abc..f", &mut without_quoting).unwrap();
        let expected: Cow<'static, str> = Cow::Owned("\"abc..f\"".into());
        assert_eq!(out, expected);
        assert!(without_quoting.count >= 4);
    }

    #[test]
    fn quote_if_needed_needed_because_end() {
        let mut without_quoting = TestUnquotedValidator::new();
        let out = quote_if_needed::<TestSpec, _>("a", &mut without_quoting).unwrap();
        let expected: Cow<'static, str> = Cow::Owned("\"a\"".into());
        assert_eq!(out, expected);
        assert!(without_quoting.count >= 1);
    }

    #[test]
    fn quote_if_needed_empty_value() {
        let mut without_quoting = TestUnquotedValidator::new();
        let out = quote_if_needed::<TestSpec, _>("", &mut without_quoting).unwrap();
        let expected: Cow<'static, str> = Cow::Owned("\"\"".into());
        assert_eq!(out, expected);
        assert_eq!(without_quoting.count, 0);
    }
}