// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers // // SPDX-License-Identifier: MPL-2.0 import Algorithms import Foundation public struct LineMap: Codable, Sendable { private let utf16LineOffsets: [Int] public init(source: String) { var offsets: [Int] = [0] for idx in source.indices { let c = source[idx] if c == "\n" || c == "\r\n" || c == "\r" { let next = source.index(after: idx) let utf16Offset = next.utf16Offset(in: source) offsets.append(utf16Offset) } } self.utf16LineOffsets = offsets } public func location(at utf16Offset: Int) -> (line: Int, column: Int) { let partitioningIndex = utf16LineOffsets.partitioningIndex { $0 > utf16Offset } let lineIndex = partitioningIndex == 0 ? 0 : partitioningIndex - 1 let lineStart = utf16LineOffsets[lineIndex] let lineNumber = lineIndex let columnNumber = utf16Offset - lineStart return (lineNumber, columnNumber) } }