| 31 | { |
| 32 | |
| 33 | auto |
| 34 | UnitParser::operator()(swoc::TextView const &src) const noexcept -> Rv<value_type> |
| 35 | { |
| 36 | value_type zret = 0; |
| 37 | TextView text = src; // Keep @a src around to report error offsets. |
| 38 | |
| 39 | while (text.ltrim_if(&isspace)) { |
| 40 | TextView parsed; |
| 41 | auto n = swoc::svtou(text, &parsed); |
| 42 | if (parsed.empty()) { |
| 43 | return Errata("Required count not found at offset {}", text.data() - src.data()); |
| 44 | } else if (n == std::numeric_limits<decltype(n)>::max()) { |
| 45 | return Errata("Count at offset {} was out of bounds", text.data() - src.data()); |
| 46 | } |
| 47 | text.remove_prefix(parsed.size()); |
| 48 | auto ptr = text.ltrim_if(&isspace).data(); // save for error reporting. |
| 49 | // Everything up to the next digit or whitespace. |
| 50 | auto unit = text.clip_prefix_of([](char c) { return !(isspace(c) || isdigit(c)); }); |
| 51 | if (unit.empty()) { |
| 52 | if (_unit_required_p) { |
| 53 | return Errata("Required unit not found at offset {}", ptr - src.data()); |
| 54 | } |
| 55 | } else { |
| 56 | auto mult = _units[unit]; // What's the multiplier? |
| 57 | if (mult == 0) { |
| 58 | return Errata("Unknown unit \"{}\" at offset {}", unit, ptr - src.data()); |
| 59 | } |
| 60 | n *= mult; |
| 61 | } |
| 62 | zret += n; |
| 63 | } |
| 64 | return zret; |
| 65 | } |
| 66 | |
| 67 | // Concrete unit parsers. |
| 68 |
nothing calls this directly
no test coverage detected