All Projects → BalestraPatrick → Stryng

BalestraPatrick / Stryng

Licence: mit
Swift strings taken to a whole new syntax level.

Programming Languages

swift
15916 projects
swift4
162 projects

Labels

Projects that are alternatives of or similar to Stryng

Fmt.jl
Python-style format strings for Julia
Stars: ✭ 31 (-87.84%)
Mutual labels:  string
safe-string-interpolation
A type driven approach to string interpolation, aiming at consistent, secure, and only-human-readable logs and console outputs !
Stars: ✭ 14 (-94.51%)
Mutual labels:  string
common-words
Updated list of the 100 most common words in the English language. Useful for excluding these words from arrays.
Stars: ✭ 13 (-94.9%)
Mutual labels:  string
utils.js
👷 🔧 zero dependencies vanilla JavaScript utils.
Stars: ✭ 14 (-94.51%)
Mutual labels:  string
static string
A fixed capacity dynamically sized string
Stars: ✭ 46 (-81.96%)
Mutual labels:  string
indexed-string-variation
Experimental JavaScript module to generate all possible variations of strings over an alphabet using an n-ary virtual tree
Stars: ✭ 16 (-93.73%)
Mutual labels:  string
stringext
Extra string functions for OCaml
Stars: ✭ 20 (-92.16%)
Mutual labels:  string
type-reverse
🦄 Lightweight reverse utility around strings, arrays, numbers and more.
Stars: ✭ 30 (-88.24%)
Mutual labels:  string
String.prototype.matchAll
Spec-compliant polyfill for String.prototype.matchAll, in ES2020
Stars: ✭ 14 (-94.51%)
Mutual labels:  string
tokenizr
String Tokenization Library for JavaScript
Stars: ✭ 70 (-72.55%)
Mutual labels:  string
BFSG
BFSG - BruteForce String Generator 😾
Stars: ✭ 16 (-93.73%)
Mutual labels:  string
StringPool
A performant and memory efficient storage for immutable strings with C++17. Supports all standard char types: char, wchar_t, char16_t, char32_t and C++20's char8_t.
Stars: ✭ 19 (-92.55%)
Mutual labels:  string
poplar-trie
C++17 implementation of memory-efficient dynamic tries
Stars: ✭ 47 (-81.57%)
Mutual labels:  string
aho-corasick-node
A Node implementation of the Aho-Corasick string matching algorithm based on DoubleArray Trie.
Stars: ✭ 16 (-93.73%)
Mutual labels:  string
kirai
String formatting library for Java, Android, Web and Unix Terminal
Stars: ✭ 69 (-72.94%)
Mutual labels:  string
tplv
👣 Nano string template library for modern, based on ES6 template string syntax.
Stars: ✭ 31 (-87.84%)
Mutual labels:  string
strings
String helper methods and an inflector
Stars: ✭ 31 (-87.84%)
Mutual labels:  string
nim-ustring
utf-8 string for Nim
Stars: ✭ 12 (-95.29%)
Mutual labels:  string
lzbase62
LZ77(LZSS) based compression algorithm in base62 for JavaScript.
Stars: ✭ 38 (-85.1%)
Mutual labels:  string
widestring-rs
A wide string Rust library for converting to and from wide Unicode strings.
Stars: ✭ 48 (-81.18%)
Mutual labels:  string

Circle CI Carthage License Platform Twitter: @BalestraPatrick

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.

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.

let string = "Example"
// With Stryng
string[..<2] // "Ex"
// Without
string[..<string.index(string.startIndex, offsetBy: 2)] // "Ex"

Retrieve the substring between two indices.

let string = "Example"
// With Stryng
string[1..<6] // "xampl"
// Without
string[string.index(string.startIndex, offsetBy: 1)..<string.index(string.startIndex, offsetBy: 6)] // "Ex"

Retrieve positions of a all substring occurences.

let string = "Example Example"
let occurences = string["xa"] // Returns a [Range<String.Index>] containing all positions of the subtring.

Convert a Substring to a String.

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 if you want to see some more real code examples.

// String[1]
public subscript(index: Int) -> Character?

// String[0..<1]
public subscript(range: Range<Int>) -> Substring?

// String[0...1]
public subscript(range: ClosedRange<Int>) -> Substring?

// String[..<1]
public subscript(value: PartialRangeUpTo<Int>) -> Substring?

// String[...1]
public subscript(value: PartialRangeThrough<Int>) -> Substring?

// String[1...]
public subscript(value: PartialRangeFrom<Int>) -> Substring?

// String["substring"]
public subscript(string: String) -> [Range<String.Index>]

// String["begin"..."end"]
public subscript(range: ClosedRange<String>) -> [ClosedRange<String.Index>]

// String["begin"..<"end"]
public subscript(range: Range<String>) -> [Range<String.Index>]

// String[Character("a")]
public subscript(character: Character) -> [String.Index]

// String["begin"...]
public subscript(range: PartialRangeFrom<String>) -> PartialRangeFrom<String.Index>?

// String[..."end"]
public subscript(range: PartialRangeThrough<String>) -> PartialRangeThrough<String.Index>?

Installation

Cocoapods

To install via Cocoapods, add the following line to your Podfile:

pod 'Stryng'

Swift Package Manager

To install via the Swift Package Manager, add the following line to the dependencies array in your Package.swift file:

.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:

$ 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 with your feedback. Bonus points if you open a Pull request with a failing test for a bug or a new feature! ⭐️

Author

I'm Patrick Balestra.

Email: [email protected]

Twitter: @BalestraPatrick.

License

Stryng is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].