package com.lemenzo.calculator.engine /** * Converts between the locale-neutral expression syntax used by the math engine * and the text shown to the user. On locales using comma as the decimal * separator, function arguments are displayed with semicolons to avoid the * ambiguous `nCr(10,3)` / decimal-comma collision. */ object ExpressionCodec { fun toDisplay(canonical: String, decimalSeparator: Char): String { if (decimalSeparator == '.') return canonical return buildString(canonical.length) { canonical.forEach { c -> append(when (c) { '.' -> decimalSeparator ',' -> ';' else -> c }) } } } fun toCanonical(display: String, decimalSeparator: Char): String { return buildString(display.length) { display.forEach { c -> append(when { c == decimalSeparator -> '.' decimalSeparator != '.' && c == ';' -> ',' c == '×' -> '*' c == '÷' -> '/' c == '−' -> '-' else -> c }) } } } }