Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/balestrapatrick/stryng
Swift strings taken to a whole new syntax level.
https://github.com/balestrapatrick/stryng
string substring swift swift4
Last synced: about 1 month ago
JSON representation
Swift strings taken to a whole new syntax level.
- Host: GitHub
- URL: https://github.com/balestrapatrick/stryng
- Owner: BalestraPatrick
- License: mit
- Created: 2017-12-02T13:15:45.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-21T12:03:21.000Z (almost 6 years ago)
- Last Synced: 2024-09-29T22:03:06.113Z (about 2 months ago)
- Topics: string, substring, swift, swift4
- Language: Swift
- Homepage:
- Size: 235 KB
- Stars: 257
- Watchers: 9
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stryng
`Stryng` is designed to make it easier to work with strings by using the common and easy to remember subscript syntax and accessing characters and ranges with `Int` indices.Swift's strings management is one of the most painful feature of the language. Sure, it's great to have Unicode correctness and efficiency, but this comes at a cost: too much verbosity and complexity.
## Examples
Retrieve a single character at a specific position.
```swift
let string = "Example"
// With Stryng
string[1] // "x"
// Without
string[string.index(string.startIndex, offsetBy: 1)] // "x"
```Retrieve the substring up to a specific index.
```swift
let string = "Example"
// With Stryng
string[..<2] // "Ex"
// Without
string[..] containing all positions of the subtring.
```Convert a `Substring` to a `String`.
```swift
let example = "Example"
example[1...5].string // Returns a `String?` instead of a `Substring?`
```## Usage
This is an up to date list of the supported subscripts. Take a look at [`StryngTests.swift`](https://github.com/BalestraPatrick/Stryng/blob/master/Tests/StryngTests/StryngTests.swift) if you want to see some more real code examples.```swift
// String[1]
public subscript(index: Int) -> Character?// String[0..<1]
public subscript(range: Range) -> Substring?// String[0...1]
public subscript(range: ClosedRange) -> Substring?// String[..<1]
public subscript(value: PartialRangeUpTo) -> Substring?// String[...1]
public subscript(value: PartialRangeThrough) -> Substring?// String[1...]
public subscript(value: PartialRangeFrom) -> Substring?// String["substring"]
public subscript(string: String) -> [Range]// String["begin"..."end"]
public subscript(range: ClosedRange) -> [ClosedRange]// String["begin"..<"end"]
public subscript(range: Range) -> [Range]// String[Character("a")]
public subscript(character: Character) -> [String.Index]// String["begin"...]
public subscript(range: PartialRangeFrom) -> PartialRangeFrom?// String[..."end"]
public subscript(range: PartialRangeThrough) -> PartialRangeThrough?
```## Installation
### Cocoapods
To install via [Cocoapods](http://cocoapods.org/), add the following line to your Podfile:
```ruby
pod 'Stryng'
```### Swift Package Manager
To install via the [Swift Package Manager](https://swift.org/package-manager/), add the following line to the `dependencies` array in your `Package.swift` file:
```swift
.package(url: "https://github.com/BalestraPatrick/Stryng.git", from: "0.4.1")
```Then, still in your `Package.swift`, add `"Stryng"` to your *target's* `dependencies` array.
Finally, in your terminal, run the following command to update your dependencies:
```bash
$ swift package update
```## Disclosure
Yes, string traversal in Swift can be slow. The reason why these subscripts don't exist in the standard library is that some people think that it hides the performance implications of traversing a string. Traversing a string from the `startIndex` until the `endIndex` has complexity O(n).
If you need to get a character at a specific index, in one way or another you will have to traverse the string, but why would you need 3 lines of code instead of 1 to do that if you know what you're doing?This is why Stryng is here to help you.
## Contribute
We'd love your help.
Head over to the [issues](https://github.com/BalestraPatrick/Stryng/issues) with your feedback.
Bonus points if you open a [Pull request](https://github.com/BalestraPatrick/Stryng/pulls) with a failing test for a bug or a new feature! ⭐️## Author
I'm [Patrick Balestra](http://www.patrickbalestra.com).
Email: [[email protected]](mailto:[email protected])
Twitter: [@BalestraPatrick](http://twitter.com/BalestraPatrick).
## License
`Stryng` is available under the MIT license. See the [LICENSE](LICENSE) file for more info.