Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soffes/diff
Simple diff library in pure Swift
https://github.com/soffes/diff
carthage ios macos swift tvos watchos
Last synced: 3 months ago
JSON representation
Simple diff library in pure Swift
- Host: GitHub
- URL: https://github.com/soffes/diff
- Owner: soffes
- License: mit
- Created: 2016-04-13T00:27:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-03T16:21:08.000Z (over 4 years ago)
- Last Synced: 2024-06-19T03:03:54.814Z (5 months ago)
- Topics: carthage, ios, macos, swift, tvos, watchos
- Language: Swift
- Size: 37.1 KB
- Stars: 123
- Watchers: 6
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.markdown
- License: LICENSE
- Support: Support/Diff.h
Awesome Lists containing this project
- awesome-ios - diff - Simple diff library in pure Swift. (Data Structures / Algorithms / Getting Started)
- awesome-ios-star - diff - Simple diff library in pure Swift. (Data Structures / Algorithms / Getting Started)
- fucking-awesome-ios - diff - Simple diff library in pure Swift. (Data Structures / Algorithms / Getting Started)
- fucking-awesome-ios - diff - Simple diff library in pure Swift. (Data Structures / Algorithms / Getting Started)
README
# Diff
[![Version](https://img.shields.io/github/release/soffes/Diff.svg)](https://github.com/soffes/Diff/releases)
[![Build Status](https://github.com/soffes/Color/workflows/Tests/badge.svg)](https://github.com/soffes/Color/actions)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)Simple diffing library in pure Swift.
## Installing
You can use [Carthage](https://github.com/Carthage/Carthage) or [Swift Package Manager](https://github.com/apple/swift-package-manager) to install Diff.
## Usage
Start by importing the package:
```swift
import Diff
```### Same
If there is no difference, the diff will be `nil`.
``` swift
diff("Hello", "Hello") // nil
```For the sake of brevity, we'll `!` the rest of the examples since we know they're different.
### Insert
``` swift
let (range, string) = diff("Hello world", "Hello there world")!
// range: 6..<6
// string: "there "
```### Remove
``` swift
let (range, string) = diff("Hello there world", "Hello world")!
// range: 6..<12
// string: ""
```### Other Types
Diff can diff any array. Here's an array of things that conform to `Equatable`:
``` swift
let (range, replacement) = diff([1, 2, 3], [1, 2, 3, 4])!
// range: 3..<3
// replacement: [4]
```You can even use arrays of anything as long as you can compare them:
```swift
let before: [Foo] = [a, b]
let after: [Foo] = [b]
let (range, replacement) = diff(before, after, compare: Foo.compare)!
// range: 0..<1
// replacement: []
```## Development
If you want to contribute to Diff, please write a test.
Building and running the tests locally with SPM is easy:
$ git clone https://github.com/soffes/Diff
$ cd Diff
$ swift build
$ swift test## Thanks
Thanks to [Jonathan Clem](https://github.com/jclem) for the original algorithm and [Caleb Davenport](https://github.com/calebd) for inspiration for the generics implementation and help debugging a few edge cases!