// Copyright 2017-2022 The MathJax Consortium // SPDX-License-Identifier: Apache-2.0 // Source: TexParser.GetDimen/GetArgument and ParseUtil.matchDimen (MathJax 3.2.2).
package tex import ( ) const dimensionNumber = `([-+]?(?:[.,][0-9]+|[0-9]+(?:[.,][0-9]*)?))` const dimensionUnit = `(pt|em|ex|mu|px|mm|cm|in|pc)` var ( dimensionSpace = regexp.MustCompile(`^` + muDimensionSpace + `*`) dimensionFull = regexp.MustCompile(`^` + muDimensionSpace + `*` + dimensionNumber + muDimensionSpace + `*` + dimensionUnit + muDimensionSpace + `*$`) dimensionPrefix = regexp.MustCompile(`^` + muDimensionSpace + `*` + dimensionNumber + muDimensionSpace + `*` + dimensionUnit + ` ?`) ) // readDimension owns only the source GetDimen argument. Its local whitespace, // balanced-brace scan and failure cursor do not change generic argument parsing. // name is the invoking control sequence used by primary parser.currentCS. func ( *parser) ( string) (string, error) { := func() (string, error) { return "", texError("MissingDimOrUnits", "Missing dimension or its units for \\%s", ) } if .pos >= len(.source) { return () } .pos += len(dimensionSpace.FindString(.source[.pos:])) if .pos >= len(.source) { return () } if .source[.pos] == '{' { := .pos + 1 .pos++ := 1 for .pos < len(.source) { switch .consumeRune() { case '\\': if .pos < len(.source) { .consumeRune() } else { // GetArgument advances once even after a final escape. This // error-only cursor is deliberately one past physical EOF. .pos++ } case '{': ++ case '}': -- if == 0 { := dimensionFull.FindStringSubmatch(.source[ : .pos-1]) if == nil { return () } return dimensionValue(), nil } } } return "", texError("MissingCloseBrace", "Missing close brace") } := dimensionPrefix.FindStringSubmatch(.source[.pos:]) if == nil { return () } .pos += len([0]) return dimensionValue(), nil } func dimensionValue( []string) string { return normalizeTeXMu(strings.Replace([1], ",", ".", 1) + [2]) }