From 05165cc8d98ef5ffa8ee3a8ba9bf1ad5e0b5a9ab Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Mon, 21 Oct 2024 23:54:10 -0600 Subject: swift-format --- iTexSnip/Utils/KatexUtils.swift | 222 ++++++++++++++++++++++------------------ 1 file changed, 121 insertions(+), 101 deletions(-) (limited to 'iTexSnip/Utils/KatexUtils.swift') diff --git a/iTexSnip/Utils/KatexUtils.swift b/iTexSnip/Utils/KatexUtils.swift index d339a71..697e700 100644 --- a/iTexSnip/Utils/KatexUtils.swift +++ b/iTexSnip/Utils/KatexUtils.swift @@ -7,125 +7,145 @@ import Foundation -func change(_ inputStr: String, oldInst: String, newInst: String, oldSurrL: Character, oldSurrR: Character, newSurrL: String, newSurrR: String) -> String { - var result = "" - var i = 0 - let n = inputStr.count - let inputArray = Array(inputStr) // Convert string to array of characters for easier access - - while i < n { - // Get the range for the substring equivalent to oldInst - if i + oldInst.count <= n && inputStr[inputStr.index(inputStr.startIndex, offsetBy: i).. 0 { - if inputArray[j] == "\\" && !escaped { - escaped = true - j += 1 - continue - } - - if inputArray[j] == oldSurrR && !escaped { - count -= 1 - if count == 0 { - break - } - } else if inputArray[j] == oldSurrL && !escaped { - count += 1 - } - - escaped = false - j += 1 - } - - if count == 0 { - let innerContent = String(inputArray[(start + 1).. String { + var result = "" + var i = 0 + let n = inputStr.count + let inputArray = Array(inputStr) // Convert string to array of characters for easier access + + while i < n { + // Get the range for the substring equivalent to oldInst + if i + oldInst.count <= n + && inputStr[ + inputStr.index( + inputStr.startIndex, offsetBy: i).. 0 { + if inputArray[j] == "\\" && !escaped { + escaped = true + j += 1 + continue + } + + if inputArray[j] == oldSurrR && !escaped { + count -= 1 + if count == 0 { + break } + } else if inputArray[j] == oldSurrL && !escaped { + count += 1 + } + + escaped = false + j += 1 } - result.append(inputArray[i]) - i += 1 - } - if oldInst != newInst && result.contains(oldInst + String(oldSurrL)) { - return change(result, oldInst: oldInst, newInst: newInst, oldSurrL: oldSurrL, oldSurrR: oldSurrR, newSurrL: newSurrL, newSurrR: newSurrR) + if count == 0 { + let innerContent = String(inputArray[(start + 1).. [Int] { - var positions: [Int] = [] - var searchRange = string.startIndex.. String { - let pattern = try! NSRegularExpression(pattern: "\\\\[a-zA-Z]+\\$.*?\\$|\\$.*?\\$", options: []) - var newContent = content - let matches = pattern.matches(in: content, options: [], range: NSRange(content.startIndex.. String { - let positions = findSubstringPositions(inputStr, substring: oldInst + String(oldSurrL)) - var result = inputStr - - for pos in positions.reversed() { - let startIndex = result.index(result.startIndex, offsetBy: pos) - let substring = String(result[startIndex.. String { + let positions = findSubstringPositions(inputStr, substring: oldInst + String(oldSurrL)) + var result = inputStr + + for pos in positions.reversed() { + let startIndex = result.index(result.startIndex, offsetBy: pos) + let substring = String(result[startIndex.. String { - var res = formula - // Remove mbox surrounding - res = changeAll(inputStr: res, oldInst: "\\mbox ", newInst: " ", oldSurrL: "{", oldSurrR: "}", newSurrL: "", newSurrR: "") - res = changeAll(inputStr: res, oldInst: "\\mbox", newInst: " ", oldSurrL: "{", oldSurrR: "}", newSurrL: "", newSurrR: "") - - // Additional processing similar to the Python version... - res = res.replacingOccurrences(of: "\\[", with: "") - res = res.replacingOccurrences(of: "\\]", with: "") - res = res.replacingOccurrences(of: "\\\\[?.!,\'\"](?:\\s|$)", with: "", options: .regularExpression) - - // Merge consecutive `text` - res = rmDollarSurr(content: res) - - // Remove extra spaces - res = res.replacingOccurrences(of: " +", with: " ", options: .regularExpression) - - return res.trimmingCharacters(in: .whitespacesAndNewlines) + var res = formula + // Remove mbox surrounding + res = changeAll( + inputStr: res, oldInst: "\\mbox ", newInst: " ", oldSurrL: "{", oldSurrR: "}", newSurrL: "", + newSurrR: "") + res = changeAll( + inputStr: res, oldInst: "\\mbox", newInst: " ", oldSurrL: "{", oldSurrR: "}", newSurrL: "", + newSurrR: "") + + // Additional processing similar to the Python version... + res = res.replacingOccurrences(of: "\\[", with: "") + res = res.replacingOccurrences(of: "\\]", with: "") + res = res.replacingOccurrences( + of: "\\\\[?.!,\'\"](?:\\s|$)", with: "", options: .regularExpression) + + // Merge consecutive `text` + res = rmDollarSurr(content: res) + + // Remove extra spaces + res = res.replacingOccurrences(of: " +", with: " ", options: .regularExpression) + + return res.trimmingCharacters(in: .whitespacesAndNewlines) } -- cgit v1.2.3