 
 import UIKit
 
+public enum VariableNamingFormat {
+    case CamelCase
+    case Underscores
+    case PascalCase
+}
+
 public protocol LHSStringType {
     /**
      Conforming types must provide a getter for the length of the string.

     - date: February 17, 2016
     */
    func range() -> NSRange
}

public protocol LHSURLStringType {
    mutating func URLEncode()
    mutating func slugify() throws
}

extension String: LHSStringType, LHSURLStringType {}

        return NSString(string: self).length
    }

    mutating func trim(string: String) {
        self = self.stringByTrimmingString(string)
    }

    mutating func URLEncode() {
        if let string = URLEncodedString() {
            self = string
        }
    }

    mutating func replaceSpacesWithDashes() {
        self = stringByReplacingSpacesWithDashes()
    }

    mutating func replaceCapitalsWithUnderscores() {
        self = stringByConvertingToNamingFormat(.Underscores)
    }

    func stringByLowercasingFirstLetter() -> String {
        let start = startIndex.successor()
        return substringToIndex(start).lowercaseString + substringWithRange(Range<Index>(start: start, end: endIndex))
    }

    func stringByUppercasingFirstLetter() -> String {
        let start = startIndex.successor()
        return substringToIndex(start).uppercaseString + substringWithRange(Range<Index>(start: start, end: endIndex))
    }

    func stringByTrimmingString(string: String) -> String {
        let cfString = NSMutableString(string: self) as CFMutableString
        CFStringTrim(cfString, string)
        return cfString as String
    }

    func URLEncodedString() -> String? {
        if let string = stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
            return string
        }
        else {
            return nil
        }
    }

    func stringByReplacingSpacesWithDashes() -> String {
        let regexOptions = NSRegularExpressionOptions()
        let regex = try! NSRegularExpression(pattern: "[ ]", options: regexOptions)
        return regex.stringByReplacingMatchesInString(self, options: NSMatchingOptions(), range: range(), withTemplate: "-").lowercaseString as String
    }

    public func stringByConvertingToNamingFormat(naming: VariableNamingFormat) -> String {
        switch naming {
        case .Underscores:
            let regex = try! NSRegularExpression(pattern: "([A-Z]+)", options: NSRegularExpressionOptions())
            let string = NSMutableString(string: self)
            regex.replaceMatchesInString(string, options: NSMatchingOptions(), range: range(), withTemplate: "_$0")
            return string.stringByTrimmingString("_").lowercaseString

        case .CamelCase:
            fatalError("Not implemented")

        case .PascalCase:
            fatalError("Not implemented")
        }
    }
}

