SPB Git

Repository Public MIT

SwiftLiteral.swift 3.9 KB 85 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Apps/GlassStudio/Kit/Model/SwiftLiteral.swift8// Module           : GlassStudio9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import SPGlassKit4344/// Builds Swift source fragments from values. Every piece of user text that reaches generated code goes45/// through here, so a title can never close a string literal and inject code.46public enum SwiftLiteral {47    /// A Swift string literal with quotes, backslashes, control characters and line separators escaped.48    public static func string(_ text: String) -> String {49        GlassThemeSwiftExporter.literal(text)50    }5152    public static func bool(_ value: Bool) -> String { value ? "true" : "false" }5354    /// A number without locale: `12`, `0.5`, never `0,5` and never `nan`.55    public static func number(_ value: Double) -> String {56        guard value.isFinite else { return "0" }57        if value == value.rounded(), abs(value) < 1e12 { return String(Int(value)) }58        var text = String(format: "%.4f", locale: Locale(identifier: "en_US_POSIX"), value)59        while text.hasSuffix("0") { text.removeLast() }60        return text61    }6263    /// `.caseName`, only when the name is a plain identifier; anything else becomes `fallback`.64    public static func enumCase(_ name: String, fallback: String) -> String {65        "." + (isIdentifier(name) ? name : fallback)66    }6768    public static func color(_ color: GlassRGBA) -> String {69        "Color(red: \(number((color.red * 1000).rounded() / 1000)), green: \(number((color.green * 1000).rounded() / 1000)), blue: \(number((color.blue * 1000).rounded() / 1000))" + (color.isOpaque ? ")" : ", opacity: \(number((color.alpha * 1000).rounded() / 1000)))")70    }7172    public static func isIdentifier(_ text: String) -> Bool {73        guard let first = text.unicodeScalars.first, text.utf8.count <= 64 else { return false }74        let letters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")75        let rest = letters.union(CharacterSet(charactersIn: "0123456789"))76        return letters.contains(first) && text.unicodeScalars.allSatisfy(rest.contains)77    }7879    /// Indents every non-empty line of `code` by `levels` × 4 spaces.80    public static func indent(_ code: String, levels: Int = 1) -> String {81        let prefix = String(repeating: "    ", count: max(levels, 0))82        return code.components(separatedBy: "\n").map { $0.isEmpty ? $0 : prefix + $0 }.joined(separator: "\n")83    }84}85
Files main
Menu

Theme