// Copyright 2017-2022 The MathJax Consortium// SPDX-License-Identifier: Apache-2.0// Based on ParseUtil.addArgs and ParseUtil.substituteArgs (MathJax 3.2.2).
package teximport ()const maxMacroBuffer = 5120// macroAddArgs preserves a control word at a source-splicing boundary. The// source regular expression is ASCII-only; Unicode case folding is not used.// Its size limit counts UTF-16 code units after the separator is inserted.func macroAddArgs(, string, int) (string, error) { := func( byte) bool { return >= 'a' && <= 'z' || >= 'A' && <= 'Z' }iflen() != 0 && ([0]) { := len()for > 0 && ([-1]) { -- }if < len() { := for > 0 && [-1] == '\\' { -- }if (-)%2 == 1 { += " " } } }iflen(utf16.Encode([]rune()))+len(utf16.Encode([]rune())) > {return"", texError("MaxBufferSize", "MathJax internal buffer size exceeded; is there a recursive macro call?") }return + , nil}// Macros and paired delimiters use ParseUtil.substituteArgs before replacing// their caller source. Environments retain their separate substitution path.func substituteMacroArguments( string, []string) (string, error) {returnsubstituteMacroArgumentsWithLimit(, , maxMacroBuffer)}func substituteMacroArgumentsWithLimit( string, []string, int) (string, error) {varstrings.Builder := ""for := 0; < len(); { := [] ++switch {case'\\': .WriteByte()if < len() { , := utf8.DecodeRuneInString([:]) .WriteString([ : +]) += }case'#':if >= len() {return"", texError("IllegalMacroParam", "Illegal macro parameter reference") } = [] ++if == '#' { .WriteByte()continue }if < '1' || > '9' || int(-'1') >= len() {return"", texError("IllegalMacroParam", "Illegal macro parameter reference") }varerror , = macroAddArgs(, .String(), )if != nil {return"", } , = macroAddArgs(, [-'1'], )if != nil {return"", } .Reset()default: .WriteByte() } }returnmacroAddArgs(, .String(), )}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.