const LOWER_A = 0x61
const LOWER_Z = 0x7a
const UPPER_A = 0x41
const UPPER_Z = 0x5a
const LOWER_E = 0x65
const UPPER_E = 0x45
const ZERO = 0x30
const NINE = 0x39
const ADD = 0x2b
const SUB = 0x2d
const MUL = 0x2a
const DIV = 0x2f
const OPEN_PAREN = 0x28
const CLOSE_PAREN = 0x29
const COMMA = 0x2c
const SPACE = 0x20
const PERCENT = 0x25
const UNDERSCORE = 0x5f
const BACKSLASH = 0x5c
const DOUBLE_QUOTE = 0x22
const SINGLE_QUOTE = 0x27
const MATH_FUNCTIONS = [
'abs',
'acos',
'asin',
'atan',
'atan2',
'calc',
'clamp',
'cos',
'exp',
'hypot',
'log',
'max',
'min',
'mod',
'pow',
'rem',
'round',
'sign',
'sin',
'sqrt',
'tan',
]
const MATH_FUNCTIONS_SET = new Set(MATH_FUNCTIONS)
function isIdentChar(char: number) {
return (
(char >= LOWER_A && char <= LOWER_Z) ||
(char >= UPPER_A && char <= UPPER_Z) ||
(char >= ZERO && char <= NINE) ||
char === SUB ||
char === UNDERSCORE ||
char >= 0x80
)
}
export function hasMathFn(input: string) {
let paren = input.indexOf('(')
while (paren !== -1) {
let start = paren
while (start > 0 && isIdentChar(input.charCodeAt(start - 1))) start--
if (start < paren && MATH_FUNCTIONS_SET.has(input.slice(start, paren).toLowerCase())) {
return true
}
paren = input.indexOf('(', paren + 1)
}
return false
}
function isMathFnStart(input: string, start: number) {
if (start === 0) return true
let prev = input.charCodeAt(start - 1)
if (prev !== SUB) return !isIdentChar(prev)
let identStart = start - 1
while (identStart > 0 && isIdentChar(input.charCodeAt(identStart - 1))) identStart--
return !(input.charCodeAt(identStart) === SUB && input.charCodeAt(identStart + 1) === SUB)
}
export function addWhitespaceAroundMathOperators(input: string) {
if (!MATH_FUNCTIONS.some((fn) => input.includes(fn))) {
return input
}
let result = ''
let formattable: boolean[] = []
let valuePos = null
let lastValuePos = null
for (let i = 0; i < input.length; i++) {
let char = input.charCodeAt(i)
if (char >= ZERO && char <= NINE) {
valuePos = i
}
else if (
valuePos !== null &&
(char === PERCENT ||
(char >= LOWER_A && char <= LOWER_Z) ||
(char >= UPPER_A && char <= UPPER_Z))
) {
valuePos = i
}
else {
lastValuePos = valuePos
valuePos = null
}
if (char === BACKSLASH) {
result += input.slice(i, i + 2)
i += 1
continue
}
if (char === SINGLE_QUOTE || char === DOUBLE_QUOTE) {
let start = i
while (++i < input.length) {
let nextChar = input.charCodeAt(i)
if (nextChar === BACKSLASH) {
i += 1
continue
}
if (nextChar === char) {
break
}
}
result += input.slice(start, i + 1)
continue
}
if (char === OPEN_PAREN) {
result += input[i]
let start = i
for (let j = i - 1; j >= 0; j--) {
let inner = input.charCodeAt(j)
if (inner >= ZERO && inner <= NINE) {
start = j
} else if (inner >= LOWER_A && inner <= LOWER_Z) {
start = j
} else if (inner >= UPPER_A && inner <= UPPER_Z) {
start = j
} else {
break
}
}
let fn = input.slice(start, i)
if (MATH_FUNCTIONS_SET.has(fn.toLowerCase()) && isMathFnStart(input, start)) {
formattable.unshift(true)
continue
}
else if (formattable[0] && fn === '') {
formattable.unshift(true)
continue
}
formattable.unshift(false)
continue
}
else if (char === CLOSE_PAREN) {
result += input[i]
formattable.shift()
}
else if (char === COMMA && formattable[0]) {
result += `, `
continue
}
else if (char === SPACE && formattable[0] && result.charCodeAt(result.length - 1) === SPACE) {
continue
}
else if ((char === ADD || char === MUL || char === DIV || char === SUB) && formattable[0]) {
let trimmed = result.trimEnd()
let prev = trimmed.charCodeAt(trimmed.length - 1)
let prevPrev = trimmed.charCodeAt(trimmed.length - 2)
let next = input.charCodeAt(i + 1)
if ((prev === LOWER_E || prev === UPPER_E) && prevPrev >= ZERO && prevPrev <= NINE) {
result += input[i]
continue
}
else if (prev === ADD || prev === MUL || prev === DIV || prev === SUB) {
result += input[i]
continue
}
else if (prev === OPEN_PAREN || prev === COMMA) {
result += input[i]
continue
}
else if (input.charCodeAt(i - 1) === SPACE) {
result += `${input[i]} `
}
else if (
(prev >= ZERO && prev <= NINE) ||
(next >= ZERO && next <= NINE) ||
prev === CLOSE_PAREN ||
next === OPEN_PAREN ||
next === ADD ||
next === MUL ||
next === DIV ||
next === SUB ||
(lastValuePos !== null && lastValuePos === i - 1)
) {
result += ` ${input[i]} `
}
else {
result += input[i]
}
}
else {
result += input[i]
}
}
return result
}