Guitar 🎸
A Cross-Platform String and Regular Expression Library Written in Swift.
About
This library seeks to add common string manipulation functions, including common regular expression capabilities, that are needed in both mobile and server-side development, but are missing in Swift’s Foundation library.
The full documentation can be found at http://www.sabintsev.com/Guitar/.
Status
- Release Stage: Alpha
- API Stability: Unstable
Features
- [x] Boolean Functions (
isAlpha
,isNumeric
,isUppercase
, etc.) - [x] Case Functions (
camelCased
,pascalCased
,kebabCased
, etc.) - [x] Character Functions (
first
,length
,reversed
, etc.) - [x] Latinization Functions (
.latinized()
,.withoutAccents()
) - [x] Padding Functions (
padLeft
,padRight
,pad
) - [x] Regular Expressions (with Common Patterns Built-in)
- [x] Trimming Functions (
trimLeft
,trimRight
,truncated
, etc.)
Installation Instructions
CocoaPods
pod 'Guitar'
Carthage
github "ArtSabintsev/Guitar"
Swift Package Manager
.Package(url: "https://github.com/ArtSabintsev/Guitar.git", majorVersion: 0)
Usage Examples
Regular Expression
The Guitar
structure itself is used to make it easier to use Regular Expressions to test and evaluate String
objects. Guitar.Chord
enumerates common regular expressions. GuitarChord.swift contains a list of built-in regular expressions. GuitarCommon.swift contains a list of convenience methods for common regular expression evaluations and tests, such as checking the string to determine if it’s a valid email address.
Initialization
Guitar(pattern: String) // A custom regular expression with which to initialize Guitar.
Guitar(chord: Guitar.Chord) // A common regular expression with which to initialize Guitar.
Methods
evaluateForRanges(::)
returns an array of ranges, [Range<String.Index>]
, that match a specific regular expression.
swift
Guitar(chord: .firstCharacter).evaluateForRanges(from: "hello world") // [Range(Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)..<Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 1), _countUTF16: 1)), Range(Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 6), _countUTF16: 1)..<Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 7), _countUTF16: 1))]
evaluateForStrings(::)
returns an array of strings, [String]
, that match a specific regular expression.
swift
Guitar(chord: .firstCharacter).evaluateForStrings(from: "Hello world") // ["H", "w"]
test(::)
evaluates a string with a specific regular expression. true
is returned if matches are found in the string. Otherwise, false
is returned.
swift
Guitar(chord: .email).test(string: "hello@world.com") // `true`
String Extension
This library also adds dozens of methods via String
extensions that are missing in the Swift Standard Library or not easily accessible on the String
class. Examples of each new method can be found in the jazzy-powered documentation.
More specific examples can be found in the tests folder.