GitPedia

Rearrange

Swift library for working with ranges types: NSRange, IndexSet, and String.Index

From ChimeHQ·Updated May 11, 2026·View on GitHub·

[![Build Status][build status badge]][build status] [![Platforms][platforms badge]][platforms] [![Documentation][documentation badge]][documentation] [![Matrix][matrix badge]][matrix] The project is written primarily in Swift, distributed under the BSD 3-Clause "New" or "Revised" License license, first published in 2019. Key topics include: cocoa, foundation, macos, nstextview, swift.

Latest release: 2.1.1v2.1.1
April 28, 2026View Changelog →
<div align="center">

Build Status
Platforms
Documentation
Matrix

</div>

Rearrange

Rearrange is a collection of utilities for making it easier to work with NSRange, IndexSet, and String.Index.

If you need to work with NSTextRange/UITextRange or just TextKit in general, check out Glyph.

Integration

Swift Package Manager:

swift
dependencies: [ .package(url: "https://github.com/ChimeHQ/Rearrange") ]

Types

RangeMutation

This is a struct that encapsulates a single change to an NSRange. It's useful for serializing, queuing, or otherwise storing changes and applying them.

You can also use this class to tranform locations, NSRange, or even an IndexSet. This is handy for updating values as text is changed. This might seem easy, but there are a large number of edge cases that RangeMutation handles, including mutations that invalidate (for example completely delete) a range.

Extensions

NSRange

swift
// convenience static let zero: NSRange static let notFound: NSRange var max: Int // shifting func shifted(by delta: Int) -> NSRange? func shifted(endBy delta: Int) -> NSRange? func shifted(startBy delta: Int) -> NSRange? // mutating func clamped(to limit: Int) -> NSRange func apply(_ change: RangeMutation) -> NSRange? // working with Swift String func range(in string: String) -> Range<String.Index>?

IndexSet

swift
init(integersIn range: NSRange) init(ranges: [NSRange]) mutating func insert(range: NSRange) mutating func insert(ranges: [NSRange]) mutating func remove(integersIn range: NSRange) var nsRangeView: [NSRange] func contains(integersIn range: NSRange) -> Bool func intersects(integersIn range: NSRange) -> Bool var limitSpanningRange: NSRange?

String

swift
subscript(range: Range<Int>) -> Substring? subscript(range: NSRange) -> Substring?

Contributing and Collaboration

I would love to hear from you! Issues or pull requests work great. Both a Matrix space and Discord are available for live help, but I have a strong bias towards answering in the form of documentation. You can also find me on mastodon.

I prefer collaboration, and would love to find ways to work together if you have a similar project.

I prefer indentation with tabs for improved accessibility. But, I'd rather you use the system you want and make a PR than hesitate because of whitespace.

By participating in this project you agree to abide by the Contributor Code of Conduct.

Contributors

Showing top 4 contributors by commit count.

View all contributors on GitHub →

This article is auto-generated from ChimeHQ/Rearrange via the GitHub API.Last fetched: 6/25/2026