Returns a string_view to the line with the specified line number (counting "1-based").
| 306 | |
| 307 | // Returns a string_view to the line with the specified line number (counting "1-based"). |
| 308 | std::string_view |
| 309 | GetLine(const std::string_view contents, const toml::source_index lineNumber) |
| 310 | { |
| 311 | if (lineNumber == 0) |
| 312 | { |
| 313 | return {}; |
| 314 | } |
| 315 | |
| 316 | // The position of the first character of the specified line. |
| 317 | const auto beginOfLine = [contents, lineNumber]() -> std::size_t { |
| 318 | if (lineNumber == 1) |
| 319 | { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | const auto numberOfChars = contents.size(); |
| 324 | std::size_t currentLineNumber{ 1 }; |
| 325 | |
| 326 | for (std::size_t i{}; i < numberOfChars; ++i) |
| 327 | { |
| 328 | if (contents[i] == '\n') |
| 329 | { |
| 330 | ++currentLineNumber; |
| 331 | |
| 332 | if (currentLineNumber == lineNumber) |
| 333 | { |
| 334 | return i + 1; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | return std::string_view::npos; |
| 339 | }(); |
| 340 | |
| 341 | if (beginOfLine >= contents.size()) |
| 342 | { |
| 343 | return {}; |
| 344 | } |
| 345 | |
| 346 | const auto endOfLine = contents.find_first_of("\r\n", beginOfLine); |
| 347 | |
| 348 | return contents.substr(beginOfLine, |
| 349 | (endOfLine == std::string_view::npos) ? std::string_view::npos : (endOfLine - beginOfLine)); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | // Parses the specified TOML file, and returns its key-value pairs as an elastix ParameterMap. |