DiffEngine, as used in gnorium.com
Platform-agnostic character-level diff engine for Swift.
DiffEngine computes character-level differences between two strings using Swift's CollectionDifference. Zero dependencies, works on all Apple platforms, Linux, and WebAssembly.
- Character-Level Precision: Diffs at the character level, not line level
- Pure Swift: No Foundation dependency, no platform-specific code
- Simple API: One function call, returns typed segments
- Cross-Platform: macOS, iOS, watchOS, tvOS, visionOS, Linux, WASM
Add DiffEngine to your Package.swift:
dependencies: [
.package(url: "https://github.com/gnorium/diff-engine", branch: "main")
]Then add it to your target dependencies:
.target(
name: "YourTarget",
dependencies: [
.product(name: "DiffEngine", package: "diff-engine")
]
)import DiffEngine
let segments = DiffEngine.diff(old: "hello world", new: "hello earth")
for segment in segments {
switch segment {
case .unchanged(let text): print(text)
case .deleted(let text): print("-\(text)")
case .inserted(let text): print("+\(text)")
}
}
// "hello " → unchanged
// "wo" → deleted
// "ea" → inserted
// "r" → unchanged
// "ld" → deleted
// "th" → insertedpublic enum DiffSegment: Sendable, Equatable {
case unchanged(String) // Present in both
case inserted(String) // Added in new
case deleted(String) // Removed from old
var text: String { ... }
}- Swift 5.1+ (uses
CollectionDifference)
Apache License 2.0 - See LICENSE for details
Contributions welcome! Please open an issue or submit a pull request.
- admin-core - Core admin functionalities for web applications
- design-tokens - Universal design tokens based on Apple HIG
- embedded-swift-utilities - Utility functions for Embedded Swift environments
- markdown-utilities - Markdown rendering with media attribution support
- web-apis - Web API implementations for Swift WebAssembly
- web-builders - HTML, CSS, JS, and SVG DSL builders
- web-components - Reusable UI components for web applications
- web-formats - Structured data format builders
- web-security - Portable security utilities for web applications
- web-types - Shared web types and design tokens