Skip to content

gnorium/diff-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DiffEngine, as used in gnorium.com

Platform-agnostic character-level diff engine for Swift.

Overview

DiffEngine computes character-level differences between two strings using Swift's CollectionDifference. Zero dependencies, works on all Apple platforms, Linux, and WebAssembly.

Features

  • 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

Installation

Swift Package Manager

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")
    ]
)

Usage

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"     → inserted

DiffSegment

public 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 { ... }
}

Requirements

  • Swift 5.1+ (uses CollectionDifference)

License

Apache License 2.0 - See LICENSE for details

Contributing

Contributions welcome! Please open an issue or submit a pull request.

Related Packages

About

DiffEngine, as use in gnorium.com

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages