| 153 | } |
| 154 | |
| 155 | bool IsValidPlainScalar(const std::string& str, FlowType::value flowType, |
| 156 | bool allowOnlyAscii) { |
| 157 | // check against null |
| 158 | if (IsNullString(str)) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // check the start |
| 163 | const RegEx& start = (flowType == FlowType::Flow ? Exp::PlainScalarInFlow() |
| 164 | : Exp::PlainScalar()); |
| 165 | if (!start.Matches(str)) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | // and check the end for plain whitespace (which can't be faithfully kept in a |
| 170 | // plain scalar) |
| 171 | if (!str.empty() && *str.rbegin() == ' ') { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // then check until something is disallowed |
| 176 | static const RegEx& disallowed_flow = |
| 177 | Exp::EndScalarInFlow() | (Exp::BlankOrBreak() + Exp::Comment()) | |
| 178 | Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() | |
| 179 | Exp::Tab() | Exp::Ampersand(); |
| 180 | static const RegEx& disallowed_block = |
| 181 | Exp::EndScalar() | (Exp::BlankOrBreak() + Exp::Comment()) | |
| 182 | Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() | |
| 183 | Exp::Tab() | Exp::Ampersand(); |
| 184 | const RegEx& disallowed = |
| 185 | flowType == FlowType::Flow ? disallowed_flow : disallowed_block; |
| 186 | |
| 187 | StringCharSource buffer(str.c_str(), str.size()); |
| 188 | while (buffer) { |
| 189 | if (disallowed.Matches(buffer)) { |
| 190 | return false; |
| 191 | } |
| 192 | if (allowOnlyAscii && (0x80 <= static_cast<unsigned char>(buffer[0]))) { |
| 193 | return false; |
| 194 | } |
| 195 | ++buffer; |
| 196 | } |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) { |
| 202 | // TODO: check for non-printable characters? |
no test coverage detected