Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::base::parser::LengthParser Class Reference

Parser for CSS <length-percentage> strings, such as "10px", "30%", "10em", etc. More...

#include "donner/base/parser/LengthParser.h"

Classes

struct  Options
 Options to modify the parsing behavior. More...
 
struct  Result
 Container for parse result, containing the parsed result and the number of characters that were consumed to parse it. More...
 

Static Public Member Functions

static ParseResult< ResultParse (std::string_view str, Options options=Options())
 Parse a CSS <length-percentage>, see https://www.w3.org/TR/css-values/#typedef-length-percentage.
 
static std::optional< Lengthd::UnitParseUnit (std::string_view str)
 Parse a unit suffix from a string, such as "px" or "em".
 

Detailed Description

Parser for CSS <length-percentage> strings, such as "10px", "30%", "10em", etc.

For example:

const Lengthd length = maybeLengthResult.result().length;
}
static ParseResult< Result > Parse(std::string_view str, Options options=Options())
Parse a CSS <length-percentage>, see https://www.w3.org/TR/css-values/#typedef-length-percentage.
Definition LengthParser.cc:138
A parser result, which may contain a result of type T, or an error, or both.
Definition ParseResult.h:17
bool hasResult() const noexcept
Returns true if this ParseResult contains a valid result.
Definition ParseResult.h:102
T & result() &
Returns the contained result.
Definition ParseResult.h:46

Member Function Documentation

◆ Parse()

ParseResult< LengthParser::Result > donner::base::parser::LengthParser::Parse ( std::string_view str,
LengthParser::Options options = Options() )
static

Parse a CSS <length-percentage>, see https://www.w3.org/TR/css-values/#typedef-length-percentage.

Equivalent to [ <length> | <percentage> ], where <percentage> will resolve to <length>.

With the following token definitions:

  • <dimension-token> = <number-token> <ident-token>
  • <percentage-token> = <number-token> %
  • <number-token> = The result of NumberParser, a real number in either fixed or scientific notation, with optional '+' or '-' prefix.
  • <ident-token> = -?-? [ a-z A-Z _ or non-ASCII ] [ a-z A-Z _ - or non-ASCII ]. However, LengthParser is limited to valid suffixes for length, as defined by Length::Unit.

If the number is 0, the <ident-token> may be omitted since 0 is unitless. This can be extended to all numbers by setting Options::unitOptional to true.

Note that this may not consume all input, the caller should handle the result of Result::consumedChars.

Parameters
strString to parse, not all characters may be consumed.
optionsParser options.
Returns
Result containing the Length and the number of characters that were parsed.

◆ ParseUnit()

std::optional< Lengthd::Unit > donner::base::parser::LengthParser::ParseUnit ( std::string_view str)
static

Parse a unit suffix from a string, such as "px" or "em".

Parameters
strString containing the unit suffix, which must be a complete case-insensitive match for a supported <dimension-token> suffix, or '%' for <percentage-token>.
Returns
Lengthd::Unit corresponding to the suffix, or std::nullopt if there was no match.

The documentation for this class was generated from the following files: